docs.rodeo

MDN Web Docs mirror

Intl.Locale

{{JSRef}} 

The Intl.Locale object is a standard built-in property of the Intl object that represents a Unicode locale identifier.

{{InteractiveExample("JavaScript Demo: Intl.Locale")}} 

const korean = new Intl.Locale("ko", {
  script: "Kore",
  region: "KR",
  hourCycle: "h23",
  calendar: "gregory",
});

const japanese = new Intl.Locale("ja-Jpan-JP-u-ca-japanese-hc-h12");

console.log(korean.baseName, japanese.baseName);
// Expected output: "ko-Kore-KR" "ja-Jpan-JP"

console.log(korean.hourCycle, japanese.hourCycle);
// Expected output: "h23" "h12"

Description

The Intl.Locale object was created to allow for easier manipulation of Unicode locales. Unicode represents locales with a string, called a locale identifier. The locale identifier consists of a language identifier and extension tags. Language identifiers are the core of the locale, consisting of language, script, and region subtags. Additional information about the locale is stored in the optional extension tags. Extension tags hold information about locale aspects such as calendar type, clock type, and numbering system type.

Traditionally, the Intl API used strings to represent locales, just as Unicode does. This is a simple and lightweight solution that works well. Adding a Locale class, however, adds ease of parsing and manipulating the language, script, and region, as well as extension tags. The following properties of Intl.Locale correspond to Unicode locale identifier subtags:

Property Corresponding subtag
{{jsxref("Intl/Locale/language", "language")}}  language (first part)
{{jsxref("Intl/Locale/script", "script")}}  script (second part)
{{jsxref("Intl/Locale/region", "region")}}  region (second/third part)
{{jsxref("Intl/Locale/calendar", "calendar")}}  ca (extension)
{{jsxref("Intl/Locale/caseFirst", "caseFirst")}}  kf (extension)
{{jsxref("Intl/Locale/collation", "collation")}}  co (extension)
{{jsxref("Intl/Locale/hourCycle", "hourCycle")}}  hc (extension)
{{jsxref("Intl/Locale/numberingSystem", "numberingSystem")}}  nu (extension)
{{jsxref("Intl/Locale/numeric", "numeric")}}  kn (extension)

The information above is exactly provided as-is when the Locale object is constructed, without consulting any external database. The Intl.Locale object additionally provides some methods that return information about the locale’s real-world information, such as available calendars, collations, and numbering systems.

Constructor

Instance properties

These properties are defined on Intl.Locale.prototype and shared by all Intl.Locale instances.

Instance methods

Examples

Basic usage

At its very simplest, the {{jsxref("Intl/Locale/Locale", "Intl.Locale()")}}  constructor takes a locale identifier string as its argument:

const us = new Intl.Locale("en-US");

Using the Locale constructor with an options object

The constructor also takes an optional configuration object argument, which can contain any of several extension types. For example, set the {{jsxref("Intl/Locale/hourCycle", "hourCycle")}}  property of the configuration object to your desired hour cycle type, and then pass it into the constructor:

const us12hour = new Intl.Locale("en-US", { hourCycle: "h12" });
console.log(us12hour.hourCycle); // Prints "h12"

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN