Reputation: 2183
Is it possible to pass a variable into a translation file, and have it translated inside the translation file?
// translation.json
"country":{
"CHINA":"中国",
"US":"United States"
}
"text":"I live in $translate('country.{{country}}'), {{ someOtherVar }}.
Upvotes: 0
Views: 532
Reputation: 1632
I believe this structure below would help for you to achieve what you want.
In the English translation file
"country": {
"CHINA": "China",
"US": "United States",
...
},
"text":"I live in {{country}}, {{ someOtherVar }}."
In the Chinese translation file
"country": {
"CHINA": "中国",
"US": "美国",
...
},
"text":"我住在{{country}}, {{ someOtherVar }}."
In HTML
<span translate="text" translate-values="{country: translated_country_name, someOtherVar: someothervar}"></span>
In controller
$scope.someothervar = // some value
var countrykey = //CHINA or US...
$translate('country.' + countrykey).then(function(trans){
$scope.translated_country_name = trans;
})
Upvotes: 1
Reputation: 5989
You could always preprocess the translation file (e.g. by $httpProvider.interceptors
).
But you have the country
variable available for you from (probably) the scope.
So you should write something like this:
var country = ...;
country = $translate(country);
$scope.translatedText = $translate('text', { country: country, someOtherVar: 'value' });
where your text
should be:
"text":"I live in {{country}}, {{ someOtherVar }}."
Here you've got the documentation for dynamic translation.
Upvotes: 0