Hi
I need a mechanism the translates to the default language if no entry exists for a word.
I mean, if I use a browser with he-IL language, and default is en-US and I need to translate a word x, and x doesn't appear in He-IL json file, I want it to be in en-US.
According to documentation, it has to work after using the setDefaultLang method, but it doesn't.
Code follows:
In c'tor:
`let userLang = navigator.language; // use navigator lang if available
/* tslint:disable:max-line-length */
userLang = /(sr-Latn-CS)|(af)|(it-IT)|(pl-PL)|(sv-SE)|(ja-JP)|(es-ES)|(en-GB)|(cs-CZ)|(zh-CN)|(nl-NL)|(tr-TR)|(ru-RU)|(da-DK)|(sl-SI)|(ro-RO)|(hu-HU)|(pt-BR)|(el-GR)|(nb-NO)|(fi-FI)|(ar-SA)|(hr-HR)|(pt-PT)|(zh-TW)|(ko-KR)|(es-MX)|(sk-SK)|(de-DE)|(fr-FR)|(he-IL)/gi.test(userLang) ? userLang : ''; // update the regex to add more supported languages
/* tslint:enable:max-line-length */
if (userLang.length !== 0){
userLang = "." + userLang;
}
userLang = 'LNGM' + userLang;
// this language will be used as a fallback when a translation isn't found in the current language
translate.setDefaultLang('LNGM');
// the lang to use, if the lang isn't available, it will use the current loader to get them
translate.use(userLang);
//translate.getTranslation(userLang); //I tried with and without this line, neither works`
My files names are called LNGM.
If en-US, it's only LNGM.json
In providers:
`{
provide : TranslateLoader,
useFactory: (http:Http) => new TranslateStaticLoader(http, 'uxApp/assets/lang', '.json'),
deps : [Http]
},`
All langauges files are in uxApp/assets/lang
All imports exist.
Why doesn't it go to the fallback option and tranlates to default if no translations exists in he-IL?
Thanks!
It should show the default yes, if the key exists in the default lang, otherwise it should only show the key.
But now that I think about it, I think that it only works if your default lang has been loaded, if you started the app with another lang and it doesn't has the key, and you never loaded the default lang then it probably doesn't work.
Yes, same problem for me. It works if I change language on the site. But going directly to a page that's missing a key it does not fallback to default.
As a workaround i'm forcing the default lang to be loaded (in the AppComponent constructor) :
this.translate.setDefaultLang(this.defaultLang);
this.translate.getTranslation(this.defaultLang).subscribe(() => {});
But i'm not sure this is safe. Anyway it is not ideal.
I have the same issue. The bundle for the default language is not even fetched.
Right now, the workaround described by Basemasta is doing the job.
I think the description for the defaultLang is deceiving. I also understand it as that it should return default translation (e.g. english) for all missing translations. But it is stated nowhere that the default translation must be loaded by user first, I think it would be also counter-intuitive to do so. It should be loaded automatically as soon as defaultLang is set.
This should be fixed in 5.0.0, the default lang will be loaded as well (even if it's not the current lang).