Switch language at runtime?


#1

How to switch language at runtime. I am not able to switch language using:

import { TranslateService } from “@ngx-translate/core”;

export class SomeClass {
constructor(private translate:TranslateService){
}

changeLanguage=()=>{
this.translate.setDefaultLang(“en”);

if (this.translate.getBrowserLang() !== undefined) {
  this.translate.use(this.translate.getBrowserLang());
} else {
  this.translate.use("hi");
}

}
}
}


#2

Where does this come from?


#3

import { TranslateService } from “@ngx-translate/core”;

export class SomeClass {
constructor(private translate:TranslateService){
}

changeLanguage=()=>{
this.translate.setDefaultLang(“en”);

if (this.translate.getBrowserLang() !== undefined) {
  this.translate.use(this.translate.getBrowserLang());
} else {
  this.translate.use("hi");
}

}
}
}


#4

Then this has nothing to do with Ionic Native, changed the category.


#5

I’m using this ion-select component

      <ion-select [(ngModel)]="language" (ionChange)="switchLanguage()">
        <ion-option value="de">Deutsch</ion-option>
        <ion-option value="es">Español</ion-option>
        <ion-option value="en">English</ion-option>
        <ion-option value="fr">Français</ion-option>
      </ion-select>

And in your ts just

language: string = "es";
switchLanguage() {
    this.translate.use(this.language);
  }

#6

I am doing same but it is not working. Can you please tell ? At how many places you are importing TranslateModule ?


#7

Only app.module.ts on imports array.
Can you please show me you app.module.ts file?


#8

These line are only available in app.module.ts in imports:[]

TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: (createTranslateLoader),
deps: [HttpClient]
}
})