admin管理员组

文章数量:1323323

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 pletely different.

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 ?

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 pletely different.

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 ?

Share edited May 23, 2017 at 10:30 CommunityBot 11 silver badge asked Oct 18, 2015 at 22:56 undefinedundefined 34.3k23 gold badges134 silver badges202 bronze badges 2
  • en-GB is the proper language/locale, it stands for english great britain. – Wobbles Commented Oct 18, 2015 at 23:07
  • Language is pletely independent of "locale". But don't tell the W3C that. – RobG Commented Oct 18, 2015 at 23:46
Add a ment  | 

1 Answer 1

Reset to default 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.

本文标签: javascriptRetrieving browser language in chromeStack Overflow