core
core copied to clipboard
Bug: Do not load file's translations after using setTranslation() when using use() to change language
[x] bug report => check the FAQ and search github for a similar issue or PR before submitting [ ] support request => check the FAQ and search github for a similar issue before submitting [ ] feature request
Current behavior
Actually, when we add translations with setTranslation method for a language we have not load yet translations, the translations from his file are not loaded when we decide to use this language.
Expected behavior
All translations, from setTranslation method calls and from translation's file must be accessibles.
How do you think that we should fix this?
translate.service.ts - retrieveTranslations method - line 227
// if this language is unavailable, ask for it
if (typeof this.translations[lang] === "undefined") {
I think we have to add one attribute to store information if we have always merge translations added by setTranslation method per language and if it's equal to true, load language file when we use it.
Minimal reproduction of the problem with instructions
Add two languages with translation's files in your app, (module, xx.json, etc.) In your place to call use() method :
translate.use(FIRST_LANGUAGE);
translate.setTranslation(FIRST_LANGUAGE, { TEST: 'Test First'}, true);
translate.setTranslation(SECOND_LANGUAGE, { TEST: 'Test Second'}, true);
translate.use(SECOND_LANGUAGE);
Translations from SECOND_LANGUAGE's file will not be load.
Environment
ngx-translate version: 10.x
Angular version: 6.0.x
Browser: All
Bumping this issue, having the some problem. A year later. Is there any known fix for this?
Do you guys solved it somehow? I need to find a fix or workaround for this at my application, too.
EDIT: I fixed it for my application by using setTranslation
only after the language is initialized. This means I need to subscribe to onLangChange
and add my dynamic additional translation there.
Really annoying issue. I solved it with onLangChange, too. but it requires quite some boiler plate code to prevent using setTranslation() twice for the same language and checking which language is already set with translateService.currentLang.
In my case, one setTranslation works, but not 2 (like one in french works, but when I try to add another setTranslate with english it doesn't work)
I just found out, Add extend in your config
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: httpLoaderFactory,
deps: [HttpClient]
},
extend: true
}),```
This allows the json to be loaded in
https://github.com/ngx-translate/core/blob/master/packages/core/lib/translate.service.ts#L237
I still have to figure out if it breaks something, but my json loads :)