I'm trying to get translations work for both the app components and the lib components. For now I can only get app or lib components to work, but not at the same time.
Structure:
I can make the home component in the app work if I add import TranslateModule and provider in the libs/features/ui/ui.module.ts:
@NgModule({
imports: [
ReactiveFormsModule,
FormsModule,
CommonModule,
ToastrModule.forRoot(),
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: createTranslateLoader,
deps: [HttpClient]
}
})],
providers: [
{
provide: APP_INITIALIZER,
useFactory: appInitializerFactory,
deps: [TranslateService, Injector],
multi: true
}
],
declarations: [LoginComponent],
exports: [
LoginComponent
]
})
Now the translations for the login works, but the home component just writes out the translation key "home.greet"
I can also make the login component in the app work if I instead add the same import TranslateModule and provider in the libs/core/core.module.ts
If I add the code in the two different place at the same time, the login will only translate, but the home component again just writes out the translation key "home.greet"
Also it seems wrong to initialize it twice, so where should I put my import and provider for the translations and how should I manage to translate in both places?
I was able to solve the same with the folowing steps:
Update angular.json app->architect->build->options->assets with
{ "input": "libs/feature/src/assets/i18n", "glob": "*.json", "output": "assets/i18n/feature" }
Update Translate module forChild() in Lib->feature.module
TranslateModule.forChild({
loader: {
provide: TranslateLoader,
useFactory: (http: HttpClient) => {
return new TranslateHttpLoader(
http,
`${environment.i18nPrefix}/assets/i18n/feature/`,
'.json'
);
},
deps: [HttpClient]
},
isolate: true
})
setTranslateServiceLanguage$ = createEffect(
() =>
this.store.pipe(
select(selectSettingsLanguage),
distinctUntilChanged(),
tap(language => this.translateService.use(language))
),
{ dispatch: false }
);
Thanks to the below repo from tomastrajan https://github.com/tomastrajan/angular-ngrx-material-starter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With