ngx-translate / core

The internationalization (i18n) library for Angular
MIT License
4.53k stars 580 forks source link

Angular translation forchild not loading #1266

Open Totot0 opened 4 years ago

Totot0 commented 4 years ago

I have an angular application that uses ngx-translation. For this, I made a Shared module that loads a translation file containing different keys (general errors...) :

`@NgModule({ imports: [ TranslateModule.forChild({ loader: { provide: TranslateLoader, useFactory: TranslateFactory, deps: [HttpClient] } }), ], exports: [TranslateModule], })

export class TranslatorModule { constructor(translate: TranslateService){ translate.addLangs(['fr']); translate.setDefaultLang('fr');

const browserLang = translate.getBrowserLang();
translate.use(browserLang.match(/fr/) ? browserLang : 'fr');

} }

export function TranslateFactory(http: HttpClient) { return new TranslationService(http, 'assets/i18n'); }`

In my AppModule, I load this module and I also load another translation file via forRoot :

`@NgModule({ declarations: [ AppComponent, ], imports: [ BrowserModule, AppRoutingModule, SharedModule, TranslateModule.forRoot({ loader: { provide: TranslateLoader, useFactory: TranslateFactory, deps: [HttpClient] } }), ], bootstrap: [AppComponent] })

export class AppModule{

}

export function TranslateFactory(http: HttpClient) { return new TranslationService(http, 'assets/i18n/myApp'); }`

My problem is that only translateService.forRoot works. Translations from translateService.forChild are not loaded

Then I just use my shareModule in my loaded modules in lazy loading mode

Yqnn commented 3 years ago

I've the same issue, I think it's because the same TranslateService is shared in the 2 modules, and it uses the configuration given in 'TranslateModule.forRoot'.

eulersson commented 3 years ago

For lazy-loaded modules with different translation loaders (loading .json from different files) it seems to be either (in the case of the lazy-loaded):

It's like I can't blend the two.

I got pretty close though maybe you could have a look and play within StackBlitz: https://stackblitz.com/edit/translations-and-lazy-loading?file=README.md