Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How/Where does JavaScript detect the default locale?

Today, the 11th of September, 2017, JavaScript's toLocaleDateString() method is outputting 9/11/2017 for me. But I am in the UK, so the formatting is wrong in this case. The MDN Web Docs tell me that this method returns "a formatted string in the default locale".

So, where/how is JavaScript detecting my default locale? Where is this set, or what does it depend on, and (how) can I change it?

Edited to add: I'm using Firefox 55.0.3 on Windows 10 if that makes any difference.

like image 601
osullic Avatar asked Sep 10 '17 23:09

osullic


People also ask

What is locale in JavaScript?

Locale (Runtime - JavaScript) A Locale object represents a specific geographical, political, or cultural region. An operation that requires a Locale to perform its task is called locale-sensitive and uses the Locale to tailor information for the user.

How do I find user locale?

To get the user's locale in the browser, access the first element of the languages property on the navigator object, e.g. navigator. languages[0] . The property returns an array of strings that represent the user's preferred languages.

How is locale determined?

A locale is a location-based language setting that determines which conversational settings and strings to display. The user specifies their locale using settings on their device. They may change this setting whenever they wish, including during a conversation with a Business Messages agent.

What is browser locale?

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.


1 Answers

To summarize shortly, detecting the current locale is implementation dependent and may differ from environment to environment. Your default language may also depend on the installer you've used to install your browser.

The not so short version:

Following the ECMAScript spec, conforming browsers (and other environments, such as Node.js) should implement localization following the ECMAScript Internationalization API (ECMA-402), which only outlines the following for getting the default locale:

The DefaultLocale abstract operation returns a String value representing the [...] language tag for the host environment’s current locale.

This means that getting the default locale is implementation dependent and can differ from browser to browser. This is intentional, as it allows browser vendors to keep their current, differing implementations to stay conforming without much fuss.

While it's true that it would be nice to have this standardized as well, it's more beneficial to get everyone on board for a broad spec first and then work out the little kinks later.

Most modern browsers allow you to change your current default locale in their preferences (Chrome shown):

Sample settings interface

like image 75
Etheryte Avatar answered Oct 12 '22 13:10

Etheryte