[Solved] Ionic 3 lazy loading - some languages not found
See original GitHub issueI’m submitting a … (check one with “x”)
[ ] bug report => check the FAQ and search github for a similar issue or PR before submitting
[X] support request => check the FAQ and search github for a similar issue before submitting
[ ] feature request
Current behavior
- 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.
Expected/desired behavior
- Switching from one language file to the other should read from the JSON file and load the translations. Right now the translations object loads as empty when subscribed to
translate.onLangChange
Reproduction of the problem
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 translations are located in src/assets/i18n
:
en.json
es.json
fr.json
hi.json
What is the expected behavior?
- Switching from one language file to the other should read from the JSON file and load the translations. Right now the translations object loads as empty when subscribed to
translate.onLangChange
What is the motivation / use case for changing the behavior?
Please tell us about your environment:
ionic serve
-
ngx-translate version: 6.0.1
-
Angular version: 4.1.2
-
Browser: Chrome, Android Web Browser, iOS Safari
Issue Analytics
- State:
- Created 6 years ago
- Comments:8
Top Results From Across the Web
Ngx-translate and lazy loading - some languages not found
I am using Ionic 3 lazy loaded modules with ngx-translate. I am having an issue where most of my languages .json translation files...
Read more >Language Normalization with Lazy Loading in Ionic3
1 Answer 1 · Import MissingTranslationHandler from @ngx-translate/core · Export a custom class called MyMissingTranslationHandler as shown below.
Read more >Lazy loading with Ionic 3 - Mastering Ionic
Ionic 3 + allows developers the opportunity to organise their application into different modules and then “lazy load” those sections as and where...
Read more >NG8003: No directive found with export - Angular
Angular can't find a directive with {{ PLACEHOLDER }} export name. This is common with a missing import or a missing exportAs on...
Read more >Ngx-translate and lazy loading - some languages not ... - Ionic Forum
module"; import {Http} from "@angular/http"; @NgModule({ declarations: [...], imports: [ IonicPageModule.forChild(SettingsPage), TranslateModule.forChild({ ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have the same problem,but it worked when i imported the TranslateModule twice.
app.module.ts:
app.component.ts:
shared.module.ts:
home.module.ts:
home.ts:
got any better idea?
@zim-lee This solve the issue, thanks so much.
However - a few things:
Only the page that is changing the language needs to have the second loader in it: (
TranslateModule.forChild({...}
). All other pages just needTranslateModule.forChild()
Instead of having 2
TranslateLoaderFactory
… you can just import the factory from yourapp.module.ts
So in the
settings.ts
page where I have the control to change the language I have this:and then of course the default set up stuff in the app.module and app.component files. (Link to Ionic ngx-translate setup guide)