Intl

Intl object is the namespace for the ECMAScript Internationalization API, which provides language sensitive string comparison, number formatting, and date and time formatting. The Intl object provides access to several constructors as well as functionality common to the internationalization constructors and other language sensitive functions.

Constructor properties

Intl.Collator()
Constructor for collators, which are objects that enable language-sensitive string comparison.
Intl.DateTimeFormat()
Constructor for objects that enable language-sensitive date and time formatting.
Intl.DisplayNames()
Constructor for objects that enable the consistent translation of language, region and script display names.
Intl.ListFormat()
Constructor for objects that enable language-sensitive list formatting.
Intl.Locale()
Constructor for objects that represents a Unicode locale identifier.
Intl.NumberFormat()
Constructor for objects that enable language-sensitive number formatting.
Intl.PluralRules()
Constructor for objects that enable plural-sensitive formatting and language-specific rules for plurals.
Intl.RelativeTimeFormat()
Constructor for objects that enable language-sensitive relative time formatting.

静态方法

Intl.getCanonicalLocales()
Returns canonical locale names.

Locale identification and negotiation

The internationalization constructors as well as several language sensitive methods of other constructors (listed under 另请参阅 ) use a common pattern for identifying locales and determining the one they will actually use: they all accept locales and options arguments, and negotiate the requested locale(s) against the locales they support using an algorithm specified in the options.localeMatcher 特性。

locales argument

locales argument requests that a particular locale (or a locale from a list of them) be considered for use in a given operation.  A single locale may be specified by either an Intl.Locale object or a string that is a Unicode BCP 47 locale identifier .  Multiple locales may be specified (and a best-supported locale determined by evaluating each of them in order and comparing against the locales supported by the implementation) by passing an array (or array-like object, with a length property and corresponding indexed elements) whose elements are either Intl.Locale objects or values that convert to Unicode BCP 47 locale identifier strings.  If the locales argument is not provided or is undefined, the runtime's default locale is used.

A Unicode BCP 47 locale identifier consists of

  1. a language code,
  2. (optionally) a script code,
  3. (optionally) a region (or country) code,
  4. (optionally) one or more variant codes, and
  5. (optionally) one or more extension sequences,

with all present components separated by hyphens.  Locale identifiers are case-insensitive ASCII.  However, it's conventional to use title case (first letter capitalized, successive letters lower case) for script code, upper case for region codes, and lower case for everything else.  For example:

  • "hi" : Hindi (language)
  • "de-AT" : German (language) as used in Austria (region)
  • "zh-Hans-CN" : Chinese (language) written in simplified characters (script) as used in China (region)
  • "en-emodeng" : English (language) in the "Early modern English" dialect (variant)

The subtags identifying languages, scripts, regions (including countries), and (rarely used) variants in Unicode BCP 47 locale identifiers are registered in the IANA Language Subtag Registry .  This registry is periodically updated over time, and implementations may not always be up to date, so be careful not to rely too much on tags being universally supported.

BCP 47 also allows for extensions, each consisting of a single digit or letter (other than "x" ) and one or more two- to eight-letter or digit tags, all separated by hyphens. JavaScript internationalization functions use the "u" (Unicode) extension, which can be used to request additional customization of Collator , NumberFormat ,或 DateTimeFormat objects. Examples:

  • "de-DE-u-co-phonebk" : Use the phonebook variant of the German sort order, which interprets umlauted vowels as corresponding character pairs: ä → ae, ö → oe, ü → ue.
  • "th-TH-u-nu-thai" : Use Thai digits (๐, ๑, ๒, ๓, ๔, ๕, ๖, ๗, ๘, ๙) in number formatting.
  • "ja-JP-u-ca-japanese" : Use the Japanese calendar in date and time formatting, so that 2013 is expressed as the year 25 of the Heisei period, or 平成25.
  • "en-GB-u-ca-islamic" : use British English with the Islamic (Hijri) calendar, where the Gregorian date 14 October, 2017 is the Hijri date 24 Muharram, 1439.

Other BCP 47 extension tags can be found in the Unicode CLDR Project .

Finally, an extension using the letter "x" may appear, followed by one or one- to eight-letter or digit tags.  This extension allows applications to encode information for their own private use, that will be ignored by all Intl operations.

Locale negotiation

The list of locales specified by the locales argument, after Unicode extensions have been removed from them, is interpreted as a prioritized request from the application. The runtime compares it against the locales it has available and picks the best one available. Two matching algorithms exist: the " lookup " matcher follows the Lookup algorithm specified in BCP 47 ; the " best fit " matcher lets the runtime provide a locale that's at least, but possibly more, suited for the request than the result of the Lookup algorithm. If the application doesn't provide a locales argument, or the runtime doesn't have a locale that matches the request, then the runtime's default locale is used. The matcher can be selected using a property of the options argument (see below).

If the selected language tag had a Unicode extension substring, that extension is now used to customize the constructed object or the behavior of the function. Each constructor or function supports only a subset of the keys defined for the Unicode extension, and the supported values often depend on the language tag. For example, the " co " key (collation) is only supported by Collator , and its " phonebk " value is only supported for German.

