I am using Ionic 3 lazy loaded modules with ngx-translate. I am having an issue where most of my languages .json translation files aren’t being loaded when I switch to them. Might be an Ionic lazy loading issue so I decided to post here.
in my main module (app.module.ts):
// AoT requires an exported function for factories
export function createTranslateLoader(http: Http) {
return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}
@NgModule({
declarations: [...],
imports: [
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: (createTranslateLoader),
deps: [Http]
}
})
]
})
In my main component (app.component.ts):
constructor(translate: TranslateService) {
translate.addLangs(['en', 'fr', 'es', 'hi']);
translate.setDefaultLang('en');
translate.use('es');
// Listen for changes
translate.onLangChange.subscribe((event:LangChangeEvent) => {
console.log('onLangChange', event.translations));
}
}
Then I have a page set up where I can change the language (settings.ts):
constructor(public translate: TranslateService) {}
changeLang(value:string) {
this.translate.use(value);
}
My issue is that when I switch to the language en or es, the language translations load as I can see in translate.onLangChange, but when I try the other languages that are not in my main app component as default or use, the translations are an empty object… I really can’t figure out why.
My translations are located in src/assets/i18n:
en.json
es.json
fr.json
hi.json
Anyone have any experience with ngx-translate and ionic3? I followed the ngx-translate guide multiple times. Just seems like if the language is not used immediately, the language cannot be loaded after the the app is ready.