I’m using the ngx-translate plugin, and when I start my application, I display a ion-select that allows the user to choose the language of my app.
The plugin detects the user language, so I would like my ion-select to display this language as a selectedItem / default value.
The problem is : the plugin takes a little time to get the user language, so when I use translate.currentLang in my ionViewDidLoad(), I get an undefined value, and my ion-select don’t have any selectedItem.
I tried using a setTimeout(() => { this.lang = this.translate.currentLang}, 1000), it works but maybe there is a better solution…
Do you have an idea on how to call translate.currentLang as soon as it is no longer an undefined value ?
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage:any = BienvenuePage;
constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, private translate: TranslateService,) {
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();
translate.addLangs(["en", "fr"]);
translate.setDefaultLang('en');
let browserLang = translate.getBrowserLang();
translate.use(browserLang.match(/en|fr/) ? browserLang : 'en');
});
}
}
Gotcha, okay. Thanks, sorry never used the plugin before. So as you say your timeout works, and it’s a simple solution, but there are two possible downsides. The most likely is that you make your user wait a full second when the language loading only takes like 100ms or something. The less likely one is that it takes longer than one second to load the language and then your item is still undefined.
There are a couple ways to do this, but I would suspect you could simply just move all your translate related code outside the platform.ready block. Platform ready is only for native code related plugins, which this is not. So there’s no reason for you to wait for platorm ready, just do it in the constructor and it should work.
If for some reason that doesn’t work (it should), the translate service has an onLangChanged method you can subscribe to:
ionViewDidLoad() {
this.translateService.onLangChange.subscribe((event: LangChangeEvent) => {
this.langApp = event.lang; // or you could keep the assignment from currentLang
});
}