Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browser default locale - Intl.DateTimeFormat vs navigator.language

When coding a website and formatting a date, I want to use the locale that the user has set in their browser.

So for example, if a user has customised their chrome://settings/languages setting in Chrome to something non-default, that's the value I want to use.

However, when I run the code below in the console of such a browser, I get two different values.

 [window.navigator.language, new Intl.DateTimeFormat().resolvedOptions().locale]
// Array [ "en-AU", "en-US" ]

navigator.language gives me the expected value, but new Intl.DateTimeFormat().resolvedOptions().locale does not.

MDN for both items seems (to me) to indicate that they should be returning the same value:

  • NavigatorLanguage.language

    returns a string representing the preferred language of the user

  • Intl.DateTimeFormat locale parameter

    To use the browser's default locale, omit this argument or pass undefined.

Should both of these calls be returning the locale the user configured in their browser settings?

like image 802
Jono Job Avatar asked Aug 26 '19 23:08

Jono Job


People also ask

What is navigator language?

The Navigator language property is used for returning the browser's language version. It is a read-only property and it returns a string representing the language version of the browser. Some of the possible language codes are : en-US. fr.

What is Intl DateTimeFormat () resolvedOptions () timezone?

The Intl. DateTimeFormat. prototype. resolvedOptions() method returns a new object with properties reflecting the locale and date and time formatting options computed during initialization of this Intl. DateTimeFormat object.

What does browser locale mean?

Locale is a set of language- or country-based preferences for a user interface. A program draws its locale settings from the language of the host system. Among other things, locales represent paper format, currency, date format, and numbers according to the protocols in the given region.

How do I find my browser locale?

In this article, we'll go through three different ways of detecting a user's locale: through the browser's navigator. language s (on the client) object, through the Accept-Language HTTP header (on the server), and through geolocation using the user's IP address (on the server).


2 Answers

I'm using macOS and found that Chrome's navigator.languages reflects the browser language setting while Intl.DateTimeFormat().resolvedOptions().locale reflects the OS language setting. Don't know if this is also the case in other OSs.

like image 138
kimamula Avatar answered Sep 21 '22 19:09

kimamula


I also saw this and have done a bit of testing.

Chrome

On Windows Chrome 85 appears offers two different ways to set the language in it's configuration:

  1. A list of languages in order of preference. This appears to drive navigator.languages.
  2. A language which is used to "display the Google Chrome UI". This appears to drive Intl.DateTimeFormat().resolvedOptions().locale.

For example when I have this set: Google Chrome language settings

I get this:

navigator.languages                            : en-US, en-GB, en
navigator.language                             : en-US
Intl.DateTimeFormat().resolvedOptions().locale : en-GB

Note that on Mac with Chrome 85 there did not appear to be these two separate settings, but just the ability to set an order of languages (not an option for setting the language to "display the Google Chrome UI").

Node

In Node I found that the value of Intl.DateTimeFormat().resolvedOptions().locale was driven by the language settings of my operating system. Note I only tested this on Windows, but I assume the same is true elsewhere.

Side note on time zone

I saw that for Intl.DateTimeFormat().resolvedOptions().timeZone both Chrome and Node use the OS setting for the time zone (also only tested on Windows). Chrome does not appear to have its own setting for time zone in its settings so I guess just uses the OS configuration.

like image 25
Hugo Avatar answered Sep 19 '22 19:09

Hugo