21

I am determining the current locale of the browser using this API:

var language = window.navigator.userLanguage || window.navigator.language;

This return "fr-FR" in IE, but it returns just "fr" in Chrome (and similarly for other locales).

Is there another API that will return "fr-FR" in Chrome as well?

We rely on this to load the appropriate culture files.

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Amar
  • 1,906
  • 2
  • 16
  • 26
  • Just as a tip, if you're planning to rely on this information to actually figure out the user locale, I wouldn't recommend it. Most of the users never change this setting, so it'll probably find `en-US`, which is the default value. – emerson.marini Sep 01 '14 at 13:27
  • Please answer on this post http://stackoverflow.com/questions/1043339/javascript-for-detecting-browser-language-preference – Mudaser Ali Sep 01 '14 at 13:38
  • Does this answer your question? [Best way to determine user's locale within browser](https://stackoverflow.com/questions/673905/best-way-to-determine-users-locale-within-browser) – Penny Liu May 12 '20 at 06:35

2 Answers2

23

Update:

As of Sept 2021 Chrome has pretty extensive support for the Intl library.

function getClientLocale() {
  if (typeof Intl !== 'undefined') {
    try {
      return Intl.NumberFormat().resolvedOptions().locale;
    } catch (err) {
      console.error("Cannot get locale from Intl")
    }
  }
}

Otherwise you can fallback to the window.navigator object:

Firefox and Chrome have a languages array on the navigator object. Normally the first element in that array is the users chosen locale (normally inherited from the OS settings)

var language;
if (window.navigator.languages) {
    language = window.navigator.languages[0];
} else {
    language = window.navigator.userLanguage || window.navigator.language;
}

The navigator.languages array in chrome for me is ["en-GB", "en-US", "en"] whereas navigator.language = "en-US", slightly different but important

bm_i
  • 568
  • 5
  • 9
  • 5
    This question was about getting the locale, not only the language. A locale contains much more information than only the language (most importantly date-time and number-formatting settings). – exhuma Feb 05 '19 at 09:04
  • @exhuma, maybe so, but it's apparent that the OP asked about the locale string – Eliran Malka Aug 11 '19 at 11:07
  • @exhuma, you don't pass around the entire locale pack. It's just the locale id, usually the Unicode one, that gets passed around (e.g. `en-GB`). See W3's doc: https://www.w3.org/TR/ltli/#locale and MDN's: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale to consume, you can pass that ID to `.toLocaleString()` of different objects, like `Date`. – Aidin Nov 14 '21 at 08:27
  • Other `Intl` constructors work as well, e.g. `Intl.DateTimeFormat().resolvedOptions().locale` or `Intl.Collator().resolvedOptions().locale`. – mrienstra Jan 21 '23 at 20:08
  • For me here in AU ``getClientLocale()`` returns 'en-US'. Whereas the navigator. version returns 'en-AU' – nevf May 12 '23 at 05:35
5

In the latest versions of Chrome browser,
it var language = window.navigator.userLanguage || window.navigator.language; returns 'fr-FR'.

If you are still facing the same issue, you can try var language = navigator.language;

Raveendra007
  • 1,060
  • 1
  • 8
  • 13
  • 1
    This returns strictly the user language, but **not** their locale. For instance, i have a browser setting where all of the above return `de-DE`, whilst `Intl.DateTimeFormat().resolvedOptions().locale` returns `en-US`. So, sorry to say, but this answer does not help the OPs question. – Philzen Jun 05 '22 at 19:56