We have a website with angular translate. It works perfectly. We have a variable that we want to fixate with a certain language key. Let's say the site's language has 'en' and 'zh' as options, I want a certain translation to return with 'zh' no matter the language choice.
By browsing the API reference, I found a method called Instant to do just that. However, it doesn't work when we try to call
$translate.instant('zh', 'TRANSLATION_ID')
It returns an error of
TypeError: Object function (a,b,e){var f=d?n[d]:n,i=e?w[e]:u;if(f&&f.hasOwnProperty(a))return i.interpolate(f[a],b);if(h&&!v&&g.get(h)(a,d),d&&c&&d!==c){var j=n[c][a];if(j){var k;return i.setLocale(c),k=i.interpolate(j,b),i.setLocale(d),k}}return l&&(a=[l,a...<omitted>...a} has no method 'instant'
I wonder how to use the isntant method correctly.
We have a website with angular translate. It works perfectly. We have a variable that we want to fixate with a certain language key. Let's say the site's language has 'en' and 'zh' as options, I want a certain translation to return with 'zh' no matter the language choice.
By browsing the API reference, I found a method called Instant to do just that. However, it doesn't work when we try to call
$translate.instant('zh', 'TRANSLATION_ID')
It returns an error of
TypeError: Object function (a,b,e){var f=d?n[d]:n,i=e?w[e]:u;if(f&&f.hasOwnProperty(a))return i.interpolate(f[a],b);if(h&&!v&&g.get(h)(a,d),d&&c&&d!==c){var j=n[c][a];if(j){var k;return i.setLocale(c),k=i.interpolate(j,b),i.setLocale(d),k}}return l&&(a=[l,a...<omitted>...a} has no method 'instant'
I wonder how to use the isntant method correctly.
Share Improve this question asked Apr 2, 2014 at 6:50 Chris YeungChris Yeung 2,7236 gold badges37 silver badges61 bronze badges 1- 2 Can you point to the docs for the function also when debugging you should use non-minified source when available otherwise the errors dont' tell you much. – shaunhusain Commented Apr 2, 2014 at 7:07
2 Answers
Reset to default 9It seems like you don't use the method correctly, or probably just misunderstood it.
$translate.instant('ID')
expects the translation id as first parameter and interpolation params as second parameter. It then translate the id synchronously instead of asynchronously (which is what $translate()
does).
What you want is explicitly translating a translation id in a certain locale no matter what language key is currently used. This is currently not supported yet.
Hope that makes things clear.
Why don't you just embed the word in 'zh'
. Don't use $translate
there since you obviously don't want to translate that word.
If there's another reason for this; I would suggest:
// store the current language
var currentLanguage = $translate.use();
// change the language
$translate.use("zh").then(function (translation) {
// then translate here
$log.debug($translate.instant('SOME_WORD'));
// set the previous language back when you're fulfilled
$translate.use(currentLanguage);
});
But since this is async, this may translate some other words to 'zh'
in the meantime.
A third way to achieve this would be setting the same translation value for SOME_WORD
in each language file.
And a forth way I could think of is translating SOME_WORD
only in the 'en'
file (no translation value in the 'zh'
file) and using 'en'
as the fallback language. Such as: $translate.fallbackLanguage('en')