The Intl API: Built-In Formatting for Web i18n

The Intl API: Built-In Formatting for Web i18n

Hand-rolled date, number, and sorting helpers re-implement part of what browsers and default Node.js builds already ship. The Intl API family at working level, with caching and consistency rules.

Every major browser engine, and Node.js in its default build, ships a locale-aware formatting engine, and the specification recommends Unicode CLDR as its locale data. Hand-rolled date, number, and sorting helpers re-implement part of it, and a helper written for one market can be wrong for the rest. This article covers the JavaScript Intl API at working level: the core four formatters, the supporting members, locale negotiation and options, formatter caching, and the consistency rules that keep output correct across engines and between server and client.

The formatting engine already shipped

Codebases that have grown past one market can accumulate the same artifacts: a formatPrice() helper with a thousands-separator regex, a date utility that hardcodes month-first ordering, a sort that puts "Zürich" after "Zug" or ahead of everything. Each is a small re-implementation of something the platform already does, usually correctly for one locale and silently wrong for the rest.

The platform piece is the ECMAScript Internationalization API, exposed as the Intl namespace (MDN reference). ECMA-402 recommends, but does not require, that implementations use the locale data provided by Unicode CLDR, the same repository that operating systems and major platforms draw on, and it leaves the set of supported locales to each implementation. In current major browsers and default Node.js builds, the formatting knowledge, including which separator a locale uses, how its dates are ordered, and how its alphabet sorts, is already in the runtime; a Node.js build configured without ICU has no Intl object, a small-ICU build generally works only with English unless additional ICU data is supplied at runtime, and a system-ICU build depends on the ICU installed on the operating system.

One architectural rule makes the API useful: format at the edge, store canonical. Raw values flow through the application unchanged: Date objects or ISO strings for time, numbers for amounts, plain strings for names. Locale-aware rendering happens once, at display time, using the viewer's resolved locale. A formatted string is output, never data. It is not stored, not sent to an API, and not parsed back.

The family, in one orienting table:

MemberIts one job
DateTimeFormatRender a date and time in a locale's conventions and a chosen time zone
NumberFormatRender numbers, currency amounts, percentages, units, and compact figures
PluralRulesReturn the CLDR plural category for a number in a locale
CollatorCompare strings in a locale's alphabetical order for sorting and searching
RelativeTimeFormatRender a supplied offset like three days ago or in two hours
ListFormatJoin items into a locale-correct and, or, or unit list
DisplayNamesName languages, regions, scripts, currencies, calendars, and date-time fields in a given locale
SegmenterSplit text into graphemes, words, or sentences by locale rules

Scope for this article: the API surface. Which conventions each locale expects is locale data's job, and the insight literature on locale formats covers the conventions themselves. Message grammar, meaning plurals and selects inside translated sentences, is the MessageFormat article's territory.

The core four at working level

DateTimeFormat has two ways in. The presets, dateStyle and timeStyle, pick a locale-appropriate full, long, medium, or short form and are the right default for most UI. Component options (year, month, weekday, hour) give finer control when a design needs a specific shape, and the engine still orders and punctuates them per locale.

javascript
const when = new Date("2026-09-26T14:30:00Z");

new Intl.DateTimeFormat("en-US", { dateStyle: "medium", timeStyle: "short", timeZone: "America/New_York" }).format(when);
// "Sep 26, 2026, 10:30 AM"

new Intl.DateTimeFormat("de-DE", { dateStyle: "medium", timeStyle: "short", timeZone: "Europe/Berlin" }).format(when);
// "26.09.2026, 16:30"

The timeZone option is the display-time zone decision. Which zone applies, and what gets stored, is the dates-and-timezones article's territory; here it is one option. The common pitfall is the hour cycle: forcing hour12: true or false overrides the locale's convention, which is occasionally right for a product and often a bug copied from a single-market origin.

NumberFormat covers four styles (decimal, currency, percent, and unit) plus a compact notation worth knowing:

javascript
const n = 1234567.891;

new Intl.NumberFormat("en-IN").format(n);                   // "12,34,567.891"
new Intl.NumberFormat("fr-FR").format(n);                   // "1 234 567,891"  (U+202F narrow no-break spaces)

