question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

translate.use(langCode) is not working in some cases

See original GitHub issue

Current behavior

In app.component.ts I have set up the TranslateService as follows:

translate.addLangs(['en', 'bn']);
translate.use('en');

translate.onLangChange.subscribe((change) => {
      console.log('langChange', change);
})

In another module, I’m doing this to change the current language,

this.translate.use(langCode);

When I change the language to bn, it does not work.

BUT, if I write translate.use('bn'); in app.component.ts, language change starts working.

What I’ve noticed is that, when I set en as the language in app.component.ts the onLangChange prints an empty object as the translations property.

// in onLangChange callback 
Object { lang: "bn", translations: {} }

Expected behavior

Changing language should work regardless of what language was set during initialization

How do you think that we should fix this?

Minimal reproduction of the problem with instructions

Environment


ngx-translate version: 13.0.0
Angular version: 10.1.5


Browser:
- [ ] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ x] Firefox version 81
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: 12.16   
- Platform:   Windows

Others:

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:8
  • Comments:8

github_iconTop GitHub Comments

5reactions
sguillycommented, Jan 6, 2021

This workaround works for me (angular 11 et ionic 5) :

I force to load all possible language in app.components :

this.translate.addLangs([‘fr’, ‘en’, ‘es’, ‘de’])

this.translate.use(‘en’) this.translate.use(‘es’) this.translate.use(‘de’) this.translate.use(‘fr’)

then in other component, i can call again this.translate.use(‘xxx’)

This issue was not present in previous version for angular 8

2reactions
joseph-navantcommented, Mar 16, 2021

Hi guys! I also noticed this “issue”. After a few attempts, I got it by using same loader config in forChild as in forRoot:

app.module.ts (can be core.module or whatever):

export const HttpLoaderFactory = (httpClient: HttpClient) => {
  return new ...
};

@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [
    ...,
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: HttpLoaderFactory,
        deps: [HttpClient],
      },
    }),
  ],
  providers: [
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
  ],
  bootstrap: [AppComponent],
})
export class AppModule {}

app.component.ts:

export class AppComponent {
  constructor(private readonly translateService: TranslateService) {
    this.setDefaultLang();
  }

  private setDefaultLang() {
    this.translateService.addLangs(['en', 'fr', 'es-ES', 'es-419']); // array of available langs
    this.translateService.setDefaultLang('es-ES');
  }
}

lang-test-module.ts (the module from where we want to change lang):

import { HttpClient } from '@angular/common/http';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { HttpLoaderFactory } from 'src/app/app.module.ts';
...
@NgModule({
  imports: [
    ...,
    TranslateModule.forChild({
      loader: {
        provide: TranslateLoader,
        useFactory: HttpLoaderFactory,
        deps: [HttpClient],
      },
    }),
  ],
  declarations: [LangTestPage],
})
export class LangTestModule {}

I don’t know if it is a good solution…

Read more comments on GitHub >

github_iconTop Results From Across the Web

ngx-translate is not working for lazy loaded module
The Problem. Calling TranslateService.use('[LANG-CODE]') does change the translation in my menu-bar component.
Read more >
Cannot save translated nodes after upgrading to 8.8 ... - Drupal
Problem /Motivation Hm. Perhaps I'm doing sth. wrong, but after upgrading a few sites to Drupal 8.8 I found that I cannot save...
Read more >
How to translate your Angular app with ngx-translate
1. Add ngx-translate to your Angular application 2. Set up the TranslateService in your app.module.ts 3. Create your main language translation file (in...
Read more >
Translation | Django documentation
Installing a global _() function causes interference. Explicitly importing gettext() as _() avoids this problem. What functions may be aliased as ...
Read more >
ng2-translate - Bountysource
Another moment when i use like this it will not translate ... It had worked for me on AngularJS with my library ocLazyLoad...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found