translate directive doesn't update the text when changing the language via use() function
See original GitHub issueHello,
First of all, thanks for the wonderful library. It was very easy and straight forward to get it up and running.
The issue
Unfortunately, I do have an issue when I try to switch to another language. The translate directive doesn’t update the texts. Texts that are translated via the pipe do get updated.
In my AppComponent, I set the inital language to ‘nl’.
export class AppComponent {
constructor(private translateService: TranslateService) {
// this language will be used as a fallback when a translation isn't found in the current language
translateService.setDefaultLang('en');
// the lang to use, if the lang isn't available, it will use the current loader to get them
translateService.use('nl');
}
}
In my view, there’s
<div [translate]="'member.firstname'">First Name</div>
<div>{{ 'member.firstname' | translate }}</div>
Somewhere in my application, I have a button to switch the language to english, which calls
this.translateService.use('en');
When I click the button, the pipe does its thing, but the directive does not.
Presumable cause
When checking translate.directive.ts, I noticed that the node.lastKey is always equal to key, and that’s why the translation does not occur.
updateValue(key: string, node: any, translations: any) {
if(key) {
let interpolateParams: Object = this.translateParams;
if(node.lastKey === key && this.lastParams === interpolateParams) {
return;
}
When I comment it out (the check “if (node.lastKey …”), the translation does occur and it works fine. The if statement is probably there for a reason, but I guess that the fact the language changed should be added to the check as well.
I use version of g2-translate, and version 2.3.1 of angular.
Could you please have a look at this?
Thanks, Jeroen.
Issue Analytics
- State:
- Created 7 years ago
- Comments:26
Top GitHub Comments
As everyone I have a same issue where the translations are getting updated with translate pipe but not with other methods like .instant() or .get(); The only work around seems to be working is below
Is this solved or can anyone suggest better workaround here?
I have a similar issue with:
I have a template like you suggest:
<h2 [translate]="'WELCOME_HELLO_TEXT'">test</h2>
and I have 4 languages (en.json, fr.json, es.json and ga.json)
initTranslate()
function on my app.components.tstranslate.use('fr')
When I debug the app on the browser I found the next problem:
If my current language is English (en) and I change to another one (fr, es or ga) the translation isn’t updated on the template until I reload the page (F5). (The template show the untranslated text like WELCOME_HELLO_TEXT)
If my current language is Français, Galego or Español I can change to English and the template gets updated accordingly but I cannot change these combinations:
For the last 3 combinations I need to reload the page with F5 because when I change language I see the raw text like WELCOME_HELLO_TEXT
Any clue what I’m doing wrong ? The thing I cannot understand is why changing to English always works but changing to another language doesn’t, and If I reload the page all languages works.
This is my
initTranslate()
on my app.components.ts:and this is how I init ngx-translate on my app.module.ts: