Yes or you could also do it with an observable/subject/provider. There you will not have to declare it again in your modules.
I did implement a simple example of that in the following demo app for the Ionic Zürich Meetup
See app.component.ts and providers/i18n-switcher/i18n-switcher.ts
thanks, had the same issue… this fixed it… Mercie
Xiwi
August 29, 2018, 10:57am
24
instead SettingsPage can you have myApp? I can run the server without forRoot:
Could not find IonicModule.forRoot call in “imports”
bitti
October 17, 2018, 7:00am
25
Thanks, I had the same issue. This fixed it!
this solution worked with me.
For information for whoever would stumble upon this while searching for Ionic 4 , the solution given by @reedrichards here is still the one you seek Ngx translate is broken when used with ionic lazy loading
Based on @reedrichards and @valentim solutions, I have tested a simple provider to change the language. Then you don’t need to import TranslateLoader, createTranslateLoader and HttpClient at every page module and you only need to import the provider.
It looks like this:
create the following file at providers/i18n/i18n.ts
import { Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
@Injectable()
export class I18nProvider {
constructor(private translate: TranslateService) {}
public switchLanguage(lang: string) {
this.translate.use(lang);
}
}
add the provider at app.module.ts
import { I18nProvider } from '../providers/i18n/i18n';
@NgModule({
providers: [
I18nProvider
]
})
at your page module.ts file
just use the regular TranslateModule.forChild(), without any loader, as instructed in the documentation
at your page component .ts file
add the following import:
import { I18nProvider } from '../../providers/i18n/i18n';
add the provider at the constructor:
constructor(
private i18n: I18nProvider
)
and when you need to switch languages, just call
this.i18n.switchLanguage('en'); // Example for english language
That’s it!
You just need to import and use this I18nProvider at any pages that need to switch languages!
this is best solution sir but after jump to new page langage is not translated why?
Weird. I actually haven’t tested it jumping to new page after switching language. I will check it and reply back here.
Ok sir please chek it and let me know.
Because of lazy loading, the modules has different injectors. you should define ngxTranslateModule in the app.module.ts and register it using forRoot static method , then in each lazy loaded module or in the shared module if you use, you should use forChild method without passing and arguments to it .
For more info, read this interesting section
I have also uploaded my code with working example in the following gists
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';
/* third parties */
import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import {TranslateHttpLoader} from "@ngx-translate/http-loader";
import {ENV} from '@app/env';
import {TranslateLoader, TranslateModule} from "@ngx-translate/core";
import {HttpClient, HttpClientModule} from "@angular/common/http";
This file has been truncated. show original
shared.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { IonicModule } from '@ionic/angular';
import { TranslateModule } from "@ngx-translate/core";
import {ConvertNumbersPipe} from "../pipes/convert-numbers/convert-numbers";
@NgModule({
imports: [
IonicModule,
This file has been truncated. show original
login.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { Routes, RouterModule } from '@angular/router';
import { IonicModule } from '@ionic/angular';
import { LoginPage } from './login';
import {SharedModule} from '../shared'
import {TranslateLoader, TranslateModule} from "@ngx-translate/core";
import {HttpClient} from "@angular/common/http";
import {TranslateHttpLoader} from "@ngx-translate/http-loader";
This file has been truncated. show original