Sub topic 8
Intl Basics: Locales, Options, and Resolved Options
This chapter assumes you can run JavaScript in a browser console or Node.js, and that you know a Date holds one instant. Every Intl formatter you meet later in this tutorial (DateTimeFormat, RelativeTimeFormat, PluralRules, NumberFormat) is built with the same three-part pattern. Learn the pattern once here and the later chapters only add new options.
The constructor pattern
Each Intl constructor takes a locale argument and an options object. It returns a formatter whose settings you can check with resolvedOptions().
const date = new Date(Date.UTC(2024, 6, 4, 16, 30));
const fmt = new Intl.DateTimeFormat("de-DE", { timeZone: "UTC" });
console.log(fmt.format(date)); // "4.7.2024"
console.log(fmt.resolvedOptions().locale); // "de-DE"If you pass undefined as the locale, the formatter uses the runtime's default locale. That default is the browser's language or the operating system's setting in Node.js.
BCP 47 locale tags
A locale is written as a BCP 47 language tag. The tag is made of subtags separated by hyphens:
| Tag | Meaning |
|---|---|
| `en` | English, no region |
| `en-GB` | English as used in the United Kingdom |
| `zh-Hant-TW` | Chinese, Traditional script, Taiwan |
| `en-US-u-hc-h23` | US English with a 24-hour clock |
| `th-TH-u-ca-buddhist` | Thai with the Buddhist calendar |
Anything after -u- is a Unicode extension. Its keys include ca (calendar), nu (numbering system) and hc (hour cycle). They let a single tag carry preferences that would otherwise go in the options object.
Tags are case-insensitive, and Intl.getCanonicalLocales normalizes them. A tag that is structurally invalid throws a RangeError. The most common mistake is using an underscore, as in POSIX locale names:
console.log(Intl.getCanonicalLocales("EN-us")); // ["en-US"]
try {
new Intl.DateTimeFormat("en_US");
} catch (err) {
console.log(err instanceof RangeError); // true
}Locale negotiation
You can pass an array of locales in order of preference. The runtime picks the first one it supports and falls back to its default if it supports none of them. A tag that is well formed but unsupported does not throw. It is skipped without any warning.
Call supportedLocalesOf when you need to find out ahead of time which locales will be matched:
console.log(Intl.DateTimeFormat.supportedLocalesOf(["zz-ZZ", "fr-CA", "en"]));
// ["fr-CA", "en"] ("zz-ZZ" is valid syntax but not supported)Options and resolvedOptions
The options object sets everything the locale does not decide, such as timeZone, hour12 or calendar. If a setting appears both as a Unicode extension in the tag and in options, the options value wins.
resolvedOptions() reports what the formatter actually uses after negotiation and defaults are applied. Use it to debug output that looks wrong and to read the user's environment:
const r = new Intl.DateTimeFormat("ar-EG-u-nu-latn", { calendar: "gregory" })
.resolvedOptions();
console.log(r.locale); // "ar-EG-u-nu-latn"
console.log(r.numberingSystem); // "latn"
console.log(r.calendar); // "gregory"
console.log(r.timeZone); // the runtime's zone, e.g. "Europe/Berlin"Exercise
You call new Intl.DateTimeFormat(["xx-YY", "pt-BR"]). The runtime supports pt-BR but not xx-YY. Does the constructor throw, and what will resolvedOptions().locale report?
Show answer
It does not throw, because "xx-YY" is a well-formed BCP 47 tag. Negotiation skips the unsupported tag and picks the next one, so resolvedOptions().locale reports "pt-BR". Only a malformed tag such as "pt_BR" would throw a RangeError.
Exercise
How do you request a 24-hour clock for US English using only the locale tag, and how do you confirm it took effect?
Show answer
Use the tag "en-US-u-hc-h23", then check new Intl.DateTimeFormat("en-US-u-hc-h23", { timeStyle: "short" }).resolvedOptions().hourCycle, which should be "h23".