I'm submitting a ... (check one with "x")
[ x] bug report => check the FAQ and search github for a similar issue or PR before submitting
[ ] support request => check the FAQ and search github for a similar issue before submitting
[ ] feature request
Current behavior
this.translateService.currentLang is undefined.
Here's my code:
app.component.ts:
...
import { TranslateService } from 'ng2-translate';
...
@Component({
templateUrl: 'app.html'
})
export class MyApp {
@ViewChild(Nav) nav: Nav;
rootPage: any = WeightlevelPage;
pages: Array<{title: string, component: any}>;
pageComponent: any;
private admobid: any;
constructor(
public platform: Platform
, public statusBar: StatusBar
, public splashScreen: SplashScreen
, public translateService: TranslateService
){}
initializeApp() {
this.platform.ready().then(() => {
this.translateService.setDefaultLang('en');
console.log('tratratar', this.translateService.currentLang)
if (this.globalization) {
this.globalization.getPreferredLanguage()
.then(res => {
console.log("res", res)
this.translateService.use((res.value).split('-')[0])
.subscribe(
data => console.log('translateService.use SUCCESS -> ' + data),
error => console.log(' translateService.useERROR -> ' + error)
);
})
.catch(e => console.log('ERROR -> ' + e));
}
....
Please tell us about your environment:
ionic info
Cordova CLI: 6.5.0
Ionic Framework Version: 2.2.0
Ionic CLI Version: 2.2.1
Ionic App Lib Version: 2.2.0
Ionic App Scripts Version: 1.1.4
ios-deploy version: Not installed
ios-sim version: Not installed
OS: Linux 4.4
Node Version: v7.4.0
Xcode version: Not installed
ngx-translate version: 5.0.0
Angular version: 2.4.8
Browser: Chromium
You only get currentLang once this.translateService.use has resolved (which means that the lang has really been loaded).
Thanks @ocombe, that solves the problem.
Thanks, I did set the default language, but not the use. I only set the use when the language was changed. Now it works:
ngOnInit() {
//maybe later we can set the language based on the user language.
this.translate.setDefaultLang(this.translate.getBrowserLang());
this.translate.use(this.translate.getBrowserLang()); //--> I didn't had this line before
}
Most helpful comment
You only get currentLang once
this.translateService.usehas resolved (which means that the lang has really been loaded).