Sub topic 8
Intl Basics: Locales, Options, and Resolved Options
Every Intl formatter, including Intl.DateTimeFormat, Intl.RelativeTimeFormat, Intl.PluralRules, and Intl.NumberFormat, is built the same way. You pass a list of locales and an options object. The engine works out what it can actually provide, and resolvedOptions() reports what it chose. Once you understand this pattern, the later formatter chapters become a matter of learning each one's options.
This chapter assumes you know JavaScript objects and arrays, can run code in Node.js or a browser console, and have read the earlier chapters on Date. It does not cover date formatting options in detail. The next chapter does that.
The constructor pattern
All Intl constructors take the same two arguments:
const formatter = new Intl.NumberFormat(locales, options);localesis a string, an array of strings, anIntl.Localeobject, orundefined. An array lists your preferences in order.optionsis a plain object. Each constructor recognizes its own keys and ignores the rest.
Passing undefined (or leaving the argument out) means "use the runtime's default locale." In a browser that usually follows the user's language settings. In Node.js it comes from the environment, such as the LANG variable, through the ICU library that Node bundles.
const fmt = new Intl.NumberFormat();
console.log(fmt.resolvedOptions().locale); // e.g. "en-US", depends on the runtimeBCP 47 locale tags
A locale tag is a string in BCP 47 format. BCP 47 is the IETF standard for naming languages and regional variants. A tag is made of subtags separated by hyphens:
| Subtag | Example | Meaning |
|---|---|---|
| language | `sr` | Serbian (2–3 letters, lowercase by convention) |
| script | `Cyrl` | Cyrillic script (4 letters, title case) |
| region | `RS` | Serbia (2 letters uppercase, or 3 digits like `419`) |
| variant | `1996` | A registered variant (rare in practice) |
| Unicode extension | `-u-ca-gregory` | Locale preferences: calendar, numbering system, hour cycle |
Only the language subtag is required. en, en-GB, zh-Hant-TW, and es-419 (Latin American Spanish) are all valid.
Tags are case-insensitive, but the engine canonicalizes them. Intl.getCanonicalLocales shows the normalized form and checks the syntax:
console.log(Intl.getCanonicalLocales(["EN-us", "zh-hant-tw"]));
// ["en-US", "zh-Hant-TW"]
try {
Intl.getCanonicalLocales("en_US"); // underscore is not valid BCP 47
} catch (err) {
console.log(err.name); // "RangeError"
}The Intl.Locale object
Intl.Locale parses a tag into its parts and can fill in or strip likely subtags:
const loc = new Intl.Locale("sr-Cyrl-RS-u-ca-gregory-nu-latn");
console.log(loc.language); // "sr"
console.log(loc.script); // "Cyrl"
console.log(loc.region); // "RS"
console.log(loc.calendar); // "gregory"
console.log(loc.numberingSystem); // "latn"
console.log(loc.baseName); // "sr-Cyrl-RS"
console.log(new Intl.Locale("zh-TW").maximize().toString()); // "zh-Hant-TW"
console.log(new Intl.Locale("en-Latn-US").minimize().toString()); // "en"maximize() uses the Unicode CLDR "likely subtags" data. That is how the engine knows Taiwanese Chinese is written in Traditional script. You can also build a locale from options: new Intl.Locale("en", { region: "GB", hourCycle: "h23" }) gives "en-GB-u-hc-h23".
Unicode extension keys
The -u- section carries preferences as key/value pairs. These are the ones that matter for dates and numbers:
| Key | Option name | Example values |
|---|---|---|
| `ca` | `calendar` | `gregory`, `japanese`, `islamic`, `buddhist` |
| `nu` | `numberingSystem` | `latn`, `arab`, `deva`, `thai` |
| `hc` | `hourCycle` | `h11`, `h12`, `h23`, `h24` |
Each key has a matching constructor option. If both are present, the option wins:
const fromTag = new Intl.DateTimeFormat("en-US-u-hc-h23", { hour: "numeric" });
console.log(fromTag.resolvedOptions().hourCycle); // "h23"
const overridden = new Intl.DateTimeFormat("en-US-u-hc-h23", {
hour: "numeric",
hourCycle: "h12",
});
console.log(overridden.resolvedOptions().hourCycle); // "h12"
const arabic = new Intl.NumberFormat("ar-EG");
const latinDigits = new Intl.NumberFormat("ar-EG", { numberingSystem: "latn" });
console.log(arabic.format(1234.5)); // e.g. "١٬٢٣٤٫٥"
console.log(latinDigits.format(1234.5)); // Western digits, e.g. "1,234.5"Use the extension when the preference belongs to the user, for example a stored profile tag like "ja-JP-u-ca-japanese". Use the option when your code needs a specific behavior no matter what the user prefers.
Locale negotiation
The engine does not have data for every possible tag. When you construct a formatter, it negotiates. It walks your list in order and picks the first locale it supports. If nothing in the list matches, it falls back to the default locale.
Matching is not exact string equality. A request for de-AT can match a de data set, and the resolved locale may come back as either, depending on the engine. The localeMatcher option picks the algorithm: "lookup" is the strict BCP 47 algorithm, which drops subtags from the end until something matches. "best fit" is the default and lets the engine use smarter matching.
To check support without building a formatter, call the static supportedLocalesOf:
console.log(Intl.DateTimeFormat.supportedLocalesOf(["tlh", "fr-CA", "de"]));
// typically ["fr-CA", "de"] — "tlh" (Klingon) has no dataOptions: validation and silent ignoring
Options are checked in two different ways, and that difference produces a common bug:
- An unknown value for a known key throws a
RangeError. - An unknown key, such as a typo, is ignored without any error.
try {
new Intl.NumberFormat("en", { style: "bogus" });
} catch (err) {
console.log(err.name); // "RangeError"
}
const typo = new Intl.DateTimeFormat("en-US", { dateStlye: "full" }); // misspelled key
console.log(typo.resolvedOptions().dateStyle); // undefined — the option was never applied
console.log(typo.format(new Date(Date.UTC(2024, 6, 4, 12)))); // default numeric form, e.g. "7/4/2024"Some options also depend on other options. currency is required when style is "currency", and leaving it out throws a TypeError. In Intl.DateTimeFormat, mixing dateStyle with component options like month throws a TypeError. The next chapter covers that case.
resolvedOptions: what you actually got
resolvedOptions() returns a new plain object describing the formatter's final settings. That includes values you never set, filled in from locale defaults:
const fmt = new Intl.DateTimeFormat(["xx-YY", "fr-CA"], { hour: "numeric" });
const r = fmt.resolvedOptions();
console.log(r.locale); // "fr-CA" — the first supported entry
console.log(r.calendar); // "gregory"
console.log(r.numberingSystem); // "latn"
console.log(r.timeZone); // the runtime's zone, e.g. "Europe/Paris"
console.log(r.hourCycle); // "h23" — French Canadian uses a 24-hour clockTypical uses:
- Detecting the user's time zone:
new Intl.DateTimeFormat().resolvedOptions().timeZone. - Finding out whether the user's locale uses a 12-hour or 24-hour clock before you build a time picker.
- Logging the effective locale when debugging a "wrong language" report.
- Checking whether fallback happened, by comparing
localewith what you asked for.
The returned object is a snapshot. Changing it has no effect on the formatter, because formatters are immutable after construction. To change a setting, build a new formatter.
Common errors at a glance
| Symptom | Cause | Fix |
|---|---|---|
| `RangeError: Incorrect locale information provided` | Underscore or malformed tag | Convert to BCP 47 with hyphens |
| Output in the wrong language, no error | Locale unsupported, fell back | Check `resolvedOptions().locale` |
| Option seems to do nothing | Misspelled option key | Compare against `resolvedOptions()` |
| Different output in Node and browser | Different ICU/CLDR data versions | Avoid comparing formatted strings exactly in tests |
| Extension key ignored | An explicit option overrode it | Remove the conflicting option |
Exercise
You run new Intl.DateTimeFormat("en_GB", { hour: "numeric" }) and get a RangeError. After fixing it to "en-GB", you also want to force 12-hour time for this one formatter while leaving the user's tag "en-GB-u-hc-h23" untouched elsewhere. Write the constructor call, and say how you would confirm the hour cycle that was applied.
Show answer
Pass the user's tag and override with an option: new Intl.DateTimeFormat("en-GB-u-hc-h23", { hour: "numeric", hourCycle: "h12" }). Options take precedence over -u- extension keys, so this formatter uses 12-hour time while the stored tag stays the same. Confirm with .resolvedOptions().hourCycle, which should return "h12".
Exercise
A user's browser sends ["gsw-CH", "de-CH", "en"]. How do you find out which of these your runtime's NumberFormat can actually serve, and which one a formatter built from this list would use?
Show answer
Call Intl.NumberFormat.supportedLocalesOf(["gsw-CH", "de-CH", "en"]) to get the supported subset in order. Then build new Intl.NumberFormat(["gsw-CH", "de-CH", "en"]) and read .resolvedOptions().locale, which shows the locale chosen by negotiation, or the runtime default if none matched.