new Intl.NumberFormat("en-US", { style: "currency", currency: "JPY" }).format(4980);
// "¥4,980"   (yen has no minor unit, so no decimals)
new Intl.NumberFormat("en-GB", { style: "currency", currency: "EUR", currencyDisplay: "code" }).format(49.9);
// "EUR 49.90"   (U+00A0 no-break space after the code)

new Intl.NumberFormat("en", { style: "percent", maximumFractionDigits: 1 }).format(0.1234); // "12.3%"
new Intl.NumberFormat("en", { style: "unit", unit: "kilometer-per-hour" }).format(88);   // "88 km/h"
new Intl.NumberFormat("en", { notation: "compact" }).format(2_400_000);                  // "2.4M"

The currency style deserves one sentence of emphasis: NumberFormat formats a currency amount; it does not convert it. The amount and the currency code are both inputs. Exchange rates and price localization are commerce decisions made before the formatter is ever called.

Collator handles locale-aware sorting and comparison. Sorting with the default Array.prototype.sort() compares UTF-16 code units, which puts precomposed accented capitals such as "Ö" after "z", orders "10" before "9", and scrambles non-Latin scripts. The Unicode Collation Algorithm exists because alphabetical order is language-specific: Swedish sorts "ö" after "z", German sorts it with "o".

javascript
const cities = ["Zug", "Zürich", "Aarau", "Ölten", "zermatt"];

cities.sort();
// ["Aarau", "Zug", "Zürich", "zermatt", "Ölten"]   codepoint order

const de = new Intl.Collator("de", { sensitivity: "base" });
cities.sort(de.compare);
// ["Aarau", "Ölten", "zermatt", "Zug", "Zürich"]

const files = ["item10", "item9", "item100"];
files.sort(new Intl.Collator("en", { numeric: true }).compare);
// ["item9", "item10", "item100"]

The sensitivity option controls whether accents and case count as differences, which also makes Collator the right tool for accent-insensitive matching in client-side filters. The rule is simple: any user-facing sorted list uses Collator, never codepoint order.

PluralRules is the primitive underneath message-layer plurals. Its select() method returns the CLDR category (zero, one, two, few, many, or other) for a number in a locale:

javascript
const pr = new Intl.PluralRules("pl");
[1, 2, 5, 22].map((n) => pr.select(n)); // ["one", "few", "many", "few"]

Reach for it directly when the output is not a translated sentence: choosing a badge variant, picking a standalone unit label, or building a component that selects between design assets. When the output is a sentence, let the message layer handle the plural so the translator controls the grammar.

The supporting cast

RelativeTimeFormat produces "3 days ago" or "in 2 hours" without a library. Its honest limit is that it formats a supplied value and unit; deciding that 26 hours should read as "yesterday" rather than "26 hours ago" is still application logic, and so are the thresholds between units.

ListFormat solves the "A, B, and C" problem that string concatenation gets wrong per locale: conjunctions, serial commas, and spacing all vary.

javascript
const names = ["Lisbon", "Porto", "Faro"];
new Intl.ListFormat("en", { type: "conjunction" }).format(names); // "Lisbon, Porto, and Faro"
new Intl.ListFormat("es", { type: "conjunction" }).format(names); // "Lisbon, Porto y Faro"
new Intl.ListFormat("en", { type: "disjunction" }).format(names); // "Lisbon, Porto, or Faro"

DisplayNames names languages, regions, scripts, currencies, calendars, and date-time fields in the locales the runtime supports. It is the building block for a locale picker that shows each language in its own name, called an endonym, by passing the same code as both the locale and the value. The picker's UX is the insight locale-detection literature's territory; the data comes from here.

Segmenter splits text by grapheme, word, or sentence using locale rules. It matters because .length counts UTF-16 code units: an emoji with a skin-tone modifier or a family sequence reports a length of several, and truncating at a character count can cut one in half. Segmenter can also find word boundaries in scripts written without spaces, such as Thai or Japanese, which a whitespace split cannot. ECMA-402 leaves boundary determination to each implementation, pointing to the general default algorithms in Unicode Standard Annex #29 and recommending locale-sensitive tailorings such as CLDR's, so check the word splits in the engines you support. As of Nov 2026, Intl.Segmenter is Baseline Widely available: it reached all Baseline core browsers on April 16, 2024 and became Widely available on October 16, 2026 (web-features 3.45.0; MDN); older runtimes still in a support matrix need a check or a fallback.

