JavaScript/Figuring out the locale
[i]Learn· Internationalization

Figuring out the locale

A shared concern for internationalization is: how do I know what locale to use?

The most obvious answer is "what the user prefers." Browsers expose the user's language preferences through the navigator.languages property. This is an array of language identifiers that can be directly passed to the formatter constructor—more on this later. The user can configure this list in their browser settings. You can also pass an empty array or undefined, which both cause the browser's default locale to be used.

const numberFormatter = new Intl.NumberFormat(navigator.languages);
console.log(numberFormatter.format(1234567.89));

const numberFormatter2 = new Intl.NumberFormat([]);

However, this may not always provide the most desirable result. Strings formatted by Intl formatters represent a tiny fraction of text displayed on your site; most localized content is provided by you, the site developer. For example, suppose your site is only offered in two languages: English and French. If a Japanese user visits your site and expects to use your site in English, they will be baffled when they see the English text interleaved with numbers and dates in Japanese!

Usually, you don't want to use the browser's default language. Rather, you want to use the same language that the rest of your site is offered in. Suppose your site has a language switcher that stores the user's choice somewhere — you could directly use that.

// Suppose this can be changed by some site-wide control
const userSettings = {
  locale: "en-US",
  colorMode: "dark",
};
const numberFormatter = new Intl.NumberFormat(userSettings.locale);
console.log(numberFormatter.format(1234567.89));

If your site has a backend that dynamically selects the language based on the user's Accept-Language header and sends back different HTML based on that, you could also use the HTML element's HTMLElement.lang property: new Intl.NumberFormat(document.documentElement.lang).

If your site is only offered in one language, you could also hardcode the locale in your code: new Intl.NumberFormat("en-US").

As previously mentioned, you can also pass an array of locales to the constructor, representing a list of fallback choices. The first example using navigator.languages is an example of this: if the first user-configured locale is not supported for the particular operation, the next one is tried, and so on, until we find a requested locale for which the runtime has data. You can do this manually as well. In the example below, we specify a list of locales in decreasing order of specificity which all represent languages likely to be understood by a Hong Kong Chinese speaker, so the formatter picks the most specific one that it supports.

const numberFormatter = new Intl.NumberFormat([
  "yue-Hant",
  "zh-Hant-HK",
  "zh-Hant",
  "zh",
]);

There is no API for listing all supported locales, but there are a few methods for handling the locale list:

  • Intl.getCanonicalLocales(): This function takes a list of locale identifiers and returns a list of canonicalized locale identifiers. This is useful for understanding the canonicalization process for each Intl constructor.
  • The supportedLocalesOf() static method on each Intl object (like Intl.DateTimeFormat.supportedLocalesOf()): This method takes the same arguments as the constructor (locales and options) and returns a subset of the given locale tags that match the given data. This is useful for understanding which locales are supported by the runtime for a particular operation, for example, to show a language switcher containing only the supported languages.
Source: Internationalization by Mozilla Contributors, licensed under CC-BY-SA-2.5. Retrieved 2026-11-18. Changes: Extracted the section "Figuring out the locale"; removed the code-block language labels; made links absolute; examples are the page's own code blocks, unchanged.