Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving browser language in chrome

I've been trying to get momentjs to correctly detect the browser language and localise the time displays. (as per Locale detection with Moment.js)

But I have been having a bit of trouble getting the correct locale out of chrome.

It seems like chrome has the correct list of languages configured for my browser in navigator.languages but navigator.language is returning something completely different.

enter image description here

I'm guessing either I have chrome setup wrong (which doesn't seem likely given the correct languages are in navigator.languages) or the method for selecting the browser language isn't quite right?

Is there a different way I should be using to get the language other than window.navigator.userLanguage || window.navigator.language ?

like image 448
Not loved Avatar asked Oct 18 '15 22:10

Not loved


1 Answers

The W3C draft states that:

  • navigator.language is the user's "preferred language" or a "plausible language". This is not necessarily the same as navigator.languages[0].
  • navigator.languages lists the "user's preferred languages, with the most preferred language first". It is expected to have the same value as the Accept-Language header.

By the looks of quickly experimenting in a VM, navigator.languages[0] leads to the most accurate results, navigator.language being the language of the OS.

It looks as if the "en-GB" you are getting refers to the language of the user, whereas the "en-NZ" refers to the user locale (number & date/time formatting).

Since this is still a draft, the results you get may change with implementation.

like image 62
Leon Adler Avatar answered Sep 27 '22 19:09

Leon Adler