国际化

Intl 命名空间对象包含多个构造函数以及国际化构造函数和其他语言敏感函数所共有的功能。它们共同组成了 ECMAScript 国际化 API,它提供语言敏感的字符串比较、数字格式化、日期和时间格式化等。

¥The Intl namespace object contains several constructors as well as functionality common to the internationalization constructors and other language sensitive functions. Collectively, they comprise the ECMAScript Internationalization API, which provides language sensitive string comparison, number formatting, date and time formatting, and more.

描述

¥Description

与大多数全局对象不同,Intl 不是构造函数。你不能将其与 new 运算符 一起使用或将 Intl 对象作为函数调用。Intl 的所有属性和方法都是静态的(就像 Math 对象一样)。

¥Unlike most global objects, Intl is not a constructor. You cannot use it with the new operator or invoke the Intl object as a function. All properties and methods of Intl are static (just like the Math object).

国际化构造函数以及其他构造函数的几种语言敏感方法(在 也可以看看 下列出)使用一种通用模式来识别语言环境并确定它们实际使用的语言环境:它们都接受 localesoptions 参数,并使用 options.localeMatcher 属性中指定的算法根据它们支持的语言环境协商请求的语言环境。

¥The internationalization constructors as well as several language sensitive methods of other constructors (listed under See also) 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 property.

语言环境参数

¥locales argument

locales 参数用于确定给定操作中使用的区域设置。JavaScript 实现检查 locales,然后计算它理解的最接近满足所表达的偏好的区域设置。locales 可能是:

¥The locales argument is used to determine the locale used in a given operation. The JavaScript implementation examines locales, and then computes a locale it understands that comes closest to satisfying the expressed preference. locales may be:

  • undefined(或省略):将使用实现的默认区域设置。
  • 区域设置:区域设置标识符或封装区域设置标识符的 Intl.Locale 对象。
  • 区域设置列表:任何其他值,将被转换为对象,然后被视为区域设置数组。

在后两种情况下,实际使用的区域设置是 区域设置协商 确定的最受支持的区域设置。如果区域设置标识符不是字符串或对象,则会抛出 TypeError。如果区域设置标识符是语法上无效的字符串,则会抛出 RangeError。如果区域设置标识符格式正确,但实现无法识别它,则会忽略它并考虑列表中的下一个区域设置,最终回退到系统的区域设置。但是,你不应该依赖于被忽略的特定区域设置名称,因为该实现可能会在将来添加任何区域设置的数据。例如,new Intl.DateTimeFormat("default") 使用实现的默认区域设置只是因为 "default" 在语法上有效,但不被识别为任何区域设置。

¥In the latter two cases, the actual locale used is the best-supported locale determined by locale negotiation. If a locale identifier is not a string or an object, a TypeError is thrown. If a locale identifier is a string that's syntactically invalid, a RangeError is thrown. If a locale identifier is well-formed but the implementation doesn't recognize it, it is ignored and the next locale in the list is considered, eventually falling back to the system's locale. However, you shouldn't rely on a particular locale name being ignored, because the implementation may add data for any locale in the future. For example, new Intl.DateTimeFormat("default") uses the implementation's default locale only because "default" is syntactically valid but not recognized as any locale.

区域设置标识符是一个字符串,包含以下内容:

¥A locale identifier is a string that consists of:

  1. 包含 2-3 或 5-8 个字母的语言子标签
  2. 包含 4 个字母 Optional 的脚本子标签
  3. 具有 2 个字母或 3 个数字 Optional 的区域子标签
  4. 一个或多个变体子标签(所有子标签都必须是唯一的),每个子标签包含 5-8 个字母数字或一个数字后跟 3 个字母数字 Optional
  5. 一个或多个 BCP 47 扩展序列 Optional
  6. 私有使用的扩展序列 Optional

每个子标签和序列均由连字符分隔。区域设置标识符不区分大小写 ASCII。但是,通常对脚本子标签使用标题大小写(第一个字母大写,后续字母小写),对区域子标签使用大写,对其他所有内容使用小写。例如:

¥Each subtag and sequence are separated by hyphens. Locale identifiers are case-insensitive ASCII. However, it's conventional to use title case (the first letter is capitalized, successive letters are lower case) for script subtags, upper case for region subtags, and lower case for everything else. For example:

  • "hi":印地语(语言)
  • "de-AT":奥地利(地区)使用的德语(语言)
  • "zh-Hans-CN":中国(地区)使用的简体字(文字)书写的中文(语言)
  • "en-emodeng":"早期现代英语" 方言(变体)中的英语(语言)

识别语言、脚本、地区(包括国家)和(很少使用)变体的子标签已在 IANA 语言子标签注册表 中注册。该注册表会随着时间的推移定期更新,并且实现可能并不总是最新的,因此不要过于依赖普遍支持的子标签。

¥Subtags identifying languages, scripts, regions (including countries), and (rarely used) variants 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 don't rely too much on subtags being universally supported.

BCP 47 扩展序列由单个数字或字母("x" 除外)和一个或多个由连字符分隔的两到八个字母或数字子标签组成。每个数字或字母只允许有一个序列:"de-a-foo-a-foo" 无效。BCP 47 扩展子标签在 Unicode CLDR 项目.X 中定义。目前只有两个扩展定义了语义:

