Core: currentLang is undefined

Created on 3 Apr 2017  路  3Comments  路  Source: ngx-translate/core

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

Most helpful comment

You only get currentLang once this.translateService.use has resolved (which means that the lang has really been loaded).

All 3 comments

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

  }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

gmquiroga picture gmquiroga  路  3Comments

jellene4eva picture jellene4eva  路  3Comments

egornoveo picture egornoveo  路  4Comments

jvquarck picture jvquarck  路  3Comments

chris31389 picture chris31389  路  3Comments