options argument

options argument must be an object with properties that vary between constructors and functions. If the options argument is not provided or is undefined, default values are used for all properties.

One property is supported by all language sensitive constructors and functions: The localeMatcher property, whose value must be a string " lookup " or " best fit " and which selects one of the locale matching algorithms described above.

范例

Formatting dates and numbers

可以使用 Intl to format dates and numbers in a form that's conventional for a specific language and region:

const count = 26254.39;
const date = new Date("2012-05-24");
function log(locale) {
  console.log(
    `${new Intl.DateTimeFormat(locale).format(date)} ${new Intl.NumberFormat(locale).format(count)}`
  );
}
log("en-US");
// expected output: 5/24/2012 26,254.39
log("de-DE");
// expected output: 24.5.2012 26.254,39
					

规范

规范
ECMAScript Internationalization API (ECMA-402)
The definition of 'Intl' in that specification.

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
Intl Chrome 24 Edge 12 Firefox 29 IE 11 Opera 15 Safari 10 WebView Android 4.4 Chrome Android 25 Firefox Android 56 Opera Android 14 Safari iOS 10 Samsung Internet Android 1.5 nodejs 0.12
0.12
Before version 13.0.0, only the locale data for en-US is available by default. When other locales are specified, the Intl APIs silently fall back to en-US . To make full ICU (locale) data available for versions prior to 13, see Node.js documentation on the --with-intl option and how to provide the data.
Collator Chrome 24 Edge 12 Firefox 29 IE 11 Opera 15 Safari 10 WebView Android 4.4 Chrome Android 25 Firefox Android 56 Opera Android 14 Safari iOS 10 Samsung Internet Android 1.5 nodejs 0.12
0.12
Before version 13.0.0, only the locale data for en-US is available by default. See the Collator() 构造函数 了解更多细节。
DateTimeFormat Chrome 24 Edge 12 Firefox 29 IE 11 Opera 15 Safari 10 WebView Android 4.4 Chrome Android 25 Firefox Android 56 Opera Android 14 Safari iOS 10 Samsung Internet Android 1.5 nodejs 0.12
0.12
Before version 13.0.0, only the locale data for en-US is available by default. See the DateTimeFormat() 构造函数 了解更多细节。
DisplayNames Chrome 81 Edge 81 Firefox No IE No Opera 68 Safari No WebView Android 81 Chrome Android 81 Firefox Android No Opera Android 58 Safari iOS No Samsung Internet Android No nodejs 14.0.0
getCanonicalLocales Chrome 54 Edge 16 Firefox 48 IE No Opera No Safari 11 WebView Android No Chrome Android No Firefox Android 56 Opera Android No Safari iOS 11 Samsung Internet Android No nodejs 7.0.0
ListFormat
Chrome 72 Edge No Firefox 78 IE No Opera 60 Safari No WebView Android 72 Chrome Android 72 Firefox Android No Opera Android 51 Safari iOS No Samsung Internet Android No nodejs 12.0.0
12.0.0
Before version 13.0.0, only the locale data for en-US is available by default. See the ListFormat() 构造函数 了解更多细节。
Locale Chrome 74 Edge 79 Firefox 75 IE No Opera 62 Safari 14
14
Safari 14 Technology Preview 107-111 returns a string instead of a Locale object from the minimize and maximize methods.
WebView Android 74 Chrome Android 74 Firefox Android No Opera Android 53 Safari iOS 14
14
Safari 14 Technology Preview 107-111 returns a string instead of a Locale object from the minimize and maximize methods.
Samsung Internet Android 11.0 nodejs 12.0.0
12.0.0
Before version 13.0.0, only the locale data for en-US is available by default. See the Locale() 构造函数 了解更多细节。
NumberFormat Chrome 24 Edge 12 Firefox 29 IE 11 Opera 15 Safari 10 WebView Android 4.4 Chrome Android 25 Firefox Android 56 Opera Android 14 Safari iOS 10 Samsung Internet Android 1.5 nodejs 0.12
0.12
Before version 13.0.0, only the locale data for en-US is available by default. See the NumberFormat() 构造函数 了解更多细节。
PluralRules Chrome 63 Edge 18 Firefox 58 IE No Opera 50 Safari 13 WebView Android 63 Chrome Android 63 Firefox Android 58 Opera Android 46 Safari iOS 13 Samsung Internet Android 8.0 nodejs 10.0.0
10.0.0
Before version 13.0.0, only the locale data for en-US is available by default. See the PluralRules() 构造函数 了解更多细节。
RelativeTimeFormat Chrome 71 Edge 79 Firefox 65 IE No Opera 58 Safari No WebView Android 71 Chrome Android 71 Firefox Android 65 Opera Android 50 Safari iOS No Samsung Internet Android 10.0 nodejs 12.0.0
12.0.0
Before version 13.0.0, only the locale data for en-US is available by default. See the RelativeTimeFormat() 构造函数 了解更多细节。

图例

完整支持
完整支持
不支持
不支持
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
见实现注意事项。

另请参阅

Metadata