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?
Open the browser settings, and in the advanced section scroll down to find Languages . Open Language and Input Settings and add the language or language+region choice you want from the list available. Order the resulting list so that it is in descending order of preference. You don't need to restart Chrome.
In this article, we'll go through three different ways of detecting a user's locale: through the browser's navigator. language s (on the client) object, through the Accept-Language HTTP header (on the server), and through geolocation using the user's IP address (on the server).
By using JavaScript, this task can be easily done by using: Languages property is available for the navigator interface, which returns the most preferred / user preferred language set in the web browser.
The Navigator language property is used for returning the browser's language version. It is a read-only property and it returns a string representing the language version of the browser.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With