Overview
The Intl object is the namespace for the ECMAScript Internationalization API, which provides a wide range of locale- and culture-sensitive data and operations.
The Intl object is very use-case driven. It provides a separate object for each use case that requires locale-specific logic. Currently, it provides the following functionalities:
- Getting information about a locale using
Intl.Locale. - Formatting data using
Intl.DateTimeFormat,Intl.DurationFormat,Intl.ListFormat,Intl.NumberFormat, andIntl.RelativeTimeFormat. - Collation (i.e., comparing strings for sorting or searching) using
Intl.Collator. - Selecting plural forms using
Intl.PluralRules. - Segmenting text into units such as words, sentences, or graphemes using
Intl.Segmenter. - Getting the displayed name for currencies, languages, scripts, regions, and time zones using
Intl.DisplayNames.
Most Intl APIs share a similar design (Intl.Locale is the only exception). You start by constructing an instance with the desired locale and options. This defines a set of rules for the desired operation (formatting, collation, segmentation, etc.). Then, when you call the method on the instance, such as format(), compare(), or segment(), the object applies the specified rule to the passed data.
// 1. Construct a formatter object, specifying the locale and formatting options:
const price = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
});
// 2. Use the `format` method of the formatter object to format a number:
console.log(price.format(5.259)); // $5.26
The general signature of the constructor is:
new Intl.SomeObject(locales, options)
localesOptional-
A string with a BCP 47 language tag or an
Intl.Localeinstance, or an array of such locale identifiers. The runtime's default locale is used whenundefinedis passed or when none of the specified locale identifiers is supported. For the general form and interpretation of thelocalesargument, see the parameter description on theIntlmain page. optionsOptional-
An object containing properties that customize specific aspects of the operation, which is key to understanding how to use each
Intlobject.