27

I need in my Angular2 app detect browser language. Based on this language I need to send request (to a REST API of backend) with localization and IDs of my variables, which I need to translate. After that I received response with translated variables.

So the app workflow is to detect browser language, ok it is for example en-US, after that I am going to sent request to backend give me lang for en-US for variables with IDs 1,2,3,4,5. The response is {{id:1, var:pay}, {id:1, var:title}} etc.

So how can I detect with Angular2 (developed with typescript) browser language?

Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87
Loutocký
  • 822
  • 2
  • 15
  • 28

1 Answers1

69
var userLang = navigator.language || navigator.userLanguage; 

2021 Edit

Looks like userLanguage does not exist anymore (at least with Typescript lib).

And it's not on docs : just use navigator.language

MDN DOC

BorisD
  • 1,611
  • 17
  • 22
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 4
    @Loutocký FYI, `navigator.language` is the standard, according to [specs](http://w3c.github.io/html/webappapis.html#language-preferences). `userLanguage` is a non-standard implementation in some browsers, and the only one in [IE](https://developer.mozilla.org/en-US/docs/Web/API/NavigatorLanguage/language#Browser_compatibility) (besides `browserLanguage`). – John Weisz Apr 28 '16 at 12:17
  • 2
    Thank you for your time and informations. – Loutocký Apr 28 '16 at 12:24
  • 3
    Property 'userLanguage' does not exist on type 'Navigator'. – Emeric Sep 15 '19 at 08:08
  • 2
    const lang = navigator.language || window.navigator.language; Worked for me – Shahid Hussain Abbasi Sep 25 '20 at 09:04