Working with Internationalization (Intl)
1. Formatting Numbers (Intl.NumberFormat)
Example
new Intl.NumberFormat("en-IN").format(1234567); // "12,34,567"
new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(99.5);
// "99,50 €"
2. Formatting Dates (Intl.DateTimeFormat)
Example
new Intl.DateTimeFormat("en-GB", { dateStyle: "full", timeStyle: "short" }).format(new Date());
// "Monday, 14 October 2024 at 09:30"
3. Formatting Currencies
| Option | Use |
|---|---|
style: "currency" | Format as money |
currency: "USD" | ISO 4217 code |
currencyDisplay: "code"|"symbol"|"name" | Presentation |
minimumFractionDigits / maximumFractionDigits | Precision |
4. Comparing Strings (Intl.Collator)
Example
const c = new Intl.Collator("sv", { sensitivity: "base" });
["å", "z", "a"].sort(c.compare); // ["a", "z", "å"]
5. Pluralization (Intl.PluralRules)
6. Relative Time (Intl.RelativeTimeFormat)
Example
const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
rtf.format(-1, "day"); // "yesterday"
rtf.format(3, "month"); // "in 3 months"
7. List Formatting (Intl.ListFormat)
8. Display Names (Intl.DisplayNames)
Example
new Intl.DisplayNames("en", { type: "region" }).of("FR"); // "France"
new Intl.DisplayNames("en", { type: "language" }).of("ja"); // "Japanese"
9. Locale Negotiation
| API | Use |
|---|---|
Intl.getCanonicalLocales(tags) | Normalize |
Intl.NumberFormat.supportedLocalesOf(...) | Find best matches |
new Intl.Locale("en-US") | Locale object with subtags |
10. Loading ICU Data (--with-intl)
| Build | Detail |
|---|---|
full-icu (default) | All locales (~30 MB) |
small-icu | English only (smaller binary) |
NODE_ICU_DATA=/path | Load ICU data at runtime |
--icu-data-dir | CLI equivalent |