Hi Reedrichards,
First of all, thank you very much for helping.
I have a simple app. On the homepage, user can click to change languages. Everything works fine before lazy loading.
After using lazy loading, language files are not loaded when clicking to switch language. Just an empty object:
{lang: "es", translations: {}}
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import {HttpClientModule, HttpClient} from '@angular/common/http';
import {TranslateModule, TranslateLoader} from '@ngx-translate/core';
import {TranslateHttpLoader} from '@ngx-translate/http-loader';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';
import { MyApp } from './app.component';
export function createTranslateLoader(http: HttpClient) {
return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}
@NgModule({
declarations: [
MyApp
],
imports: [
BrowserModule,
HttpClientModule,
IonicModule.forRoot(MyApp),
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: (createTranslateLoader),
deps: [HttpClient]
}
})
],
bootstrap: [IonicApp],
entryComponents: [
MyApp
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}
app.component.ts
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { TranslateService, LangChangeEvent} from '@ngx-translate/core';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage:any = 'HomePage';
constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, translate: TranslateService) {
translate.setDefaultLang('en');
translate.onLangChange.subscribe((event: LangChangeEvent) => {
console.log("lang changed detected", event);
translate.setDefaultLang(event.lang);
});
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
statusBar.styleDefault();
splashScreen.hide();
});
}
}
home.module.ts
import { TranslateModule} from '@ngx-translate/core'; //For lazy loading
import { HomePage } from './home';
@NgModule({
declarations: [
HomePage,
],
imports: [
IonicPageModule.forChild(HomePage),
TranslateModule.forChild() //For lazy loading
],
exports: [
HomePage
]
})
export class HomePageModule {}
home.component.ts
import { Component } from '@angular/core';
import { IonicPage, NavController, ModalController } from 'ionic-angular';
import { LangChangeEvent, TranslateService } from '@ngx-translate/core';
import { PopoverController } from 'ionic-angular';
@IonicPage()
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(public popoverCtrl: PopoverController, public navCtrl: NavController, public modalCtrl: ModalController, public translateService: TranslateService) {
}
translateToSpanish() {
this.translateService.use("es");
}
goToPage(page) {
this.navCtrl.push(page);
}
}
home.html
<ion-col col-sm-3 col-md-1>
<div>
<div>
<p><b>{{ 'languageLinkText' | translate }}</b></p>
</div>
<ul style="list-style-type:none; padding-left: 0pt;">
<li style="margin-bottom:5px;"><a style="text-decoration: none;" href="#" (click)="translateToEnglish()">{{ 'englishLinkText' | translate }}</a></li>
<li style="margin-bottom:5px;"><a style="text-decoration: none;" href="#" (click)="translateToDutch()">{{ 'dutchLinkText' | translate }}</a></li>
<li style="margin-bottom:5px;"><a style="text-decoration: none;" href="#" (click)="translateToSpanish()">{{ 'spanishLinkText' | translate }}</a></li>
<li style="margin-bottom:5px;"><a style="text-decoration: none;" href="#" (click)="translateToCNS()">{{ 'chsLinkText' | translate }}</a></li>
<li style="margin-bottom:5px;"><a style="text-decoration: none;" href="#" (click)="translateToCNT()">{{ 'chtLinkText' | translate }}</a></li>
<li style="margin-bottom:5px;"><a style="text-decoration: none;" href="#" (click)="translateToRussian()">{{ 'russianLinkText' | translate }}</a></li>
</ul>
</div>
</ion-col>