5

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 ?

Community
  • 1
  • 1
undefined
  • 33,537
  • 22
  • 129
  • 198

1 Answers1

5

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.

Leon Adler
  • 2,993
  • 1
  • 29
  • 42
  • in regards to 'It looks as if the "en-GB" you are getting refers to the language of the user' do you know where that value would be coming from, neither my browser or OS seems to be set to the UK, they are both set to NZ. it sounds to me a little like I should preferentially use languages over language from what you are saying though it sounds a little weird to do so. Also my headers also state NZ `Accept-Language:en-NZ,en;q=0.8` – undefined Oct 18 '15 at 23:24
  • "en" is a language, "GB" and "NZ" are regions. Great confusion is created by combining the two in one word (either language, region or locale). Way back in the dark ages, such settings were called "language" and "regional preferences". The use of "locale" is a relatively recent misnomer: it refers to a place, typically a small area such as a village or landmark less than say 5km square. – RobG Oct 18 '15 at 23:54
  • @LukeMcGregor since moment.js does not have any date formats for en-NZ, you might have to add them yourselves (or go with en-GB). I just meant using the first part of the `Accept-Header` seems most accurate, and `navigator.language` is not necessarily the same. @RobG yes, strictly speaking, "en-US" is not a language, but a dialect. Hence the difference between "color" (en-US) and "colour" (en-GB). C#, for example, calls this a "culture", which is the best description I have found yet. Cultures may vary on a language/translation base (GB / US) and on a format base (DD/MM/YYYY vs. MM/DD/YYYY). – Leon Adler Oct 19 '15 at 00:03
  • yeah I added a variant to momentjs, https://github.com/moment/moment/pull/2683 for NZ, I have just been having trouble getting a reliable way of detecting the current culture in the browser to tell moment what to use. – undefined Oct 19 '15 at 01:39