I am making ionic application 2 with angular 2 and typescript, and I had to use the globalization plugin offered by ionic / Cordova. The plugin always encounters an error when determining the preferred language the second time the application loads.
So, the error scenario is as follows: I change the language of my phone to French from English and run my application. The application works flawlessly and the plugin detects that I am using French. I close the application and open it again, then the plugin detects an error and catches it, and then displays it as a warning.
This happens all the time.
here is the code i use to determine the language:
let ln=''; this.globalization.getPreferredLanguage() .then(res => {ln =res.value; console.log(ln); if(ln.substring(0,2) ==='fr'){ this.translate.setDefaultLang('fr'); this.translate.reloadLang('fr'); localStorage.setItem("lang","1"); }else{ if(ln.substring(0,2)==="en"){ this.translate.setDefaultLang('en'); this.translate.reloadLang('en'); localStorage.setItem("lang","2"); }else{ this.translate.setDefaultLang('en'); this.translate.reloadLang('en'); localStorage.setItem("lang","2"); } } }) .catch(e => { console.log(e); this.translate.setDefaultLang('en'); this.translate.reloadLang('en'); localStorage.setItem("lang","2"); let altert= this.AlertC.create({ title: "Language selection problem" , message: "We have encountered an error in identifying your preferred language, so we put it to english as default.", buttons: [ { text: 'OK', handler: () => { console.log('Cancel clicked'); } }, ] }); altert.present(); });
Any ideas what could be the problem?
angular cordova globalization ionic2
Med mansour
source share