¥BCP 47 extension sequences consist of a single digit or letter (other than "x") and one or more two- to eight-letter or digit subtags separated by hyphens. Only one sequence is permitted for each digit or letter: "de-a-foo-a-foo" is invalid. BCP 47 extension subtags are defined in the Unicode CLDR Project. Currently only two extensions have defined semantics:

  • "u" (Unicode) 扩展可用于请求 Intl API 对象的其他自定义。示例:
    • "de-DE-u-co-phonebk":使用德语排序顺序的调用簿变体,它将变音元音解释为相应的字符对:ä → ae,ö → oe,ü → ue。
    • "th-TH-u-nu-thai":在数字格式中使用泰语数字(๐、๑、๒、๓、๔、๕、๖、๗、๘、๙)。
    • "ja-JP-u-ca-japanese":在日期和时间格式中使用日本日历,因此 2013 年表示为平成 25 年,即平成 25。
    • "en-GB-u-ca-islamic":使用英式英语和伊斯兰(回历)日历,其中公历日期 2017 年 10 月 14 日是回历日期 1439 年穆哈兰月 24 日。
  • "t"(已转换)扩展名表示已转换的内容:例如,从另一个语言环境翻译的文本。当前没有 Intl 功能考虑 "t" 扩展。但是,此扩展有时包含嵌套语言环境(没有扩展):例如,"de-t-en" 中转换后的扩展名包含英语的区域设置标识符。如果存在嵌套区域设置,则它必须是有效的区域设置标识符。例如,由于 "en-emodeng-emodeng" 无效(因为它包含重复的 emodeng 变体子标签),所以 "de-t-en-emodeng-emodeng" 也无效。

最后,可能会出现使用字母 "x" 的私有使用扩展序列,后跟一个或多个由连字符分隔的一到八个字母或数字子标签。这允许应用编码供自己私有使用的信息,这些信息将被所有 Intl 操作忽略。

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

选项参数

¥options argument

options 参数必须是一个对象,其属性在构造函数和函数之间有所不同。如果未提供或未定义 options 参数,则所有属性均使用默认值。

¥The 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.

所有语言敏感的构造函数和函数都支持一种属性:localeMatcher 属性,其值必须是字符串 "lookup""best fit",并且选择下面描述的区域设置匹配算法之一。

¥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 below.

区域设置识别和协商

¥Locale identification and negotiation

locales 参数指定的区域设置列表在删除了 Unicode 扩展后,将被解释为来自应用的优先请求。运行时将其与可用的语言环境进行比较,并选择最佳的可用语言环境。存在两种匹配算法:"lookup" 匹配器遵循 BCP 47 中指定的查找算法;"best fit" 匹配器让运行时提供一个区域设置,该区域设置至少(但可能比查找算法的结果)更适合请求。如果应用不提供 locales 参数,或者运行时没有与请求匹配的区域设置,则使用运行时的默认区域设置。可以使用 options 参数的属性来选择匹配器(见下文)。

¥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).

如果选定的区域设置标识符具有 Unicode 扩展序列,则该扩展现在用于自定义构造的对象或函数的行为。每个构造函数或函数仅支持为 Unicode 扩展定义的键的子集,并且支持的值通常取决于区域设置标识符。例如,"co" 键(排序规则)仅受 Intl.Collator 支持,其 "phonebk" 值仅支持德语。

¥If the selected locale identifier had a Unicode extension sequence, 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 locale identifier. For example, the "co" key (collation) is only supported by Intl.Collator, and its "phonebk" value is only supported for German.

静态属性

¥Static properties

Intl.Collator

排序器的构造函数,排序器是启用语言敏感字符串比较的对象。

Intl.DateTimeFormat

启用语言敏感的日期和时间格式的对象的构造函数。

Intl.DisplayNames

支持语言、区域和脚本显示名称一致翻译的对象的构造函数。

Intl.DurationFormat Experimental

启用区域设置敏感的持续时间格式的对象的构造函数。

Intl.ListFormat

启用语言敏感列表格式的对象的构造函数。

Intl.Locale

表示 Unicode 区域设置标识符的对象的构造函数。

Intl.NumberFormat

启用语言敏感数字格式的对象的构造函数。

Intl.PluralRules

启用复数敏感格式和特定于语言的复数规则的对象的构造函数。

Intl.RelativeTimeFormat

启用语言敏感的相对时间格式的对象的构造函数。

Intl.Segmenter

启用区域设置敏感文本分段的对象的构造函数。

Intl[@@toStringTag]

@@toStringTag 属性的初始值为字符串 "Intl"。该属性在 Object.prototype.toString() 中使用。

静态方法

¥Static methods

Intl.getCanonicalLocales()

返回规范的语言环境名称。

Intl.supportedValuesOf()

返回一个排序数组,其中包含实现支持的唯一日历、排序规则、货币、编号系统或单位值。

示例

¥Examples

设置日期和数字的格式

¥Formatting dates and numbers

你可以使用 Intl 以特定语言和区域的常规形式格式化日期和数字:

¥You can use Intl to format dates and numbers in a form that's conventional for a specific language and region:

js
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"); // 5/24/2012 26,254.39

log("de-DE"); // 24.5.2012 26.254,39

使用浏览器的首选语言

¥Using the browser's preferred language

你可以使用 navigator.language 提供的用户首选语言,而不是将硬编码的区域设置名称传递给 Intl 方法:

¥Instead of passing a hardcoded locale name to the Intl methods, you can use the user's preferred language provided by navigator.language:

js
const date = new Date("2012-05-24");

const formattedDate = new Intl.DateTimeFormat(navigator.language).format(date);

或者,navigator.languages 属性提供用户首选语言的排序列表。该列表可以直接传递给 Intl 构造函数,以实现基于首选项的语言环境后备选择。区域设置协商 进程用于选择最合适的可用区域设置:

¥Alternatively, the navigator.languages property provides a sorted list of the user's preferred languages. This list can be passed directly to the Intl constructors to implement preference-based fallback selection of locales. The locale negotiation process is used to pick the most appropriate locale available:

js
const count = 26254.39;

const formattedCount = new Intl.NumberFormat(navigator.languages).format(count);

规范

Specification
ECMAScript Internationalization API Specification
# intl-object

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看