Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 631
Allen Avatar asked Mar 09 '17 15:03

Allen


People also ask

How does Ngx-translate work?

NGX-Translate is an internationalization library for Angular. It lets you define translations for your content in different languages and switch between them easily. Check out the demo on StackBlitz. It gives you access to a service, a directive and a pipe to handle any dynamic or static content.

How do you translate parameters in NGX?

Create an entry in the translation file (assets/i18n/en. json). The string interpolation syntax {{ }} is used to indicate a named parameter.

What is translate pipe in Angular?

The service also contains a method called translate. It is later called by the pipe to get the translation for a specific key. It always uses the language specified in the 'language'-variable of the service, to lookup the translation. That's it.


1 Answers

import translate service and add in constructor

constructor(private translate: TranslateService){ } 

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

like image 147
Allen Avatar answered Sep 19 '22 11:09

Allen