46

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

Allen
  • 2,037
  • 3
  • 15
  • 21

2 Answers2

82

import translate service and add in constructor

constructor(private translate: TranslateService){
}

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

NiklasPor
  • 9,116
  • 1
  • 45
  • 47
Allen
  • 2,037
  • 3
  • 15
  • 21
4
constructor(private translate: TranslateService){}

ngOnInit() {
    this.translate.currentLang;
}
  • not sure why it's giving me `undefined` – Ricky Levi Mar 14 '23 at 08:01
  • 1
    @RickyLevi you need to set the language first using this.translate.use() and using the string of the language as a parameter ('es', 'en', etc.). If you have not set the language, or it it's overwritten or lost during the app's lifecycle, currentLang will be undefined – JurgenBlitz May 04 '23 at 08:06