Locale negotiation and options discipline

Every constructor except Intl.Locale accepts a list of locales, not one. Pass the user's ordered preference chain as BCP 47 tags, and the engine picks the best supported match using its best fit matcher by default. Passing only one hardcoded tag throws away the information the engine was built to use. How that chain is designed, including regional-to-base fallback and the default at the end, is the locale-fallback article's job.

Unicode extension keywords in the tag are the escape hatch for per-user overrides without new options code. The -u- extension carries keys like numbering system, calendar, and hour cycle:

javascript
new Intl.NumberFormat("ar-EG-u-nu-latn").format(1234.5);                 // Latin digits in an Arabic locale
new Intl.DateTimeFormat("en-GB-u-hc-h12", { timeStyle: "short" }).format(new Date());

When output surprises you, resolvedOptions() is the debugging tool. It reports what the engine actually chose, as opposed to what was requested: the matched locale and the resolved options, which for a DateTimeFormat include the numbering system, calendar, time zone, and, when the format shows hours, the hour cycle. When Intl seems broken, check first for a fallback to a different locale than the developer assumed.

And when the design needs styling inside a formatted value, such as a smaller currency symbol or a bold day, formatToParts() returns typed segments instead of a string:

javascript
new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" }).formatToParts(1299.5);
// [{type:"currency",value:"$"}, {type:"integer",value:"1"}, {type:"group",value:","},
//  {type:"integer",value:"299"}, {type:"decimal",value:"."}, {type:"fraction",value:"50"}]

Map the parts to markup and the locale's ordering stays intact. The alternative, regex-parsing the formatted string to find the symbol, breaks the moment a locale puts the symbol after the number.

Cost, consistency, and where Intl ends

Construction is where the locale work happens. Creating a formatter negotiates the locale and resolves the options once, and calling format() on an existing one reuses that result rather than negotiating again. MDN notes that every toLocaleString() call searches a big database of localization strings, which is potentially inefficient, and that for calls repeated many times with the same arguments it is better to create a formatter object, because the object remembers its arguments and may cache a slice of that database. Create one formatter per locale-and-options combination, cache it, and reuse it. Constructing a new Intl.NumberFormat inside a list item's render function repeats the locale negotiation and option resolution once per row. A small memoized factory keyed on locale and serialized options removes the repeated construction. The loading and bundle-budget view is the i18n-performance article's territory.

Output differs across engines and over time. Formatted strings are CLDR-data-dependent, and engines update CLDR on their own schedules. A narrow no-break space appears where a regular space used to be, an abbreviation gains or loses a period, a locale's default grouping changes. Two rules follow. Never snapshot-test exact formatted strings; assert on inputs, options, or formatToParts types instead, or the suite breaks on a browser update. And never parse formatted output back into data, because the format is not a contract.

Server and client must agree on inputs. When a page renders on the server and hydrates on the client, both sides must format with the same locale and the same time zone, or the markup differs; React, for example, expects the two renders to match and warns about hydration mismatches in development mode. The dates-and-timezones article carries the worked case and its resolutions.

Where a library still earns its place, stated honestly as of Nov 2026:

  • Message-level formatting, meaning grammar inside translated sentences. Intl formats values; a message library composes them into sentences. A native message API remains an early-stage proposal.
  • Calendar arithmetic and zoned date-time manipulation beyond formatting, where Temporal is not yet Baseline and libraries or polyfills still fill the gap.
  • Older runtimes in a support matrix that lack newer members, where a polyfill's weight has to be weighed against the audience it serves.

Everything else, meaning dates, numbers, currencies, sorting, lists, language names, and text segmentation, is already in the runtime of current major browsers and default Node.js builds. Before adding a formatting dependency, check whether the job is on the table above. It usually is.

Project Inquiry