是否可以将变量传递到转换文件中,并在转换文件中对其进行转换?
// translation.json
"country":{
"CHINA":"中国",
"US":"United States"
}
"text":"I live in $translate('country.{{country}}'), {{ someOtherVar }}.发布于 2015-04-17 09:19:05
我相信下面的结构会帮助你实现你想要的东西。
在英文翻译文件中
"country": {
"CHINA": "China",
"US": "United States",
...
},
"text":"I live in {{country}}, {{ someOtherVar }}."在中文翻译文件中
"country": {
"CHINA": "中国",
"US": "美国",
...
},
"text":"我住在{{country}}, {{ someOtherVar }}."在HTML中
<span translate="text" translate-values="{country: translated_country_name, someOtherVar: someothervar}"></span>在控制器中
$scope.someothervar = // some value
var countrykey = //CHINA or US...
$translate('country.' + countrykey).then(function(trans){
$scope.translated_country_name = trans;
})发布于 2015-04-17 09:13:42
您可以始终对转换文件进行预处理(例如,通过$httpProvider.interceptors)。
但是您可以从(可能)作用域中获得country变量。所以你应该写这样的代码:
var country = ...;
country = $translate(country);
$scope.translatedText = $translate('text', { country: country, someOtherVar: 'value' });你的text应该在哪里:
"text":"I live in {{country}}, {{ someOtherVar }}."这里有dynamic translation的文档。
https://stackoverflow.com/questions/29687067
复制相似问题