Sunday, September 30, 2018

Javascript Browser language check with basics and examples

In this blog post, We are going to get/check browser language detection in javascript with examples.

Browser language in javascript

In javascript, the NavigatorLanguage class has properties for knowing the language of a browser or CLient UI.
There are two read-only properties to retrieve browser language details 

NavigatorLanguage.language  property 

It is a property used to tell the language of the client browser.
This property is always useful to check below things
  1. Delivering personalized regional information based on the user language preferences
  2. It will be useful for localization of your application
The possible values returned with this property are "en","en-US"...etc
console.log(navigator.language) // output en-U

NavigatorLanguage.languages property

It is a property to return a list of client browser preferred languages based on user preferred language comes first in the order
console.log(navigator.languages) // ["en-US", "en"]

javascript Example

The below are different usage examples in various browsers.
Chrome -  to get User preferred language of browser UI  
console.log(window.navigator.language)
console.log(window.navigator.languages)
output is
 
en-US
["en-US", "en"]
Firefox - to get User preferred language of browser UI
console.log(window.navigator.language)
console.log(window.navigator.languages)
output is
 
en-US
["en-US", "en"]
Internet Explorer or IE - To get Browser Language 
In IE older browsers versions less than 10 version, It always returns "en-us". In latest versions, It returns "en-US"
console.log(window.navigator.userLanguage)
console.log(window.navigator.browserLanguage)
console.log(window.navigator.systemLanguage)
Output is
en-US
en-US
en-US
Safari - to get User preferred language of browser UI 
In safari older browser versions less than 10 version, It always returns "en-us". In latest versions, It returns "en-US".
console.log(window.navigator.language)
console.log(window.navigator.languages)
output is
 en-US
["en-US", "en"]

Related article


EmoticonEmoticon