Browser language detection [duplicate]
Asked Answered
A

1

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?

Aubreir answered 28/4, 2016 at 12:6 Comment(2)
#1043839Exudation
Not sure this is a duplicate - Angular 2/4 has a special injectable to help with localization, LOCALE_ID: angular.io/api/core/LOCALE_IDInduction
A
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

Accipitrine answered 28/4, 2016 at 12:8 Comment(4)
@Aubreir FYI, navigator.language is the standard, according to specs. userLanguage is a non-standard implementation in some browsers, and the only one in IE (besides browserLanguage).Pandean
Thank you for your time and informations.Rodeo
Property 'userLanguage' does not exist on type 'Navigator'.Stafani
const lang = navigator.language || window.navigator.language; Worked for meDaffie

© 2022 - 2024 — McMap. All rights reserved.