Allen
Allen

Reputation: 2177

How to get current language with ngx-translate in angular2

I want to get current language with this.translate.getBrowserLang(), but it always return 'en', whatever the current language is and what the supported languages is.

Here is the plnkr: https://plnkr.co/edit/JEXgj8WcISYGtDg75VoR?p=preview,


maybe plnkr has broken, you can try stackblitz: https://stackblitz.com/edit/github-kk1mud-a7gxbb

Upvotes: 49

Views: 56165

Answers (3)

zeroquaranta
zeroquaranta

Reputation: 412

if you want to get a valid language, even though you never set one, make sure to set the default language when you import the ngx-translate module

app.module.ts

...
@NgModule({
   imports: [
      BrowserModule,
      TranslateModule.forRoot({
         defaultLanguage: 'en'
      })
   ],
   bootstrap: [AppComponent]
})
export class AppModule { }

Upvotes: 0

Alireza Ebrahimkhani
Alireza Ebrahimkhani

Reputation: 551

constructor(private translate: TranslateService){}

ngOnInit() {
    this.translate.currentLang;
}

Upvotes: 4

Allen
Allen

Reputation: 2177

import translate service and add in constructor

constructor(private translate: TranslateService){
}

I find it use this.translate.currentLang to get the current language

Upvotes: 86

Related Questions