Draft ECMA-402 / March 24, 2026 ECMAScript® 2026 Internationalization API Specification
介绍
本规范的源代码可在 https://github.com/tc39/ecma402 找到。
ECMAScript 2026 国际化 API 规范(ECMA-402 第13版)为 ECMA-262 提供了关键的语言敏感功能。其功能选自诸如 Unicode 国际化组件(ICU)库 (https://unicode-org.github.io/icu-docs/ )、.NET 框架和 Java
平台等成熟的国际化 API。
第1版 API 由 Ecma TC39 于2010年9月成立的临时小组开发,基于 Nebojša Ćirić 和 Jungshik Shin 的提案。
第2版 API 于2015年6月由大会采纳,作为 ECMAScript 第6版的补充。
第3版 API 是在 Ecma TC39 新的年度发布节奏和开放开发流程下发布的首个版本。ECMA-402 源文档被构建为纯文本源文件,作为后续完全在 GitHub
上开发的基础。在本标准开发的一年中,提交了数十个拉取请求和问题,涵盖了许多错误修复、编辑修正和其他改进。此外,还开发了许多软件工具以协助此工作,包括 Ecmarkup、Ecmarkdown 和 Grammarkdown。
许多组织的数十位个人在 Ecma TC39 内为本版及之前各版的开发做出了重要贡献。此外,一个充满活力的社区也在支持 TC39 的 ECMAScript
工作。该社区审查了众多草案,提交了数十份错误报告,进行了实现实验,贡献了测试套件,并向全球开发者社区普及了 ECMAScript 国际化。遗憾的是,无法识别和感谢每一位为此做出贡献的个人和组织。
Norbert Lindenberg
ECMA-402,第1版项目编辑
Rick Waldron
ECMA-402,第2版项目编辑
Caridy Patiño
ECMA-402,第3、4、5版项目编辑
Caridy Patiño, Daniel Ehrenberg, Leo Balter
ECMA-402,第6版项目编辑
Leo Balter, Valerie Young, Isaac Durazo
ECMA-402,第7版项目编辑
Leo Balter, Richard Gibson
ECMA-402,第8版项目编辑
Leo Balter, Richard Gibson, Ujjwal Sharma
ECMA-402,第9版项目编辑
Richard Gibson, Ujjwal Sharma
ECMA-402,第10版项目编辑
Richard Gibson, Ujjwal Sharma
ECMA-402,第11版项目编辑
Ben Allen, Richard Gibson, Ujjwal Sharma
ECMA-402,第12版项目编辑
Ben Allen, Richard Gibson, Ujjwal Sharma
ECMA-402,第13版项目编辑
1 范围
本标准定义了 ECMAScript 对象的应用程序接口,支持需要适应不同人类语言和国家所使用的语言和文化习惯的程序。
2 一致性
本规范的符合性实现必须符合 ECMA-262 ,并且必须提供和支持本规范中描述的所有对象、属性、函数和程序语义。本规范中的任何内容都不应允许 ECMA-262 明令禁止的行为,任何此类冲突应被视为编辑错误,而不是对 ECMA-262
限制的覆盖。
符合性实现允许提供本规范之外的其他对象、属性和函数。特别是,符合性实现允许为本规范中描述的对象提供未在本规范中描述的属性及其值。符合性实现不允许为本规范中定义的函数添加可选参数。
符合性实现允许接受额外的值,并在以下 options 参数的属性中具有实现定义的行为,而不是抛出 RangeError :
所有构造函数和 supportedLocalesOf 方法中的 options 属性 "localeMatcher" 。
Collator 构造函数中的 options 属性 "usage" 和 "sensitivity" 。
NumberFormat 构造函数中的 options 属性
"style" 、"currencyDisplay" 、"notation" 、"compactDisplay" 、"signDisplay" 、"currencySign" 和 "unitDisplay" 。
NumberFormat 构造函数中的 options 属性
"minimumIntegerDigits" 、"minimumFractionDigits" 、"maximumFractionDigits" 、"minimumSignificantDigits" 和
"maximumSignificantDigits" ,前提是这些额外的值被解释为高于指定限制的整数值。
DateTimeFormat 构造函数中的 Table 16 所列的 options 属性。
DateTimeFormat 构造函数中的 options 属性 "formatMatcher" 。
PluralRules 构造函数中的 options 属性 "minimumIntegerDigits" 、"minimumFractionDigits" 、"maximumFractionDigits" 和
"minimumSignificantDigits" ,前提是这些额外的值被解释为高于指定限制的整数值。
PluralRules 构造函数中的 options 属性 "type" 。
RelativeTimeFormat 构造函数中的 options 属性 "style" 和 "numeric" 。
DisplayNames 构造函数中的 options 属性 "style" 和 "type" 。
3 规范性引用
下列参考文献是本文件应用所必需的。对于有日期的引用,仅适用于所引用的版本。对于无日期的引用,适用参考文献的最新版本(包括任何修订)。
ECMAScript 2026 语言规范(ECMA-262 第17版或后续版本)。
https://www.ecma-international.org/publications/standards/Ecma-262.htm
Note
本规范依赖于这些参考文献的部分会尽力保持更新,但不能保证与这些标准保持同步。
4 概述
本节为非规范性内容。
4.1 国际化、本地化和全球化
软件的国际化是指设计软件,使其支持或能够轻松适应不同语言和文化期望用户的需求,并实现全球范围内的交流。本地化则是实际针对特定语言和文化进行适配。软件全球化通常被理解为国际化和本地化的结合。全球化从最低层开始,采用支持所有语言的文本表示,并使用标准标识符来识别语言、国家、时区及其他相关参数。它还包括使用用户界面语言和用户理解的数据展示,最后通常需要针对用户的语言、文化和环境进行产品特定的适配。
ECMA-262 通过使用Unicode进行文本表示,并提供少量语言敏感的函数,为国际化奠定了基础,但应用程序对这些函数的行为控制有限。本规范在此基础上,提供了一套可定制的语言敏感功能。即使应用程序本身未国际化,该API也很有用,因为即便只针对一种语言和地区,也需要正确支持该语言和地区。然而,该API也支持同时支持多种语言和地区的应用程序,这在服务器环境中可能是必要的。
4.2 API概述
本规范旨在补充ECMA-262 ,提供关键的语言敏感功能,可以整体或部分添加到其实现中。本规范引入了ECMAScript代码可观察的新语言值(如[[FallbackSymbol]] 内部槽的值,以及通过属性访问 从%Intl% 可递归访问的值集合),并细化了ECMA-262 中某些函数的定义(如下所述)。这两类都不禁止ECMA-262 中定义的值和接口允许的其他行为,以支持任何实现采用本规范。
本规范提供了大多数应用程序所需的几项关键语言敏感功能:区域选择与检查、字符串比较(排序)与大小写转换、复数规则、文本分段,以及数字、绝对和相对日期与时间、持续时间和列表的格式化。虽然ECMA-262 为这些基本功能提供了函数(在Array.prototype 上:toLocaleString;在String.prototype 上:localeCompare、toLocaleLowerCase、toLocaleUpperCase;在Number.prototype 上:toLocaleString;在Date.prototype 上:toLocaleString、toLocaleDateString和toLocaleTimeString),但它们的实际行为在很大程度上由实现决定。本规范提供了更多功能、对语言和行为细节的控制,以及更完整的功能规范。
应用程序可以通过两种方式使用API:
直接使用,通过服务构造函数 构造对象,指定首选语言列表和配置选项。该对象提供主要函数(如compare、select、format等),可重复调用。它还提供resolvedOptions函数,应用程序可用其查询对象的具体配置。
间接使用,通过上述ECMA-262 的函数。排序和格式化函数在本规范中重新定义,接受与Collator、NumberFormat和DateTimeFormat构造函数相同的参数,并产生与其compare或format方法相同的结果。大小写转换函数重新定义为接受首选语言列表。
Intl对象用于封装本规范定义的所有功能,以避免名称冲突。
Note 虽然API包含多种格式化器,但不提供任何解析功能。这是有意为之,经过充分讨论,并在权衡所有利弊后做出决定。详见
问题追踪 。
4.3 API约定
每个Intl构造函数 应表现得如同由类定义,在作为函数(无NewTarget)调用时抛出TypeError 异常。为兼容以往版本,%Intl.Collator% 、%Intl.DateTimeFormat% 和%Intl.NumberFormat% 在作为函数调用时仍会构造并返回新对象。
Note
在ECMA 402 v1中,Intl构造函数支持一种操作模式,即用已有对象作为接收者调用时,会向该对象添加相关内部槽,使其变为类实例。在ECMA 402 v2中,为避免向现有对象添加内部槽,该能力被移除。在ECMA 402
v3中,该能力以“规范性可选”模式重新添加,即构造函数调用时会将底层Intl实例链到任何对象上。详情见
Issue
57 。
4.4 实现依赖
由于国际化的特殊性,本规范必须将若干细节留给实现决定:
实现支持的具有充分本地化的区域集合: 语言学家已描述了数千种人类语言,IANA语言子标签 注册表包含7000多个主语言子标签 (用于区域标识符)。即使是大型区域数据集,如Common Locale Data
Repository,也只覆盖所有语言及其地区或方言变体的一小部分。针对资源受限设备的实现可能还需进一步缩减集合。
本地化内容如格式模式的具体形式: 许多情况下,区域相关的惯例并未标准化,因此可能存在多种形式,或随时间变化。不同国际化库可能实现了不同形式,但实际上都不算错误。为允许该API基于现有库实现,必须允许这些差异。
Unicode子集: 某些操作(如排序)可作用于包含整个Unicode字符集的字符串。然而,Unicode标准和ECMAScript标准都允许实现将功能限制在Unicode字符集的子集。此外,区域惯例通常只规定与该区域相关字符的期望行为,而非整个Unicode字符集。Unicode排序算法结合了整个Unicode字符集的默认排序顺序和本地惯例的定制,子集和定制仍会导致行为差异。
在浏览器实现中,某一来源可见的初始区域、货币、日历、编号系统及其他可枚举项集合,必须对所有共享同一用户代理字符串(引擎和平台版本)的用户一致。此外,这些集合的动态变化不得导致用户之间可区分。此约束旨在降低国际化固有的指纹识别风险,未来版本可能会放宽。受此约束,允许按需安装区域的浏览器实现首次收到某来源请求可能需安装新区域时,不得透露该区域是否已安装。
在本规范中,实现和区域相关的行为称为ILD ,实现、区域和编号系统相关的行为称为ILND 。
4.4.1 跨实现的兼容性
ECMA
402描述了其函数所用数据的结构。数据内容由实现决定,预期随时间变化且各实现不同。程序员可观察到这些差异,并可构造依赖特定输出的程序。但本规范力求描述合理约束,使编写良好的程序能在各实现间运行。鼓励实现继续努力协调语言数据。
5 符号约定
本标准使用 ECMA-262 的符号约定子集:
Note
如
ECMA-262 所述,算法用于精确指定 ECMAScript 构造的必要语义,但并不意味着必须采用任何特定的实现技术。内部槽用于定义对象值的语义,但不是 API 的一部分。它们仅用于说明目的。API
的实现必须表现得如同以此处描述的方式生成和操作内部槽。
作为 Record 规范类型的扩展,符号“[[<name >]]”表示字段名由变量 name 给出,name 必须为字符串值。例如,如果变量 s 的值为 "a" ,则 [[<s >]] 表示字段
[[a]] 。
本规范使用标记为 规范性可选 的区块,表示 ECMA 262 中 附录 B 的含义。即,规范性可选部分在
ECMAScript 宿主为网页浏览器时是必需的。如果 ECMAScript 宿主不是网页浏览器,则该部分内容是规范性的但可选。
5.1 知名内在对象
下表扩展了 Well-Known Intrinsic Objects 中定义的知名内在对象表。
Table 1: 知名内在对象(扩展)
6 语言环境、货币、时区、度量单位、数字系统、排序规则和日历的标识
本条款描述了本规范中用于标识语言环境、货币、时区、度量单位、数字系统、排序规则、日历和模式字符串 的字符串值。
6.1 大小写敏感性与大小写映射
用于标识语言环境、货币、脚本和时区的字符串值以 ASCII 大小写不敏感方式解释,将代码单元 0x0041 到 0x005A(对应 Unicode 字符 LATIN CAPITAL LETTER A 到 LATIN
CAPITAL LETTER Z)视为与相应代码单元 0x0061 到 0x007A(对应 Unicode 字符 LATIN SMALL LETTER A 到 LATIN SMALL LETTER
Z)等价。不会应用其他大小写折叠等价关系。
Note
例如,"ß" (U+00DF)不得匹配或映射到 "SS" (U+0053, U+0053)。"ı" (U+0131)不得匹配或映射到 "I" (U+0049)。
字符串值 S 的 ASCII-大写 是通过将 S 中每个 ASCII 小写字母代码单元(0x0061 到 0x007A,包括)替换为相应的 ASCII 大写字母代码单元(0x0041 到
0x005A,包括),同时保留所有其他代码单元而得到的字符串值。
字符串值 S 的 ASCII-小写 是通过将 S 中每个 ASCII 大写字母代码单元(0x0041 到 0x005A,包括)替换为相应的 ASCII 小写字母代码单元(0x0061 到
0x007A,包括),同时保留所有其他代码单元而得到的字符串值。
如果字符串值 A 的 ASCII-大写与字符串值 B 的 ASCII-大写完全相同,则 A 是 B 的 ASCII-大小写不敏感匹配 。如果 Unicode 代码点序列 A 的
CodePointsToString (A ) 与 B 是 ASCII-大小写不敏感匹配,则 A 与 B 是 ASCII-大小写不敏感匹配。
6.2 语言标签
本规范使用 Unicode BCP 47 语言环境标识符 (定义见 Unicode 技术标准 #35 第一部分核心,第 3.3 节 BCP 47
合规性 ),其算法引用 第 3 节 Unicode
语言和语言环境标识符 语法中定义的 Unicode 语言环境非终结符 。
每个标识符也可称为 语言标签 ,实际上也是 BCP 47 中使用的有效语言标签。
按 Unicode 技术标准 #35 第一部分核心,第 3.2.1
节 Canonical Unicode Locale Identifiers 规范的规范形式语言环境标识符称为 "Unicode 规范化语言环境标识符 "。
语言环境标识符由大小写不敏感的 Unicode 基本拉丁字母数字 子标签 组成,以 "-" (U+002D
HYPHEN-MINUS)字符分隔,单字符 子标签 称为 "单例子标签 "。
Unicode 技术标准 #35 第一部分核心,第 3.6 节 Unicode BCP 47 U 扩展
子标签 序列被广泛使用,术语 " Unicode 语言环境扩展序列 " 描述了可被
unicode_locale_extensions Unicode 语言环境非终结符匹配且不属于 "-x-…" 私有用途 子标签序列 的最长
子字符串 。它以 "-u-" 开头,并包含所有紧随其后的非单例 子标签 及其前导 "-"
分隔符。例如,"en-US-u-fw-mon-x-u-ex-foobar" 的 Unicode 语言环境扩展序列 为 "-u-fw-mon" 。
所有格式良好的语言标签 都适用于本规范定义的 API,但实现不要求使用 Unicode 通用语言环境数据存储库(CLDR )数据进行验证;实现支持的语言环境集合及其语言标签是实现定义的。Intl 构造函数 将请求的语言标签映射到各自实现支持的语言环境。
6.2.1 IsWellFormedLanguageTag ( locale )
The abstract operation IsWellFormedLanguageTag takes argument locale (一个字符串) and returns 一个布尔值. 判断 locale 是否为符合 unicode_bcp47_locale_id
格式良好性约束的 格式良好的语言标签 。不考虑 locale
是否具有任何有意义的语义,也不区分别名子标签 与其首选替换子标签 ,也不要求规范大小写或子标签 排序。 It performs the following steps when called:
令 lowerLocale 为 locale 的 ASCII-小写 。 如果 lowerLocale 不能被 unicode_locale_id Unicode 语言环境非终结符 匹配,则返回 false 。 如果 lowerLocale 使用了 Unicode 技术标准 #35 第一部分核心,第
3.3 节 BCP 47 合规性 中描述的任何向后兼容语法,则返回 false 。 令 baseName 为 GetLocaleBaseName (lowerLocale )。 令 variants 为 GetLocaleVariants (baseName )。 如果 variants 不为 undefined ,则 如果 variants 包含任何重复子标签 ,则返回 false 。 令 extensions 为 lowerLocale 中 baseName 后的后缀。 注:被 pu_extensions Unicode 语言环境非终结符 匹配的 "-x-…" 私有用途子标签 序列必须被忽略,但孤立的末尾 "x" 子标签 且无后续内容不影响以下检查。 令 puIndex 为 StringIndexOf (extensions , "-x-" , 0)。 如果 puIndex 不为 not-found ,则将 extensions 设置为 extensions 从 0 到 puIndex 的子字符串。 如果 extensions 不是空字符串,则 如果 extensions 包含任何重复单例子标签 ,则返回 false 。 令 transformExtension 为 extensions 中被 transformed_extensions Unicode 语言环境非终结符 匹配的最长
子字符串 。如果没有这样的 子字符串 ,则返回 true 。 断言:transformExtension 从 0 到 3 的子字符串为 "-t-" 。 令 tPrefix 为 transformExtension 从 3 开始的子字符串。 令 tlang 为 tPrefix 中被 tlang Unicode 语言环境非终结符 匹配的最长前缀。如果没有这样的前缀,则返回 true 。 令 tlangVariants 为 GetLocaleVariants (tlang )。 如果 tlangVariants 包含任何重复子标签 ,则返回 false 。 返回 true 。
6.2.2 CanonicalizeUnicodeLocaleId ( locale )
The abstract operation CanonicalizeUnicodeLocaleId takes argument locale (一个语言标签 ) and returns 一个 Unicode 规范化语言环境标识符 . 返回 locale 的规范化且大小写规则化形式。 It performs the following steps when called:
令 localeId 为执行 Processing LocaleIds
算法,将 locale 转换为规范形式,按 Unicode 技术标准 #35
第一部分核心,附录 C LocaleId Canonicalization 。 如果 localeId 包含一个 子字符串 是
Unicode 语言环境扩展序列 ,则 令 extension 为 localeId 中 Unicode 语言环境扩展序列 的 子字符串 。 令 newExtension 为 "-u" 。 令 components 为 UnicodeExtensionComponents (extension )。 对 components .[[Attributes]] 的每个元素 attr ,执行 将 newExtension 设置为 newExtension 、"-" 和 attr 的字符串连接。 对 components .[[Keywords]] 的每个记录 { [[Key]] , [[Value]] } keyword ,执行 将 newExtension 设置为 newExtension 、"-" 和 keyword .[[Key]] 的字符串连接。 如果 keyword .[[Value]] 不是空字符串,则 将 newExtension 设置为 newExtension 、"-" 和 keyword .[[Value]] 的字符串连接。 断言:newExtension 不是 "-u" 。 将 localeId 设置为 localeId 的副本,其中首次出现的 子字符串 extension 被替换为 newExtension 。 返回 localeId 。
Note
步骤
2 确保返回的
语言标签 中的
Unicode 语言环境扩展序列 包含:
仅输入中每个属性的第一个实例,且
仅输入中每个键的第一个 关键字 。
6.2.3 DefaultLocale ( )
The implementation-defined abstract operation DefaultLocale takes no arguments and returns 一个 Unicode 规范化语言环境标识符 . 返回的字符串值表示主机环境当前语言环境的格式良好(6.2.1 )且规范化(6.2.2 )语言标签 。不得包含 Unicode 语言环境扩展序列 。
Note
返回值是潜在的指纹识别向量。在浏览器环境中,应与
navigator.language
匹配,以避免提供任何额外的区分信息。
6.3 货币代码
本规范使用 ISO 4217 定义的 3 字母货币代码标识货币。其规范形式为大写。
允许所有格式良好的 3 字母 ISO 4217 货币代码。但实现支持的货币代码与语言标签 组合是否有本地化货币符号是实现依赖的。如果没有本地化货币符号,则格式化时使用 ISO 4217 货币代码。
6.3.1 IsWellFormedCurrencyCode ( currency )
The abstract operation IsWellFormedCurrencyCode takes argument currency (一个字符串) and returns 一个布尔值. 验证 currency 参数是否表示格式良好的 3 字母 ISO 4217 货币代码。 It performs the following steps when called:
如果 currency 的长度不是 3,则返回 false 。 令 normalized 为 currency 的 ASCII-大写 。 如果 normalized 包含任何代码单元不在 0x0041 到 0x005A(对应 Unicode 字符 LATIN CAPITAL LETTER A 到 LATIN CAPITAL LETTER
Z)范围内,则返回 false 。 返回 true 。
6.4 AvailableCanonicalCurrencies ( )
The implementation-defined abstract operation AvailableCanonicalCurrencies takes no arguments and returns 字符串列表. 返回的列表按字典序代码单元排序,包含唯一、格式良好且大写规范化的 3 字母 ISO 4217 货币代码,标识实现为 Intl.DisplayNames 和 Intl.NumberFormat 对象提供功能的货币。
6.5 使用 IANA 时区数据库
采用本规范的实现必须支持时区:必须使用 IANA 时区数据库 https://www.iana.org/time-zones/
提供可用命名时区标识符和 ECMAScript 计算与格式化所用数据。
本节定义时区支持实现应如何使用 IANA 时区数据库。
ECMAScript 内置对象返回的可用命名时区标识符必须是 IANA 时区数据库中的 Zone 名称或 Link 名称。
可用命名时区标识符必须使用 IANA 时区数据库中的原始大小写。
每个 IANA 时区数据库中的 Zone 必须是主时区标识符,每个 Link 名称必须是非主时区标识符并解析为其对应的 Zone 名称,以下例外情况由 AvailableNamedTimeZoneIdentifiers 实现:
出于历史原因,"UTC" 必须是主时区标识符。
"Etc/UTC" 、"Etc/GMT" 和 "GMT" 以及解析为它们的所有 Link 名称,必须是解析为 "UTC" 的非主时区标识符。
zone.tab 文件 “TZ” 列中出现的任何 Link 名称必须是主时区标识符。
例如,"Europe/Prague" 和 "Europe/Bratislava" 都必须是主时区标识符。
此要求保证每个 ISO 3166-1 Alpha-2
国家代码至少有一个主时区标识符,并确保未来某国时区规则变更不会影响使用其他国家时区的 ECMAScript 程序,除非这些国家的领土边界也发生变化。
zone.tab 文件 “TZ” 列未列出的任何 Link 名称,且代表完全位于单一 ISO 3166-1
Alpha-2 国家代码领土内的地理区域,必须解析为同一国家代码领土内的主标识符。
例如,"Atlantic/Jan_Mayen" 必须解析为 "Arctic/Longyearbyen" 。
Note
IANA 时区数据库提供影响主时区标识符的构建选项。
默认构建选项会合并不同国家的时区,例如 "Atlantic/Reykjavik" 被构建为指向 Zone "Africa/Abidjan" 的 Link。
地理和政治上不同的地区未来可能会在 IANA 时区数据库新版本中引入不同的时区规则。
上述例外旨在缓解这些未来兼容性问题。
Unicode 通用语言环境数据存储库(CLDR )在确定哪些可用命名时区标识符是主或非主时区标识符时实现了上述大部分例外。
虽然建议使用 CLDR 数据以实现间一致性,但并非强制。
非 CLDR 基础实现仍可使用 CLDR 的 timezone.xml
标识符数据。
实现也可直接构建 IANA 时区数据库,例如使用 PACKRATDATA=backzone PACKRATLIST=zone.tab 构建选项,并进行必要的后处理以确保符合上述要求。
IANA 时区数据库通常每年更新五到十次。
这些更新可能添加新的 Zone 或 Link 名称,可能将 Zone 改为 Link,也可能更改与任何 Zone 相关的 UTC 偏移和转换。
建议实现尽快包含 IANA 时区数据库的更新。
这样可确保 ECMAScript 程序能准确进行时区敏感计算,并能使用外部输入或主机环境提供的新可用命名时区标识符。
虽然 IANA 时区数据库维护者力求稳定,但在极少数情况下(平均每年不到一次)Zone 可能被新 Zone 替换。
例如,2022 年 "Europe/Kiev " 被弃用为指向新 "Europe/Kyiv " Zone 的 Link。
被弃用的 Link 称为 重命名时区标识符 ,新添加的 Zone 称为 替换时区标识符 。
为减少这些罕见变更带来的影响,实现应最初将每个替换时区标识符作为解析为现有重命名时区标识符的非主时区标识符添加。
这样 ECMAScript 程序可识别两个标识符,同时减少将替换时区标识符发送到尚未识别它的其他系统的概率。
在 重命名等待期 后,实现应将新 Zone 提升为主时区标识符,同时将重命名时区标识符降为非主。
为给其他系统提供充足更新时间,建议的重命名等待期 为两年。
但无需精确或动态。
实现应在正常发布流程中更新时区数据时,将替换时区标识符设为主。
仅当新 Zone 被添加以替换现有 Zone 时,才应适用等待期。
如果现有 Zone 和 Link 被交换,则未发生重命名,无需等待期。
如果实现于代理生命周期内修订时区信息,则要求可用命名时区标识符列表、与任何可用命名时区标识符关联的主时区标识符,以及与任何可用命名时区标识符关联的 UTC 偏移和转换,与代理先前观察到的结果一致。
鉴于支持此要求的复杂性,建议实现为每个代理生命周期维护 IANA 时区数据库的完全一致副本。
本节补充但不取代 21.4.1.19 。
6.5.1 AvailableNamedTimeZoneIdentifiers ( )
The implementation-defined abstract operation AvailableNamedTimeZoneIdentifiers takes no arguments and returns 时区标识符记录列表.
结果描述本实现中所有可用命名时区标识符,以及每个可用命名时区标识符对应的主时区标识符。
列表按每个时区标识符记录的 [[Identifier]] 字段排序。
此定义取代 21.4.1.23 中的定义。
令 identifiers 为 IANA 时区数据库中每个 Zone 或 Link 名称的字符串值列表。 断言:identifiers 中无元素与其他元素为 ASCII-大小写不敏感匹配 。 按字典序代码单元排序 identifiers 。 令 result 为新的空列表。 对 identifiers 的每个元素 identifier ,执行 令 primary 为 identifier 。 如果 identifier 是 IANA 时区数据库中的 Link 名称且未出现在 zone.tab 文件 “TZ” 列,则 令 zone 为 identifier 按 IANA 时区数据库解析 Link 名称规则解析到的 Zone 名称。 如果 zone 以 "Etc/" 开头,则 将 primary 设置为 zone 。 否则, 令 identifierCountryCode 为包含 identifier 地理区域的 ISO
3166-1 Alpha-2 国家代码。 令 zoneCountryCode 为包含 zone 地理区域的 ISO 3166-1 Alpha-2 国家代码。 如果 identifierCountryCode 等于 zoneCountryCode ,则 将 primary 设置为 zone 。 否则, 令 countryCodeLineCount 为 zone.tab 文件中 “country-code” 列为 identifierCountryCode 的行数。 如果 countryCodeLineCount 为 1,则 令 countryCodeLine 为 zone.tab 文件中 “country-code” 列为 identifierCountryCode 的行。 将 primary 设置为 countryCodeLine “TZ” 列内容。 否则, 令 backzone 为 undefined 。 令 backzoneLinkLines 为 IANA 时区数据库 backzone 文件中以 "Link " 或 "#PACKRATLIST zone.tab Link " 开头的行列表。 对 backzoneLinkLines 的每个元素 line ,执行 令 i 为 StringIndexOf (line , "Link " , 0)。 将 line 设置为 line 从 i + 5 开始的子字符串。 令 backzoneAndLink 为 StringSplitToList (line , " " )。 断言:backzoneAndLink 至少有两个元素,且 backzoneAndLink [0] 和 backzoneAndLink [1] 都是可用命名时区标识符。 如果 backzoneAndLink [1] 等于 identifier ,则 断言:backzone 为 undefined 。 将 backzone 设置为 backzoneAndLink [0]。 断言:backzone 不为 undefined 。 将 primary 设置为 backzone 。 如果 primary 是 "Etc/UTC" 、"Etc/GMT" 或 "GMT" ,则将 primary 设置为 "UTC" 。 如果 primary 是替换时区标识符 且其重命名等待期 未结束,则 令 renamedIdentifier 为 primary 替换的重命名时区标识符 。 将 primary 设置为 renamedIdentifier 。 令 record 为时区标识符记录 { [[Identifier]] : identifier , [[PrimaryIdentifier]] : primary }。 将 record 添加到 result 。 断言:result 包含一个时区标识符记录 r ,使得 r .[[Identifier]] 为 "UTC" 且 r .[[PrimaryIdentifier]] 为 "UTC" 。 返回 result 。
Note 1
上述解析 Link 到主时区标识符的算法旨在对应国际组件 ICU (ICU ) 中
icu::TimeZone::getIanaID() 的行为,以及 Unicode 通用语言环境数据存储库 (CLDR ) 中时区标识符数据维护流程。
此算法解析 Link 到主时区标识符时不会跨越 ISO 3166-1 Alpha-2 国家代码边界,使用 IANA 时区数据库 zone.tab 和 backzone 文件的数据。
如果 Link 的国家代码在 zone.tab 中只有一行,则该行的 “TZ” 列决定主时区标识符。
如果该国家代码在 zone.tab 中有多行,则需检查 backzone 历史映射以确定正确的主时区标识符。
例如,若 IANA 时区数据库默认构建选项将 "Pacific/Truk"(国家代码 "FM")标识为指向 "Pacific/Port_Moresby"(国家代码 "PG")的 Link,则需检查 zone.tab
文件中 "FM" 的行。
若仅有一行,则该行 “TZ” 列决定 "Pacific/Truk" 的主时区标识符。
若有多行,则需检查 backzone 文件,若有 "Link Pacific/Chuuk Pacific/Truk" 行,则使用 "Pacific/Chuuk" 作为主时区标识符。
注意 zone.tab 是首选映射数据来源,因为 backzone 映射在极少数情况下可能跨越 ISO 3166-1 Alpha-2 国家代码边界。
例如,"Atlantic/Jan_Mayen"(国家代码 "SJ")在 backzone 中映射到 "Europe/Oslo"(国家代码 "NO")。
截至 IANA 时区数据库 2024a 版本,"Atlantic/Jan_Mayen" 是唯一发生此情况的案例。
Note 2
IANA 时区数据库中的时区标识符随时间变化。
建议实现至少将 AvailableNamedTimeZoneIdentifiers 的结果限制为代理生命周期内
GetAvailableNamedTimeZoneIdentifier 允许的变更。
鉴于支持这些建议的复杂性,建议 AvailableNamedTimeZoneIdentifiers 的结果在代理生命周期内保持不变。
6.5.2 GetAvailableNamedTimeZoneIdentifier ( timeZoneIdentifier )
The abstract operation GetAvailableNamedTimeZoneIdentifier takes argument timeZoneIdentifier (一个字符串) and returns 时区标识符记录或 empty .
如果 timeZoneIdentifier 是可用命名时区标识符,则返回 AvailableNamedTimeZoneIdentifiers 列表中的记录之一。
否则返回 empty 。
It performs the following steps when called:
对 AvailableNamedTimeZoneIdentifiers () 的每个元素 record ,执行 如果 record .[[Identifier]] 与 timeZoneIdentifier 是 ASCII-大小写不敏感匹配 ,则返回 record 。 返回 empty 。
Note
对于任何
timeZoneIdentifier 或与其
ASCII-大小写不敏感匹配 的值,要求返回的时区标识符记录在代理生命周期内字段值保持一致。
进一步要求时区标识符在代理生命周期内不得动态从主变为非主,即如果
timeZoneIdentifier 与先前调用 GetAvailableNamedTimeZoneIdentifier 返回结果的
[[PrimaryIdentifier]] 字段是
ASCII-大小写不敏感匹配 ,则 GetAvailableNamedTimeZoneIdentifier(
timeZoneIdentifier ) 必须返回
[[Identifier]] 等于
[[PrimaryIdentifier]] 的时区标识符记录。
鉴于支持这些要求的复杂性,建议
AvailableNamedTimeZoneIdentifiers (因此 GetAvailableNamedTimeZoneIdentifier 结果)在代理生命周期内保持不变。
6.5.3 AvailablePrimaryTimeZoneIdentifiers ( )
The abstract operation AvailablePrimaryTimeZoneIdentifiers takes no arguments and returns 字符串列表. 返回的列表是 IANA 时区数据库支持的 Zone 和 Link 名称的排序列表。 It performs the following steps when called:
令 records 为 AvailableNamedTimeZoneIdentifiers ()。 令 result 为新的空列表。 对 records 的每个元素 timeZoneIdentifierRecord ,执行 如果 timeZoneIdentifierRecord .[[Identifier]] 等于 timeZoneIdentifierRecord .[[PrimaryIdentifier]] ,则 将 timeZoneIdentifierRecord .[[Identifier]] 添加到 result 。 返回 result 。
6.5.4 StringSplitToList ( S , separator )
The abstract operation StringSplitToList takes arguments S (一个字符串) and separator (一个字符串) and returns 字符串列表.
返回的列表包含 S 中所有不含 separator 且被 separator 紧邻前后的位置的互不重叠子字符串。
每个 子字符串 会是相邻 separator 之间、S 最开头的 separator 前或 S 最末尾的 separator
后的空字符串,否则不会为空。
It performs the following steps when called:
断言:S 不是空字符串。 断言:separator 不是空字符串。 令 separatorLength 为 separator 的长度。 令 substrings 为新的空列表。 令 i 为 0。 令 j 为 StringIndexOf (S , separator , 0)。 当 j 不为 not-found 时重复, 令 T 为 S 从 i 到 j 的子字符串。 将 T 添加到 substrings 。 将 i 设置为 j + separatorLength 。 将 j 设置为 StringIndexOf (S , separator , i )。 令 T 为 S 从 i 开始的子字符串。 将 T 添加到 substrings 。 返回 substrings 。
6.6 度量单位标识符
本规范使用 核心单位标识符 (或等价的 核心单位 ID ),定义见
Unicode 技术标准 #35 第二部分通用,第 6.2
节单位标识符 。其规范形式为仅包含 Unicode 基本拉丁小写字母(U+0061 LATIN SMALL LETTER A 到 U+007A LATIN SMALL LETTER
Z)及零个或多个中间连字符(U+002D HYPHEN-MINUS)的字符串。
仅允许有限集合的核心单位标识符。
尝试使用未被允许的核心单位标识符会导致 RangeError 。
6.6.1 IsWellFormedUnitIdentifier ( unitIdentifier )
The abstract operation IsWellFormedUnitIdentifier takes argument unitIdentifier (一个字符串) and returns 一个布尔值. 验证 unitIdentifier 参数是否为格式良好的核心单位标识符 ,要么是被允许的单一单位,要么是由两个被允许的单一单位通过除法组成的复合单位。 It performs the following steps when called:
如果 IsSanctionedSingleUnitIdentifier (unitIdentifier ) 为 true ,则 返回 true 。 令 i 为 StringIndexOf (unitIdentifier , "-per-" , 0)。 如果 i 为 not-found 或 StringIndexOf (unitIdentifier , "-per-" , i + 1) 不为 not-found ,则 返回 false 。 断言:五字符子字符串 "-per-" 在 unitIdentifier 中仅出现一次,索引为 i 。 令 numerator 为 unitIdentifier 从 0 到 i 的子字符串。 令 denominator 为 unitIdentifier 从 i + 5 开始的子字符串。 如果 IsSanctionedSingleUnitIdentifier (numerator ) 和 IsSanctionedSingleUnitIdentifier (denominator ) 都为
true ,则 返回 true 。 返回 false 。
6.6.2 IsSanctionedSingleUnitIdentifier ( unitIdentifier )
The abstract operation IsSanctionedSingleUnitIdentifier takes argument unitIdentifier (一个字符串) and returns 一个布尔值. 验证 unitIdentifier 参数是否在本规范当前版本允许的单一单位标识符列表中,该列表是通用语言环境数据存储库 release 38 unit
validity data 的子集;列表可能随时间增长。如 Unicode 技术标准 #35 第二部分通用,第 6.2
节单位标识符 所述,单一单位标识符是未由其他单位标识符乘除组成的核心单位标识符 。 It performs the following steps when called:
如果 unitIdentifier 在下表 Table 2 中,则返回 true 。 否则返回 false .
Table 2: ECMAScript 允许使用的单一单位
单一单位标识符
acre
bit
byte
celsius
centimeter
day
degree
fahrenheit
fluid-ounce
foot
gallon
gigabit
gigabyte
gram
hectare
hour
inch
kilobit
kilobyte
kilogram
kilometer
liter
megabit
megabyte
meter
microsecond
mile
mile-scandinavian
milliliter
millimeter
millisecond
minute
month
nanosecond
ounce
percent
petabyte
pound
second
stone
terabit
terabyte
week
yard
year
6.6.3 AvailableCanonicalUnits ( )
The abstract operation AvailableCanonicalUnits takes no arguments and returns 字符串列表. 返回的列表按字典序代码单元排序,由下表 Table 2
每行(除表头)列出的唯一简单单位标识符组成。
6.7 数字系统标识符
本规范使用 数字系统标识符 ,对应 Unicode 技术标准 #35 第三部分数字,第 1
节数字系统 引用的名称。其规范形式为仅包含 Unicode 基本拉丁小写字母(U+0061 LATIN SMALL LETTER A 到 U+007A LATIN SMALL LETTER Z)的字符串。
6.7.1 AvailableCanonicalNumberingSystems ( )
The implementation-defined abstract operation AvailableCanonicalNumberingSystems takes no arguments and returns 字符串列表. 返回的列表按字典序代码单元排序,包含唯一规范化数字系统标识符 ,标识实现为 Intl.DateTimeFormat、Intl.NumberFormat 和 Intl.RelativeTimeFormat
对象提供功能的数字系统。列表必须包含下表 Table 30 每行(除表头)的 Numbering System 值。
6.8 排序规则类型
本规范使用 排序规则类型 ,定义见 Unicode 技术标准 #35 第五部分排序,第 3.1
节排序规则类型 。其规范形式为仅包含 Unicode 基本拉丁小写字母(U+0061 LATIN SMALL LETTER A 到 U+007A LATIN SMALL LETTER
Z)及零个或多个中间连字符(U+002D HYPHEN-MINUS)的字符串。
6.8.1 AvailableCanonicalCollations ( )
The implementation-defined abstract operation AvailableCanonicalCollations takes no arguments and returns 字符串列表. 返回的列表按字典序代码单元排序,包含唯一规范化排序规则类型 ,标识实现为 Intl.Collator 对象提供功能的排序规则。
6.9 日历类型
本规范使用 日历类型 ,定义见 Unicode 技术标准 #35 第四部分日期,第 2
节日历元素 。其规范形式为仅包含 Unicode 基本拉丁小写字母(U+0061 LATIN SMALL LETTER A 到 U+007A LATIN SMALL LETTER
Z)及零个或多个中间连字符(U+002D HYPHEN-MINUS)的字符串。
6.9.1 AvailableCalendars ( )
The implementation-defined abstract operation AvailableCalendars takes no arguments and returns 字符串列表. 返回的列表按字典序代码单元排序,包含唯一规范化形式的日历类型 (6.9 ),标识实现为 Intl.DateTimeFormat
对象提供功能的日历,包括其别名(如 "islamicc" 和 "islamic-civil" 要么都包含要么都不包含)。列表必须包含 "iso8601" 。
6.10 模式字符串类型
模式字符串 是一个字符串值,包含零个或多个形如 "{key}" 的子字符串,其中 key 可以是仅由 ASCII 单词字符组成的任意非空序列。抽象模式字符串的语法是实现细节,不向 ECMA-402
用户公开。
7 ECMAScript标准内置对象的要求
除非本文件另有规定,本标准中描述的对象、函数和构造器,均需遵守ECMAScript Standard Built-in Objects 中规定的标准内置ECMAScript对象的通用要求和限制。
8 Intl 对象
Intl 对象 :
是 %Intl% 。
是全局对象的 "Intl" 属性的初始值。
是一个普通对象。
拥有 [[Prototype]] 内部槽,其值为 %Object.prototype% 。
不是函数对象。
没有 [[Construct]] 内部方法;不能使用 new 运算符作为构造函数。
没有 [[Call]] 内部方法;不能作为函数调用。
拥有 [[FallbackSymbol]] 内部槽,该槽是当前 realm 中一个新的 %Symbol% ,其 [[Description]] 为 "IntlLegacyConstructedSymbol" 。
8.1 Intl 对象的值属性
8.1.1 Intl [ %Symbol.toStringTag% ]
%Symbol.toStringTag% 属性的初始值是字符串值 "Intl" 。
该属性具有 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : true } 属性。
8.2 Intl 对象的构造函数属性
除了 Intl.Locale 之外,以下每个构造函数都是一个服务构造函数 ,用于创建提供区域敏感服务的对象。
8.2.1 Intl.Collator ( . . . )
参见 10 。
8.2.2 Intl.DateTimeFormat ( . . . )
参见 11 。
8.2.3 Intl.DisplayNames ( . . . )
参见 12 。
8.2.4 Intl.DurationFormat ( . . . )
参见 13 。
8.2.5 Intl.ListFormat ( . . . )
参见 14 。
8.2.6 Intl.Locale ( . . . )
参见 15 。
8.2.7 Intl.NumberFormat ( . . . )
参见 16 。
8.2.8 Intl.PluralRules ( . . . )
参见 17 。
8.2.9 Intl.RelativeTimeFormat ( . . . )
参见 18 。
8.2.10 Intl.Segmenter ( . . . )
参见 19 。
8.3 Intl 对象的函数属性
8.3.1 Intl.getCanonicalLocales ( locales )
当 getCanonicalLocales 方法以参数 locales 被调用时,执行以下步骤:
令 ll 为 ? CanonicalizeLocaleList (locales )。 返回 CreateArrayFromList (ll )。
8.3.2 Intl.supportedValuesOf ( key )
当 supportedValuesOf 方法以参数 key 被调用时,执行以下步骤:
令 key 为 ? ToString (key )。 如果 key 是 "calendar" ,则 令 list 为一个新的空列表。 对 AvailableCalendars () 的每个元素 identifier ,执行 令 canonical 为 CanonicalizeUValue("ca" , identifier )。 如果 identifier 等于 canonical ,则 将 identifier 添加到 list 。 否则如果 key 是 "collation" ,则 令 list 为 AvailableCanonicalCollations ()。 否则如果 key 是 "currency" ,则 令 list 为 AvailableCanonicalCurrencies ()。 否则如果 key 是 "numberingSystem" ,则 令 list 为 AvailableCanonicalNumberingSystems ()。 否则如果 key 是 "timeZone" ,则 令 list 为 AvailablePrimaryTimeZoneIdentifiers ()。 否则如果 key 是 "unit" ,则 令 list 为 AvailableCanonicalUnits ()。 否则, 抛出 RangeError 异常。 返回 CreateArrayFromList (list )。
9 语言环境与参数协商
服务构造函数 使用通用模式,将 locales 和 options
参数表示的请求与实现的实际能力进行协商。此处以内部插槽、抽象操作和专用数据类型的方式解释这些通用行为。
可用语言环境列表 是一个任意排序且无重复的语言标签 列表,每个标签都 格式正确 、规范化 ,且不包含 Unicode 语言环境扩展序列 。它表示在特定上下文中实现所提供功能的所有语言环境。
语言优先级列表 是一个 格式正确 且规范化 的语言标签 列表,表示按优先级递减的语言环境偏好顺序。该术语与 BCP 47 在 RFC 4647 section 2.3 中定义的术语一致,但禁止 "*"
元素且仅包含规范化内容。
解析选项描述符 是一个包含字段
[[Key]] (字符串,通常是 [[RelevantExtensionKeys]] 列表的元素)和 [[Property]] (字符串)的记录,也可以包含 [[Type]] (boolean 或 string )和
[[Values]] (empty 或 ECMAScript 语言值列表)。它描述了在对象构造期间如何读取与语言环境解析相关的选项。
9.1 服务构造函数的内部插槽
每个服务构造函数 具有以下内部插槽:
[[AvailableLocales]] 是一个可用语言环境列表 。它必须包含 DefaultLocale
返回的值。此外,对于每个包含多个子标签 的元素,还必须包含具有相同语言子标签 且后续子标签 为严格子集(即省略一个或多个)的较宽泛语言标签 ,以便作为 ResolveLocale 的潜在回退。例如,
如果 [[AvailableLocales]] 包含 "de-DE" ,则还必须包含 "de" (可能用于满足如 "de-AT" 和 "de-CH" 的请求语言环境)。
如果 [[AvailableLocales]] 包含 "az-Latn-AZ" ,则还必须包含 "az-AZ" (可能用于满足如 "az-Cyrl-AZ" 的请求语言环境,如果 "az-Cyrl"
不可用)。
[[RelevantExtensionKeys]] 是 Unicode 技术标准
#35 第1部分核心,第3.6.1节 键和值定义 中定义的 Unicode 语言环境扩展序列 键的列表,这些键与所构造对象的功能相关。
[[ResolutionOptionDescriptors]] 是用于在对象构造期间读取选项的解析选项描述符 列表。
[[SortLocaleData]] 和 [[SearchLocaleData]] (用于 Intl.Collator)以及
[[LocaleData]] (用于其他服务构造函数 )都是记录。除了特定于服务构造函数 的字段外,每个记录还包含 [[AvailableLocales]] 中每个语言环境的字段。每个此类语言环境命名字段的值是一个记录,其中
[[RelevantExtensionKeys]] 的每个元素标识一个字段名,该字段的值是一个非空字符串列表,表示在相关语言环境中实现支持的类型值,第一个元素为该语言环境中该键的默认值。
Note
例如,DateTimeFormat 的实现可能在其
[[AvailableLocales]] 内部插槽中包含
语言标签 "fa-IR" ,并且必须(根据
11.2.3 )在其
[[RelevantExtensionKeys]] 内部插槽中包含键
"ca" 、
"hc" 和
"nu" 。
该语言环境的默认日历通常为
"persian" ,但实现可能还支持
"gregory" 、
"islamic" 和
"islamic-civil" 。
因此,DateTimeFormat 的
[[LocaleData]] 内部插槽中的记录会包含一个 [[fa-IR]] 字段,其值为类似 {
[[ca]] : «
"persian" ,
"gregory" ,
"islamic" ,
"islamic-civil" »,
[[hc]] : « … »,
[[nu]] : « … » } 的记录,以及其他语言环境命名字段,其值结构相同但列表元素不同。
9.2 抽象操作
9.2.1 CanonicalizeLocaleList ( locales )
The abstract operation CanonicalizeLocaleList takes argument locales (一个 ECMAScript 语言值) and returns 返回一个包含语言优先级列表 的正常完成或抛出完成. It performs the following steps when called:
如果 locales 为 undefined ,则 返回一个新的空列表。 令 seen 为一个新的空列表。 如果 locales 是字符串,或 locales 是对象且具有 [[InitializedLocale]] 内部插槽,则 令 O 为 CreateArrayFromList (« locales »)。 否则, 令 O 为 ? ToObject (locales )。 令 len 为 ? LengthOfArrayLike (O )。 令 k 为 0。 重复,条件为 k < len , 令 Pk 为 ! ToString (𝔽 (k ))。 令 kPresent 为 ? HasProperty (O , Pk )。 如果 kPresent 为 true ,则 令 kValue 为 ? Get (O , Pk )。 如果 kValue 不是字符串且不是对象,则抛出 TypeError 异常。 如果 kValue 是对象且具有 [[InitializedLocale]] 内部插槽,则 令 tag 为 kValue .[[Locale]] 。 否则, 令 tag 为 ? ToString (kValue )。 如果 IsWellFormedLanguageTag (tag ) 为 false ,则抛出 RangeError 异常。 令 canonicalizedTag 为 CanonicalizeUnicodeLocaleId (tag )。 如果 seen 不包含 canonicalizedTag ,则将 canonicalizedTag 添加到 seen 。 设置 k 为 k + 1。 返回 seen 。
Note 1
非规范性摘要:该抽象操作将
locales 参数解释为数组,并将其元素复制到列表中,验证元素是否为格式正确的
语言标签 并进行规范化,且去除重复项。
Note 2
要求
kValue 为字符串或对象意味着数字值
NaN 不会被解释为
语言标签 "nan" ,该标签代表闽南语。
9.2.2
CanonicalizeUValue (
ukey : Unicode 技术标准 #35 第1部分核心 第3.6.1节
键和值定义 中定义的 Unicode 语言环境扩展序列键,
uvalue : 一个字符串,
): 一个字符串
The abstract operation UNKNOWN takes UNPARSEABLE ARGUMENTS. 返回的字符串是作为 ukey 的值的 uvalue 的规范且大小写标准化形式。 It performs the following steps when called:
令 lowerValue 为 uvalue 的 ASCII 小写形式。 令 canonicalized 为将 lowerValue 作为 ukey 的值进行规范化后得到的字符串,规范化过程见 Unicode 技术标准 #35 第1部分核心 附录C LocaleId规范化 第5节
规范化语法,处理LocaleIds 。 注:建议实现使用 Common Locale Data Repository 提供的 common/bcp47 中的 'u' 扩展数据(可在 https://cldr.unicode.org/ 获取)。 返回 canonicalized 。
9.2.3 LookupMatchingLocaleByPrefix ( availableLocales , requestedLocales )
The abstract operation LookupMatchingLocaleByPrefix takes arguments availableLocales (可用语言环境列表 ) and requestedLocales (语言优先级列表 ) and returns 一个包含字段 [[locale]] (Unicode 规范化语言环境标识符 )和 [[extension]] (Unicode 语言环境扩展序列 或 empty )的记录,或 undefined . 它根据 BCP 47 在 RFC 4647 section 3.4 中定义的查找算法,确定
availableLocales 中最适合满足 requestedLocales 的元素,忽略 Unicode 语言环境扩展序列 。如果找到非默认匹配,则返回一个记录,其中 [[locale]] 字段包含
availableLocales 中匹配的语言标签 ,[[extension]] 字段包含 requestedLocales 中对应元素的 Unicode 语言环境扩展序列 (如果请求语言标签 没有此序列则为
empty )。 It performs the following steps when called:
对于 requestedLocales 的每个元素 locale ,执行 令 extension 为 empty 。 如果 locale 包含 Unicode 语言环境扩展序列 ,则 设置 extension 为 locale 的 Unicode 语言环境扩展序列 。 设置 locale 为去除任何 Unicode 语言环境扩展序列 后的字符串值。 令 prefix 为 locale 。 重复,条件为 prefix 不是空字符串, 如果 availableLocales 包含 prefix ,则返回记录 { [[locale]] : prefix , [[extension]] : extension }。 如果 prefix 包含 "-" (码元 0x002D HYPHEN-MINUS),令 pos 为 prefix 中最后一次出现 "-" 的索引;否则令 pos 为 0。 重复,条件为 pos ≥ 2 且 prefix 从 pos - 2 到 pos - 1 的子串为 "-" , 设置 pos 为 pos - 2。 设置 prefix 为 prefix 从 0 到 pos 的子串。 返回 undefined 。
Note
当请求语言环境包含
Unicode 技术标准 #35 第1部分核心 BCP 47 T 扩展
子标签 序列时,该算法中的截断可能会临时生成无效
语言标签 。但这些标签不会被返回,因为
availableLocales 只包含有效
语言标签 。
9.2.4 LookupMatchingLocaleByBestFit ( availableLocales , requestedLocales )
The implementation-defined abstract operation LookupMatchingLocaleByBestFit takes arguments availableLocales (可用语言环境列表 ) and requestedLocales (语言优先级列表 ) and returns 一个包含字段 [[locale]] (Unicode 规范化语言环境标识符 )和 [[extension]] (Unicode 语言环境扩展序列 或 empty )的记录,或 undefined . 它根据实现定义的算法,确定 availableLocales 中最适合满足 requestedLocales 的元素,忽略 Unicode 语言环境扩展序列 。该算法由实现决定,但应产生典型用户认为至少与
LookupMatchingLocaleByPrefix 算法同等优良的结果。如果找到非默认匹配,则返回一个记录,其中 [[locale]] 字段包含 availableLocales
中匹配的语言标签 ,[[extension]] 字段包含 requestedLocales 中对应元素的 Unicode 语言环境扩展序列 (如果请求语言标签 没有此序列则为 empty )。
9.2.5 UnicodeExtensionComponents ( extension )
The abstract operation UnicodeExtensionComponents takes argument extension (Unicode 语言环境扩展序列 ) and returns 一个包含字段 [[Attributes]] 和 [[Keywords]] 的记录. 它将 extension 分解为唯一属性列表和唯一键的 关键字 列表。属性或
关键字 键的重复出现仅第一次有效,后续忽略。
It performs the following steps when called:
断言:extension 的 ASCII 小写形式为 extension 。 断言:extension 从 0 到 3 的子串为 "-u-" 。 令 attributes 为一个新的空列表。 令 keywords 为一个新的空列表。 令 keyword 为 undefined 。 令 size 为 extension 的长度。 令 k 为 3。 重复,条件为 k < size , 令 e 为 StringIndexOf (extension , "-" , k )。 如果 e 为 not-found ,令 len 为 size - k ;否则令 len 为 e - k 。 令 subtag 为 extension 从 k 到 k + len 的子串。 注:关键字 是子标签 序列,第一个为长度2的键,后续(如有)长度为3到8,合并为值。属性是长度为3到8且在所有
关键字 之前的单个子标签 。 断言:len ≥ 2。 如果 keyword 为 undefined 且 len ≠ 2,则 如果 subtag 不是 attributes 的元素,则将 subtag 添加到 attributes 。 否则如果 len = 2,则 设置 keyword 为记录 { [[Key]] : subtag , [[Value]] : "" }。 如果 keywords 不包含 [[Key]] 为 keyword .[[Key]] 的元素,则将 keyword 添加到 keywords 。 否则如果 keyword .[[Value]] 为空字符串,则 设置 keyword .[[Value]] 为 subtag 。 否则, 设置 keyword .[[Value]] 为 keyword .[[Value]] 、"-" 和 subtag 的字符串拼接。 设置 k 为 k + len + 1。 返回记录 { [[Attributes]] : attributes , [[Keywords]] : keywords }。
9.2.6 InsertUnicodeExtensionAndCanonicalize ( locale , attributes , keywords )
The abstract operation InsertUnicodeExtensionAndCanonicalize takes arguments locale (一个语言标签 ), attributes (字符串列表), and keywords (记录列表) and returns 一个 Unicode 规范化语言环境标识符 . 它将 attributes 和 keywords 合并到 locale 中作为 Unicode 语言环境扩展序列 ,并返回规范化结果。 It performs the following steps when called:
断言:locale 不包含 Unicode 语言环境扩展序列 。 令 extension 为 "-u" 。 对于 attributes 的每个元素 attr ,执行 设置 extension 为 extension 、"-" 和 attr 的字符串拼接。 对于 keywords 的每个记录 { [[Key]] , [[Value]] } keyword ,执行 设置 extension 为 extension 、"-" 和 keyword .[[Key]] 的字符串拼接。 如果 keyword .[[Value]] 不是空字符串,则设置 extension 为 extension 、"-" 和 keyword .[[Value]] 的字符串拼接。 如果 extension 为 "-u" ,返回 CanonicalizeUnicodeLocaleId (locale )。 令 privateIndex 为 StringIndexOf (locale , "-x-" , 0)。 如果 privateIndex 为 not-found ,则 令 newLocale 为 locale 和 extension 的字符串拼接。 否则, 令 preExtension 为 locale 从 0 到 privateIndex 的子串。 令 postExtension 为 locale 从 privateIndex 开始的子串。 令 newLocale 为 preExtension 、extension 和 postExtension 的字符串拼接。 断言:IsWellFormedLanguageTag (newLocale ) 为 true 。 返回 CanonicalizeUnicodeLocaleId (newLocale )。
9.2.7 ResolveLocale ( availableLocales , requestedLocales , options , relevantExtensionKeys , localeData )
The abstract operation ResolveLocale takes arguments availableLocales (可用语言环境列表 ), requestedLocales (语言优先级列表 ), options (记录), relevantExtensionKeys (字符串列表), and localeData (记录) and returns 记录. 它根据 BCP 47 在 RFC 4647 section 3 中定义的“查找”,使用
options .[[localeMatcher]] 指定的算法(LookupMatchingLocaleByBestFit 或 LookupMatchingLocaleByPrefix ),忽略 Unicode
语言环境扩展序列 ,确定 availableLocales 中最适合满足 requestedLocales 的元素,并返回包含匹配数据、每个相关扩展键的解析值(默认取自匹配语言环境的数据,可被请求的 Unicode
语言环境扩展序列 和 options 覆盖)的记录。如果 requestedLocales 中匹配元素包含 Unicode 语言环境扩展序列 ,则将其复制到返回记录的 [[Locale]]
字段的语言标签 上,省略任何关键字 Unicode 语言环境非终结符 ,其 key 不在 relevantExtensionKeys 中或 type 被 options 覆盖。 It performs the following steps when called:
令 matcher 为 options .[[localeMatcher]] 。 如果 matcher 为 "lookup" ,则 令 r 为 LookupMatchingLocaleByPrefix (availableLocales , requestedLocales )。 否则, 令 r 为 LookupMatchingLocaleByBestFit (availableLocales , requestedLocales )。 如果 r 为 undefined ,则设置 r 为记录 { [[locale]] : DefaultLocale (), [[extension]] : empty }。 令 foundLocale 为 r .[[locale]] 。 令 foundLocaleData 为 localeData .[[<foundLocale >]]。 断言:foundLocaleData 为记录。 令 result 为一个新的记录。 设置 result .[[LocaleData]] 为 foundLocaleData 。 如果 r .[[extension]] 不是 empty ,则 令 components 为 UnicodeExtensionComponents (r .[[extension]] )。 令 keywords 为 components .[[Keywords]] 。 否则, 令 keywords 为一个新的空列表。 令 supportedKeywords 为一个新的空列表。 对于 relevantExtensionKeys 的每个元素 key ,执行 令 keyLocaleData 为 foundLocaleData .[[<key >]]。 断言:keyLocaleData 为列表。 令 value 为 keyLocaleData [0]。 断言:value 为字符串或 null 。 令 supportedKeyword 为 empty 。 如果 keywords 包含 [[Key]] 为 key 的元素,则 令 entry 为 keywords 中 [[Key]] 为 key 的元素。 令 requestedValue 为 entry .[[Value]] 。 如果 requestedValue 不是空字符串,则 如果 keyLocaleData 包含 requestedValue ,则 设置 value 为 requestedValue 。 设置 supportedKeyword 为记录 { [[Key]] : key , [[Value]] : value }。 否则如果 keyLocaleData 包含 "true" ,则 设置 value 为 "true" 。 设置 supportedKeyword 为记录 { [[Key]] : key , [[Value]] : "" }。 断言:options 有字段 [[<key >]]。 令 optionsValue 为 options .[[<key >]]。 断言:optionsValue 为字符串,或为 undefined 或 null 。 如果 optionsValue 为字符串,则 令 ukey 为 key 的 ASCII 小写形式。 设置 optionsValue 为 CanonicalizeUValue(ukey , optionsValue )。 如果 optionsValue 为空字符串,则 设置 optionsValue 为 "true" 。 如果 SameValue (optionsValue , value ) 为 false 且 keyLocaleData 包含 optionsValue ,则 设置 value 为 optionsValue 。 设置 supportedKeyword 为 empty 。 如果 supportedKeyword 不是 empty ,则将 supportedKeyword 添加到 supportedKeywords 。 设置 result .[[<key >]] 为 value 。 如果 supportedKeywords 非空,则 令 supportedAttributes 为一个新的空列表。 设置 foundLocale 为 InsertUnicodeExtensionAndCanonicalize (foundLocale , supportedAttributes ,
supportedKeywords )。 设置 result .[[Locale]] 为 foundLocale 。 返回 result 。
9.2.8 ResolveOptions ( constructor , localeData , locales , options [ , specialBehaviours [ , modifyResolutionOptions ] ] )
The abstract operation ResolveOptions takes arguments constructor (服务构造函数 ), localeData (记录), locales (一个 ECMAScript 语言值), and options (一个 ECMAScript 语言值) and optional arguments specialBehaviours (枚举列表) and modifyResolutionOptions (一个参数的抽象闭包) and returns 返回一个包含字段 [[Options]] (对象)、[[ResolvedLocale]] (记录)和 [[ResolutionOptions]] (记录)的正常完成或抛出完成. 它读取 constructor 的输入并将其解析为语言环境。 It performs the following steps when called:
令 requestedLocales 为 ? CanonicalizeLocaleList (locales )。 如果 specialBehaviours 存在且包含 require-options 且 options 为 undefined ,则抛出 TypeError 异常。 如果 specialBehaviours 存在且包含 coerce-options ,则设置 options 为 ? CoerceOptionsToObject (options )。否则,设置
options 为 ? GetOptionsObject (options )。 令 matcher 为 ? GetOption (options , "localeMatcher" , string , « "lookup" , "best fit" », "best
fit" )。 令 opt 为记录 { [[localeMatcher]] : matcher }。 对于 constructor .[[ResolutionOptionDescriptors]] 的每个解析选项描述符 desc ,执行 如果 desc 有 [[Type]] 字段,令 type 为 desc .[[Type]] ;否则令 type 为 string 。 如果 desc 有 [[Values]] 字段,令 values 为 desc .[[Values]] ;否则令 values 为 empty 。 令 value 为 ? GetOption (options , desc .[[Property]] , type , values , undefined )。 如果 value 不是 undefined ,则 设置 value 为 ! ToString (value )。 如果 value 不能被 type Unicode 语言环境非终结符 匹配,则抛出 RangeError 异常。 令 key 为 desc .[[Key]] 。 设置 opt .[[<key >]] 为 value 。 如果 modifyResolutionOptions 存在,则执行 ! modifyResolutionOptions (opt )。 令 resolution 为 ResolveLocale (constructor .[[AvailableLocales]] , requestedLocales , opt ,
constructor .[[RelevantExtensionKeys]] , localeData )。 返回记录 { [[Options]] : options , [[ResolvedLocale]] : resolution , [[ResolutionOptions]] : opt }。
9.2.9 FilterLocales ( availableLocales , requestedLocales , options )
The abstract operation FilterLocales takes arguments availableLocales (可用语言环境列表 ), requestedLocales (语言优先级列表 ), and options (一个 ECMAScript 语言值) and returns 返回一个包含 Unicode 规范化语言环境标识符 列表的正常完成或抛出完成. 它根据 BCP 47 在 RFC 4647 section 3 中定义的“过滤”,返回
requestedLocales 中在指定算法下(LookupMatchingLocaleByBestFit 或 LookupMatchingLocaleByPrefix )能被 availableLocales
匹配的元素,保留其相对顺序。 It performs the following steps when called:
设置 options 为 ? CoerceOptionsToObject (options )。 令 matcher 为 ? GetOption (options , "localeMatcher" , string , « "lookup" , "best fit" », "best
fit" )。 令 subset 为一个新的空列表。 对于 requestedLocales 的每个元素 locale ,执行 如果 matcher 为 "lookup" ,则 令 match 为 LookupMatchingLocaleByPrefix (availableLocales , « locale »)。 否则, 令 match 为 LookupMatchingLocaleByBestFit (availableLocales , « locale »)。 如果 match 不是 undefined ,则将 locale 添加到 subset 。 返回 CreateArrayFromList (subset )。
9.2.10 CoerceOptionsToObject ( options )
The abstract operation CoerceOptionsToObject takes argument options (一个 ECMAScript 语言值) and returns 返回一个包含对象的正常完成或抛出完成.
它将 options 强制转换为适用于 GetOption 的对象,默认为空对象。
因为它会将非 null 原始值转换为对象,建议新功能使用 GetOptionsObject 替代。
It performs the following steps when called:
如果 options 为 undefined ,则 返回 OrdinaryObjectCreate (null )。 返回 ? ToObject (options )。
9.2.11 GetOption ( options , property , type , values , default )
The abstract operation GetOption takes arguments options (一个对象), property (属性键), type (boolean 或 string ), values (empty 或 ECMAScript 语言值列表), and default (required 或 ECMAScript 语言值) and returns 返回一个包含 ECMAScript 语言值的正常完成或抛出完成. 它提取 options 指定属性的值,转换为所需类型,若 values 非空则检查是否允许,并在值为 undefined 时替换为 default 。 It performs the following steps when called:
令 value 为 ? Get (options , property )。 如果 value 为 undefined ,则 如果 default 为 required ,则抛出 RangeError 异常。 返回 default 。 如果 type 为 boolean ,则 设置 value 为 ToBoolean (value )。 否则, 断言:type 为 string 。 设置 value 为 ? ToString (value )。 如果 values 非空且不包含 value ,则抛出 RangeError 异常。 返回 value 。
9.2.12 GetBooleanOrStringNumberFormatOption ( options , property , stringValues , fallback )
The abstract operation GetBooleanOrStringNumberFormatOption takes arguments options (一个对象), property (属性键), stringValues (字符串列表), and fallback (一个 ECMAScript 语言值) and returns 返回一个包含布尔值、字符串或 fallback 的正常完成或抛出完成. 它提取 options 中名为 property 的属性值。如果该值为 undefined ,返回 fallback ;如果为 true ,返回 true ;如果转换为布尔值为 false ,返回
false ;否则转换为字符串并在 stringValues 中允许时返回该字符串。 It performs the following steps when called:
令 value 为 ? Get (options , property )。 如果 value 为 undefined ,返回 fallback 。 如果 value 为 true ,返回 true 。 如果 ToBoolean (value ) 为 false ,返回 false 。 设置 value 为 ? ToString (value )。 如果 stringValues 不包含 value ,则抛出 RangeError 异常。 返回 value 。
9.2.13 DefaultNumberOption ( value , minimum , maximum , fallback )
The abstract operation DefaultNumberOption takes arguments value (一个 ECMAScript 语言值), minimum (整数), maximum (整数), and fallback (整数或 undefined ) and returns 返回一个包含整数或 undefined 的正常完成或抛出完成. 它将 value 转换为整数,检查是否在允许范围内,并在必要时填充 fallback 值。 It performs the following steps when called:
如果 value 为 undefined ,返回 fallback 。 设置 value 为 ? ToNumber (value )。 如果 value 非有限,或 ℝ (value ) < minimum ,或 ℝ (value ) > maximum ,则抛出 RangeError 异常。 返回 floor (ℝ (value ))。
9.2.14 GetNumberOption ( options , property , minimum , maximum , fallback )
The abstract operation GetNumberOption takes arguments options (一个对象), property (一个字符串), minimum (整数), maximum (整数), and fallback (整数或 undefined ) and returns 返回一个包含整数或 undefined 的正常完成或抛出完成. 它提取 options 中名为 property 的属性值,转换为整数,检查是否在允许范围内,并在必要时填充 fallback 值。 It performs the following steps when called:
令 value 为 ? Get (options , property )。 返回 ? DefaultNumberOption (value , minimum , maximum , fallback )。
9.2.15 PartitionPattern ( pattern )
The abstract operation PartitionPattern takes argument pattern (一个模式字符串 ) and returns 一个包含字段 [[Type]] (字符串)和 [[Value]] (字符串或 undefined )的记录列表.
当 [[Type]] 为 "literal" 时,[[Value]] 字段为字符串值,否则为 undefined 。
It performs the following steps when called:
令 result 为一个新的空列表。 令 placeholderEnd 为 -1。 令 placeholderStart 为 StringIndexOf (pattern , "{" , 0)。 重复,条件为 placeholderStart 非 not-found , 令 literal 为 pattern 从 placeholderEnd + 1 到 placeholderStart 的子串。 如果 literal 不是空字符串,则 将记录 { [[Type]] : "literal" , [[Value]] : literal } 添加到 result 。 设置 placeholderEnd 为 StringIndexOf (pattern , "}" , placeholderStart )。 断言:placeholderEnd 非 not-found 且 placeholderStart < placeholderEnd 。 令 placeholderName 为 pattern 从 placeholderStart + 1 到 placeholderEnd 的子串。 将记录 { [[Type]] : placeholderName , [[Value]] : undefined } 添加到 result 。 设置 placeholderStart 为 StringIndexOf (pattern , "{" , placeholderEnd )。 令 tail 为 pattern 从 placeholderEnd + 1 开始的子串。 如果 tail 不是空字符串,则 将记录 { [[Type]] : "literal" , [[Value]] : tail } 添加到 result 。 返回 result 。
10 Collator 对象
10.1 Intl.Collator 构造函数
Intl.Collator 构造函数:
是 %Intl.Collator% 。
是 Intl 对象 的 "Collator" 属性的初始值。
Intl 对象 所有服务构造属性的通用行为在 9.1 中指定。
10.1.1 Intl.Collator ( [ locales [ , options ] ] )
当 Intl.Collator 函数以可选参数 locales 和 options 被调用时,执行以下步骤:
如果 NewTarget 是 undefined ,令 newTarget 为活动函数对象,否则令 newTarget 为 NewTarget。 令 internalSlotsList 为 « [[InitializedCollator]] , [[Locale]] , [[Usage]] , [[Collation]] , [[Numeric]] ,
[[CaseFirst]] , [[Sensitivity]] , [[IgnorePunctuation]] , [[BoundCompare]] »。 令 collator 为 ? OrdinaryCreateFromConstructor (newTarget , "%Intl.Collator.prototype%" ,
internalSlotsList )。 注:ResolveOptions 的 locale 数据来源取决于 options 的 "usage" 属性,但以下两步必须在该查找之前可观察地执行(且不得在 ResolveOptions 内部重复执行)。 令 requestedLocales 为 ? CanonicalizeLocaleList (locales )。 将 options 设置为 ? CoerceOptionsToObject (options )。 令 usage 为 ? GetOption (options , "usage" , string , « "sort" , "search" », "sort" )。 设置 collator .[[Usage]] 为 usage 。 如果 usage 是 "sort" ,则 令 localeData 为 %Intl.Collator% .[[SortLocaleData]] 。 否则, 令 localeData 为 %Intl.Collator% .[[SearchLocaleData]] 。 令 optionsResolution 为 ? ResolveOptions (%Intl.Collator% , localeData ,
CreateArrayFromList (requestedLocales ), options )。 令 r 为 optionsResolution .[[ResolvedLocale]] 。 设置 collator .[[Locale]] 为 r .[[Locale]] 。 如果 r .[[co]] 为 null ,令 collation 为 "default" 。否则,令 collation 为 r .[[co]] 。 设置 collator .[[Collation]] 为 collation 。 设置 collator .[[Numeric]] 为 SameValue (r .[[kn]] , "true" )。 设置 collator .[[CaseFirst]] 为 r .[[kf]] 。 令 resolvedLocaleData 为 r .[[LocaleData]] 。 如果 usage 是 "sort" ,令 defaultSensitivity 为 "variant" 。否则,令 defaultSensitivity 为
resolvedLocaleData .[[sensitivity]] 。 设置 collator .[[Sensitivity]] 为 ? GetOption (options , "sensitivity" , string , « "base" , "accent" ,
"case" , "variant" », defaultSensitivity )。 令 defaultIgnorePunctuation 为 resolvedLocaleData .[[ignorePunctuation]] 。 设置 collator .[[IgnorePunctuation]] 为 ? GetOption (options , "ignorePunctuation" , boolean , empty ,
defaultIgnorePunctuation )。 返回 collator 。
Note
10.2 Intl.Collator 构造函数的属性
Intl.Collator 构造函数:
10.2.1 Intl.Collator.prototype
Intl.Collator.prototype 的值为 %Intl.Collator.prototype% 。
该属性具有 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : false } 属性。
10.2.2 Intl.Collator.supportedLocalesOf ( locales [ , options ] )
当 supportedLocalesOf 方法以参数 locales 和 options 被调用时,执行以下步骤:
令 availableLocales 为 %Intl.Collator% .[[AvailableLocales]] 。 令 requestedLocales 为 ? CanonicalizeLocaleList (locales )。 返回 ? FilterLocales (availableLocales , requestedLocales , options )。
10.2.3 内部槽
[[AvailableLocales]] 内部槽的值在 9.1
描述的约束范围内由实现定义。[[RelevantExtensionKeys]] 内部槽的值是一个列表,必须包含元素 "co" ,可以包含 "kf" 和 "kn" 元素中的任意一个或全部,且不得包含其他元素。
Note
Unicode 技术标准 #35 第1部分核心,3.6.1节 键和值定义
描述了与排序相关的十个区域扩展键:
"co" 用于排序器用途和特化,
"ka" 用于替代处理,
"kb" 用于反向二级权重,
"kc" 用于大小写级别,
"kf" 用于首字母大小写,
"kh"
用于平假名四级,
"kk" 用于归一化,
"kn" 用于数字,
"kr" 用于重排序,
"ks" 用于排序强度,以及
"vt" 用于变量顶。Collator 要求通过 options 对象的
"usage"
属性指定用途,通过
"ignorePunctuation" 属性指定替代处理,通过
"sensitivity" 属性指定大小写级别和强度。
语言标签 中的
"co"
键仅支持排序器特化,
"kb" 、
"kh" 、
"kk" 、
"kr" 和
"vt" 键在本版本的国际化 API 中不允许。其余键的支持由实现决定。
[[ResolutionOptionDescriptors]] 内部槽的值为 « { [[Key]] : "co" , [[Property]] : "collation" }, { [[Key]] : "kn" ,
[[Property]] : "numeric" , [[Type]] : boolean }, { [[Key]] : "kf" , [[Property]] : "caseFirst" , [[Values]] : «
"upper" , "lower" , "false" » } »。
[[SortLocaleData]] 和 [[SearchLocaleData]] 内部槽的值在 9.1
描述的约束范围内由实现定义,并且有以下额外约束,对所有区域值 locale :
[[SortLocaleData]] .[[<locale >]].[[co]] 和 [[SearchLocaleData]] .[[<locale >]].[[co]] 的第一个元素必须为
null 。
"standard" 和 "search" 不得作为任何 [[SortLocaleData]] .[[<locale >]].[[co]] 和
[[SearchLocaleData]] .[[<locale >]].[[co]] 列表的元素。
[[SearchLocaleData]] .[[<locale >]] 必须有一个 [[sensitivity]] 字段,其值为 "base" 、"accent" 、"case" 或
"variant" 。
[[SearchLocaleData]] .[[<locale >]] 和 [[SortLocaleData]] .[[<locale >]] 必须有一个 [[ignorePunctuation]]
字段,其值为布尔值。
10.3 Intl.Collator 原型对象的属性
Intl.Collator 原型对象 :
是 %Intl.Collator.prototype% 。
是一个普通对象。
不是 Intl.Collator 实例,不具有 [[InitializedCollator]] 内部槽或任何 Intl.Collator 实例对象的其他内部槽。
具有 [[Prototype]] 内部槽,其值为 %Object.prototype% 。
10.3.1 Intl.Collator.prototype.constructor
Intl.Collator.prototype.constructor 的初始值为 %Intl.Collator% 。
10.3.2 Intl.Collator.prototype.resolvedOptions ( )
此函数提供对对象初始化期间计算的区域和选项的访问。
令 collator 为 this 值。 执行 ? RequireInternalSlot (collator , [[InitializedCollator]] )。 令 options 为 OrdinaryObjectCreate (%Object.prototype% )。 对 Table 3 的每一行(除表头行),按表顺序,执行 令 p 为当前行的 Property 值。 令 v 为 collator 的内部槽,其名称为当前行的 Internal Slot 值。 如果当前行有 Extension Key 值,则 令 extensionKey 为当前行的 Extension Key 值。 如果 %Intl.Collator% .[[RelevantExtensionKeys]] 不包含 extensionKey ,则 设置 v 为 undefined 。 如果 v 不为 undefined ,则 执行 ! CreateDataPropertyOrThrow (options , p , v )。 返回 options 。
Table 3: Collator 实例的已解析选项
内部槽
属性
扩展键
[[Locale]]
"locale"
[[Usage]]
"usage"
[[Sensitivity]]
"sensitivity"
[[IgnorePunctuation]]
"ignorePunctuation"
[[Collation]]
"collation"
[[Numeric]]
"numeric"
"kn"
[[CaseFirst]]
"caseFirst"
"kf"
10.3.3 get Intl.Collator.prototype.compare
此命名访问器属性返回一个函数,该函数根据此 Collator 对象的排序顺序比较两个字符串。
Intl.Collator.prototype.compare 是一个访问器属性,其 set 访问器函数为 undefined 。其 get 访问器函数执行以下步骤:
令 collator 为 this 值。 执行 ? RequireInternalSlot (collator , [[InitializedCollator]] )。 如果 collator .[[BoundCompare]] 为 undefined ,则 令 F 为一个新的内置函数对象,如 10.3.3.1 所定义。 设置 F .[[Collator]] 为 collator 。 设置 collator .[[BoundCompare]] 为 F 。 返回 collator .[[BoundCompare]] 。
Note
返回的函数绑定到 collator ,因此可以直接传递给 Array.prototype.sort 或其他函数。
10.3.3.1 Collator 比较函数
Collator 比较函数是一个匿名内置函数,具有 [[Collator]] 内部槽。
当 Collator 比较函数 F 以参数 x 和 y 被调用时,执行以下步骤:
令 collator 为 F .[[Collator]] 。 断言:collator 是一个对象且具有 [[InitializedCollator]] 内部槽。 如果未提供 x ,令 x 为 undefined 。 如果未提供 y ,令 y 为 undefined 。 令 X 为 ? ToString (x )。 令 Y 为 ? ToString (y )。 返回 CompareStrings (collator , X , Y )。
Collator 比较函数的 "length" 属性为 2 𝔽 。
10.3.3.2 CompareStrings ( collator , x , y )
The implementation-defined abstract operation CompareStrings takes arguments collator (一个 Intl.Collator), x (一个字符串), and y (一个字符串) and returns 一个数字,但不是 NaN .
如下所述的行为依赖于区域敏感地识别字符串的排序元素序列,特别是“基本字母”,不同的基本字母总是比较为不相等(导致包含它们的字符串也比较为不相等)。同一基本字母的不同变体(大小写、变音符号等)比较结果进一步取决于
collator .[[Sensitivity]] ,如下:
Table 4: Collator 敏感度的影响
[[Sensitivity]]
描述
"a" 与 "á" 比较
"a" 与 "A" 比较
"base"
具有相同基本字母的字符不比较为不相等,无论大小写或变音符号差异。
相等
相等
"accent"
具有相同基本字母的字符仅在变音符号等差异时比较为不相等,无论大小写。
不相等
相等
"case"
具有相同基本字母的字符仅在大小写差异时比较为不相等,无论变音符号。
相等
不相等
"variant"
具有相同基本字母的字符在大小写、变音符号及其他差异时比较为不相等。
不相等
不相等
Note 1
输入代码点到基本字母的映射可以包含任意收缩、扩展和冲突,包括对某些带变音符号字符的特殊处理。例如,在瑞典语中,“ö”是与“o”不同的基本字母,“v”和“w”被视为同一基本字母。在斯洛伐克语中,“ch”是一个基本字母,在英语中,“æ”是以“a”开头、“e”结尾的基本字母序列。
如果 collator .[[IgnorePunctuation]] 为 true ,则忽略标点(例如,仅标点不同的字符串比较为相等)。
关于通过区域扩展键设置的选项解释,请参见 Unicode 技术标准 #35
第1部分核心,3.6.1节 键和值定义 。
实际返回值由实现定义,以允许编码附加信息,但对于任何给定 collator ,作为 x 和 y 的函数,该操作必须是一个一致的比较器,在所有字符串集合上定义一个全序。该操作还必须识别并遵循 Unicode
标准的规范等价,包括在比较可区分但规范等价的字符串时返回 +0 𝔽 。
Note 2
推荐 CompareStrings 抽象操作按照
Unicode 技术标准 #10:Unicode Collation
Algorithm 实现,并针对
collator 的有效区域和排序选项进行定制。推荐实现使用由 Common Locale Data Repository 提供的定制(
https://cldr.unicode.org/ )。
Note 3
应用程序不应假定具有相同已解析选项的 Collator 实例的 CompareStrings 抽象操作行为在同一实现的不同版本间保持不变。
10.3.4 Intl.Collator.prototype [ %Symbol.toStringTag% ]
%Symbol.toStringTag% 属性的初始值为字符串 "Intl.Collator" 。
该属性具有 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : true } 属性。
10.4 Intl.Collator 实例的属性
Intl.Collator 实例是普通对象,继承自 %Intl.Collator.prototype% 的属性。
Intl.Collator 实例具有 [[InitializedCollator]] 内部槽。
Intl.Collator 实例还具有多个内部槽,这些槽由 Intl.Collator 构造函数 计算:
[[Locale]] 是一个字符串值,表示用于排序的区域的语言标签 。
[[Usage]] 是字符串值 "sort" 或 "search" ,标识排序器用途。
[[Sensitivity]] 是字符串值 "base" 、"accent" 、"case" 或 "variant" ,标识排序器的敏感度。
[[IgnorePunctuation]] 是布尔值,指定比较时是否忽略标点。
[[Collation]] 是一个字符串值,表示用于排序的 Unicode
Collation Identifier ,"standard" 和 "search" 不允许,"default" 允许。
如果 Table 3 中对应内部槽名称的键包含在 Intl.Collator 的
[[RelevantExtensionKeys]] 内部槽中,则 Intl.Collator 实例还具有以下内部槽:
[[Numeric]] 是布尔值,指定是否使用数字排序。
[[CaseFirst]] 是字符串值 "upper" 、"lower" 或 "false" 。
最后,Intl.Collator 实例具有 [[BoundCompare]] 内部槽,用于缓存 compare 访问器返回的函数(10.3.3 )。
11 DateTimeFormat 对象
11.1 Intl.DateTimeFormat 构造函数
Intl.DateTimeFormat 构造函数:
是 %Intl.DateTimeFormat% 。
是 Intl 对象 的 "DateTimeFormat" 属性的初始值。
所有 Intl 对象 服务构造属性的通用行为在 9.1 中指定。
11.1.1 Intl.DateTimeFormat ( [ locales [ , options ] ] )
当调用 Intl.DateTimeFormat 函数并传入可选参数 locales 和 options 时,执行以下步骤:
如果 NewTarget 是 undefined ,则令 newTarget 为当前活动的函数对象,否则令 newTarget 为 NewTarget。 令 dateTimeFormat 为 ? CreateDateTimeFormat (newTarget , locales , options , any , date )。 如果实现支持 4.3 Note 1 的规范可选构造器模式,则 令 this 为 this 值。 返回 ? ChainDateTimeFormat (dateTimeFormat , NewTarget, this )。 返回 dateTimeFormat 。
11.1.1.1 ChainDateTimeFormat ( dateTimeFormat , newTarget , this )
The abstract operation ChainDateTimeFormat takes arguments dateTimeFormat (一个 Intl.DateTimeFormat), newTarget (一个 ECMAScript 语言值), and this (一个 ECMAScript 语言值) and returns 正常完成包含一个对象或抛出完成. It performs the following steps when called:
如果 newTarget 是 undefined 且 ? OrdinaryHasInstance (%Intl.DateTimeFormat% , this ) 为 true ,则 执行 ? DefinePropertyOrThrow (this , %Intl% .[[FallbackSymbol]] , PropertyDescriptor{ [[Value]] :
dateTimeFormat , [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : false })。 返回 this 。 返回 dateTimeFormat 。
11.1.2 CreateDateTimeFormat ( newTarget , locales , options , required , defaults )
The abstract operation CreateDateTimeFormat takes arguments newTarget (一个构造函数), locales (一个 ECMAScript 语言值), options (一个 ECMAScript 语言值), required (date , time , 或 any ), and defaults (date , time , 或 all ) and returns 正常完成包含一个 DateTimeFormat 对象或抛出完成. It performs the following steps when called:
令 dateTimeFormat 为 ? OrdinaryCreateFromConstructor (newTarget , "%Intl.DateTimeFormat.prototype%" , «
[[InitializedDateTimeFormat]] , [[Locale]] , [[Calendar]] , [[NumberingSystem]] , [[TimeZone]] , [[HourCycle]] ,
[[DateStyle]] , [[TimeStyle]] , [[DateTimeFormat]] , [[BoundFormat]] »)。 令 hour12 为 undefined 。 令 modifyResolutionOptions 为一个新的抽象闭包,参数为 (options ),捕获 hour12 并在调用时执行以下步骤: 设置 hour12 为 options .[[hour12]] 。 从 options 中移除字段 [[hour12]] 。 如果 hour12 不为 undefined ,则设置 options .[[hc]] 为 null 。 令 optionsResolution 为 ? ResolveOptions (%Intl.DateTimeFormat% , %Intl.DateTimeFormat% .[[LocaleData]] ,
locales , options , « coerce-options », modifyResolutionOptions )。 设置 options 为 optionsResolution .[[Options]] 。 令 r 为 optionsResolution .[[ResolvedLocale]] 。 设置 dateTimeFormat .[[Locale]] 为 r .[[Locale]] 。 令 resolvedCalendar 为 r .[[ca]] 。 设置 dateTimeFormat .[[Calendar]] 为 resolvedCalendar 。 设置 dateTimeFormat .[[NumberingSystem]] 为 r .[[nu]] 。 令 resolvedLocaleData 为 r .[[LocaleData]] 。 如果 hour12 为 true ,则 令 hc 为 resolvedLocaleData .[[hourCycle12]] 。 否则如果 hour12 为 false ,则 令 hc 为 resolvedLocaleData .[[hourCycle24]] 。 否则, 断言:hour12 为 undefined 。 令 hc 为 r .[[hc]] 。 如果 hc 为 null ,则设置 hc 为 resolvedLocaleData .[[hourCycle]] 。 令 timeZone 为 ? Get (options , "timeZone" )。 如果 timeZone 为 undefined ,则 设置 timeZone 为 SystemTimeZoneIdentifier ()。 否则, 设置 timeZone 为 ? ToString (timeZone )。 如果 IsTimeZoneOffsetString (timeZone ) 为 true ,则 令 parseResult 为 ParseText (StringToCodePoints (timeZone ), UTCOffset )。 断言:parseResult 是一个 Parse Node 。 如果 parseResult 包含多个 MinuteSecond Parse Node ,则抛出 RangeError 异常。 令 offsetNanoseconds 为 ParseTimeZoneOffsetString (timeZone )。 令 offsetMinutes 为 offsetNanoseconds / (6 × 1010 )。 断言:offsetMinutes 是整数。 设置 timeZone 为 FormatOffsetTimeZoneIdentifier (offsetMinutes )。 否则, 令 timeZoneIdentifierRecord 为 GetAvailableNamedTimeZoneIdentifier (timeZone )。 如果 timeZoneIdentifierRecord 为 empty ,则抛出 RangeError 异常。 设置 timeZone 为 timeZoneIdentifierRecord .[[PrimaryIdentifier]] 。 设置 dateTimeFormat .[[TimeZone]] 为 timeZone 。 令 formatOptions 为一个新记录。 设置 formatOptions .[[hourCycle]] 为 hc 。 令 hasExplicitFormatComponents 为 false 。 对 Table 16 的每一行(除表头外,按表顺序),执行: 令 prop 为当前行 Property 列给出的名称。 如果 prop 为 "fractionalSecondDigits" ,则 令 value 为 ? GetNumberOption (options , "fractionalSecondDigits" , 1, 3, undefined )。 否则, 令 values 为当前行 Values 列给出的字符串列表。 令 value 为 ? GetOption (options , prop , string , values , undefined )。 设置 formatOptions .[[<prop >]] 为 value 。 如果 value 不为 undefined ,则 设置 hasExplicitFormatComponents 为 true 。 令 formatMatcher 为 ? GetOption (options , "formatMatcher" , string , « "basic" , "best fit" », "best
fit" )。 令 dateStyle 为 ? GetOption (options , "dateStyle" , string , « "full" , "long" , "medium" , "short"
», undefined )。 设置 dateTimeFormat .[[DateStyle]] 为 dateStyle 。 令 timeStyle 为 ? GetOption (options , "timeStyle" , string , « "full" , "long" , "medium" , "short"
», undefined )。 设置 dateTimeFormat .[[TimeStyle]] 为 timeStyle 。 如果 dateStyle 不为 undefined 或 timeStyle 不为 undefined ,则 如果 hasExplicitFormatComponents 为 true ,则 抛出 TypeError 异常。 如果 required 为 date 且 timeStyle 不为 undefined ,则 抛出 TypeError 异常。 如果 required 为 time 且 dateStyle 不为 undefined ,则 抛出 TypeError 异常。 令 styles 为 resolvedLocaleData .[[styles]] .[[<resolvedCalendar >]]。 令 bestFormat 为 DateTimeStyleFormat (dateStyle , timeStyle , styles )。 否则, 令 needDefaults 为 true 。 如果 required 为 date 或 any ,则 对 « "weekday" , "year" , "month" , "day" » 的每个属性名 prop ,执行: 令 value 为 formatOptions .[[<prop >]]。 如果 value 不为 undefined ,设置 needDefaults 为 false 。 如果 required 为 time 或 any ,则 对 « "dayPeriod" , "hour" , "minute" , "second" , "fractionalSecondDigits" » 的每个属性名 prop ,执行: 令 value 为 formatOptions .[[<prop >]]。 如果 value 不为 undefined ,设置 needDefaults 为 false 。 如果 needDefaults 为 true 且 defaults 为 date 或 all ,则 对 « "year" , "month" , "day" » 的每个属性名 prop ,执行: 设置 formatOptions .[[<prop >]] 为 "numeric" 。 如果 needDefaults 为 true 且 defaults 为 time 或 all ,则 对 « "hour" , "minute" , "second" » 的每个属性名 prop ,执行: 设置 formatOptions .[[<prop >]] 为 "numeric" 。 令 formats 为 resolvedLocaleData .[[formats]] .[[<resolvedCalendar >]]。 如果 formatMatcher 为 "basic" ,则 令 bestFormat 为 BasicFormatMatcher (formatOptions , formats )。 否则, 令 bestFormat 为 BestFitFormatMatcher (formatOptions , formats )。 设置 dateTimeFormat .[[DateTimeFormat]] 为 bestFormat 。 如果 bestFormat 有字段 [[hour]] ,则 设置 dateTimeFormat .[[HourCycle]] 为 hc 。 返回 dateTimeFormat 。
11.1.3 FormatOffsetTimeZoneIdentifier ( offsetMinutes )
The abstract operation FormatOffsetTimeZoneIdentifier takes argument offsetMinutes (一个整数) and returns 一个字符串.
将 UTC 偏移(以分钟为单位)格式化为 ±HH:MM 格式的 UTC 偏移字符串。
It performs the following steps when called:
如果 offsetMinutes ≥ 0,令 sign 为码元 0x002B(加号);否则令 sign 为码元 0x002D(减号)。 令 absoluteMinutes 为 abs (offsetMinutes )。 令 hours 为 floor (absoluteMinutes / 60)。 令 minutes 为 absoluteMinutes 模 60。 返回 sign 、ToZeroPaddedDecimalString (hours , 2)、码元 0x003A(冒号)、ToZeroPaddedDecimalString (minutes , 2) 的字符串拼接。
11.2 Intl.DateTimeFormat 构造函数的属性
Intl.DateTimeFormat 构造函数:
11.2.1 Intl.DateTimeFormat.prototype
Intl.DateTimeFormat.prototype 的值为 %Intl.DateTimeFormat.prototype% 。
该属性具有 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : false } 属性。
11.2.2 Intl.DateTimeFormat.supportedLocalesOf ( locales [ , options ] )
当调用 supportedLocalesOf 方法并传入参数 locales 和 options 时,执行以下步骤:
令 availableLocales 为 %Intl.DateTimeFormat% .[[AvailableLocales]] 。 令 requestedLocales 为 ? CanonicalizeLocaleList (locales )。 返回 ? FilterLocales (availableLocales , requestedLocales , options )。
11.2.3 内部槽
[[AvailableLocales]] 内部槽的值由实现定义,但需遵循 9.1 中的约束。
[[RelevantExtensionKeys]] 内部槽的值为 « "ca" , "hc" , "nu" »。
Note 1
Unicode 技术标准 #35 第1部分核心,第3.6.1节 键和值类型定义
描述了四个与日期和时间格式相关的区域扩展键:
"ca" 表示日历,
"hc" 表示小时周期,
"nu" 表示数字系统(用于格式化数字),
"tz" 表示时区。但 DateTimeFormat 要求时区通过构造函数
options 对象中的
"timeZone" 属性指定。
[[ResolutionOptionDescriptors]] 内部槽的值为 « { [[Key]] : "ca" , [[Property]] : "calendar" }, { [[Key]] : "nu" ,
[[Property]] : "numberingSystem" }, { [[Key]] : "hour12" , [[Property]] : "hour12" , [[Type]] : boolean }, {
[[Key]] : "hc" , [[Property]] : "hourCycle" , [[Values]] : « "h11" , "h12" , "h23" , "h24" » } »。
[[LocaleData]] 内部槽的值由实现定义,但需遵循 9.1 及以下附加约束,对于所有区域值 locale :
[[LocaleData]] .[[<locale >]].[[nu]] 必须是一个列表,且不包含 "native" 、"traditio" 或 "finance" 。
[[LocaleData]] .[[<locale >]].[[hc]] 必须为 « null , "h11" , "h12" , "h23" , "h24" »。
[[LocaleData]] .[[<locale >]].[[hourCycle]] 必须为 "h11" 、"h12" 、"h23" 或 "h24" 。
[[LocaleData]] .[[<locale >]].[[hourCycle12]] 必须为 "h11" 或 "h12" 。
[[LocaleData]] .[[<locale >]].[[hourCycle24]] 必须为 "h23" 或 "h24" 。
[[LocaleData]] .[[<locale >]] 必须有 [[formats]] 字段。该字段的值必须是一个记录,包含每个日历值 calendar 的 [[<calendar >]]
字段。每个 [[<calendar >]] 字段的值必须是 DateTime Format Record 的列表。列表中的多个记录可以使用相同字段子集,只要对应值至少有一个不同。每个区域必须至少包含以下字段组合:
weekday, year, month, day, hour, minute, second, fractionalSecondDigits
weekday, year, month, day, hour, minute, second
weekday, year, month, day
year, month, day
year, month
month, day
month
hour, minute, second, fractionalSecondDigits
hour, minute, second
hour, minute
dayPeriod, hour, minute, second, fractionalSecondDigits
dayPeriod, hour, minute, second
dayPeriod, hour, minute
dayPeriod, hour
[[LocaleData]] .[[<locale >]] 必须有 [[styles]] 字段。该字段的值必须是一个记录,包含每个日历值 calendar 的 [[<calendar >]]
字段,每个字段的值为 DateTime Styles Record 。
11.2.3.1 DateTime Format Record
每个 DateTime Format Record 都具有
Table 5 中定义的字段。
Table 5: DateTime Format Record
字段名
值类型
描述
[[weekday]]
[[Weekday]] ,参见 Table 16 的 Values 列
可选字段。如果 [[pattern]] 包含 "{weekday}" 子串则存在。
[[era]]
[[Era]] ,参见 Table 16 的 Values 列
可选字段。如果 [[pattern]] 包含 "{era}" 子串则存在。
[[year]]
[[Year]] ,参见 Table 16 的 Values 列
可选字段。如果 [[pattern]] 包含 "{year}" 、"{yearName}" 或 "{relatedYear}" 子串则存在。
[[month]]
[[Month]] ,参见 Table 16 的 Values 列
可选字段。如果 [[pattern]] 包含 "{month}" 子串则存在。
[[day]]
[[Day]] ,参见 Table 16 的 Values 列
可选字段。如果 [[pattern]] 包含 "{day}" 子串则存在。
[[dayPeriod]]
[[DayPeriod]] ,参见 Table 16 的 Values 列
可选字段。如果 [[pattern]] 包含 "{dayPeriod}" 子串则存在。
[[hour]]
[[Hour]] ,参见 Table 16 的 Values 列
可选字段。如果 [[pattern]] 包含 "{hour}" 子串则存在。
[[minute]]
[[Minute]] ,参见 Table 16 的 Values 列
可选字段。如果 [[pattern]] 包含 "{minute}" 子串则存在。
[[second]]
[[Second]] ,参见 Table 16 的 Values 列
可选字段。如果 [[pattern]] 包含 "{second}" 子串则存在。
[[fractionalSecondDigits]]
[[FractionalSecondDigits]] ,参见 Table 16 的 Values 列
可选字段。如果 [[pattern]] 包含 "{fractionalSecondDigits}" 子串则存在。
[[timeZoneName]]
[[TimeZoneName]] ,参见 Table 16 的 Values 列
可选字段。如果 [[pattern]] 包含 "{timeZoneName}" 子串则存在。
[[pattern]]
Pattern 字符串
包含 每个日期和时间格式组件字段的子串,格式为 "{" + 字段名 + "}" 。如果记录有 [[year]] 字段,字符串可能包含
"{yearName}" 和 "{relatedYear}" 。
[[pattern12]]
Pattern 字符串
可选字段。如果 [[hour]] 字段存在则存在。除 [[pattern]] 字段的子串外,还包含至少一个 "{ampm}" 或 "{dayPeriod}" 子串。
[[rangePatterns]]
DateTime Range Pattern Record
该字段中的模式字符串 与 [[pattern]] 类似。
[[rangePatterns12]]
DateTime Range Pattern Record
可选字段。如果 [[hour]] 字段存在则存在。该字段中的模式字符串 与 [[pattern12]] 类似。
11.2.3.2 DateTime Range Pattern Record
每个 DateTime Range Pattern Record 都具有 Table 6 中定义的字段。
Table 6: DateTime Range Pattern Record
11.2.3.3 DateTime Range Pattern Format Record
每个 DateTime Range Pattern Format Record 都具有
Table 7 中定义的字段。
Table 7: DateTime Range Pattern Format Record
字段名
值类型
描述
[[weekday]]
[[Weekday]] ,参见 Table 16 的 Values 列
可选字段。如果 [[PatternParts]] 中的模式字符串 包含 "{weekday}" 子串则存在。
[[era]]
[[Era]] ,参见 Table 16 的 Values 列
可选字段。如果 [[PatternParts]] 中的模式字符串 包含 "{era}" 子串则存在。
[[year]]
[[Year]] ,参见 Table 16 的 Values 列
可选字段。如果 [[PatternParts]] 中的模式字符串 包含 "{year}" 、"{yearName}" 或 "{relatedYear}" 子串则存在。
[[month]]
[[Month]] ,参见 Table 16 的 Values 列
可选字段。如果 [[PatternParts]] 中的模式字符串 包含 "{month}" 子串则存在。
[[day]]
[[Day]] ,参见 Table 16 的 Values 列
可选字段。如果 [[PatternParts]] 中的模式字符串 包含 "{day}" 子串则存在。
[[dayPeriod]]
[[DayPeriod]] ,参见 Table 16 的 Values 列
可选字段。如果 [[PatternParts]] 中的模式字符串 包含 "{dayPeriod}" 子串则存在。
[[hour]]
[[Hour]] ,参见 Table 16 的 Values 列
可选字段。如果 [[PatternParts]] 中的模式字符串 包含 "{hour}" 子串则存在。
[[minute]]
[[Minute]] ,参见 Table 16 的 Values 列
可选字段。如果 [[PatternParts]] 中的模式字符串 包含 "{minute}" 子串则存在。
[[second]]
[[Second]] ,参见 Table 16 的 Values 列
可选字段。如果 [[PatternParts]] 中的模式字符串 包含 "{second}" 子串则存在。
[[fractionalSecondDigits]]
[[FractionalSecondDigits]] ,参见 Table 16 的 Values 列
可选字段。如果 [[PatternParts]] 中的模式字符串 包含 "{fractionalSecondDigits}" 子串则存在。
[[timeZoneName]]
[[TimeZoneName]] ,参见 Table 16 的 Values 列
可选字段。如果 [[PatternParts]] 中的模式字符串 包含 "{timeZoneName}" 子串则存在。
[[PatternParts]]
DateTime Range Pattern Part Record 的列表
每个记录表示范围模式的一部分。
11.2.3.4 DateTime Range Pattern Part Record
每个 DateTime
Range Pattern Part Record 都具有 Table 8 中定义的字段。
Table 8: DateTime Range Pattern Part Record
字段名
值类型
描述
[[Source]]
"shared" 、"startRange" 或 "endRange"
指示范围的哪个日期应使用 [[Pattern]] 字段的值进行格式化。
[[Pattern]]
Pattern 字符串
与常规日期模式字符串 格式相同的字符串。
11.2.3.5 DateTime Styles Record
每个 DateTime Styles Record 都具有 Table 9 中定义的字段。
Table 9: DateTime Styles Record
11.2.3.6 DateTime Style Record
每个 DateTime Style Record 都具有 Table 10 中定义的字段。
Table 10: DateTime Style Record
11.2.3.7 DateTime Connector Record
每个 DateTime Connector Record 都具有 Table 11 中定义的字段。所有连接器模式字符串 必须包含 "{0}" 和 "{1}" 。
Table 11: DateTime Connector Record
字段名
值类型
描述
[[full]]
Pattern 字符串
日期样式为 "full" 时的连接器模式。
[[long]]
Pattern 字符串
日期样式为 "long" 时的连接器模式。
[[medium]]
Pattern 字符串
日期样式为 "medium" 时的连接器模式。
[[short]]
Pattern 字符串
日期样式为 "short" 时的连接器模式。
11.2.3.8 DateTime Date Range Record
每个 DateTime Date Range Record 都具有 Table 12 中定义的字段。
Table 12: DateTime Date Range Record
11.2.3.9 DateTime Time Range Record
每个 DateTime Time Range Record 都具有 Table 13 中定义的字段。
Table 13: DateTime Time Range Record
11.2.3.10 DateTime Style Range Record
每个 DateTime Style Range Record 都具有 Table 14 中定义的字段。
Table 14: DateTime Style Range Record
Note 2
例如,实现可能在其英文区域数据中包含如下记录:
[[hour]] : "numeric"
[[minute]] : "numeric"
[[pattern]] : "{hour}:{minute}"
[[pattern12]] : "{hour}:{minute} {ampm}"
[[rangePatterns]] :
[[Hour]] :
[[hour]] : "numeric"
[[minute]] : "numeric"
[[PatternParts]] :
{[[Source]] : "startRange" , [[Pattern]] : "{hour}:{minute}" }
{[[Source]] : "shared" , [[Pattern]] : " – " }
{[[Source]] : "endRange" , [[Pattern]] : "{hour}:{minute}" }
[[Minute]] :
[[hour]] : "numeric"
[[minute]] : "numeric"
[[PatternParts]] :
{[[Source]] : "startRange" , [[Pattern]] : "{hour}:{minute}" }
{[[Source]] : "shared" , [[Pattern]] : " – " }
{[[Source]] : "endRange" , [[Pattern]] : "{hour}:{minute}" }
[[Default]] :
[[year]] : "2-digit"
[[month]] : "numeric"
[[day]] : "numeric"
[[hour]] : "numeric"
[[minute]] : "numeric"
[[PatternParts]] :
{[[Source]] : "startRange" , [[Pattern]] : "{day}/{month}/{year}, {hour}:{minute}" }
{[[Source]] : "shared" , [[Pattern]] : " – " }
{[[Source]] : "endRange" , [[Pattern]] : "{day}/{month}/{year}, {hour}:{minute}" }
[[rangePatterns12]] :
[[Hour]] :
[[hour]] : "numeric"
[[minute]] : "numeric"
[[PatternParts]] :
{[[Source]] : "startRange" , [[Pattern]] : "{hour}:{minute}" }
{[[Source]] : "shared" , [[Pattern]] : " – " }
{[[Source]] : "endRange" , [[Pattern]] : "{hour}:{minute}" }
{[[Source]] : "shared" , [[Pattern]] : " {ampm}" }
[[Minute]] :
[[hour]] : "numeric"
[[minute]] : "numeric"
[[PatternParts]] :
{[[Source]] : "startRange" , [[Pattern]] : "{hour}:{minute}" }
{[[Source]] : "shared" , [[Pattern]] : " – " }
{[[Source]] : "endRange" , [[Pattern]] : "{hour}:{minute}" }
{[[Source]] : "shared" , [[Pattern]] : " {ampm}" }
[[Default]] :
[[year]] : "2-digit"
[[month]] : "numeric"
[[day]] : "numeric"
[[hour]] : "numeric"
[[minute]] : "numeric"
[[PatternParts]] :
{[[Source]] : "startRange" , [[Pattern]] : "{day}/{month}/{year}, {hour}:{minute} {ampm}" }
{[[Source]] : "shared" , [[Pattern]] : " – " }
{[[Source]] : "endRange" , [[Pattern]] : "{day}/{month}/{year}, {hour}:{minute} {ampm}" }
Note 3
推荐实现使用由
https://cldr.unicode.org/ 提供的 Common Locale Data Repository
区域数据。
11.3 Intl.DateTimeFormat 原型对象的属性
Intl.DateTimeFormat 原型对象 :
是 %Intl.DateTimeFormat.prototype% 。
是一个普通对象。
不是 Intl.DateTimeFormat 实例,不具有 [[InitializedDateTimeFormat]] 内部槽,也没有 Intl.DateTimeFormat 实例对象的其他内部槽。
拥有 [[Prototype]] 内部槽,其值为 %Object.prototype% 。
11.3.1 Intl.DateTimeFormat.prototype.constructor
Intl.DateTimeFormat.prototype.constructor 的初始值为 %Intl.DateTimeFormat% 。
11.3.2 Intl.DateTimeFormat.prototype.resolvedOptions ( )
此函数用于访问对象初始化时计算的区域和选项。
令 dtf 为 this 值。 如果实现支持 4.3 Note 1 的规范可选构造器模式,则 设置 dtf 为 ? UnwrapDateTimeFormat (dtf )。 执行 ? RequireInternalSlot (dtf , [[InitializedDateTimeFormat]] )。 令 options 为 OrdinaryObjectCreate (%Object.prototype% )。 对 Table 15 的每一行(除表头外,按表顺序),执行: 令 p 为当前行的 Property 值。 如果当前行有 Internal Slot 值,则 令 v 为 dtf 的内部槽,其名称为当前行的 Internal Slot 值。 否则, 令 format 为 dtf .[[DateTimeFormat]] 。 如果 format 有字段 [[<p >]] 且 dtf .[[DateStyle]] 为 undefined 且 dtf .[[TimeStyle]] 为 undefined ,则 令 v 为 format .[[<p >]]。 否则, 令 v 为 undefined 。 如果 v 不为 undefined ,则 如果当前行有 Conversion 值,则 令 conversion 为当前行的 Conversion 值。 如果 conversion 为 hour12 ,则 如果 v 为 "h11" 或 "h12" ,设置 v 为 true 。否则,设置 v 为 false 。 否则, 断言:conversion 为 number 。 设置 v 为 𝔽 (v )。 执行 ! CreateDataPropertyOrThrow (options , p , v )。 返回 options 。
Table 15: DateTimeFormat 实例的 resolvedOptions 属性
内部槽
属性
转换
[[Locale]]
"locale"
[[Calendar]]
"calendar"
[[NumberingSystem]]
"numberingSystem"
[[TimeZone]]
"timeZone"
[[HourCycle]]
"hourCycle"
[[HourCycle]]
"hour12"
hour12
"weekday"
"era"
"year"
"month"
"day"
"dayPeriod"
"hour"
"minute"
"second"
"fractionalSecondDigits"
number
"timeZoneName"
[[DateStyle]]
"dateStyle"
[[TimeStyle]]
"timeStyle"
出于网页兼容性考虑,如果设置了 "hourCycle" 属性,则当 "hourCycle" 为 "h11" 或 "h12" 时,"hour12" 属性应设置为 true ;当
"hourCycle" 为 "h23" 或 "h24" 时,"hour12" 属性应设置为 false 。
Note 1
在本版本 API 中,如果未在 Intl.DateTimeFormat 构造函数的 options 对象中提供 "timeZone" 属性,则 "timeZone" 属性将为宿主环境的时区标识符。第一版在此情况下将
"timeZone" 属性设为 undefined 。
Note 2
为兼容第五版之前的版本,"hour12" 属性会与 "hourCycle" 属性一起设置。
11.3.3 get Intl.DateTimeFormat.prototype.format
Intl.DateTimeFormat.prototype.format 是一个访问器属性,其 set 访问器函数为 undefined 。其 get 访问器函数执行以下步骤:
令 dtf 为 this 值。 如果实现支持 4.3 Note 1 的规范可选构造器模式,则 设置 dtf 为 ? UnwrapDateTimeFormat (dtf )。 执行 ? RequireInternalSlot (dtf , [[InitializedDateTimeFormat]] )。 如果 dtf .[[BoundFormat]] 为 undefined ,则 令 F 为 DateTime Format Functions (11.5.4 ) 中定义的新内置函数对象。 设置 F .[[DateTimeFormat]] 为 dtf 。 设置 dtf .[[BoundFormat]] 为 F 。 返回 dtf .[[BoundFormat]] 。
Note
返回的函数绑定到 dtf ,因此可以直接传递给 Array.prototype.map 或其他函数。
这是历史遗留的惯例,新特性已不再遵循,但为兼容现有程序而保留。
11.3.4 Intl.DateTimeFormat.prototype.formatRange ( startDate , endDate )
当调用 formatRange 方法并传入参数 startDate 和 endDate 时,执行以下步骤:
令 dtf 为 this 值。 执行 ? RequireInternalSlot (dtf , [[InitializedDateTimeFormat]] )。 如果 startDate 或 endDate 为 undefined ,抛出 TypeError 异常。 令 x 为 ? ToNumber (startDate )。 令 y 为 ? ToNumber (endDate )。 返回 ? FormatDateTimeRange (dtf , x , y )。
11.3.5 Intl.DateTimeFormat.prototype.formatRangeToParts ( startDate , endDate )
当调用 formatRangeToParts 方法并传入参数 startDate 和 endDate 时,执行以下步骤:
令 dtf 为 this 值。 执行 ? RequireInternalSlot (dtf , [[InitializedDateTimeFormat]] )。 如果 startDate 或 endDate 为 undefined ,抛出 TypeError 异常。 令 x 为 ? ToNumber (startDate )。 令 y 为 ? ToNumber (endDate )。 返回 ? FormatDateTimeRangeToParts (dtf , x , y )。
11.3.6 Intl.DateTimeFormat.prototype.formatToParts ( date )
当调用 formatToParts 方法并传入参数 date 时,执行以下步骤:
令 dtf 为 this 值。 执行 ? RequireInternalSlot (dtf , [[InitializedDateTimeFormat]] )。 如果 date 为 undefined ,则 令 x 为 ! Call (%Date.now%, undefined )。 否则, 令 x 为 ? ToNumber (date )。 返回 ? FormatDateTimeToParts (dtf , x )。
11.3.7 Intl.DateTimeFormat.prototype [ %Symbol.toStringTag% ]
%Symbol.toStringTag% 属性的初始值为字符串 "Intl.DateTimeFormat" 。
该属性具有 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : true } 属性。
11.4 Intl.DateTimeFormat 实例的属性
Intl.DateTimeFormat 实例是普通对象,继承自 %Intl.DateTimeFormat.prototype% 的属性。
Intl.DateTimeFormat 实例具有 [[InitializedDateTimeFormat]] 内部槽。
Intl.DateTimeFormat 实例还具有多个内部槽,这些槽由 Intl.DateTimeFormat 构造函数 计算:
[[Locale]] 是一个字符串值,表示用于格式化的区域语言标签 。
[[Calendar]] 是一个字符串值,表示用于格式化的 Unicode
日历标识符 。
[[NumberingSystem]] 是一个字符串值,表示用于格式化的 Unicode 数字系统标识符 。
[[TimeZone]] 是一个用于格式化的字符串值,可以是可用的命名时区标识符,也可以是偏移时区标识符。
[[HourCycle]] 是一个字符串值,指示应使用 12 小时制("h11" 、"h12" )还是 24 小时制("h23" 、"h24" )。"h11" 和 "h23" 从 0 开始,分别到 11
和 23;"h12" 和 "h24" 从 1 开始,分别到 12 和 24。仅当 [[DateTimeFormat]] 有 [[hour]] 字段时才使用 [[HourCycle]] 。
[[DateStyle]] 、[[TimeStyle]] 各自为 undefined 或字符串值,取值为 "full" 、"long" 、"medium" 或 "short" 。
[[DateTimeFormat]] 是一个 DateTime Format Record 。
最后,Intl.DateTimeFormat 实例具有 [[BoundFormat]] 内部槽,用于缓存格式访问器返回的函数(11.3.3 )。
11.5 DateTimeFormat 对象的抽象操作
多个 DateTimeFormat 算法会使用下表中的值,该表提供了日期和时间格式组件的内部槽、属性名和允许值:
Table 16: 日期和时间格式的组件
字段名
属性
值
[[Weekday]]
"weekday"
"narrow" 、"short" 、"long"
[[Era]]
"era"
"narrow" 、"short" 、"long"
[[Year]]
"year"
"2-digit" 、"numeric"
[[Month]]
"month"
"2-digit" 、"numeric" 、"narrow" 、"short" 、"long"
[[Day]]
"day"
"2-digit" 、"numeric"
[[DayPeriod]]
"dayPeriod"
"narrow" 、"short" 、"long"
[[Hour]]
"hour"
"2-digit" 、"numeric"
[[Minute]]
"minute"
"2-digit" 、"numeric"
[[Second]]
"second"
"2-digit" 、"numeric"
[[FractionalSecondDigits]]
"fractionalSecondDigits"
1、2、3
[[TimeZoneName]]
"timeZoneName"
"short" 、"long" 、"shortOffset" 、"longOffset" 、"shortGeneric" 、"longGeneric"
11.5.1 DateTimeStyleFormat ( dateStyle , timeStyle , styles )
The abstract operation DateTimeStyleFormat takes arguments dateStyle ("full" 、"long" 、"medium" 、"short" 或 undefined ,), timeStyle ("full" 、"long" 、"medium" 、"short" 或 undefined ,), and styles (一个 DateTime Styles Record ,) and returns 一个 DateTime Format Record . It performs the following steps when called:
断言:dateStyle 不为 undefined 或 timeStyle 不为 undefined 。 如果 timeStyle 不为 undefined ,则 断言:timeStyle 是 "full" 、"long" 、"medium" 或 "short" 之一。 令 timeFormat 为 styles .[[Time]] .[[<timeStyle >]]。 如果 dateStyle 不为 undefined ,则 断言:dateStyle 是 "full" 、"long" 、"medium" 或 "short" 之一。 令 dateFormat 为 styles .[[Date]] .[[<dateStyle >]]。 如果 dateStyle 和 timeStyle 都不为 undefined ,则 令 format 为一个新的 DateTime Format Record 。 将 dateFormat 的所有字段(除 [[pattern]] 和 [[rangePatterns]] )添加到 format 。 将 timeFormat 的所有字段(除 [[pattern]] 、[[rangePatterns]] 、[[pattern12]] 和 [[rangePatterns12]] ,如有)添加到 format 。 令 connector 为 styles .[[Connector]] .[[<dateStyle >]]。 令 pattern 为 connector 字符串,将 "{0}" 替换为 timeFormat .[[pattern]] ,"{1}" 替换为 dateFormat .[[pattern]] 。 设置 format .[[pattern]] 为 pattern 。 如果 timeFormat 有 [[pattern12]] 字段,则 令 pattern12 为 connector 字符串,将 "{0}" 替换为 timeFormat .[[pattern12]] ,"{1}" 替换为
dateFormat .[[pattern]] 。 设置 format .[[pattern12]] 为 pattern12 。 令 dateTimeRangeFormat 为 styles .[[DateTimeRangeFormat]] .[[<dateStyle >]].[[<timeStyle >]]。 设置 format .[[rangePatterns]] 为 dateTimeRangeFormat .[[rangePatterns]] 。 如果 dateTimeRangeFormat 有 [[rangePatterns12]] 字段,则 设置 format .[[rangePatterns12]] 为 dateTimeRangeFormat .[[rangePatterns12]] 。 返回 format 。 如果 timeStyle 不为 undefined ,则 返回 timeFormat 。 断言:dateStyle 不为 undefined 。 返回 dateFormat 。
11.5.2 BasicFormatMatcher ( options , formats )
The abstract operation BasicFormatMatcher takes arguments options (一个记录,) and formats (DateTime Format Record 的列表,) and returns 一个 DateTime Format Record . It performs the following steps when called:
令 removalPenalty 为 120。 令 additionPenalty 为 20。 令 longLessPenalty 为 8。 令 longMorePenalty 为 6。 令 shortLessPenalty 为 6。 令 shortMorePenalty 为 3。 令 offsetPenalty 为 1。 令 bestScore 为 -∞。 令 bestFormat 为 undefined 。 对 formats 的每个元素 format ,执行 令 score 为 0。 按 Table 16 的表顺序(除表头),对每一行执行 令 property 为当前行 Property 列给出的名称。 如果 options 有 [[<property >]] 字段,令 optionsProp 为 options .[[<property >]];否则令 optionsProp 为
undefined 。 如果 format 有 [[<property >]] 字段,令 formatProp 为 format .[[<property >]];否则令 formatProp 为
undefined 。 如果 optionsProp 为 undefined 且 formatProp 不为 undefined ,则 设置 score 为 score - additionPenalty 。 否则如果 optionsProp 不为 undefined 且 formatProp 为 undefined ,则 设置 score 为 score - removalPenalty 。 否则如果 property 为 "timeZoneName" ,则 如果 optionsProp 为 "short" 或 "shortGeneric" ,则 如果 formatProp 为 "shortOffset" ,设置 score 为 score - offsetPenalty 。 否则如果 formatProp 为 "longOffset" ,设置 score 为 score - (offsetPenalty + shortMorePenalty )。 否则如果 optionsProp 为 "short" 且 formatProp 为 "long" ,设置 score 为 score - shortMorePenalty 。 否则如果 optionsProp 为 "shortGeneric" 且 formatProp 为 "longGeneric" ,设置 score 为 score -
shortMorePenalty 。 否则如果 optionsProp ≠ formatProp ,设置 score 为 score - removalPenalty 。 否则如果 optionsProp 为 "shortOffset" 且 formatProp 为 "longOffset" ,则 设置 score 为 score - shortMorePenalty 。 否则如果 optionsProp 为 "long" 或 "longGeneric" ,则 如果 formatProp 为 "longOffset" ,设置 score 为 score - offsetPenalty 。 否则如果 formatProp 为 "shortOffset" ,设置 score 为 score - (offsetPenalty + longLessPenalty )。 否则如果 optionsProp 为 "long" 且 formatProp 为 "short" ,设置 score 为 score - longLessPenalty 。 否则如果 optionsProp 为 "longGeneric" 且 formatProp 为 "shortGeneric" ,设置 score 为 score -
longLessPenalty 。 否则如果 optionsProp ≠ formatProp ,设置 score 为 score - removalPenalty 。 否则如果 optionsProp 为 "longOffset" 且 formatProp 为 "shortOffset" ,则 设置 score 为 score - longLessPenalty 。 否则如果 optionsProp ≠ formatProp ,则 设置 score 为 score - removalPenalty 。 否则如果 optionsProp ≠ formatProp ,则 如果 property 为 "fractionalSecondDigits" ,则 令 values 为 « 1、2、3 »。 否则, 令 values 为 « "2-digit" 、"numeric" 、"narrow" 、"short" 、"long" »。 令 optionsPropIndex 为 optionsProp 在 values 中的索引。 令 formatPropIndex 为 formatProp 在 values 中的索引。 令 delta 为 max (min (formatPropIndex - optionsPropIndex , 2), -2)。 如果 delta = 2,设置 score 为 score - longMorePenalty 。 否则如果 delta = 1,设置 score 为 score - shortMorePenalty 。 否则如果 delta = -1,设置 score 为 score - shortLessPenalty 。 否则如果 delta = -2,设置 score 为 score - longLessPenalty 。 如果 score > bestScore ,则 设置 bestScore 为 score 。 设置 bestFormat 为 format 。 返回 bestFormat 。
11.5.3 BestFitFormatMatcher ( options , formats )
The implementation-defined abstract operation BestFitFormatMatcher takes arguments options (一个记录,) and formats (DateTime Format Record 的列表,) and returns 一个 DateTime Format Record .
11.5.4 DateTime 格式化函数
DateTime 格式化函数是一个匿名内置函数,具有 [[DateTimeFormat]] 内部槽。
当 DateTime 格式化函数 F 被调用并传入可选参数 date 时,执行以下步骤:
令 dtf 为 F .[[DateTimeFormat]] 。 断言:dtf 是一个对象且具有 [[InitializedDateTimeFormat]] 内部槽。 如果未提供 date 或 date 为 undefined ,则 令 x 为 ! Call (%Date.now%, undefined )。 否则, 令 x 为 ? ToNumber (date )。 返回 ? FormatDateTime (dtf , x )。
DateTime 格式化函数的 "length" 属性为 1 𝔽 。
11.5.5 FormatDateTimePattern ( dateTimeFormat , format , pattern , epochNanoseconds )
The abstract operation FormatDateTimePattern takes arguments dateTimeFormat (一个 Intl.DateTimeFormat,), format (一个 DateTime Format Record 或 DateTime Range Pattern Format Record ,), pattern (一个 Pattern 字符串,), and epochNanoseconds (一个 BigInt,) and returns 一个包含 [[Type]] (字符串)和 [[Value]] (字符串)字段的记录列表. It performs the following steps when called:
令 locale 为 dateTimeFormat .[[Locale]] 。 令 nfOptions 为 OrdinaryObjectCreate (null )。 执行 ! CreateDataPropertyOrThrow (nfOptions , "numberingSystem" , dateTimeFormat .[[NumberingSystem]] )。 执行 ! CreateDataPropertyOrThrow (nfOptions , "useGrouping" , false )。 令 nf 为 ! Construct (%Intl.NumberFormat% , « locale , nfOptions »)。 令 nf2Options 为 OrdinaryObjectCreate (null )。 执行 ! CreateDataPropertyOrThrow (nf2Options , "minimumIntegerDigits" , 2 𝔽 )。 执行 ! CreateDataPropertyOrThrow (nf2Options , "numberingSystem" , dateTimeFormat .[[NumberingSystem]] )。 执行 ! CreateDataPropertyOrThrow (nf2Options , "useGrouping" , false )。 令 nf2 为 ! Construct (%Intl.NumberFormat% , « locale , nf2Options »)。 如果 format 有 [[fractionalSecondDigits]] 字段,则 令 fractionalSecondDigits 为 format .[[fractionalSecondDigits]] 。 令 nf3Options 为 OrdinaryObjectCreate (null )。 执行 ! CreateDataPropertyOrThrow (nf3Options , "minimumIntegerDigits" , 𝔽 (fractionalSecondDigits ))。 执行 ! CreateDataPropertyOrThrow (nf3Options , "numberingSystem" , dateTimeFormat .[[NumberingSystem]] )。 执行 ! CreateDataPropertyOrThrow (nf3Options , "useGrouping" , false )。 令 nf3 为 ! Construct (%Intl.NumberFormat% , « locale , nf3Options »)。 令 tm 为 ToLocalTime (epochNanoseconds , dateTimeFormat .[[Calendar]] , dateTimeFormat .[[TimeZone]] )。 令 patternParts 为 PartitionPattern (pattern )。 令 result 为一个新的空列表。 对 patternParts 的每个记录 { [[Type]] , [[Value]] } patternPart ,执行 令 p 为 patternPart .[[Type]] 。 如果 p 为 "literal" ,则 将记录 { [[Type]] : "literal" , [[Value]] : patternPart .[[Value]] } 添加到 result 。 否则如果 p 为 "fractionalSecondDigits" ,则 断言:format 有 [[fractionalSecondDigits]] 字段。 令 v 为 tm .[[Millisecond]] 。 设置 v 为 floor (v × 10( fractionalSecondDigits - 3 ) )。 令 fv 为 FormatNumeric (nf3 , v )。 将记录 { [[Type]] : "fractionalSecond" , [[Value]] : fv } 添加到 result 。 否则如果 p 为 "dayPeriod" ,则 断言:format 有 [[dayPeriod]] 字段。 令 f 为 format .[[dayPeriod]] 。 令 fv 为表示 tm 的日间段的字符串,形式由 f 指定;字符串值依赖于实现和 dateTimeFormat 的有效区域。 将记录 { [[Type]] : p , [[Value]] : fv } 添加到 result 。 否则如果 p 为 "timeZoneName" ,则 断言:format 有 [[timeZoneName]] 字段。 令 f 为 format .[[timeZoneName]] 。 令 v 为 dateTimeFormat .[[TimeZone]] 。 令 fv 为表示 v 的字符串,形式由 f 指定;字符串值依赖于实现和 dateTimeFormat 的有效区域。字符串值也可能依赖于 tm 的 [[InDST]] 字段(如果 f 为
"short" 、"long" 、"shortOffset" 或 "longOffset" )。如果实现没有该本地化表示,则使用 v 的字符串值。 将记录 { [[Type]] : p , [[Value]] : fv } 添加到 result 。 否则如果 p 匹配 Table 16 的 Property 列,则 断言:format 有 [[<p >]] 字段。 令 f 为 format .[[<p >]]。 令 v 为 tm 的字段,其名称为匹配行的 Internal Slot 列。 如果 p 为 "year" 且 v ≤ 0,设置 v 为 1 - v 。 如果 p 为 "month" ,设置 v 为 v + 1。 如果 p 为 "hour" 且 dateTimeFormat .[[HourCycle]] 为 "h11" 或 "h12" ,则 设置 v 为 v 模 12。 如果 v 为 0 且 dateTimeFormat .[[HourCycle]] 为 "h12" ,设置 v 为 12。 如果 p 为 "hour" 且 dateTimeFormat .[[HourCycle]] 为 "h24" ,则 如果 v 为 0,设置 v 为 24。 如果 f 为 "numeric" ,则 令 fv 为 FormatNumeric (nf , v )。 否则如果 f 为 "2-digit" ,则 令 fv 为 FormatNumeric (nf2 , v )。 令 codePoints 为 StringToCodePoints (fv )。 令 count 为 codePoints 的元素数量。 如果 count > 2,则 令 tens 为 codePoints [count - 2]。 令 ones 为 codePoints [count - 1]。 设置 fv 为 CodePointsToString (« tens , ones »)。 否则如果 f 为 "narrow" 、"short" 或 "long" ,则 令 fv 为表示 v 的字符串,形式由 f 指定;字符串值依赖于实现、有效区域和日历。如果 p 为 "month" ,则字符串值也可能依赖于 format .[[day]]
是否存在。如果实现没有该本地化表示,则使用 v 的字符串值。 将记录 { [[Type]] : p , [[Value]] : fv } 添加到 result 。 否则如果 p 为 "ampm" ,则 令 v 为 tm .[[Hour]] 。 如果 v 大于 11,则 令 fv 为 ILD 字符串,表示 "post meridiem" 。 否则, 令 fv 为 ILD 字符串,表示 "ante meridiem" 。 将记录 { [[Type]] : "dayPeriod" , [[Value]] : fv } 添加到 result 。 否则如果 p 为 "relatedYear" ,则 令 v 为 tm .[[RelatedYear]] 。 令 fv 为 FormatNumeric (nf , v )。 将记录 { [[Type]] : "relatedYear" , [[Value]] : fv } 添加到 result 。 否则如果 p 为 "yearName" ,则 令 v 为 tm .[[YearName]] 。 令 fv 为表示 v 的 ILD 字符串。 将记录 { [[Type]] : "yearName" , [[Value]] : fv } 添加到 result 。 否则, 令 unknown 为基于 epochNanoseconds 和 p 的实现、区域和数字系统相关字符串。 将记录 { [[Type]] : "unknown" , [[Value]] : unknown } 添加到 result 。 返回 result 。
Note
推荐实现使用 Common Locale Data Repository(
https://cldr.unicode.org/ )提供的区域和日历相关字符串。DateTimeFormat
"short" 字符串建议使用
CLDR
"abbreviated" ,
"long" 字符串建议使用 CLDR
"wide" 。
11.5.6 PartitionDateTimePattern ( dateTimeFormat , x )
The abstract operation PartitionDateTimePattern takes arguments dateTimeFormat (一个 Intl.DateTimeFormat,) and x (一个 Number,) and returns 正常完成包含一个记录列表(字段 [[Type]] (字符串)、[[Value]] (字符串)),或抛出完成. It performs the following steps when called:
令 x 为 TimeClip (x )。 如果 x 为 NaN ,抛出 RangeError 异常。 令 epochNanoseconds 为 ℤ (ℝ (x ) × 106 )。 令 format 为 dateTimeFormat .[[DateTimeFormat]] 。 如果 dateTimeFormat .[[HourCycle]] 为 "h11" 或 "h12" ,则 令 pattern 为 format .[[pattern12]] 。 否则, 令 pattern 为 format .[[pattern]] 。 令 result 为 FormatDateTimePattern (dateTimeFormat , format , pattern , epochNanoseconds )。 返回 result 。
11.5.7 FormatDateTime ( dateTimeFormat , x )
The abstract operation FormatDateTime takes arguments dateTimeFormat (一个 Intl.DateTimeFormat,) and x (一个 Number,) and returns 正常完成包含一个字符串或抛出完成. It performs the following steps when called:
令 parts 为 ? PartitionDateTimePattern (dateTimeFormat , x )。 令 result 为空字符串。 对 parts 的每个记录 { [[Type]] , [[Value]] } part ,执行 设置 result 为 result 与 part .[[Value]] 的字符串拼接。 返回 result 。
11.5.8 FormatDateTimeToParts ( dateTimeFormat , x )
The abstract operation FormatDateTimeToParts takes arguments dateTimeFormat (一个 Intl.DateTimeFormat,) and x (一个 Number,) and returns 正常完成包含一个数组或抛出完成. It performs the following steps when called:
令 parts 为 ? PartitionDateTimePattern (dateTimeFormat , x )。 令 result 为 ! ArrayCreate (0)。 令 n 为 0。 对 parts 的每个记录 { [[Type]] , [[Value]] } part ,执行 令 O 为 OrdinaryObjectCreate (%Object.prototype% )。 执行 ! CreateDataPropertyOrThrow (O , "type" , part .[[Type]] )。 执行 ! CreateDataPropertyOrThrow (O , "value" , part .[[Value]] )。 执行 ! CreateDataPropertyOrThrow (result , ! ToString (𝔽 (n )), O )。 n 加 1。返回 result 。
11.5.9 PartitionDateTimeRangePattern ( dateTimeFormat , x , y )
The abstract operation PartitionDateTimeRangePattern takes arguments dateTimeFormat (一个 Intl.DateTimeFormat,), x (一个 Number,), and y (一个 Number,) and returns 正常完成包含一个记录列表(字段 [[Type]] (字符串)、[[Value]] (字符串)、[[Source]] (字符串)),或抛出完成. It performs the following steps when called:
设置 x 为 TimeClip (x )。 如果 x 为 NaN ,抛出 RangeError 异常。 设置 y 为 TimeClip (y )。 如果 y 为 NaN ,抛出 RangeError 异常。 令 xEpochNanoseconds 为 ℤ (ℝ (x ) × 106 )。 令 yEpochNanoseconds 为 ℤ (ℝ (y ) × 106 )。 令 tm1 为 ToLocalTime (xEpochNanoseconds , dateTimeFormat .[[Calendar]] , dateTimeFormat .[[TimeZone]] )。 令 tm2 为 ToLocalTime (yEpochNanoseconds , dateTimeFormat .[[Calendar]] , dateTimeFormat .[[TimeZone]] )。 令 format 为 dateTimeFormat .[[DateTimeFormat]] 。 如果 dateTimeFormat .[[HourCycle]] 为 "h11" 或 "h12" ,则 令 pattern 为 format .[[pattern12]] 。 令 rangePatterns 为 format .[[rangePatterns12]] 。 否则, 令 pattern 为 format .[[pattern]] 。 令 rangePatterns 为 format .[[rangePatterns]] 。 令 selectedRangePattern 为 undefined 。 令 relevantFieldsEqual 为 true 。 令 checkMoreFields 为 true 。 按 Table 6 的表顺序(除表头),对每一行执行 令 fieldName 为当前行 Field Name 列给出的名称。 如果 rangePatterns 有名为 fieldName 的字段,令 rangePattern 为该字段;否则令 rangePattern 为 undefined 。 如果 selectedRangePattern 不为 undefined 且 rangePattern 为 undefined ,则 注:没有该字段的范围模式,后续不再检查。 设置 checkMoreFields 为 false 。 如果 fieldName 不等于 [[Default]] 且 relevantFieldsEqual 为 true 且 checkMoreFields 为 true ,则 设置 selectedRangePattern 为 rangePattern 。 如果 fieldName 为 [[AmPm]] ,则 如果 tm1 .[[Hour]] 小于 12,令 v1 为 "am" ;否则令 v1 为 "pm" 。 如果 tm2 .[[Hour]] 小于 12,令 v2 为 "am" ;否则令 v2 为 "pm" 。 否则如果 fieldName 为 [[DayPeriod]] ,则 令 v1 为表示 tm1 日间段的字符串,依赖于实现和 dateTimeFormat 的有效区域。 令 v2 为表示 tm2 日间段的字符串,依赖于实现和 dateTimeFormat 的有效区域。 否则如果 fieldName 为 [[FractionalSecondDigits]] ,则 如果 format 有 [[fractionalSecondDigits]] 字段,则 令 fractionalSecondDigits 为 format .[[fractionalSecondDigits]] 。 否则, 令 fractionalSecondDigits 为 3。 令 exp 为 fractionalSecondDigits - 3。 令 v1 为 floor (tm1 .[[Millisecond]] × 10exp )。 令 v2 为 floor (tm2 .[[Millisecond]] × 10exp )。 否则, 令 v1 为 tm1 的字段,其名称为 fieldName 。 令 v2 为 tm2 的字段,其名称为 fieldName 。 如果 v1 ≠ v2 ,则 设置 relevantFieldsEqual 为 false 。 如果 relevantFieldsEqual 为 true ,则 令 collapsedResult 为一个新的空列表。 令 resultParts 为 FormatDateTimePattern (dateTimeFormat , format , pattern , xEpochNanoseconds )。 对 resultParts 的每个记录 { [[Type]] , [[Value]] } r ,执行 将记录 { [[Type]] : r .[[Type]] , [[Value]] : r .[[Value]] , [[Source]] : "shared" } 添加到 collapsedResult 。 返回 collapsedResult 。 令 rangeResult 为一个新的空列表。 如果 selectedRangePattern 为 undefined ,则 设置 selectedRangePattern 为 rangePatterns .[[Default]] 。 对 selectedRangePattern .[[PatternParts]] 的每个记录 { [[Pattern]] , [[Source]] } rangePatternPart ,执行 令 pattern 为 rangePatternPart .[[Pattern]] 。 令 source 为 rangePatternPart .[[Source]] 。 如果 source 为 "startRange" 或 "shared" ,则 令 z 为 xEpochNanoseconds 。 否则, 令 z 为 yEpochNanoseconds 。 令 resultParts 为 FormatDateTimePattern (dateTimeFormat , selectedRangePattern , pattern , z )。 对 resultParts 的每个记录 { [[Type]] , [[Value]] } r ,执行 将记录 { [[Type]] : r .[[Type]] , [[Value]] : r .[[Value]] , [[Source]] : source } 添加到 rangeResult 。 返回 rangeResult 。
11.5.10 FormatDateTimeRange ( dateTimeFormat , x , y )
The abstract operation FormatDateTimeRange takes arguments dateTimeFormat (一个 Intl.DateTimeFormat,), x (一个 Number,), and y (一个 Number,) and returns 正常完成包含一个字符串或抛出完成. It performs the following steps when called:
令 parts 为 ? PartitionDateTimeRangePattern (dateTimeFormat , x , y )。 令 result 为空字符串。 对 parts 的每个记录 { [[Type]] , [[Value]] , [[Source]] } part ,执行 设置 result 为 result 与 part .[[Value]] 的字符串拼接。 返回 result 。
11.5.11 FormatDateTimeRangeToParts ( dateTimeFormat , x , y )
The abstract operation FormatDateTimeRangeToParts takes arguments dateTimeFormat (一个 Intl.DateTimeFormat,), x (一个 Number,), and y (一个 Number,) and returns 正常完成包含一个数组或抛出完成. It performs the following steps when called:
令 parts 为 ? PartitionDateTimeRangePattern (dateTimeFormat , x , y )。 令 result 为 ! ArrayCreate (0)。 令 n 为 0。 对 parts 的每个记录 { [[Type]] , [[Value]] , [[Source]] } part ,执行 令 O 为 OrdinaryObjectCreate (%Object.prototype% )。 执行 ! CreateDataPropertyOrThrow (O , "type" , part .[[Type]] )。 执行 ! CreateDataPropertyOrThrow (O , "value" , part .[[Value]] )。 执行 ! CreateDataPropertyOrThrow (O , "source" , part .[[Source]] )。 执行 ! CreateDataPropertyOrThrow (result , ! ToString (𝔽 (n )), O )。 n 加 1。返回 result 。
11.5.12 ToLocalTime ( epochNs , calendar , timeZoneIdentifier )
The implementation-defined abstract operation ToLocalTime takes arguments epochNs (一个 BigInt,), calendar (一个字符串,), and timeZoneIdentifier (一个字符串,) and returns 一个 ToLocalTime Record . It performs the following steps when called:
如果 IsTimeZoneOffsetString (timeZoneIdentifier ) 为 true ,则 令 offsetNs 为 ParseTimeZoneOffsetString (timeZoneIdentifier )。 否则, 断言:GetAvailableNamedTimeZoneIdentifier (timeZoneIdentifier ) 不为 empty 。 令 offsetNs 为 GetNamedTimeZoneOffsetNanoseconds (timeZoneIdentifier , epochNs )。 令 tz 为 ℝ (epochNs ) + offsetNs 。 如果 calendar 为 "gregory" ,则 返回一个 ToLocalTime Record ,字段按 Table 17 计算。 否则, 返回一个 ToLocalTime Record ,字段按给定 calendar 计算。计算应使用关于指定 calendar 的最佳可用信息。
11.5.13 ToLocalTime 记录
每个 ToLocalTime Record 都具有 Table 17 中定义的字段。
Table 17: ToLocalTime 返回的记录
11.5.14 UnwrapDateTimeFormat ( dtf )
The abstract operation UnwrapDateTimeFormat takes argument dtf (一个 ECMAScript 语言值,) and returns 正常完成包含一个 ECMAScript 语言值或抛出完成. It performs the following steps when called:
如果 dtf 不是对象,抛出 TypeError 异常。 如果 dtf 没有 [[InitializedDateTimeFormat]] 内部槽且 ? OrdinaryHasInstance (%Intl.DateTimeFormat% , dtf ) 为 true ,则 返回 ? Get (dtf , %Intl% .[[FallbackSymbol]] )。 返回 dtf 。
12 DisplayNames 对象
12.1 Intl.DisplayNames 构造函数
Intl.DisplayNames 构造函数:
是 %Intl.DisplayNames% 。
是 Intl 对象 的 "DisplayNames" 属性的初始值。
Intl 对象 所有服务构造属性的通用行为在 9.1 中指定。
12.1.1 Intl.DisplayNames ( locales , options )
当 Intl.DisplayNames 函数以参数 locales 和 options 被调用时,执行以下步骤:
如果 NewTarget 是 undefined ,抛出 TypeError 异常。 令 displayNames 为 ? OrdinaryCreateFromConstructor (NewTarget, "%Intl.DisplayNames.prototype%" , «
[[InitializedDisplayNames]] , [[Locale]] , [[Style]] , [[Type]] , [[Fallback]] , [[LanguageDisplay]] , [[Fields]] »)。 令 optionsResolution 为 ? ResolveOptions (%Intl.DisplayNames% , %Intl.DisplayNames% .[[LocaleData]] , locales ,
options , « require-options »)。 设置 options 为 optionsResolution .[[Options]] 。 令 r 为 optionsResolution .[[ResolvedLocale]] 。 令 style 为 ? GetOption (options , "style" , string , « "narrow" , "short" , "long" », "long" )。 设置 displayNames .[[Style]] 为 style 。 令 type 为 ? GetOption (options , "type" , string , « "language" , "region" , "script" , "currency" ,
"calendar" , "dateTimeField" », undefined )。 如果 type 是 undefined ,抛出 TypeError 异常。 设置 displayNames .[[Type]] 为 type 。 令 fallback 为 ? GetOption (options , "fallback" , string , « "code" , "none" », "code" )。 设置 displayNames .[[Fallback]] 为 fallback 。 设置 displayNames .[[Locale]] 为 r .[[Locale]] 。 令 resolvedLocaleData 为 r .[[LocaleData]] 。 令 types 为 resolvedLocaleData .[[types]] 。 断言:types 是一个 Record (见 12.2.3 )。 令 languageDisplay 为 ? GetOption (options , "languageDisplay" , string , « "dialect" , "standard" »,
"dialect" )。 令 typeFields 为 types .[[<type >]]。 断言:typeFields 是一个 Record (见 12.2.3 )。 如果 type 是 "language" ,则 设置 displayNames .[[LanguageDisplay]] 为 languageDisplay 。 设置 typeFields 为 typeFields .[[<languageDisplay >]]。 断言:typeFields 是一个 Record (见 12.2.3 )。 令 styleFields 为 typeFields .[[<style >]]。 断言:styleFields 是一个 Record (见 12.2.3 )。 设置 displayNames .[[Fields]] 为 styleFields 。 返回 displayNames 。
12.2 Intl.DisplayNames 构造函数的属性
Intl.DisplayNames 构造函数:
12.2.1 Intl.DisplayNames.prototype
Intl.DisplayNames.prototype 的值为 %Intl.DisplayNames.prototype% 。
该属性具有 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : false } 属性。
12.2.2 Intl.DisplayNames.supportedLocalesOf ( locales [ , options ] )
当 supportedLocalesOf 方法以参数 locales 和 options 被调用时,执行以下步骤:
令 availableLocales 为 %Intl.DisplayNames% .[[AvailableLocales]] 。 令 requestedLocales 为 ? CanonicalizeLocaleList (locales )。 返回 ? FilterLocales (availableLocales , requestedLocales , options )。
12.2.3 内部槽
[[AvailableLocales]] 内部槽的值在 9.1 描述的约束范围内由实现定义。
[[RelevantExtensionKeys]] 内部槽的值为 « »。
[[ResolutionOptionDescriptors]] 内部槽的值为 « »。
[[LocaleData]] 内部槽的值在 9.1 描述的约束及以下附加约束范围内由实现定义:
[[LocaleData]] .[[<locale >]] 必须为所有 locale 值 locale 提供 [[types]] 字段。该字段的值必须是一个
Record ,且必须包含所有显示名称类型的字段名:"language" 、"region" 、"script" 、"currency" 、"calendar" 和 "dateTimeField" 。
字段 "language" 的值必须是一个 Record ,且必须包含有效语言显示名称的字段名:"dialect" 和 "standard" 。
显示名称类型 "language" 下的语言显示字段应包含 Record ,且必须包含有效显示名称样式的字段名:"narrow" 、"short" 和 "long" 。
字段 "region" 、"script" 、"currency" 、"calendar" 和 "dateTimeField" 的值必须是
Record ,且必须包含所有显示名称样式的字段名:"narrow" 、"short" 和 "long" 。
显示名称类型 "language" 下的显示名称样式字段应包含与 unicode_language_id Unicode locale
非终结符匹配的语言代码的键。该字段的值必须是字符串。
显示名称类型 "region" 下的显示名称样式字段应包含区域代码的键。该字段的值必须是字符串。
显示名称类型 "script" 下的显示名称样式字段应包含脚本代码的键。该字段的值必须是字符串。
显示名称类型 "currency" 下的显示名称样式字段应包含货币代码的键。该字段的值必须是字符串。
显示名称类型 "calendar" 下的显示名称样式字段应包含与 type Unicode locale 非终结符匹配的日历标识符的键。该字段的值必须是字符串。
显示名称类型 "dateTimeField" 下的显示名称样式字段应包含 Table 19
中列出的代码的键。该字段的值必须是字符串。
Note
推荐实现使用由 Common Locale Data Repository 提供的本地化数据(可访问
https://cldr.unicode.org/ )。
12.3 Intl.DisplayNames 原型对象的属性
Intl.DisplayNames 原型对象 :
是 %Intl.DisplayNames.prototype% 。
是一个普通对象。
不是 Intl.DisplayNames 实例,不具有 [[InitializedDisplayNames]] 内部槽或任何 Intl.DisplayNames 实例对象的其他内部槽。
具有 [[Prototype]] 内部槽,其值为 %Object.prototype% 。
12.3.1 Intl.DisplayNames.prototype.constructor
Intl.DisplayNames.prototype.constructor 的初始值为 %Intl.DisplayNames% 。
12.3.2 Intl.DisplayNames.prototype.resolvedOptions ( )
此函数提供在对象初始化期间计算的 locale 和 options 的访问。
令 displayNames 为 this 值。 执行 ? RequireInternalSlot (displayNames , [[InitializedDisplayNames]] )。 令 options 为 OrdinaryObjectCreate (%Object.prototype% )。 对 Table 18 的每一行(除表头行外),按表顺序执行 令 p 为当前行的 Property 值。 令 v 为 displayNames 的内部槽,其名称为当前行的 Internal Slot 值。 如果 v 不为 undefined ,则 执行 ! CreateDataPropertyOrThrow (options , p , v )。 返回 options 。
Table 18: DisplayNames 实例的已解析选项
内部槽
属性
[[Locale]]
"locale"
[[Style]]
"style"
[[Type]]
"type"
[[Fallback]]
"fallback"
[[LanguageDisplay]]
"languageDisplay"
12.3.3 Intl.DisplayNames.prototype.of ( code )
当 Intl.DisplayNames.prototype.of 以参数 code 被调用时,执行以下步骤:
令 displayNames 为 this 值。 执行 ? RequireInternalSlot (displayNames , [[InitializedDisplayNames]] )。 令 code 为 ? ToString (code )。 设置 code 为 ? CanonicalCodeForDisplayNames (displayNames .[[Type]] , code )。 令 fields 为 displayNames .[[Fields]] 。 如果 fields 有字段 [[<code >]],返回 fields .[[<code >]]。 如果 displayNames .[[Fallback]] 是 "code" ,返回 code 。 返回 undefined 。
12.3.4 Intl.DisplayNames.prototype [ %Symbol.toStringTag% ]
%Symbol.toStringTag% 属性的初始值为字符串 "Intl.DisplayNames" 。
该属性具有 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : true } 属性。
12.4 Intl.DisplayNames 实例的属性
Intl.DisplayNames 实例是普通对象,从 %Intl.DisplayNames.prototype% 继承属性。
Intl.DisplayNames 实例具有 [[InitializedDisplayNames]] 内部槽。
Intl.DisplayNames 实例还具有多个内部槽,这些槽由 Intl.DisplayNames 构造函数 计算:
[[Locale]] 是一个字符串值,表示用于格式化的本地化语言标签 。
[[Style]] 是字符串值之一:"narrow" 、"short" 或 "long" ,标识所用的显示名称样式。
[[Type]] 是字符串值之一:"language" 、"region" 、"script" 、"currency" 、"calendar" 或 "dateTimeField" ,标识请求的显示名称类型。
[[Fallback]] 是字符串值之一:"code" 或 "none" ,标识系统没有请求的显示名称时的回退返回。
[[LanguageDisplay]] 是字符串值之一:"dialect" 或 "standard" ,标识语言显示类型。仅当 [[Type]] 为 "language" 时使用。
[[Fields]] 是一个 Record (见 12.2.3 ),必须具有与
[[Style]] 、[[Type]] 和 [[LanguageDisplay]] 相对应代码的键。
12.5 DisplayNames 对象的抽象操作
12.5.1 CanonicalCodeForDisplayNames ( type , code )
The abstract operation CanonicalCodeForDisplayNames takes arguments type (一个字符串) and code (一个字符串) and returns 返回字符串的正常完成或抛出完成. It performs the following steps when called:
如果 type 是 "language" ,则 如果 code 不能被 unicode_language_id Unicode locale 非终结符匹配,抛出 RangeError 异常。 如果 IsWellFormedLanguageTag (code ) 为 false ,抛出 RangeError 异常。 返回 CanonicalizeUnicodeLocaleId (code )。 如果 type 是 "region" ,则 如果 code 不能被 unicode_region_subtag Unicode locale 非终结符匹配,抛出 RangeError 异常。 返回 code 的 ASCII 大写形式。 如果 type 是 "script" ,则 如果 code 不能被 unicode_script_subtag Unicode locale 非终结符匹配,抛出 RangeError 异常。 断言:code 的长度为 4,且每个代码单元都是 ASCII 字母(0x0041 到 0x005A 和 0x0061 到 0x007A,均包含)。 令 first 为 code 从 0 到 1 的子串的 ASCII 大写形式。 令 rest 为 code 从 1 开始的子串的 ASCII 小写形式。 返回 first 和 rest 的字符串拼接。 如果 type 是 "calendar" ,则 如果 code 不能被 type Unicode locale 非终结符匹配,抛出 RangeError 异常。 如果 code 使用 Unicode 技术标准 #35 第 1 部分核心,第 3.3 节
BCP 47 兼容性 描述的任何向后兼容语法,抛出 RangeError 异常。 返回 code 的 ASCII 小写形式。 如果 type 是 "dateTimeField" ,则 如果 IsValidDateTimeFieldCode (code ) 的结果为 false ,抛出 RangeError 异常。 返回 code 。 断言:type 是 "currency" 。 如果 IsWellFormedCurrencyCode (code ) 为 false ,抛出 RangeError 异常。 返回 code 的 ASCII 大写形式。
12.5.2 IsValidDateTimeFieldCode ( field )
The abstract operation IsValidDateTimeFieldCode takes argument field (一个字符串) and returns 布尔值. It performs the following steps when called:
如果 field 在 Table 19 的 Code 列中列出,返回 true 。 返回 false 。
Table 19: DisplayNames 的日期时间字段代码
代码
描述
"era"
表示纪元字段,例如公历或儒略历中的公元或公元前。
"year"
表示年份(在纪元内)。
"quarter"
表示季度,例如 Q2、第二季度等。
"month"
表示月份,例如 Sep、September 等。
"weekOfYear"
表示一年中的周数。
"weekday"
表示星期几,例如 Tue、Tuesday 等。
"day"
表示月中的日期。
"dayPeriod"
表示日间时段,例如上午、下午等,或中午、傍晚等。
"hour"
表示小时。
"minute"
表示分钟。
"second"
表示秒。
"timeZoneName"
表示时区名称,例如 PDT、Pacific Daylight Time 等。
13 DurationFormat 对象
13.1 Intl.DurationFormat 构造函数
Intl.DurationFormat 构造函数:
是 %Intl.DurationFormat% 。
是 Intl 对象 的 "DurationFormat" 属性的初始值。
Intl 对象 所有服务构造属性的通用行为在 9.1 中指定。
13.1.1 Intl.DurationFormat ( [ locales [ , options ] ] )
当以可选参数 locales 和 options 调用 Intl.DurationFormat 函数时,执行以下步骤:
如果 NewTarget 是 undefined ,抛出 TypeError 异常。 令 durationFormat 为 ? OrdinaryCreateFromConstructor (NewTarget, "%Intl.DurationFormatPrototype%" , «
[[InitializedDurationFormat]] , [[Locale]] , [[NumberingSystem]] , [[Style]] , [[YearsOptions]] , [[MonthsOptions]] ,
[[WeeksOptions]] , [[DaysOptions]] , [[HoursOptions]] , [[MinutesOptions]] , [[SecondsOptions]] ,
[[MillisecondsOptions]] , [[MicrosecondsOptions]] , [[NanosecondsOptions]] , [[HourMinuteSeparator]] ,
[[MinuteSecondSeparator]] , [[FractionalDigits]] »)。 令 optionsResolution 为 ? ResolveOptions (%Intl.DurationFormat% , %Intl.DurationFormat% .[[LocaleData]] ,
locales , options )。 设置 options 为 optionsResolution .[[Options]] 。 令 r 为 optionsResolution .[[ResolvedLocale]] 。 设置 durationFormat .[[Locale]] 为 r .[[Locale]] 。 令 resolvedLocaleData 为 r .[[LocaleData]] 。 令 digitalFormat 为 resolvedLocaleData .[[DigitalFormat]] 。 设置 durationFormat .[[HourMinuteSeparator]] 为 digitalFormat .[[HourMinuteSeparator]] 。 设置 durationFormat .[[MinuteSecondSeparator]] 为 digitalFormat .[[MinuteSecondSeparator]] 。 设置 durationFormat .[[NumberingSystem]] 为 r .[[nu]] 。 令 style 为 ? GetOption (options , "style" , string , « "long" , "short" , "narrow" , "digital" »,
"short" )。 设置 durationFormat .[[Style]] 为 style 。 令 prevStyle 为空字符串。 对 Table 20 的每一行(除表头行),按表顺序,执行: 令 slot 为当前行的 Internal Slot 值。 令 unit 为当前行的 Unit 值。 令 styles 为当前行的 Styles 值。 令 digitalBase 为当前行的 Digital Default 值。 令 unitOptions 为 ? GetDurationUnitOptions (unit , options , style , styles , digitalBase , prevStyle ,
digitalFormat .[[TwoDigitHours]] )。 设置 durationFormat 的内部槽名为 slot 的值为 unitOptions 。 如果 unit 是 "hours" 、"minutes" 、"seconds" 、"milliseconds" 或 "microseconds" 之一,则 设置 prevStyle 为 unitOptions .[[Style]] 。 设置 durationFormat .[[FractionalDigits]] 为 ? GetNumberOption (options , "fractionalDigits" , 0, 9,
undefined )。 返回 durationFormat 。
Table 20: DurationFormat 实例的内部槽和属性名
Internal Slot
Unit
Styles
Digital Default
[[YearsOptions]]
"years"
« "long" , "short" , "narrow" »
"short"
[[MonthsOptions]]
"months"
« "long" , "short" , "narrow" »
"short"
[[WeeksOptions]]
"weeks"
« "long" , "short" , "narrow" »
"short"
[[DaysOptions]]
"days"
« "long" , "short" , "narrow" »
"short"
[[HoursOptions]]
"hours"
« "long" , "short" , "narrow" , "numeric" , "2-digit" »
"numeric"
[[MinutesOptions]]
"minutes"
« "long" , "short" , "narrow" , "numeric" , "2-digit" »
"numeric"
[[SecondsOptions]]
"seconds"
« "long" , "short" , "narrow" , "numeric" , "2-digit" »
"numeric"
[[MillisecondsOptions]]
"milliseconds"
« "long" , "short" , "narrow" , "numeric" »
"numeric"
[[MicrosecondsOptions]]
"microseconds"
« "long" , "short" , "narrow" , "numeric" »
"numeric"
[[NanosecondsOptions]]
"nanoseconds"
« "long" , "short" , "narrow" , "numeric" »
"numeric"
13.2 Intl.DurationFormat 构造函数的属性
Intl.DurationFormat 构造函数:
13.2.1 Intl.DurationFormat.prototype
Intl.DurationFormat.prototype 的值为 %Intl.DurationFormat.prototype% 。
该属性具有 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : false } 属性。
13.2.2 Intl.DurationFormat.supportedLocalesOf ( locales [ , options ] )
当以参数 locales 和 options 调用 supportedLocalesOf 方法时,执行以下步骤:
令 availableLocales 为 %Intl.DurationFormat% .[[AvailableLocales]] 。 令 requestedLocales 为 ? CanonicalizeLocaleList (locales )。 返回 ? FilterLocales (availableLocales , requestedLocales , options )。
13.2.3 内部槽
[[AvailableLocales]] 内部槽的值在 9.1 描述的约束范围内由实现定义。
[[RelevantExtensionKeys]] 内部槽的值为 « "nu" »。
[[ResolutionOptionDescriptors]] 内部槽的值为 « { [[Key]] : "nu" , [[Property]] : "numberingSystem" } »。
[[LocaleData]] 内部槽的值在 9.1 描述的约束范围内由实现定义,并且对所有区域设置值 locale
有以下附加约束:
[[LocaleData]] .[[<locale >]] 必须是包含 [[nu]] 和 [[DigitalFormat]] 字段的 Record 。
[[LocaleData]] .[[<locale >]].[[nu]] 必须是 16.2.3 指定的 List ,并且不得包含 "native" 、"traditio" 或
"finance" 。
[[LocaleData]] .[[<locale >]].[[DigitalFormat]] 必须是包含与该区域设置可用的每个数字系统对应的键的 Record 。每个键对应的值必须是包含以下字段的
Record :
[[HourMinuteSeparator]] 必须是字符串值,表示在使用 "numeric" 或 "2-digit" 样式时该区域设置和数字系统组合下小时与分钟之间的适当分隔符。
[[MinuteSecondSeparator]] 必须是字符串值,表示在使用 "numeric" 或 "2-digit" 样式时该区域设置和数字系统组合下分钟与秒之间的适当分隔符。
[[TwoDigitHours]] 必须是布尔值,指示在使用 "numeric" 样式时小时是否总是以两位数字显示。
Note 建议实现使用 Common Locale Data Repository 提供的区域数据(可在
http://cldr.unicode.org/ 获取)。
13.3 Intl.DurationFormat 原型对象的属性
Intl.DurationFormat 原型对象 :
是 %Intl.DurationFormat.prototype% 。
是一个普通对象。
不是 Intl.DurationFormat 实例,不具有 [[InitializedDurationFormat]] 内部槽,也没有 Intl.DurationFormat 实例对象的其他内部槽。
具有 [[Prototype]] 内部槽,其值为 %Object.prototype% 。
13.3.1 Intl.DurationFormat.prototype.constructor
Intl.DurationFormat.prototype.constructor 的初始值为内在对象 %Intl.DurationFormat% 。
13.3.2 Intl.DurationFormat.prototype.resolvedOptions ( )
此函数提供对对象初始化时计算的区域设置和选项的访问。
令 df 为 this 值。 执行 ? RequireInternalSlot (df , [[InitializedDurationFormat]] )。 令 options 为 OrdinaryObjectCreate (%Object.prototype% )。 对 Table 21 的每一行(除表头行),按表顺序,执行: 令 p 为当前行的 Property 值。 令 v 为 df 的内部槽名为当前行 Internal Slot 值的值。 如果 v 不是 undefined ,则 如果当前行有 Conversion 值,令 conversion 为该值;否则令 conversion 为 empty 。 如果 conversion 为 number ,则 设置 v 为 𝔽 (v )。 否则如果 conversion 不是 empty ,则 断言:conversion 为 style+display ,且 v 为 Duration Unit Options Record 。 注:v .[[Style]] 将以属性名 p (复数 Temporal 单位)表示,然后 v .[[Display]] 以属性名 p 后缀 "Display" 表示。 令 style 为 v .[[Style]] 。 如果 style 为 "fractional" ,则 断言:IsFractionalSecondUnitName (p ) 为 true 。 设置 style 为 "numeric" 。 执行 ! CreateDataPropertyOrThrow (options , p , style )。 设置 p 为 p 与 "Display" 的字符串拼接。 设置 v 为 v .[[Display]] 。 执行 ! CreateDataPropertyOrThrow (options , p , v )。 返回 options 。
Table 21: DurationFormat 实例的 resolvedOptions 属性
Internal Slot
Property
Conversion
[[Locale]]
"locale"
[[NumberingSystem]]
"numberingSystem"
[[Style]]
"style"
[[YearsOptions]]
"years"
style+display
[[MonthsOptions]]
"months"
style+display
[[WeeksOptions]]
"weeks"
style+display
[[DaysOptions]]
"days"
style+display
[[HoursOptions]]
"hours"
style+display
[[MinutesOptions]]
"minutes"
style+display
[[SecondsOptions]]
"seconds"
style+display
[[MillisecondsOptions]]
"milliseconds"
style+display
[[MicrosecondsOptions]]
"microseconds"
style+display
[[NanosecondsOptions]]
"nanoseconds"
style+display
[[FractionalDigits]]
"fractionalDigits"
number
13.3.3 Intl.DurationFormat.prototype.format ( duration )
当调用 format 方法并传入参数 duration 时,执行以下步骤:
令 df 为 this 值。 执行 ? RequireInternalSlot (df , [[InitializedDurationFormat]] )。 令 record 为 ? ToDurationRecord (duration )。 令 parts 为 PartitionDurationFormatPattern (df , record )。 令 result 为空字符串。 对每个 Record { [[Type]] , [[Value]] , [[Unit]] } part 属于 parts ,执行 设置 result 为 result 与 part .[[Value]] 的字符串拼接。 返回 result 。
13.3.4 Intl.DurationFormat.prototype.formatToParts ( duration )
当调用 formatToParts 方法并传入参数 duration 时,执行以下步骤:
令 df 为 this 值。 执行 ? RequireInternalSlot (df , [[InitializedDurationFormat]] )。 令 record 为 ? ToDurationRecord (duration )。 令 parts 为 PartitionDurationFormatPattern (df , record )。 令 result 为 ! ArrayCreate (0)。 令 n 为 0。 对每个 Record { [[Type]] , [[Value]] , [[Unit]] } part 属于 parts ,执行 令 obj 为 OrdinaryObjectCreate (%Object.prototype% )。 执行 ! CreateDataPropertyOrThrow (obj , "type" , part .[[Type]] )。 执行 ! CreateDataPropertyOrThrow (obj , "value" , part .[[Value]] )。 如果 part .[[Unit]] 不是 empty ,执行 ! CreateDataPropertyOrThrow (obj , "unit" , part .[[Unit]] )。 执行 ! CreateDataPropertyOrThrow (result , ! ToString (n ), obj )。 设置 n 为 n + 1。 返回 result 。
13.3.5 Intl.DurationFormat.prototype [ %Symbol.toStringTag% ]
%Symbol.toStringTag% 属性的初始值为字符串 "Intl.DurationFormat" 。
该属性具有 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : true } 属性。
13.4 Intl.DurationFormat 实例的属性
Intl.DurationFormat 实例从 %Intl.DurationFormat.prototype% 继承属性。
Intl.DurationFormat 实例具有 [[InitializedDurationFormat]] 内部槽。
Intl.DurationFormat 实例还具有多个内部槽,这些槽由 Intl.DurationFormat 构造函数 计算:
[[Locale]] 是一个字符串值,表示用于格式化的区域设置的语言标签 。
[[NumberingSystem]] 是一个字符串值,表示用于格式化的 Unicode 数字系统标识符 。
[[Style]] 是 "long" 、"short" 、"narrow" 或 "digital" 之一的字符串值,表示使用的持续时间格式化样式。
[[YearsOptions]] 是一个 Duration Unit Options Record ,标识 Duration Record 的 [[Years]] 字段的格式化样式和显示条件。
[[MonthsOptions]] 是一个 Duration Unit Options Record ,标识 Duration Record 的 [[Months]] 字段的格式化样式和显示条件。
[[WeeksOptions]] 是一个 Duration Unit Options Record ,标识 Duration Record 的 [[Weeks]] 字段的格式化样式和显示条件。
[[DaysOptions]] 是一个 Duration Unit Options Record ,标识 Duration Record 的 [[Days]] 字段的格式化样式和显示条件。
[[HoursOptions]] 是一个 Duration Unit Options Record ,标识 Duration Record 的 [[Hours]] 字段的格式化样式和显示条件。
[[MinutesOptions]] 是一个 Duration Unit Options Record ,标识 Duration Record 的 [[Minutes]] 字段的格式化样式和显示条件。
[[SecondsOptions]] 是一个 Duration Unit Options Record ,标识 Duration Record 的 [[Seconds]] 字段的格式化样式和显示条件。
[[MillisecondsOptions]] 是一个 Duration Unit Options Record ,标识 Duration Record 的 [[Milliseconds]] 字段的格式化样式和显示条件。
[[MicrosecondsOptions]] 是一个 Duration Unit Options Record ,标识 Duration Record 的 [[Microseconds]] 字段的格式化样式和显示条件。
[[NanosecondsOptions]] 是一个 Duration Unit Options Record ,标识 Duration Record 的 [[Nanoseconds]] 字段的格式化样式和显示条件。
[[HourMinuteSeparator]] 是一个字符串值,表示当小时和分钟都显示且都使用数字样式格式化时,小时与分钟之间使用的分隔符。
[[MinuteSecondSeparator]] 是一个字符串值,表示当分钟和秒都显示且都使用数字样式格式化时,分钟与秒之间使用的分隔符。
[[FractionalDigits]] 是 undefined 或非负整数,表示数字样式下使用的小数位数。
13.5 DurationFormat 对象的抽象操作
13.5.1 持续时间记录
持续时间记录 是用于表示持续时间的 Record 值。
持续时间记录具有 Table 22 中列出的字段。
Table 22: 持续时间记录 字段
字段
含义
[[Years]]
持续时间中的年数。
[[Months]]
持续时间中的月数。
[[Weeks]]
持续时间中的周数。
[[Days]]
持续时间中的天数。
[[Hours]]
持续时间中的小时数。
[[Minutes]]
持续时间中的分钟数。
[[Seconds]]
持续时间中的秒数。
[[Milliseconds]]
持续时间中的毫秒数。
[[Microseconds]]
持续时间中的微秒数。
[[Nanoseconds]]
持续时间中的纳秒数。
13.5.2 ToIntegerIfIntegral ( argument )
The abstract operation ToIntegerIfIntegral takes argument argument (一个 ECMAScript 语言值) and returns 返回一个整数的正常完成,或抛出完成. It performs the following steps when called:
令 number 为 ? ToNumber (argument )。 如果 number 不是整数 Number,则抛出 RangeError 异常。 返回 ℝ (number )。
13.5.3 ToDurationRecord ( input )
The abstract operation ToDurationRecord takes argument input (一个 ECMAScript 语言值) and returns 返回一个持续时间记录 的正常完成,或抛出完成. It performs the following steps when called:
如果 input 不是对象,则 如果 input 是字符串,抛出 RangeError 异常。 抛出 TypeError 异常。 令 result 为一个新持续时间记录 ,每个字段初始值为 0。 令 days 为 ? Get (input , "days" )。 如果 days 不是 undefined ,则设置 result .[[Days]] 为 ? ToIntegerIfIntegral (days )。 令 hours 为 ? Get (input , "hours" )。 如果 hours 不是 undefined ,则设置 result .[[Hours]] 为 ? ToIntegerIfIntegral (hours )。 令 microseconds 为 ? Get (input , "microseconds" )。 如果 microseconds 不是 undefined ,则设置 result .[[Microseconds]] 为 ? ToIntegerIfIntegral (microseconds )。 令 milliseconds 为 ? Get (input , "milliseconds" )。 如果 milliseconds 不是 undefined ,则设置 result .[[Milliseconds]] 为 ? ToIntegerIfIntegral (milliseconds )。 令 minutes 为 ? Get (input , "minutes" )。 如果 minutes 不是 undefined ,则设置 result .[[Minutes]] 为 ? ToIntegerIfIntegral (minutes )。 令 months 为 ? Get (input , "months" )。 如果 months 不是 undefined ,则设置 result .[[Months]] 为 ? ToIntegerIfIntegral (months )。 令 nanoseconds 为 ? Get (input , "nanoseconds" )。 如果 nanoseconds 不是 undefined ,则设置 result .[[Nanoseconds]] 为 ? ToIntegerIfIntegral (nanoseconds )。 令 seconds 为 ? Get (input , "seconds" )。 如果 seconds 不是 undefined ,则设置 result .[[Seconds]] 为 ? ToIntegerIfIntegral (seconds )。 令 weeks 为 ? Get (input , "weeks" )。 如果 weeks 不是 undefined ,则设置 result .[[Weeks]] 为 ? ToIntegerIfIntegral (weeks )。 令 years 为 ? Get (input , "years" )。 如果 years 不是 undefined ,则设置 result .[[Years]] 为 ? ToIntegerIfIntegral (years )。 如果 years 、months 、weeks 、days 、hours 、minutes 、seconds 、milliseconds 、microseconds 和 nanoseconds
都是 undefined ,抛出 TypeError 异常。 如果 IsValidDuration ( result .[[Years]] , result .[[Months]] , result .[[Weeks]] , result .[[Days]] ,
result .[[Hours]] , result .[[Minutes]] , result .[[Seconds]] , result .[[Milliseconds]] ,
result .[[Microseconds]] , result .[[Nanoseconds]) 为 false ,则 抛出 RangeError 异常。 返回 result 。
13.5.4 DurationSign ( duration )
The abstract operation DurationSign takes argument duration (一个持续时间记录 ) and returns -1, 0 或 1. It performs the following steps when called:
对于 « duration .[[Years]] , duration .[[Months]] , duration .[[Weeks]] , duration .[[Days]] ,
duration .[[Hours]] , duration .[[Minutes]] , duration .[[Seconds]] , duration .[[Milliseconds]] ,
duration .[[Microseconds]] , duration .[[Nanoseconds]] » 中的每个值 v ,执行 如果 v < 0,返回 -1。 如果 v > 0,返回 1。 返回 0。
13.5.5 IsValidDuration ( years , months , weeks , days , hours , minutes , seconds , milliseconds , microseconds , nanoseconds )
The abstract operation IsValidDuration takes arguments years (一个整数), months (一个整数), weeks (一个整数), days (一个整数), hours (一个整数), minutes (一个整数), seconds (一个整数), milliseconds (一个整数), microseconds (一个整数), and nanoseconds (一个整数) and returns 一个布尔值. It performs the following steps when called:
令 sign 为 0。 对于 « years , months , weeks , days , hours , minutes , seconds , milliseconds , microseconds ,
nanoseconds » 中的每个值 v ,执行 如果 𝔽 (v ) 不是有限值,返回 false 。 如果 v < 0,则 如果 sign > 0,返回 false 。 设置 sign 为 -1。 否则如果 v > 0,则 如果 sign < 0,返回 false 。 设置 sign 为 1。 如果 abs (years ) ≥ 232 ,返回 false 。 如果 abs (months ) ≥ 232 ,返回 false 。 如果 abs (weeks ) ≥ 232 ,返回 false 。 令 normalizedSeconds 为 days × 86,400 + hours × 3600 + minutes × 60 + seconds + ℝ (𝔽 (milliseconds ))
× 10-3 + ℝ (𝔽 (microseconds )) × 10-6 + ℝ (𝔽 (nanoseconds )) × 10-9 。 注:上述步骤不能直接用浮点运算实现。分别乘以 10-3 、10-6 和 10-9 时,milliseconds 、microseconds 或
nanoseconds 是不安全整数时可能不精确。可以用 C++ 的 std::remquo() 实现,或用字符串操作获得精确结果,因为乘的是 10 的幂。 如果 abs (normalizedSeconds ) ≥ 253 ,返回 false 。 返回 true 。
13.5.6 GetDurationUnitOptions ( unit , options , baseStyle , stylesList , digitalBase , prevStyle , twoDigitHours )
The abstract operation GetDurationUnitOptions takes arguments unit (一个字符串), options (一个对象), baseStyle (一个字符串), stylesList (字符串列表), digitalBase (一个字符串), prevStyle (一个字符串), and twoDigitHours (一个布尔值) and returns 返回一个持续时间单位选项记录 的正常完成,或抛出完成. It performs the following steps when called:
令 style 为 ? GetOption (options , unit , string , stylesList , undefined )。 令 displayDefault 为 "always" 。 如果 style 是 undefined ,则 如果 baseStyle 是 "digital" ,则 设置 style 为 digitalBase 。 如果 unit 不是 "hours" 、"minutes" 或 "seconds" 之一,则设置 displayDefault 为 "auto" 。 否则如果 prevStyle 是 "fractional" 、"numeric" 或 "2-digit" 之一,则 设置 style 为 "numeric" 。 如果 unit 不是 "minutes" 或 "seconds" ,则设置 displayDefault 为 "auto" 。 否则, 设置 style 为 baseStyle 。 设置 displayDefault 为 "auto" 。 如果 style 是 "numeric" 且 IsFractionalSecondUnitName (unit ) 为 true ,则 设置 style 为 "fractional" 。 设置 displayDefault 为 "auto" 。 令 displayField 为 unit 与 "Display" 的字符串拼接。 令 display 为 ? GetOption (options , displayField , string , « "auto" , "always" », displayDefault )。 执行 ? ValidateDurationUnitStyle (unit , style , display , prevStyle )。 如果 unit 是 "hours" 且 twoDigitHours 为 true ,设置 style 为 "2-digit" 。 如果 unit 是 "minutes" 或 "seconds" 且 prevStyle 是 "numeric" 或 "2-digit" ,设置 style 为 "2-digit" 。 返回持续时间单位选项记录 { [[Style]] : style , [[Display]] : display }。
13.5.6.1 持续时间单位选项记录
每个 持续时间单位选项记录 都具有 Table 23 中定义的字段。
Table 23: 持续时间单位选项记录
字段名
值类型
[[Style]]
Table 20 的 Styles 列中的字符串
[[Display]]
"auto" 或 "always"
13.5.6.2 ValidateDurationUnitStyle ( unit , style , display , prevStyle )
The abstract operation ValidateDurationUnitStyle takes arguments unit (一个字符串), style (一个字符串), display (一个字符串), and prevStyle (一个字符串) and returns 返回 unused 的正常完成,或抛出完成. It performs the following steps when called:
如果 display 是 "always" 且 style 是 "fractional" ,抛出 RangeError 异常。 如果 prevStyle 是 "fractional" 且 style 不是 "fractional" ,抛出 RangeError 异常。 如果 prevStyle 是 "numeric" 或 "2-digit" 且 style 不是 "fractional" 、"numeric" 或 "2-digit" 之一,抛出
RangeError 异常。 返回 unused 。
Note unit 在上述算法中未被引用,但建议实现用于构造抛出异常的消息。
13.5.7 ComputeFractionalDigits ( durationFormat , duration )
The abstract operation ComputeFractionalDigits takes arguments durationFormat (一个 DurationFormat 对象) and duration (一个持续时间记录 ) and returns 一个数学值. It performs the following steps when called:
令 result 为 0。 令 exponent 为 3。 对 Table 24 的每一行(除表头行),按表顺序,执行 令 unitOptions 为 durationFormat 的内部槽名为当前行 Internal Slot 值的值。 如果 unitOptions .[[Style]] 为 "fractional" ,则 令 unit 为当前行的 Unit 值。 断言:IsFractionalSecondUnitName (unit ) 为 true 。 令 value 为 duration 的字段名为当前行 Value Field 值的值。 设置 result 为 result + (value / 10exponent )。 设置 exponent 为 exponent + 3。 返回 result 。
13.5.8 NextUnitFractional ( durationFormat , unit )
The abstract operation NextUnitFractional takes arguments durationFormat (一个 DurationFormat 对象) and unit (一个字符串) and returns 一个布尔值. It performs the following steps when called:
如果 unit 为 "seconds" 且 durationFormat .[[MillisecondsOptions]] .[[Style]] 为 "fractional" ,返回 true 。 如果 unit 为 "milliseconds" 且 durationFormat .[[MicrosecondsOptions]] .[[Style]] 为 "fractional" ,返回 true 。 如果 unit 为 "microseconds" 且 durationFormat .[[NanosecondsOptions]] .[[Style]] 为 "fractional" ,返回 true 。 返回 false 。
13.5.9 FormatNumericHours ( durationFormat , hoursValue , signDisplayed )
The abstract operation FormatNumericHours takes arguments durationFormat (一个 DurationFormat 对象), hoursValue (一个整数), and signDisplayed (一个布尔值) and returns 一个记录列表. It performs the following steps when called:
令 result 为一个新空列表。 令 hoursStyle 为 durationFormat .[[HoursOptions]] .[[Style]] 。 断言:hoursStyle 为 "numeric" 或 "2-digit" 。 令 nfOpts 为 OrdinaryObjectCreate (null )。 令 numberingSystem 为 durationFormat .[[NumberingSystem]] 。 执行 ! CreateDataPropertyOrThrow (nfOpts , "numberingSystem" , numberingSystem )。 如果 hoursStyle 为 "2-digit" ,则 执行 ! CreateDataPropertyOrThrow (nfOpts , "minimumIntegerDigits" , 2 𝔽 )。 如果 signDisplayed 为 false ,则 执行 ! CreateDataPropertyOrThrow (nfOpts , "signDisplay" , "never" )。 执行 ! CreateDataPropertyOrThrow (nfOpts , "useGrouping" , false )。 令 nf 为 ! Construct (%Intl.NumberFormat% , « durationFormat .[[Locale]] , nfOpts »)。 令 hoursParts 为 PartitionNumberPattern (nf , hoursValue )。 对每个记录 { [[Type]] , [[Value]] } part 属于 hoursParts ,执行 将记录 { [[Type]] : part .[[Type]] , [[Value]] : part .[[Value]] , [[Unit]] : "hour" } 添加到 result 。 返回 result 。
13.5.10 FormatNumericMinutes ( durationFormat , minutesValue , hoursDisplayed , signDisplayed )
The abstract operation FormatNumericMinutes takes arguments durationFormat (一个 DurationFormat 对象), minutesValue (一个整数), hoursDisplayed (一个布尔值), and signDisplayed (一个布尔值) and returns 一个记录列表. It performs the following steps when called:
令 result 为一个新空列表。 如果 hoursDisplayed 为 true ,则 令 separator 为 durationFormat .[[HourMinuteSeparator]] 。 将记录 { [[Type]] : "literal" , [[Value]] : separator , [[Unit]] : empty } 添加到 result 。 令 minutesStyle 为 durationFormat .[[MinutesOptions]] .[[Style]] 。 断言:minutesStyle 为 "numeric" 或 "2-digit" 。 令 nfOpts 为 OrdinaryObjectCreate (null )。 令 numberingSystem 为 durationFormat .[[NumberingSystem]] 。 执行 ! CreateDataPropertyOrThrow (nfOpts , "numberingSystem" , numberingSystem )。 如果 minutesStyle 为 "2-digit" ,则 执行 ! CreateDataPropertyOrThrow (nfOpts , "minimumIntegerDigits" , 2 𝔽 )。 如果 signDisplayed 为 false ,则 执行 ! CreateDataPropertyOrThrow (nfOpts , "signDisplay" , "never" )。 执行 ! CreateDataPropertyOrThrow (nfOpts , "useGrouping" , false )。 令 nf 为 ! Construct (%Intl.NumberFormat% , « durationFormat .[[Locale]] , nfOpts »)。 令 minutesParts 为 PartitionNumberPattern (nf , minutesValue )。 对每个记录 { [[Type]] , [[Value]] } part 属于 minutesParts ,执行 将记录 { [[Type]] : part .[[Type]] , [[Value]] : part .[[Value]] , [[Unit]] : "minute" } 添加到 result 。 返回 result 。
13.5.11 FormatNumericSeconds ( durationFormat , secondsValue , minutesDisplayed , signDisplayed )
The abstract operation FormatNumericSeconds takes arguments durationFormat (一个 DurationFormat 对象), secondsValue (一个数学值), minutesDisplayed (一个布尔值), and signDisplayed (一个布尔值) and returns 一个记录列表. It performs the following steps when called:
令 result 为一个新空列表。 如果 minutesDisplayed 为 true ,则 令 separator 为 durationFormat .[[MinuteSecondSeparator]] 。 将记录 { [[Type]] : "literal" , [[Value]] : separator , [[Unit]] : empty } 添加到 result 。 令 secondsStyle 为 durationFormat .[[SecondsOptions]] .[[Style]] 。 断言:secondsStyle 为 "numeric" 或 "2-digit" 。 令 nfOpts 为 OrdinaryObjectCreate (null )。 令 numberingSystem 为 durationFormat .[[NumberingSystem]] 。 执行 ! CreateDataPropertyOrThrow (nfOpts , "numberingSystem" , numberingSystem )。 如果 secondsStyle 为 "2-digit" ,则 执行 ! CreateDataPropertyOrThrow (nfOpts , "minimumIntegerDigits" , 2 𝔽 )。 如果 signDisplayed 为 false ,则 执行 ! CreateDataPropertyOrThrow (nfOpts , "signDisplay" , "never" )。 执行 ! CreateDataPropertyOrThrow (nfOpts , "useGrouping" , false )。 令 fractionDigits 为 durationFormat .[[FractionalDigits]] 。 如果 fractionDigits 为 undefined ,则 执行 ! CreateDataPropertyOrThrow (nfOpts , "maximumFractionDigits" , 9 𝔽 )。 执行 ! CreateDataPropertyOrThrow (nfOpts , "minimumFractionDigits" , +0 𝔽 )。 否则, 执行 ! CreateDataPropertyOrThrow (nfOpts , "maximumFractionDigits" , fractionDigits )。 执行 ! CreateDataPropertyOrThrow (nfOpts , "minimumFractionDigits" , fractionDigits )。 执行 ! CreateDataPropertyOrThrow (nfOpts , "roundingMode" , "trunc" )。 令 nf 为 ! Construct (%Intl.NumberFormat% , « durationFormat .[[Locale]] , nfOpts »)。 令 secondsParts 为 PartitionNumberPattern (nf , secondsValue )。 对每个记录 { [[Type]] , [[Value]] } part 属于 secondsParts ,执行 将记录 { [[Type]] : part .[[Type]] , [[Value]] : part .[[Value]] , [[Unit]] : "second" } 添加到 result 。 返回 result 。
13.5.12 FormatNumericUnits ( durationFormat , duration , firstNumericUnit , signDisplayed )
The abstract operation FormatNumericUnits takes arguments durationFormat (一个 DurationFormat 对象), duration (一个持续时间记录 ), firstNumericUnit (一个字符串), and signDisplayed (一个布尔值) and returns 一个记录列表. It performs the following steps when called:
断言:firstNumericUnit 是 "hours" 、"minutes" 或 "seconds" 。 令 numericPartsList 为一个新空列表。 令 hoursValue 为 duration .[[Hours]] 。 令 hoursDisplay 为 durationFormat .[[HoursOptions]] .[[Display]] 。 令 minutesValue 为 duration .[[Minutes]] 。 令 minutesDisplay 为 durationFormat .[[MinutesOptions]] .[[Display]] 。 令 secondsValue 为 duration .[[Seconds]] 。 如果 duration .[[Milliseconds]] 不为 0 或 duration .[[Microseconds]] 不为 0 或 duration .[[Nanoseconds]] 不为 0,则 设置 secondsValue 为 secondsValue + ComputeFractionalDigits (durationFormat , duration )。 令 secondsDisplay 为 durationFormat .[[SecondsOptions]] .[[Display]] 。 令 hoursFormatted 为 false 。 如果 firstNumericUnit 是 "hours" ,则 如果 hoursValue 不为 0 或 hoursDisplay 是 "always" ,则 设置 hoursFormatted 为 true 。 如果 secondsValue 不为 0 或 secondsDisplay 是 "always" ,则 令 secondsFormatted 为 true 。 否则, 令 secondsFormatted 为 false 。 令 minutesFormatted 为 false 。 如果 firstNumericUnit 是 "hours" 或 firstNumericUnit 是 "minutes" ,则 如果 hoursFormatted 为 true 且 secondsFormatted 为 true ,则 设置 minutesFormatted 为 true 。 否则如果 minutesValue 不为 0 或 minutesDisplay 是 "always" ,则 设置 minutesFormatted 为 true 。 如果 hoursFormatted 为 true ,则 如果 signDisplayed 为 true ,则 如果 hoursValue 为 0 且 DurationSign (duration ) 为 -1,则 设置 hoursValue 为 negative-zero 。 令 hoursParts 为 FormatNumericHours (durationFormat , hoursValue , signDisplayed )。 设置 numericPartsList 为 numericPartsList 与 hoursParts 的列表拼接。 设置 signDisplayed 为 false 。 如果 minutesFormatted 为 true ,则 如果 signDisplayed 为 true ,则 如果 minutesValue 为 0 且 DurationSign (duration ) 为 -1,则 设置 minutesValue 为 negative-zero 。 令 minutesParts 为 FormatNumericMinutes (durationFormat , minutesValue , hoursFormatted , signDisplayed )。 设置 numericPartsList 为 numericPartsList 与 minutesParts 的列表拼接。 设置 signDisplayed 为 false 。 如果 secondsFormatted 为 true ,则 令 secondsParts 为 FormatNumericSeconds (durationFormat , secondsValue , minutesFormatted ,
signDisplayed )。 设置 numericPartsList 为 numericPartsList 与 secondsParts 的列表拼接。 返回 numericPartsList 。
13.5.13 IsFractionalSecondUnitName ( unit )
The abstract operation IsFractionalSecondUnitName takes argument unit (一个字符串) and returns 一个布尔值. It performs the following steps when called:
如果 unit 是 "milliseconds" 、"microseconds" 或 "nanoseconds" 之一,返回 true 。 返回 false 。
13.5.14 ListFormatParts ( durationFormat , partitionedPartsList )
The abstract operation ListFormatParts takes arguments durationFormat (一个 DurationFormat 对象) and partitionedPartsList (一个记录列表的列表) and returns 一个列表. It performs the following steps when called:
令 lfOpts 为 OrdinaryObjectCreate (null )。 执行 ! CreateDataPropertyOrThrow (lfOpts , "type" , "unit" )。 令 listStyle 为 durationFormat .[[Style]] 。 如果 listStyle 是 "digital" ,则 设置 listStyle 为 "short" 。 执行 ! CreateDataPropertyOrThrow (lfOpts , "style" , listStyle )。 令 lf 为 ! Construct (%Intl.ListFormat% , « durationFormat .[[Locale]] , lfOpts »)。 令 strings 为一个新空列表。 对 partitionedPartsList 中的每个元素 parts ,执行 令 string 为空字符串。 对 parts 中的每个记录 { [[Type]] , [[Value]] , [[Unit]] } part ,执行 设置 string 为 string 与 part .[[Value]] 的字符串拼接。 将 string 添加到 strings 。 令 formattedPartsList 为 CreatePartsFromList (lf , strings )。 令 partitionedPartsIndex 为 0。 令 partitionedLength 为 partitionedPartsList 的元素数量。 令 flattenedPartsList 为一个新空列表。 对 formattedPartsList 中的每个记录 { [[Type]] , [[Value]] } listPart ,执行 如果 listPart .[[Type]] 是 "element" ,则 断言:partitionedPartsIndex < partitionedLength 。 令 parts 为 partitionedPartsList [partitionedPartsIndex ]。 对 parts 中的每个记录 { [[Type]] , [[Value]] , [[Unit]] } part ,执行 将 part 添加到 flattenedPartsList 。 设置 partitionedPartsIndex 为 partitionedPartsIndex + 1。 否则, 断言:listPart .[[Type]] 是 "literal" 。 将记录 { [[Type]] : "literal" , [[Value]] : listPart .[[Value]] , [[Unit]] : empty } 添加到 flattenedPartsList 。 返回 flattenedPartsList 。
13.5.15 PartitionDurationFormatPattern ( durationFormat , duration )
The abstract operation PartitionDurationFormatPattern takes arguments durationFormat (一个 DurationFormat) and duration (一个持续时间记录 ) and returns 一个列表. It performs the following steps when called:
令 result 为一个新空列表。 令 signDisplayed 为 true 。 令 numericUnitFound 为 false 。 当 numericUnitFound 为 false 时,按表顺序遍历 Table 24
的每一行(除表头行): 令 value 为 duration 的字段名为当前行 Value Field 值的值。 令 unitOptions 为 durationFormat 的内部槽名为当前行 Internal Slot 值的值。 令 style 为 unitOptions .[[Style]] 。 令 display 为 unitOptions .[[Display]] 。 令 unit 为当前行的 Unit 值。 令 numberFormatUnit 为当前行的 NumberFormat Unit 值。 如果 style 是 "numeric" 或 "2-digit" ,则 令 numericPartsList 为 FormatNumericUnits (durationFormat , duration , unit , signDisplayed )。 如果 numericPartsList 非空,则将 numericPartsList 添加到 result 。 设置 numericUnitFound 为 true 。 否则, 令 nfOpts 为 OrdinaryObjectCreate (null )。 如果 NextUnitFractional (durationFormat , unit ) 为 true ,则 设置 value 为 value + ComputeFractionalDigits (durationFormat , duration )。 令 fractionDigits 为 durationFormat .[[FractionalDigits]] 。 如果 fractionDigits 为 undefined ,则 执行 ! CreateDataPropertyOrThrow (nfOpts , "maximumFractionDigits" , 9 𝔽 )。 执行 ! CreateDataPropertyOrThrow (nfOpts , "minimumFractionDigits" , +0 𝔽 )。 否则, 执行 ! CreateDataPropertyOrThrow (nfOpts , "maximumFractionDigits" , fractionDigits )。 执行 ! CreateDataPropertyOrThrow (nfOpts , "minimumFractionDigits" , fractionDigits )。 执行 ! CreateDataPropertyOrThrow (nfOpts , "roundingMode" , "trunc" )。 设置 numericUnitFound 为 true 。 如果 display 是 "always" 或 value 不为 0,则 执行 ! CreateDataPropertyOrThrow (nfOpts , "numberingSystem" , durationFormat .[[NumberingSystem]] )。 如果 signDisplayed 为 true ,则 设置 signDisplayed 为 false 。 如果 value 为 0 且 DurationSign (duration ) 为 -1,则设置 value 为 negative-zero 。 否则, 执行 ! CreateDataPropertyOrThrow (nfOpts , "signDisplay" , "never" )。 执行 ! CreateDataPropertyOrThrow (nfOpts , "style" , "unit" )。 执行 ! CreateDataPropertyOrThrow (nfOpts , "unit" , numberFormatUnit )。 执行 ! CreateDataPropertyOrThrow (nfOpts , "unitDisplay" , style )。 令 nf 为 ! Construct (%Intl.NumberFormat% , « durationFormat .[[Locale]] , nfOpts »)。 令 parts 为 PartitionNumberPattern (nf , value )。 令 list 为一个新空列表。 对 parts 中的每个记录 { [[Type]] , [[Value]] } part ,执行 将记录 { [[Type]] : part .[[Type]] , [[Value]] : part .[[Value]] , [[Unit]] : numberFormatUnit } 添加到 list 。 将 list 添加到 result 。 返回 ListFormatParts (durationFormat , result )。
Table 24: PartitionDurationFormatPattern 相关的 DurationFormat 实例内部槽和属性
Value Field
Internal Slot
Unit
NumberFormat Unit
[[Years]]
[[YearsOptions]]
"years"
"year"
[[Months]]
[[MonthsOptions]]
"months"
"month"
[[Weeks]]
[[WeeksOptions]]
"weeks"
"week"
[[Days]]
[[DaysOptions]]
"days"
"day"
[[Hours]]
[[HoursOptions]]
"hours"
"hour"
[[Minutes]]
[[MinutesOptions]]
"minutes"
"minute"
[[Seconds]]
[[SecondsOptions]]
"seconds"
"second"
[[Milliseconds]]
[[MillisecondsOptions]]
"milliseconds"
"millisecond"
[[Microseconds]]
[[MicrosecondsOptions]]
"microseconds"
"microsecond"
[[Nanoseconds]]
[[NanosecondsOptions]]
"nanoseconds"
"nanosecond"
14 ListFormat 对象
14.1 Intl.ListFormat 构造函数
Intl.ListFormat 构造函数:
是 %Intl.ListFormat% 。
是 Intl 对象 的 "ListFormat" 属性的初始值。
Intl 对象 所有服务构造属性的共同行为在 9.1 中指定。
14.1.1 Intl.ListFormat ( [ locales [ , options ] ] )
当 Intl.ListFormat 函数以可选参数 locales 和 options 调用时,执行以下步骤:
如果 NewTarget 是 undefined ,抛出 TypeError 异常。 令 listFormat 为 ? OrdinaryCreateFromConstructor (NewTarget, "%Intl.ListFormat.prototype%" , «
[[InitializedListFormat]] , [[Locale]] , [[Type]] , [[Style]] , [[Templates]] »)。 令 optionsResolution 为 ? ResolveOptions (%Intl.ListFormat% , %Intl.ListFormat% .[[LocaleData]] , locales ,
options )。 将 options 设置为 optionsResolution .[[Options]] 。 令 r 为 optionsResolution .[[ResolvedLocale]] 。 将 listFormat .[[Locale]] 设置为 r .[[Locale]] 。 令 type 为 ? GetOption (options , "type" , string , « "conjunction" , "disjunction" , "unit" »,
"conjunction" )。 将 listFormat .[[Type]] 设置为 type 。 令 style 为 ? GetOption (options , "style" , string , « "long" , "short" , "narrow" », "long" )。 将 listFormat .[[Style]] 设置为 style 。 令 resolvedLocaleData 为 r .[[LocaleData]] 。 令 dataLocaleTypes 为 resolvedLocaleData .[[<type >]]。 将 listFormat .[[Templates]] 设置为 dataLocaleTypes .[[<style >]]。 返回 listFormat 。
14.2 Intl.ListFormat 构造函数的属性
Intl.ListFormat 构造函数:
14.2.1 Intl.ListFormat.prototype
Intl.ListFormat.prototype 的值为 %Intl.ListFormat.prototype% 。
该属性具有 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : false } 属性。
14.2.2 Intl.ListFormat.supportedLocalesOf ( locales [ , options ] )
当 supportedLocalesOf 方法以参数 locales 和 options 调用时,执行以下步骤:
令 availableLocales 为 %Intl.ListFormat% .[[AvailableLocales]] 。 令 requestedLocales 为 ? CanonicalizeLocaleList (locales )。 返回 ? FilterLocales (availableLocales , requestedLocales , options )。
14.2.3 内部槽
[[AvailableLocales]] 内部槽的值在 9.1 描述的约束范围内由实现定义。
[[RelevantExtensionKeys]] 内部槽的值为 « »。
[[ResolutionOptionDescriptors]] 内部槽的值为 « »。
Note 1
Intl.ListFormat 没有任何相关扩展键。
[[LocaleData]] 内部槽的值在 9.1 描述的约束范围内由实现定义,并且对于
%Intl.ListFormat% .[[AvailableLocales]] 中的每个 locale 值 locale ,还需满足以下额外约束:
[[LocaleData]] .[[<locale >]] 是一个包含三个字段 [[conjunction]] 、[[disjunction]] 和 [[unit]] 的 Record 。每个字段都是一个
Record ,必须包含三种格式化样式名称的字段:[[long]] 、[[short]] 和 [[narrow]] 。
每个字段被视为 ListFormat 模板集 ,必须是一个包含字段 [[Pair]] 、[[Start]] 、[[Middle]] 和 [[End]] 的 Record 列表。每个字段必须是 LDML
List Format Rules 指定的模板字符串。每个模板字符串必须恰好包含 "{0}" 和 "{1}" 两个子串。子串 "{0}" 应出现在 "{1}" 之前。
Note 2
推荐实现使用 Common Locale Data Repository 提供的本地数据(可在
https://cldr.unicode.org/ 获取)。在
LDML 的 listPattern 中,
conjunction
对应 "standard",
disjunction 对应 "or",
unit 对应 "unit"。
Note 3
在列表类型中,conjunction 表示基于 "和" 的列表(如 "A, B, 和 C"),disjunction 表示基于 "或" 的列表(如 "A, B, 或 C"),unit 表示带单位的值列表(如
"5磅, 12盎司")。
14.3 Intl.ListFormat 原型对象的属性
Intl.ListFormat 原型对象 :
是 %Intl.ListFormat.prototype% 。
是一个普通对象。
不是 Intl.ListFormat 实例,不具有 [[InitializedListFormat]] 内部槽或任何 Intl.ListFormat 实例对象的其他内部槽。
具有 [[Prototype]] 内部槽,其值为 %Object.prototype% 。
14.3.1 Intl.ListFormat.prototype.constructor
Intl.ListFormat.prototype.constructor 的初始值为 %Intl.ListFormat% 。
14.3.2 Intl.ListFormat.prototype.resolvedOptions ( )
此函数提供对对象初始化期间计算的 locale 和 options 的访问。
令 lf 为 this 值。 执行 ? RequireInternalSlot (lf , [[InitializedListFormat]] )。 令 options 为 OrdinaryObjectCreate (%Object.prototype% )。 对 Table 25 的每一行(除表头行外),按表顺序执行 令 p 为当前行的 Property 值。 令 v 为 lf 的内部槽,其名称为当前行的 Internal Slot 值。 断言:v 不为 undefined 。 执行 ! CreateDataPropertyOrThrow (options , p , v )。 返回 options 。
Table 25: ListFormat 实例的 Resolved Options
Internal Slot
Property
[[Locale]]
"locale"
[[Type]]
"type"
[[Style]]
"style"
14.3.3 Intl.ListFormat.prototype.format ( list )
当 format 方法以参数 list 调用时,执行以下步骤:
令 lf 为 this 值。 执行 ? RequireInternalSlot (lf , [[InitializedListFormat]] )。 令 stringList 为 ? StringListFromIterable (list )。 返回 FormatList (lf , stringList )。
14.3.4 Intl.ListFormat.prototype.formatToParts ( list )
当 formatToParts 方法以参数 list 调用时,执行以下步骤:
令 lf 为 this 值。 执行 ? RequireInternalSlot (lf , [[InitializedListFormat]] )。 令 stringList 为 ? StringListFromIterable (list )。 返回 FormatListToParts (lf , stringList )。
14.3.5 Intl.ListFormat.prototype [ %Symbol.toStringTag% ]
%Symbol.toStringTag% 属性的初始值为字符串 "Intl.ListFormat" 。
该属性具有 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : true } 属性。
14.4 Intl.ListFormat 实例的属性
Intl.ListFormat 实例从 %Intl.ListFormat.prototype% 继承属性。
Intl.ListFormat 实例具有 [[InitializedListFormat]] 内部槽。
Intl.ListFormat 实例还具有由 Intl.ListFormat 构造函数 计算的多个内部槽:
[[Locale]] 是一个字符串值,表示用于列表格式样式的本地化语言标签 。
[[Type]] 是字符串值 "conjunction" 、"disjunction" 或 "unit" 之一,标识所用的列表类型。
[[Style]] 是字符串值 "long" 、"short" 或 "narrow" 之一,标识所用的列表格式样式。
[[Templates]] 是一个 ListFormat 模板集 。
14.5 ListFormat 对象的抽象操作
14.5.1 DeconstructPattern ( pattern , placeables )
The abstract operation DeconstructPattern takes arguments pattern (一个模式字符串 ) and placeables (一个 Record ) and returns 一个列表. It performs the following steps when called:
令 patternParts 为 PartitionPattern (pattern )。 令 result 为一个新的空列表。 对每个 Record { [[Type]] , [[Value]] } patternPart 属于 patternParts ,执行 令 part 为 patternPart .[[Type]] 。 如果 part 是 "literal" ,则 将 Record { [[Type]] : "literal" , [[Value]] : patternPart .[[Value]] } 添加到 result 。 否则, 断言:placeables 有字段 [[<part >]]。 令 subst 为 placeables .[[<part >]]。 如果 subst 是一个列表,则 对每个元素 s 属于 subst ,执行 将 s 添加到 result 。 否则, 将 subst 添加到 result 。 返回 result 。
14.5.2 CreatePartsFromList ( listFormat , list )
The abstract operation CreatePartsFromList takes arguments listFormat (一个 Intl.ListFormat) and list (一个字符串列表) and returns 一个字段为 [[Type]] ("element" 或 "literal" )和 [[Value]] (字符串)的 Record 列表. It performs the following steps when called:
令 size 为 list 元素数量。 如果 size 为 0,则 返回一个新的空列表。 如果 size 为 2,则 令 n 为根据 listFormat .[[Locale]] 、list [0] 和 list [1] 实现定义的 listFormat .[[Templates]] 索引。 令 pattern 为 listFormat .[[Templates]] [n ].[[Pair]] 。 令 first 为 Record { [[Type]] : "element" , [[Value]] : list [0] }。 令 second 为 Record { [[Type]] : "element" , [[Value]] : list [1] }。 令 placeables 为 Record { [[0]] : first , [[1]] : second }。 返回 DeconstructPattern (pattern , placeables )。 令 last 为 Record { [[Type]] : "element" , [[Value]] : list [size - 1] }。 令 parts 为 « last »。 令 i 为 size - 2。 重复,条件为 i ≥ 0, 令 head 为 Record { [[Type]] : "element" , [[Value]] : list [i ] }。 令 n 为根据 listFormat .[[Locale]] 、head 和 parts 实现定义的 listFormat .[[Templates]] 索引。 如果 i 为 0,则 令 pattern 为 listFormat .[[Templates]] [n ].[[Start]] 。 否则如果 i 小于 size - 2,则 令 pattern 为 listFormat .[[Templates]] [n ].[[Middle]] 。 否则, 令 pattern 为 listFormat .[[Templates]] [n ].[[End]] 。 令 placeables 为 Record { [[0]] : head , [[1]] : parts }。 将 parts 设置为 DeconstructPattern (pattern , placeables )。 i 减 1。返回 parts 。
Note
选择多个模板的索引 n 允许并列词根据上下文变化,例如西班牙语中,"y" 或 "e" 的选择取决于后续单词。
14.5.3 FormatList ( listFormat , list )
The abstract operation FormatList takes arguments listFormat (一个 Intl.ListFormat) and list (一个字符串列表) and returns 一个字符串. It performs the following steps when called:
令 parts 为 CreatePartsFromList (listFormat , list )。 令 result 为空字符串。 对每个 Record { [[Type]] , [[Value]] } part 属于 parts ,执行 将 result 设置为 result 与 part .[[Value]] 的字符串拼接。 返回 result 。
14.5.4 FormatListToParts ( listFormat , list )
The abstract operation FormatListToParts takes arguments listFormat (一个 Intl.ListFormat) and list (一个字符串列表) and returns 一个数组. It performs the following steps when called:
令 parts 为 CreatePartsFromList (listFormat , list )。 令 result 为 ! ArrayCreate (0)。 令 n 为 0。 对每个 Record { [[Type]] , [[Value]] } part 属于 parts ,执行 令 O 为 OrdinaryObjectCreate (%Object.prototype% )。 执行 ! CreateDataPropertyOrThrow (O , "type" , part .[[Type]] )。 执行 ! CreateDataPropertyOrThrow (O , "value" , part .[[Value]] )。 执行 ! CreateDataPropertyOrThrow (result , ! ToString (𝔽 (n )), O )。 n 加 1。返回 result 。
14.5.5 StringListFromIterable ( iterable )
The abstract operation StringListFromIterable takes argument iterable (一个 ECMAScript 语言值) and returns 正常完成包含字符串列表或抛出完成. It performs the following steps when called:
如果 iterable 是 undefined ,则 返回一个新的空列表。 令 iteratorRecord 为 ? GetIterator (iterable , sync )。 令 list 为一个新的空列表。 重复, 令 next 为 ? IteratorStepValue (iteratorRecord )。 如果 next 是 done ,则 返回 list 。 如果 next 不是字符串,则 令 error 为 ThrowCompletion (新创建的 TypeError 对象)。 返回 ? IteratorClose (iteratorRecord , error )。 将 next 添加到 list 。
Note
该算法在遇到非字符串值时会抛出异常,因为没有明显的针对任意值的本地化转换方式。
15 语言环境对象
15.1 Intl.Locale 构造函数
Intl.Locale 构造函数:
是 %Intl.Locale% 。
是 Intl 对象 的 "Locale" 属性的初始值。
15.1.1 Intl.Locale ( tag [ , options ] )
当调用 Intl.Locale 函数并传入参数 tag 和可选参数 options 时,执行以下步骤:
如果 NewTarget 是 undefined ,抛出 TypeError 异常。 令 localeExtensionKeys 为 %Intl.Locale% .[[LocaleExtensionKeys]] 。 令 internalSlotsList 为 « [[InitializedLocale]] , [[Locale]] , [[Calendar]] , [[Collation]] , [[FirstDayOfWeek]] ,
[[HourCycle]] , [[NumberingSystem]] »。 如果 localeExtensionKeys 包含 "kf" ,则 将 [[CaseFirst]] 添加到 internalSlotsList 。 如果 localeExtensionKeys 包含 "kn" ,则 将 [[Numeric]] 添加到 internalSlotsList 。 令 locale 为 ? OrdinaryCreateFromConstructor (NewTarget, "%Intl.Locale.prototype%" , internalSlotsList )。 如果 tag 不是字符串且不是对象,抛出 TypeError 异常。 如果 tag 是对象且 tag 拥有 [[InitializedLocale]] 内部槽,则 令 tag 为 tag .[[Locale]] 。 否则, 令 tag 为 ? ToString (tag )。 将 options 设置为 ? CoerceOptionsToObject (options )。 如果 IsWellFormedLanguageTag (tag ) 为 false ,抛出 RangeError 异常。 注:由于 LanguageId 规范化 可根据 supplementalMetadata.xml
的 Alias Rules 任意改变 tag ,因此必须在应用 options 的覆盖前进行规范化。 将 tag 设置为 CanonicalizeUnicodeLocaleId (tag )。 将 tag 设置为 ? UpdateLanguageId (tag , options )。 令 opt 为新建的 Record 。 令 calendar 为 ? GetOption (options , "calendar" , string , empty , undefined )。 如果 calendar 不是 undefined ,则 如果 calendar 不能被 type Unicode locale 非终结符匹配,抛出 RangeError 异常。 设置 opt .[[ca]] 为 calendar 。 令 collation 为 ? GetOption (options , "collation" , string , empty , undefined )。 如果 collation 不是 undefined ,则 如果 collation 不能被 type Unicode locale 非终结符匹配,抛出 RangeError 异常。 设置 opt .[[co]] 为 collation 。 令 fw 为 ? GetOption (options , "firstDayOfWeek" , string , empty , undefined )。 如果 fw 不是 undefined ,则 将 fw 设置为 WeekdayToUValue (fw )。 如果 fw 不能被 type Unicode locale 非终结符匹配,抛出 RangeError 异常。 设置 opt .[[fw]] 为 fw 。 令 hc 为 ? GetOption (options , "hourCycle" , string , « "h11" , "h12" , "h23" , "h24" »,
undefined )。 设置 opt .[[hc]] 为 hc 。 令 kf 为 ? GetOption (options , "caseFirst" , string , « "upper" , "lower" , "false" », undefined )。 设置 opt .[[kf]] 为 kf 。 令 kn 为 ? GetOption (options , "numeric" , boolean , empty , undefined )。 如果 kn 不是 undefined ,将 kn 设置为 ! ToString (kn )。 设置 opt .[[kn]] 为 kn 。 令 numberingSystem 为 ? GetOption (options , "numberingSystem" , string , empty , undefined )。 如果 numberingSystem 不是 undefined ,则 如果 numberingSystem 不能被 type Unicode locale 非终结符匹配,抛出 RangeError 异常。 设置 opt .[[nu]] 为 numberingSystem 。 令 r 为 MakeLocaleRecord (tag , opt , localeExtensionKeys )。 设置 locale .[[Locale]] 为 r .[[locale]] 。 设置 locale .[[Calendar]] 为 r .[[ca]] 。 设置 locale .[[Collation]] 为 r .[[co]] 。 设置 locale .[[FirstDayOfWeek]] 为 r .[[fw]] 。 设置 locale .[[HourCycle]] 为 r .[[hc]] 。 如果 localeExtensionKeys 包含 "kf" ,则 设置 locale .[[CaseFirst]] 为 r .[[kf]] 。 如果 localeExtensionKeys 包含 "kn" ,则 如果 SameValue (r .[[kn]] , "true" ) 为 true 或 r .[[kn]] 是空字符串,则 设置 locale .[[Numeric]] 为 true 。 否则, 设置 locale .[[Numeric]] 为 false 。 设置 locale .[[NumberingSystem]] 为 r .[[nu]] 。 返回 locale 。
15.1.2 UpdateLanguageId ( tag , options )
The abstract operation UpdateLanguageId takes arguments tag (一个格式良好的语言标签 ) and options (一个对象) and returns 正常完成包含格式良好的语言标签 或抛出完成. 根据 options 的对应属性更新 tag 中的 unicode_language_id 子标签 ,验证并返回结果。 It performs the following steps when called:
令 baseName 为 GetLocaleBaseName (tag )。 令 language 为 ? GetOption (options , "language" , string , empty , GetLocaleLanguage (baseName ))。 如果 language 不能被 unicode_language_subtag Unicode locale 非终结符匹配,抛出 RangeError 异常。 令 script 为 ? GetOption (options , "script" , string , empty , GetLocaleScript (baseName ))。 如果 script 不是 undefined ,则 如果 script 不能被 unicode_script_subtag Unicode locale 非终结符匹配,抛出 RangeError 异常。 令 region 为 ? GetOption (options , "region" , string , empty , GetLocaleRegion (baseName ))。 如果 region 不是 undefined ,则 如果 region 不能被 unicode_region_subtag Unicode locale 非终结符匹配,抛出 RangeError 异常。 令 variants 为 ? GetOption (options , "variants" , string , empty , GetLocaleVariants (baseName ))。 如果 variants 不是 undefined ,则 如果 variants 是空字符串,抛出 RangeError 异常。 令 lowerVariants 为 variants 的 ASCII 小写形式。 令 variantSubtags 为 StringSplitToList (lowerVariants , "-" )。 对 variantSubtags 的每个元素 variant ,执行 如果 variant 不能被 unicode_variant_subtag Unicode locale 非终结符匹配,抛出 RangeError 异常。 如果 variantSubtags 包含任何重复元素,抛出 RangeError 异常。 令 allExtensions 为 tag 中 baseName 后的后缀。 令 newTag 为 language 。 如果 script 不是 undefined ,将 newTag 设置为 newTag 与 "-" 和 script 的字符串连接。 如果 region 不是 undefined ,将 newTag 设置为 newTag 与 "-" 和 region 的字符串连接。 如果 variants 不是 undefined ,将 newTag 设置为 newTag 与 "-" 和 variants 的字符串连接。 将 newTag 设置为 newTag 与 allExtensions 的字符串连接。 返回 newTag 。
15.1.3 MakeLocaleRecord ( tag , options , localeExtensionKeys )
The abstract operation MakeLocaleRecord takes arguments tag (一个语言标签 ), options (一个 Record ), and localeExtensionKeys (字符串列表) and returns 一个 Record . 构造并返回一个 Record ,其中每个 localeExtensionKeys 元素定义一个对应字段,数据来自 tag 的 Unicode locale 扩展序列并被 options
的对应字段覆盖,并且还包含一个 [[locale]] 字段,其值为将这些字段合并到 tag 后得到的 Unicode 规范化语言环境标识符 。 It performs the following steps when called:
如果 tag 包含一个 substring 是 Unicode locale 扩展序列,则 令 extension 为 tag 中 Unicode locale 扩展序列的 substring 。 令 components 为 UnicodeExtensionComponents (extension )。 令 attributes 为 components .[[Attributes]] 。 令 keywords 为 components .[[Keywords]] 。 否则, 令 attributes 为新空列表。 令 keywords 为新空列表。 令 result 为新建的 Record 。 对 localeExtensionKeys 的每个元素 key ,执行 如果 keywords 包含一个 [[Key]] 为 key 的元素,则 令 entry 为 keywords 中 [[Key]] 为 key 的元素。 令 value 为 entry .[[Value]] 。 否则, 令 entry 为 empty 。 令 value 为 undefined 。 断言:options 拥有字段 [[<key >]]。 令 overrideValue 为 options .[[<key >]]。 如果 overrideValue 不是 undefined ,则 将 value 设置为 CanonicalizeUValue(key , overrideValue )。 如果 entry 不是 empty ,则 设置 entry .[[Value]] 为 value 。 否则, 将 Record { [[Key]] : key , [[Value]] : value } 添加到 keywords 。 设置 result .[[<key >]] 为 value 。 令 locale 为 tag 移除所有 Unicode locale 扩展序列后的字符串值。 如果 attributes 非空或 keywords 非空,则 设置 result .[[locale]] 为 InsertUnicodeExtensionAndCanonicalize (locale , attributes , keywords )。 否则, 设置 result .[[locale]] 为 CanonicalizeUnicodeLocaleId (locale )。 返回 result 。
15.2 Intl.Locale 构造函数的属性
Intl.Locale 构造函数:
15.2.1 Intl.Locale.prototype
Intl.Locale.prototype 的值为 %Intl.Locale.prototype% 。
该属性的属性为 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : false }。
15.2.2 内部槽
[[LocaleExtensionKeys]] 内部槽的值是一个列表,必须包含 « "ca" , "co" , "fw" , "hc" , "nu" » 的所有元素,还必须包含
%Intl.Collator% .[[RelevantExtensionKeys]] 中的 « "kf" , "kn" » 的任何元素,且不得包含其他元素。
15.3 Intl.Locale 原型对象的属性
Intl.Locale 原型对象 :
是 %Intl.Locale.prototype% 。
是一个普通对象。
不是 Intl.Locale 实例,不拥有 [[InitializedLocale]] 内部槽或任何 Intl.Locale 实例对象的其他内部槽。
拥有 [[Prototype]] 内部槽,其值为 %Object.prototype% 。
15.3.1 Intl.Locale.prototype.constructor
Intl.Locale.prototype.constructor 的初始值为 %Intl.Locale% 。
15.3.2 get Intl.Locale.prototype.baseName
Intl.Locale.prototype.baseName 是一个访问器属性,其 set 访问器函数为 undefined 。其 get 访问器函数执行以下步骤:
令 loc 为 this 值。 执行 ? RequireInternalSlot (loc , [[InitializedLocale]] )。 返回 GetLocaleBaseName (loc .[[Locale]] )。
15.3.3 get Intl.Locale.prototype.calendar
Intl.Locale.prototype.calendar 是一个访问器属性,其 set 访问器函数为 undefined 。其 get 访问器函数执行以下步骤:
令 loc 为 this 值。 执行 ? RequireInternalSlot (loc , [[InitializedLocale]] )。 返回 loc .[[Calendar]] 。
15.3.4 get Intl.Locale.prototype.caseFirst
仅当 %Intl.Locale% .[[LocaleExtensionKeys]] 包含 "kf" 时,该属性存在。
Intl.Locale.prototype.caseFirst 是一个访问器属性,其 set 访问器函数为 undefined 。其 get 访问器函数执行以下步骤:
令 loc 为 this 值。 执行 ? RequireInternalSlot (loc , [[InitializedLocale]] )。 返回 loc .[[CaseFirst]] 。
15.3.5 get Intl.Locale.prototype.collation
Intl.Locale.prototype.collation 是一个访问器属性,其 set 访问器函数为 undefined 。其 get 访问器函数执行以下步骤:
令 loc 为 this 值。 执行 ? RequireInternalSlot (loc , [[InitializedLocale]] )。 返回 loc .[[Collation]] 。
15.3.6 get Intl.Locale.prototype.firstDayOfWeek
Intl.Locale.prototype.firstDayOfWeek 是一个访问器属性,其 set 访问器函数为 undefined 。其 get 访问器函数执行以下步骤:
令 loc 为 this 值。 执行 ? RequireInternalSlot (loc , [[InitializedLocale]] )。 返回 loc .[[FirstDayOfWeek]] 。
15.3.7 get Intl.Locale.prototype.hourCycle
Intl.Locale.prototype.hourCycle 是一个访问器属性,其 set 访问器函数为 undefined 。其 get 访问器函数执行以下步骤:
令 loc 为 this 值。 执行 ? RequireInternalSlot (loc , [[InitializedLocale]] )。 返回 loc .[[HourCycle]] 。
15.3.8 get Intl.Locale.prototype.language
Intl.Locale.prototype.language 是一个访问器属性,其 set 访问器函数为 undefined 。其 get 访问器函数执行以下步骤:
令 loc 为 this 值。 执行 ? RequireInternalSlot (loc , [[InitializedLocale]] )。 返回 GetLocaleLanguage (loc .[[Locale]] )。
15.3.9 Intl.Locale.prototype.maximize ( )
令 loc 为 this 值。 执行 ? RequireInternalSlot (loc , [[InitializedLocale]] )。 令 maximal 为 Add Likely Subtags 算法应用于
loc .[[Locale]] 的结果。如果发生错误,则将 maximal 设置为 loc .[[Locale]] 。 返回 ! Construct (%Intl.Locale% , maximal )。
15.3.10 Intl.Locale.prototype.minimize ( )
令 loc 为 this 值。 执行 ? RequireInternalSlot (loc , [[InitializedLocale]] )。 令 minimal 为 Remove Likely Subtags 算法应用于
loc .[[Locale]] 的结果。如果发生错误,则将 minimal 设置为 loc .[[Locale]] 。 返回 ! Construct (%Intl.Locale% , minimal )。
15.3.11 get Intl.Locale.prototype.numberingSystem
Intl.Locale.prototype.numberingSystem 是一个访问器属性,其 set 访问器函数为 undefined 。其 get 访问器函数执行以下步骤:
令 loc 为 this 值。 执行 ? RequireInternalSlot (loc , [[InitializedLocale]] )。 返回 loc .[[NumberingSystem]] 。
15.3.12 get Intl.Locale.prototype.numeric
仅当 %Intl.Locale% .[[LocaleExtensionKeys]] 包含 "kn" 时,该属性存在。
Intl.Locale.prototype.numeric 是一个访问器属性,其 set 访问器函数为 undefined 。其 get 访问器函数执行以下步骤:
令 loc 为 this 值。 执行 ? RequireInternalSlot (loc , [[InitializedLocale]] )。 返回 loc .[[Numeric]] 。
15.3.13 get Intl.Locale.prototype.region
Intl.Locale.prototype.region 是一个访问器属性,其 set 访问器函数为 undefined 。其 get 访问器函数执行以下步骤:
令 loc 为 this 值。 执行 ? RequireInternalSlot (loc , [[InitializedLocale]] )。 返回 GetLocaleRegion (loc .[[Locale]] )。
15.3.14 get Intl.Locale.prototype.script
Intl.Locale.prototype.script 是一个访问器属性,其 set 访问器函数为 undefined 。其 get 访问器函数执行以下步骤:
令 loc 为 this 值。 执行 ? RequireInternalSlot (loc , [[InitializedLocale]] )。 返回 GetLocaleScript (loc .[[Locale]] )。
15.3.15 Intl.Locale.prototype.toString ( )
令 loc 为 this 值。 执行 ? RequireInternalSlot (loc , [[InitializedLocale]] )。 返回 loc .[[Locale]] 。
15.3.16 Intl.Locale.prototype.getCalendars ( )
调用 getCalendars 方法时,执行以下步骤:
令 loc 为 this 值。 执行 ? RequireInternalSlot (loc , [[InitializedLocale]] )。 返回 CalendarsOfLocale (loc )。
15.3.17 Intl.Locale.prototype.getCollations ( )
调用 getCollations 方法时,执行以下步骤:
令 loc 为 this 值。 执行 ? RequireInternalSlot (loc , [[InitializedLocale]] )。 返回 CollationsOfLocale (loc )。
15.3.18 Intl.Locale.prototype.getHourCycles ( )
调用 getHourCycles 方法时,执行以下步骤:
令 loc 为 this 值。 执行 ? RequireInternalSlot (loc , [[InitializedLocale]] )。 返回 HourCyclesOfLocale (loc )。
15.3.19 Intl.Locale.prototype.getNumberingSystems ( )
调用 getNumberingSystems 方法时,执行以下步骤:
令 loc 为 this 值。 执行 ? RequireInternalSlot (loc , [[InitializedLocale]] )。 返回 NumberingSystemsOfLocale (loc )。
15.3.20 Intl.Locale.prototype.getTimeZones ( )
调用 getTimeZones 方法时,执行以下步骤:
令 loc 为 this 值。 执行 ? RequireInternalSlot (loc , [[InitializedLocale]] )。 返回 TimeZonesOfLocale (loc )。
15.3.21 Intl.Locale.prototype.getTextInfo ( )
调用 getTextInfo 方法时,执行以下步骤:
令 loc 为 this 值。 执行 ? RequireInternalSlot (loc , [[InitializedLocale]] )。 令 info 为 OrdinaryObjectCreate (%Object.prototype% )。 令 dir 为 TextDirectionOfLocale (loc )。 执行 ! CreateDataPropertyOrThrow (info , "direction" , dir )。 返回 info 。
15.3.22 Intl.Locale.prototype.getWeekInfo ( )
调用 getWeekInfo 方法时,执行以下步骤:
令 loc 为 this 值。 执行 ? RequireInternalSlot (loc , [[InitializedLocale]] )。 令 info 为 OrdinaryObjectCreate (%Object.prototype% )。 令 wi 为 WeekInfoOfLocale (loc )。 执行 ! CreateDataPropertyOrThrow (info , "firstDay" , wi .[[FirstDay]] )。 执行 ! CreateDataPropertyOrThrow (info , "weekend" , CreateArrayFromList (wi .[[Weekend]] ))。 返回 info 。
15.3.23 get Intl.Locale.prototype.variants
Intl.Locale.prototype.variants 是一个访问器属性,其 set 访问器函数为 undefined 。其 get 访问器函数执行以下步骤:
令 loc 为 this 值。 执行 ? RequireInternalSlot (loc , [[InitializedLocale]] )。 返回 GetLocaleVariants (loc .[[Locale]] )。
15.3.24 Intl.Locale.prototype [ %Symbol.toStringTag% ]
%Symbol.toStringTag% 属性的初始值为字符串 "Intl.Locale" 。
该属性的属性为 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : true }。
15.4 Intl.Locale 实例的属性
Intl.Locale 实例是普通对象,继承自 %Intl.Locale.prototype% 的属性。
Intl.Locale 实例拥有 [[InitializedLocale]] 内部槽。
Intl.Locale 实例还拥有多个内部槽,这些槽由 Intl.Locale 构造函数 计算:
[[Locale]] 是一个字符串值,表示用于格式化的语言环境标签。
[[Calendar]] 是 undefined 或一个字符串值,表示规范形式的 Unicode 日历标识符 。
[[Collation]] 是 undefined 或一个字符串值,表示规范形式的 Unicode 排序标识符 。
[[FirstDayOfWeek]] 是 undefined 或一个字符串值,表示规范形式的 Unicode 首日标识符 。
[[HourCycle]] 是 undefined 或一个字符串值,表示规范形式的 Unicode 小时周期标识符 。
[[NumberingSystem]] 是 undefined 或一个字符串值,表示规范形式的 Unicode 数字系统标识符 。
[[CaseFirst]] 是 undefined 或字符串值 "upper" 、"lower" 或 "false" 。仅当 %Intl.Locale% 的 [[LocaleExtensionKeys]]
内部槽包含 "kf" 时,该内部槽存在。
[[Numeric]] 是 undefined 或布尔值,指定该语言环境是否使用数字排序。仅当 %Intl.Locale% 的 [[LocaleExtensionKeys]] 内部槽包含 "kn"
时,该内部槽存在。
15.5 语言环境对象的抽象操作
15.5.1 GetLocaleBaseName ( locale )
The abstract operation GetLocaleBaseName takes argument locale (字符串) and returns 字符串. It performs the following steps when called:
断言:locale 能被 unicode_locale_id Unicode locale 非终结符匹配。 返回 locale 被 unicode_language_id Unicode locale 非终结符匹配的最长前缀。
15.5.2 GetLocaleLanguage ( locale )
The abstract operation GetLocaleLanguage takes argument locale (字符串) and returns 字符串. It performs the following steps when called:
令 baseName 为 GetLocaleBaseName (locale )。 断言:baseName 的第一个子标签 能被 unicode_language_subtag Unicode locale 非终结符匹配。 返回 baseName 的第一个子标签 。
15.5.3 GetLocaleScript ( locale )
The abstract operation GetLocaleScript takes argument locale (字符串) and returns 字符串或 undefined . It performs the following steps when called:
令 baseName 为 GetLocaleBaseName (locale )。 断言:baseName 至多包含一个能被 unicode_script_subtag Unicode locale 非终结符匹配的子标签 。 如果 baseName 包含一个被 unicode_script_subtag 匹配的子标签 ,则返回该子标签 。 返回 undefined 。
15.5.4 GetLocaleRegion ( locale )
The abstract operation GetLocaleRegion takes argument locale (字符串) and returns 字符串或 undefined . It performs the following steps when called:
令 baseName 为 GetLocaleBaseName (locale )。 注:unicode_region_subtag 子标签 仅在初始 unicode_language_subtag 子标签 之后有效,可选地在其间有一个
unicode_script_subtag。在该位置,unicode_region_subtag 不会与其他有效子标签 混淆,因为所有产生式互不重叠。 断言:baseName 的第一个子标签 能被 unicode_language_subtag Unicode locale 非终结符匹配。 令 baseNameTail 为 baseName 第一个子标签 后的后缀。 断言:baseNameTail 至多包含一个能被 unicode_region_subtag Unicode locale 非终结符匹配的子标签 。 如果 baseNameTail 包含一个被 unicode_region_subtag 匹配的子标签 ,则返回该子标签 。 返回 undefined 。
15.5.5 GetLocaleVariants ( locale )
The abstract operation GetLocaleVariants takes argument locale (字符串) and returns 字符串或 undefined . It performs the following steps when called:
令 baseName 为 GetLocaleBaseName (locale )。 注:baseName 中每个以 "-" 前缀的子标签 要么是 unicode_script_subtag、unicode_region_subtag 或
unicode_variant_subtag,但任何被 unicode_variant_subtag 匹配的
substring 都比其前缀更长,且前缀不会被其他产生式匹配。 令 variants 为 baseName 的最长后缀,该后缀以 "-" 和被 unicode_variant_subtag 匹配的
substring 开始。如果没有这样的后缀,返回 undefined 。 返回 variants 从第 1 个字符开始的子字符串。
15.5.6 UnicodeExtensionValue ( locale , key )
The abstract operation UnicodeExtensionValue takes arguments locale (Unicode 规范化语言环境标识符 ) and key (字符串) and returns 字符串或 empty . It performs the following steps when called:
如果 locale 包含 Unicode locale 扩展序列,则 令 extension 为 locale 的 Unicode locale 扩展序列。 令 components 为 UnicodeExtensionComponents (extension )。 令 keywords 为 components .[[Keywords]] 。 如果存在 keywords 元素 entry ,其 [[Key]] 为 key ,则返回 entry .[[Value]] 。 返回 empty 。
15.5.7 CanonicalUnicodeSubdivision ( locale , key )
The abstract operation CanonicalUnicodeSubdivision takes arguments locale (Unicode 规范化语言环境标识符 ) and key ("rg" 或 "sd" ) and returns 字符串或 undefined . It performs the following steps when called:
令 subdivision 为 UnicodeExtensionValue (locale , key )。 如果 subdivision 为 empty ,返回 undefined 。 如果 subdivision 不能被 unicode_subdivision_id Unicode locale 非终结符匹配,返回 undefined 。 令 region 为 subdivision 被 unicode_region_subtag Unicode locale 非终结符匹配的最长前缀。 令 regionLocale 为 "und-" 与 region 的字符串连接。 将 regionLocale 设置为 CanonicalizeUnicodeLocaleId (regionLocale )。 返回 GetLocaleRegion (regionLocale )。
15.5.8 RegionPreference ( locale )
The abstract operation RegionPreference takes argument locale (Unicode 规范化语言环境标识符 ) and returns 一个 Record ,字段 [[Region]] (字符串)和 [[RegionOverride]] (字符串或 undefined ). It performs the following steps when called:
令 region 为 GetLocaleRegion (locale )。 如果 region 为 undefined ,则 将 region 设置为 CanonicalUnicodeSubdivision (locale , "sd" )。 如果 region 为 undefined ,则 令 maximal 为 Add Likely Subtags 算法应用于
locale 的结果。如果发生错误,则将 maximal 设置为 locale 。 将 maximal 设置为 CanonicalizeUnicodeLocaleId (maximal )。 将 region 设置为 GetLocaleRegion (maximal )。 如果 region 为 undefined ,则 将 region 设置为 "001" 。 令 regionOverride 为 CanonicalUnicodeSubdivision (locale , "rg" )。 返回 { [[Region]] : region , [[RegionOverride]] : regionOverride }。
15.5.9 CalendarsOfLocale ( loc )
The implementation-defined abstract operation CalendarsOfLocale takes argument loc (Intl.Locale) and returns 数组. 以下算法参考 Unicode 技术标准 #35
第 4 部分 日期,日历偏好数据 。 It performs the following steps when called:
如果 loc .[[Calendar]] 不是 undefined ,则 返回 CreateArrayFromList (« loc .[[Calendar]] »)。 令 preference 为 RegionPreference (loc .[[Locale]] )。 令 region 为 preference .[[Region]] 。 令 regionOverride 为 preference .[[RegionOverride]] 。 如果 regionOverride 不是 undefined 且 regionOverride 有日历偏好数据,则 令 lookupRegion 为 regionOverride 。 否则, 令 lookupRegion 为 region 。 令 list 为 lookupRegion 中用于日期和时间格式化的常用日历类型 的唯一规范形式列表(6.9 ),按偏好降序排序。如果 lookupRegion 没有日历偏好数据,则列表为空。 如果 list 为空,将 list 设置为 « "gregory" »。 返回 CreateArrayFromList (list )。
15.5.10 CollationsOfLocale ( loc )
The implementation-defined abstract operation CollationsOfLocale takes argument loc (Intl.Locale) and returns 数组. It performs the following steps when called:
如果 loc .[[Collation]] 不是 undefined ,则 返回 CreateArrayFromList (« loc .[[Collation]] »)。 令 language 为 GetLocaleLanguage (loc .[[Locale]] )。 如果 language 不是 "und" ,则 令 r 为 LookupMatchingLocaleByPrefix (%Intl.Collator% .[[AvailableLocales]] , « loc .[[Locale]] »)。 如果 r 不是 undefined ,则 令 foundLocale 为 r .[[locale]] 。 否则, 令 foundLocale 为 DefaultLocale ()。 令 foundLocaleData 为 %Intl.Collator% .[[SortLocaleData]] .[[<foundLocale >]]。 令 list 为 foundLocaleData .[[co]] 的副本。 断言:list [0] 为 null 。 移除 list 的第一个元素。 否则, 令 list 为 « "emoji" , "eor" »。 令 sorted 为 list 的副本,按字典序排序。 返回 CreateArrayFromList (sorted )。
15.5.11 HourCyclesOfLocale ( loc )
The implementation-defined abstract operation HourCyclesOfLocale takes argument loc (Intl.Locale) and returns 数组. 以下算法参考 Unicode 技术标准 #35 第 4 部分
日期,时间数据 。 It performs the following steps when called:
如果 loc .[[HourCycle]] 不是 undefined ,则 返回 CreateArrayFromList (« loc .[[HourCycle]] »)。 令 preference 为 RegionPreference (loc .[[Locale]] )。 令 region 为 preference .[[Region]] 。 令 regionOverride 为 preference .[[RegionOverride]] 。 如果 regionOverride 不是 undefined 且 regionOverride 有时间数据,则 令 lookupRegion 为 regionOverride 。 否则, 令 lookupRegion 为 region 。 令 list 为 lookupRegion 中用于日期和时间格式化的常用小时周期标识符的唯一小写字符串列表,表示 12 小时制("h11" , "h12" )或 24 小时制("h23" ,
"h24" ),按偏好降序排序。如果 lookupRegion 没有时间数据,则列表为空。 如果 list 为空,将 list 设置为 « "h23" »。 返回 CreateArrayFromList (list )。
15.5.12 NumberingSystemsOfLocale ( loc )
The implementation-defined abstract operation NumberingSystemsOfLocale takes argument loc (Intl.Locale) and returns 数组. It performs the following steps when called:
如果 loc .[[NumberingSystem]] 不是 undefined ,则 返回 CreateArrayFromList (« loc .[[NumberingSystem]] »)。 令 r 为 LookupMatchingLocaleByPrefix (%Intl.NumberFormat% .[[AvailableLocales]] , « loc .[[Locale]] »)。 如果 r 不是 undefined ,则 令 foundLocale 为 r .[[locale]] 。 令 foundLocaleData 为 %Intl.NumberFormat% .[[LocaleData]] .[[<foundLocale >]]。 令 numberingSystems 为 foundLocaleData .[[nu]] 。 令 list 为 « numberingSystems [0] »。 否则, 令 list 为 « "latn" »。 返回 CreateArrayFromList (list )。
15.5.13 TimeZonesOfLocale ( loc )
The implementation-defined abstract operation TimeZonesOfLocale takes argument loc (Intl.Locale) and returns 数组或 undefined . It performs the following steps when called:
令 region 为 GetLocaleRegion (loc .[[Locale]] )。 如果 region 为 undefined ,返回 undefined 。 令 list 为 region 中常用的唯一规范时区标识符列表,必须是 IANA 时区数据库的规范 Zone 名称字符串。如果 region 没有常用时区,则列表为空。列表按字典序排序。 返回 CreateArrayFromList ( list )。
15.5.14 TextDirectionOfLocale ( loc )
The implementation-defined abstract operation TextDirectionOfLocale takes argument loc (Intl.Locale) and returns 字符串或 undefined . 以下算法参考 Unicode 技术标准 #35 第 1 部分
核心,脚本元数据 。 It performs the following steps when called:
令 locale 为 loc .[[Locale]] 。 令 script 为 GetLocaleScript (locale )。 如果 script 为 undefined ,则 令 maximal 为 Add Likely Subtags 算法应用于
locale 的结果。如果发生错误,返回 undefined 。 将 script 设置为 GetLocaleScript (maximal )。 如果 script 为 undefined ,返回 undefined 。 如果 script 的默认字符排列方向为右到左,返回 "rtl" 。 如果 script 的默认字符排列方向为左到右,返回 "ltr" 。 返回 undefined 。
Note 1
当无法确定 script 的默认字符排列方向,或方向既不是右到左也不是左到右时,将返回 undefined 。
Note 2
一种语言的文本可能有多种方向。用某种脚本书写的语言文本也可能有多种方向。因此,如果文本的元数据中包含方向信息,则该方向信息应作为主要依据。TextDirectionOfLocale
返回的信息仅作为没有元数据时的备用机制,仅在没有方向信息但已知语言信息时使用。Web 开发者不应过度依赖此 API 来决定文本方向。
15.5.15 WeekdayToUValue ( fw )
The abstract operation WeekdayToUValue takes argument fw (字符串) and returns 字符串. 将数字表示的星期几字符串转换为 Unicode
首日标识符 ,并返回其他字符串原样。 It performs the following steps when called:
对 Table 26 的每一行(除表头),按表顺序执行 令 w 为当前行的 Weekday 值。 令 s 为当前行的字符串值。 如果 fw 等于 w ,返回 s 。 返回 fw 。
Table 26: 星期字符串与值
Weekday
String
Value
"0"
"sun"
7 𝔽
"1"
"mon"
1 𝔽
"2"
"tue"
2 𝔽
"3"
"wed"
3 𝔽
"4"
"thu"
4 𝔽
"5"
"fri"
5 𝔽
"6"
"sat"
6 𝔽
"7"
"sun"
7 𝔽
15.5.16 WeekdayUValueToNumber ( fw )
The abstract operation WeekdayUValueToNumber takes argument fw (字符串) and returns 整数或 undefined . 将 Unicode 首日标识符 转换为数字 ISO 8601
calendar day of week ,对于其他字符串返回 undefined 。
It performs the following steps when called:
对 Table 26 的每一行(除表头),按表顺序执行 令 s 为当前行的字符串值。 令 v 为当前行的 Value 值。 如果 fw 等于 s ,返回 v 。 返回 undefined 。
15.5.17 WeekInfoOfLocale ( loc )
The implementation-defined abstract operation WeekInfoOfLocale takes argument loc (Intl.Locale) and returns 一个 Record . 以下算法参考 Unicode
技术标准 #35 第 4 部分 日期,周元素 。 It performs the following steps when called:
令 locale 为 loc .[[Locale]] 。 令 r 为 Table 27 定义的字段的 Record ,值基于 locale 。 令 fws 为 loc .[[FirstDayOfWeek]] 。 令 fw 为 WeekdayUValueToNumber (fws )。 如果 fw 不是 undefined ,则 设置 r .[[FirstDay]] 为 fw 。 返回 r 。
Note
Table 27: WeekInfo 记录字段
Field Name
Value
Meaning
[[FirstDay]]
日历周的 ISO 8601 calendar day of week ,即其在以星期一为 1 𝔽 、星期日为 7 𝔽 的周序列中的 1
基序号。
用于日历的“首日”是哪一天。
[[Weekend]]
一个或多个 ISO 8601 calendar day of week 整数值 1 𝔽 (星期一)到 7 𝔽 (星期日),按升序排列。
表示哪些星期几被视为“周末”的列表。注意,不同语言环境的周末天数可能不同,且可能不连续。
16 NumberFormat 对象
16.1 Intl.NumberFormat 构造函数
Intl.NumberFormat 构造函数:
是 %Intl.NumberFormat% 。
是 Intl 对象 的 "NumberFormat" 属性的初始值。
所有 Intl 对象 服务构造属性的通用行为在 9.1 中指定。
16.1.1 Intl.NumberFormat ( [ locales [ , options ] ] )
当以可选参数 locales 和 options 调用 Intl.NumberFormat 函数时,执行以下步骤:
如果 NewTarget 是 undefined ,令 newTarget 为活动函数对象,否则令 newTarget 为 NewTarget。 令 numberFormat 为 ? OrdinaryCreateFromConstructor (newTarget , "%Intl.NumberFormat.prototype%" , «
[[InitializedNumberFormat]] , [[Locale]] , [[LocaleData]] , [[NumberingSystem]] , [[Style]] , [[Unit]] ,
[[UnitDisplay]] , [[Currency]] , [[CurrencyDisplay]] , [[CurrencySign]] , [[MinimumIntegerDigits]] ,
[[MinimumFractionDigits]] , [[MaximumFractionDigits]] , [[MinimumSignificantDigits]] ,
[[MaximumSignificantDigits]] , [[RoundingType]] , [[Notation]] , [[CompactDisplay]] , [[UseGrouping]] ,
[[SignDisplay]] , [[RoundingIncrement]] , [[RoundingMode]] , [[ComputedRoundingPriority]] , [[TrailingZeroDisplay]] ,
[[BoundFormat]] »)。 令 optionsResolution 为 ? ResolveOptions (%Intl.NumberFormat% , %Intl.NumberFormat% .[[LocaleData]] , locales ,
options , « coerce-options »)。 设置 options 为 optionsResolution .[[Options]] 。 令 r 为 optionsResolution .[[ResolvedLocale]] 。 设置 numberFormat .[[Locale]] 为 r .[[Locale]] 。 设置 numberFormat .[[LocaleData]] 为 r .[[LocaleData]] 。 设置 numberFormat .[[NumberingSystem]] 为 r .[[nu]] 。 执行 ? SetNumberFormatUnitOptions (numberFormat , options )。 令 style 为 numberFormat .[[Style]] 。 令 notation 为 ? GetOption (options , "notation" , string , « "standard" , "scientific" ,
"engineering" , "compact" », "standard" )。 设置 numberFormat .[[Notation]] 为 notation 。 如果 style 是 "currency" 且 notation 是 "standard" ,则 令 currency 为 numberFormat .[[Currency]] 。 令 cDigits 为 CurrencyDigits (currency )。 令 mnfdDefault 为 cDigits 。 令 mxfdDefault 为 cDigits 。 否则, 令 mnfdDefault 为 0。 如果 style 是 "percent" ,则 令 mxfdDefault 为 0。 否则, 令 mxfdDefault 为 3。 执行 ? SetNumberFormatDigitOptions (numberFormat , options , mnfdDefault , mxfdDefault , notation )。 令 compactDisplay 为 ? GetOption (options , "compactDisplay" , string , « "short" , "long" »,
"short" )。 令 defaultUseGrouping 为 "auto" 。 如果 notation 是 "compact" ,则 设置 numberFormat .[[CompactDisplay]] 为 compactDisplay 。 设置 defaultUseGrouping 为 "min2" 。 注:出于历史原因,字符串 "true" 和 "false" 被接受并替换为默认值。 令 useGrouping 为 ? GetBooleanOrStringNumberFormatOption (options , "useGrouping" , « "min2" , "auto" ,
"always" , "true" , "false" », defaultUseGrouping )。 如果 useGrouping 是 "true" 或 "false" ,设置 useGrouping 为 defaultUseGrouping 。 如果 useGrouping 是 true ,设置 useGrouping 为 "always" 。 设置 numberFormat .[[UseGrouping]] 为 useGrouping 。 令 signDisplay 为 ? GetOption (options , "signDisplay" , string , « "auto" , "never" , "always" ,
"exceptZero" , "negative" », "auto" )。 设置 numberFormat .[[SignDisplay]] 为 signDisplay 。 如果实现支持 4.3 Note 1 的规范可选构造器模式,则 令 this 为 this 值。 返回 ? ChainNumberFormat (numberFormat , NewTarget, this )。 返回 numberFormat 。
16.1.1.1 ChainNumberFormat ( numberFormat , newTarget , this )
The abstract operation ChainNumberFormat takes arguments numberFormat (一个 Intl.NumberFormat), newTarget (一个 ECMAScript 语言值), and this (一个 ECMAScript 语言值) and returns 正常完成包含一个对象或抛出完成. It performs the following steps when called:
如果 newTarget 是 undefined 且 ? OrdinaryHasInstance (%Intl.NumberFormat% , this ) 是 true ,则 执行 ? DefinePropertyOrThrow (this , %Intl% .[[FallbackSymbol]] , PropertyDescriptor{ [[Value]] :
numberFormat , [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : false })。 返回 this 。 返回 numberFormat 。
16.1.2 SetNumberFormatDigitOptions ( intlObj , options , mnfdDefault , mxfdDefault , notation )
The abstract operation SetNumberFormatDigitOptions takes arguments intlObj (一个对象), options (一个对象), mnfdDefault (一个整数), mxfdDefault (一个整数), and notation (一个字符串) and returns 正常完成包含 unused 或抛出完成. It performs the following steps when called:
令 mnid 为 ? GetNumberOption (options , "minimumIntegerDigits" , 1, 21, 1)。 令 mnfd 为 ? Get (options , "minimumFractionDigits" )。 令 mxfd 为 ? Get (options , "maximumFractionDigits" )。 令 mnsd 为 ? Get (options , "minimumSignificantDigits" )。 令 mxsd 为 ? Get (options , "maximumSignificantDigits" )。 设置 intlObj .[[MinimumIntegerDigits]] 为 mnid 。 令 roundingIncrement 为 ? GetNumberOption (options , "roundingIncrement" , 1, 5000, 1)。 如果 roundingIncrement 不在 « 1, 2, 5, 10, 20, 25, 50, 100, 200, 250, 500, 1000, 2000, 2500, 5000 » 中,抛出
RangeError 异常。 令 roundingMode 为 ? GetOption (options , "roundingMode" , string , « "ceil" , "floor" , "expand" ,
"trunc" , "halfCeil" , "halfFloor" , "halfExpand" , "halfTrunc" , "halfEven" », "halfExpand" )。 令 roundingPriority 为 ? GetOption (options , "roundingPriority" , string , « "auto" , "morePrecision" ,
"lessPrecision" », "auto" )。 令 trailingZeroDisplay 为 ? GetOption (options , "trailingZeroDisplay" , string , « "auto" ,
"stripIfInteger" », "auto" )。 注:SetNumberFormatDigitOptions 所需的所有字段已从 options 读取。以下 AO 解释这些选项并可能抛出异常。 如果 roundingIncrement 不为 1,设置 mxfdDefault 为 mnfdDefault 。 设置 intlObj .[[RoundingIncrement]] 为 roundingIncrement 。 设置 intlObj .[[RoundingMode]] 为 roundingMode 。 设置 intlObj .[[TrailingZeroDisplay]] 为 trailingZeroDisplay 。 如果 mnsd 是 undefined 且 mxsd 是 undefined ,令 hasSd 为 false 。否则,令 hasSd 为 true 。 如果 mnfd 是 undefined 且 mxfd 是 undefined ,令 hasFd 为 false 。否则,令 hasFd 为 true 。 令 needSd 为 true 。 令 needFd 为 true 。 如果 roundingPriority 是 "auto" ,则 设置 needSd 为 hasSd 。 如果 needSd 为 true ,或 hasFd 为 false 且 notation 为 "compact" ,则 设置 needFd 为 false 。 如果 needSd 为 true ,则 如果 hasSd 为 true ,则 设置 intlObj .[[MinimumSignificantDigits]] 为 ? DefaultNumberOption (mnsd , 1, 21, 1)。 设置 intlObj .[[MaximumSignificantDigits]] 为 ? DefaultNumberOption (mxsd ,
intlObj .[[MinimumSignificantDigits]] , 21, 21)。 否则, 设置 intlObj .[[MinimumSignificantDigits]] 为 1。 设置 intlObj .[[MaximumSignificantDigits]] 为 21。 如果 needFd 为 true ,则 如果 hasFd 为 true ,则 设置 mnfd 为 ? DefaultNumberOption (mnfd , 0, 100, undefined )。 设置 mxfd 为 ? DefaultNumberOption (mxfd , 0, 100, undefined )。 如果 mnfd 是 undefined ,设置 mnfd 为 min (mnfdDefault , mxfd )。 否则如果 mxfd 是 undefined ,设置 mxfd 为 max (mxfdDefault , mnfd )。 否则如果 mnfd 大于 mxfd ,抛出 RangeError 异常。 设置 intlObj .[[MinimumFractionDigits]] 为 mnfd 。 设置 intlObj .[[MaximumFractionDigits]] 为 mxfd 。 否则, 设置 intlObj .[[MinimumFractionDigits]] 为 mnfdDefault 。 设置 intlObj .[[MaximumFractionDigits]] 为 mxfdDefault 。 如果 needSd 为 false 且 needFd 为 false ,则 设置 intlObj .[[MinimumFractionDigits]] 为 0。 设置 intlObj .[[MaximumFractionDigits]] 为 0。 设置 intlObj .[[MinimumSignificantDigits]] 为 1。 设置 intlObj .[[MaximumSignificantDigits]] 为 2。 设置 intlObj .[[RoundingType]] 为 more-precision 。 设置 intlObj .[[ComputedRoundingPriority]] 为 "morePrecision" 。 否则如果 roundingPriority 是 "morePrecision" ,则 设置 intlObj .[[RoundingType]] 为 more-precision 。 设置 intlObj .[[ComputedRoundingPriority]] 为 "morePrecision" 。 否则如果 roundingPriority 是 "lessPrecision" ,则 设置 intlObj .[[RoundingType]] 为 less-precision 。 设置 intlObj .[[ComputedRoundingPriority]] 为 "lessPrecision" 。 否则如果 hasSd 为 true ,则 设置 intlObj .[[RoundingType]] 为 significant-digits 。 设置 intlObj .[[ComputedRoundingPriority]] 为 "auto" 。 否则, 设置 intlObj .[[RoundingType]] 为 fraction-digits 。 设置 intlObj .[[ComputedRoundingPriority]] 为 "auto" 。 如果 roundingIncrement 不为 1,则 如果 intlObj .[[RoundingType]] 不是 fraction-digits ,抛出 TypeError 异常。 如果 intlObj .[[MaximumFractionDigits]] 不是 intlObj .[[MinimumFractionDigits]] ,抛出 RangeError 异常。 返回 unused 。
16.1.3 SetNumberFormatUnitOptions ( intlObj , options )
The abstract operation SetNumberFormatUnitOptions takes arguments intlObj (一个 Intl.NumberFormat) and options (一个对象) and returns 正常完成包含 unused 或抛出完成. It performs the following steps when called:
令 style 为 ? GetOption (options , "style" , string , « "decimal" , "percent" , "currency" , "unit" »,
"decimal" )。 设置 intlObj .[[Style]] 为 style 。 令 currency 为 ? GetOption (options , "currency" , string , empty , undefined )。 如果 currency 是 undefined ,则 如果 style 是 "currency" ,抛出 TypeError 异常。 否则, 如果 IsWellFormedCurrencyCode (currency ) 是 false ,抛出 RangeError 异常。 令 currencyDisplay 为 ? GetOption (options , "currencyDisplay" , string , « "code" , "symbol" ,
"narrowSymbol" , "name" », "symbol" )。 令 currencySign 为 ? GetOption (options , "currencySign" , string , « "standard" , "accounting" »,
"standard" )。 令 unit 为 ? GetOption (options , "unit" , string , empty , undefined )。 如果 unit 是 undefined ,则 如果 style 是 "unit" ,抛出 TypeError 异常。 否则, 如果 IsWellFormedUnitIdentifier (unit ) 是 false ,抛出 RangeError 异常。 令 unitDisplay 为 ? GetOption (options , "unitDisplay" , string , « "short" , "narrow" , "long" »,
"short" )。 如果 style 是 "currency" ,则 设置 intlObj .[[Currency]] 为 currency 的 ASCII 大写。 设置 intlObj .[[CurrencyDisplay]] 为 currencyDisplay 。 设置 intlObj .[[CurrencySign]] 为 currencySign 。 如果 style 是 "unit" ,则 设置 intlObj .[[Unit]] 为 unit 。 设置 intlObj .[[UnitDisplay]] 为 unitDisplay 。 返回 unused 。
16.2 Intl.NumberFormat 构造函数的属性
Intl.NumberFormat 构造函数:
16.2.1 Intl.NumberFormat.prototype
Intl.NumberFormat.prototype 的值为 %Intl.NumberFormat.prototype% 。
该属性具有 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : false } 属性。
16.2.2 Intl.NumberFormat.supportedLocalesOf ( locales [ , options ] )
当以参数 locales 和 options 调用 supportedLocalesOf 方法时,执行以下步骤:
令 availableLocales 为 %Intl.NumberFormat% .[[AvailableLocales]] 。 令 requestedLocales 为 ? CanonicalizeLocaleList (locales )。 返回 ? FilterLocales (availableLocales , requestedLocales , options )。
16.2.3 内部槽
[[AvailableLocales]] 内部槽的值在 9.1 描述的约束范围内由实现定义。
[[RelevantExtensionKeys]] 内部槽的值为 « "nu" »。
[[ResolutionOptionDescriptors]] 内部槽的值为 « { [[Key]] : "nu" , [[Property]] : "numberingSystem" } »。
Note 1
Unicode 技术标准 #35 第 1 部分核心,3.6.1 键和值定义
描述了与数字格式相关的三个区域扩展键:
"cu" 表示货币,
"cf" 表示货币格式样式,
"nu" 表示数字系统。然而,Intl.NumberFormat 要求货币格式的货币通过选项对象中的 currency
属性指定,货币格式样式通过选项对象中的 currencySign 属性指定。
[[LocaleData]] 内部槽的值在 9.1 描述的约束范围内由实现定义,并有以下附加约束:
任何 [[LocaleData]] 的 "nu" 字段的值的列表不得包含 "native" 、"traditio" 或 "finance" 。
[[LocaleData]] .[[<locale >]] 必须为所有 locale 值 locale 提供 [[patterns]]
字段。该字段的值必须是一个记录,且必须有四种数字格式样式的字段:"decimal" 、"percent" 、"currency" 和 "unit" 。
上述 "currency" 和 "unit" 字段必须是至少包含 "fallback" 字段的记录。
"currency" 可以有额外字段,其键对应 6.3 的货币代码。
每个 "currency" 字段必须是一个记录,包含对应 currencyDisplay 值的字段:"code" 、"symbol" 、"narrowSymbol" 和 "name" 。
每个这些字段必须包含对应 currencySign 值的记录:"standard" 或 "accounting" 。unit 字段(在
[[LocaleData]] .[[<locale >]])可以有除必需字段 "fallback" 外的其他字段,其键对应 6.6 的核心测量单位标识符。
每个 "unit" 字段必须是一个记录,包含对应 unitDisplay 值的字段:"narrow" 、"short" 和 "long" 。
目前为止描述的 patterns 树的所有叶字段("decimal" 、"percent" 、"currency" 的曾孙、"unit" 的孙)必须是包含
"positivePattern" 、"zeroPattern" 和 "negativePattern" 键的记录。
上述字段(依赖符号的 pattern 字段)的值必须是包含子串 "{number}" 的字符串。
"positivePattern" 必须包含子串 "{plusSign}" 但不包含 "{minusSign}" ;"negativePattern" 必须包含子串 "{minusSign}" 但不包含
"{plusSign}" ;"zeroPattern" 不得包含 "{plusSign}" 或 "{minusSign}" 。
此外,"percent" 字段中的值还必须包含子串 "{percentSign}" ;"currency"
字段中的值还必须包含以下子串之一:"{currencyCode}" 、"{currencyPrefix}" 或 "{currencySuffix}" ;"unit"
字段中的值还必须包含以下子串之一:"{unitPrefix}" 或 "{unitSuffix}" 。
当这些 pattern 字符串按 ECMA-262 描述的 UTF-16 编码代码点序列解释时,不能包含 Unicode 标准中 General Category "Number, decimal digit"
的任何代码点。
[[LocaleData]] .[[<locale >]] 还必须为所有 locale 值 locale 提供 [[notationSubPatterns]]
字段。该字段的值必须是一个记录,包含两个字段:[[scientific]] 和 [[compact]] 。[[scientific]] 字段必须是包含子串
"{number}" 、"{scientificSeparator}" 和 "{scientificExponent}" 的字符串。[[compact]] 字段必须是一个记录,包含两个字段:"short"
和 "long" 。每个字段必须是一个记录,包含所有实现支持的 compact notation 离散数量级的整数键。每个字段必须是一个字符串值,可以包含子串 "{number}" 。"short"
下的字符串必须包含子串 "{compactSymbol}" ,"long" 下的字符串必须包含子串 "{compactName}" 。
Note 2
推荐实现使用由 Common Locale Data Repository 提供的区域数据(可在
https://cldr.unicode.org/ 获取)。
16.3 Intl.NumberFormat 原型对象的属性
Intl.NumberFormat 原型对象 :
是 %Intl.NumberFormat.prototype% 。
是一个普通对象。
不是 Intl.NumberFormat 实例,不具有 [[InitializedNumberFormat]] 内部槽或 Intl.NumberFormat 实例对象的其他内部槽。
有一个 [[Prototype]] 内部槽,其值为 %Object.prototype% 。
16.3.1 Intl.NumberFormat.prototype.constructor
Intl.NumberFormat.prototype.constructor 的初始值为 %Intl.NumberFormat% 。
16.3.2 Intl.NumberFormat.prototype.resolvedOptions ( )
此函数提供对对象初始化期间计算的区域和选项的访问。
令 nf 为 this 值。 如果实现支持 4.3 Note 1 的规范可选构造器模式,则 设置 nf 为 ? UnwrapNumberFormat (nf )。 执行 ? RequireInternalSlot (nf , [[InitializedNumberFormat]] )。 令 options 为 OrdinaryObjectCreate (%Object.prototype% )。 对 Table 28 的每一行(除表头行外),按表顺序执行: 令 p 为当前行的 Property 值。 令 v 为 nf 的内部槽,其名称为当前行的 Internal Slot 值。 如果 v 不是 undefined ,则 如果当前行有 Conversion 值,则 断言:当前行的 Conversion 值为 number 。 设置 v 为 𝔽 (v )。 执行 ! CreateDataPropertyOrThrow (options , p , v )。 返回 options 。
Table 28: NumberFormat 实例的 Resolved Options
内部槽
属性
转换
[[Locale]]
"locale"
[[NumberingSystem]]
"numberingSystem"
[[Style]]
"style"
[[Currency]]
"currency"
[[CurrencyDisplay]]
"currencyDisplay"
[[CurrencySign]]
"currencySign"
[[Unit]]
"unit"
[[UnitDisplay]]
"unitDisplay"
[[MinimumIntegerDigits]]
"minimumIntegerDigits"
number
[[MinimumFractionDigits]]
"minimumFractionDigits"
number
[[MaximumFractionDigits]]
"maximumFractionDigits"
number
[[MinimumSignificantDigits]]
"minimumSignificantDigits"
number
[[MaximumSignificantDigits]]
"maximumSignificantDigits"
number
[[UseGrouping]]
"useGrouping"
[[Notation]]
"notation"
[[CompactDisplay]]
"compactDisplay"
[[SignDisplay]]
"signDisplay"
[[RoundingIncrement]]
"roundingIncrement"
number
[[RoundingMode]]
"roundingMode"
[[ComputedRoundingPriority]]
"roundingPriority"
[[TrailingZeroDisplay]]
"trailingZeroDisplay"
16.3.3 获取 Intl.NumberFormat.prototype.format
Intl.NumberFormat.prototype.format 是一个访问器属性,其 set 访问器函数为 undefined 。其 get 访问器函数执行以下步骤:
令 nf 为 this 值。 如果实现支持 4.3 Note 1 的规范可选构造器模式,则 设置 nf 为 ? UnwrapNumberFormat (nf )。 执行 ? RequireInternalSlot (nf , [[InitializedNumberFormat]] )。 如果 nf .[[BoundFormat]] 为 undefined ,则 令 F 为一个新的内置函数对象,如 Number Format Functions (16.5.2 ) 所定义。 设置 F .[[NumberFormat]] 为 nf 。 设置 nf .[[BoundFormat]] 为 F 。 返回 nf .[[BoundFormat]] 。
Note
返回的函数绑定到 nf ,因此可以直接传递给 Array.prototype.map 或其他函数。
这是历史遗留的惯例,作为兼容现有程序而保留,不再用于新特性。
16.3.4 Intl.NumberFormat.prototype.formatRange ( start , end )
当调用 formatRange 方法并传入参数 start 和 end 时,执行以下步骤:
令 nf 为 this 值。 执行 ? RequireInternalSlot (nf , [[InitializedNumberFormat]] )。 如果 start 为 undefined 或 end 为 undefined ,抛出 TypeError 异常。 令 x 为 ? ToIntlMathematicalValue (start )。 令 y 为 ? ToIntlMathematicalValue (end )。 返回 ? FormatNumericRange (nf , x , y )。
16.3.5 Intl.NumberFormat.prototype.formatRangeToParts ( start , end )
当调用 formatRangeToParts 方法并传入参数 start 和 end 时,执行以下步骤:
令 nf 为 this 值。 执行 ? RequireInternalSlot (nf , [[InitializedNumberFormat]] )。 如果 start 为 undefined 或 end 为 undefined ,抛出 TypeError 异常。 令 x 为 ? ToIntlMathematicalValue (start )。 令 y 为 ? ToIntlMathematicalValue (end )。 返回 ? FormatNumericRangeToParts (nf , x , y )。
16.3.6 Intl.NumberFormat.prototype.formatToParts ( value )
当调用 formatToParts 方法并传入可选参数 value 时,执行以下步骤:
令 nf 为 this 值。 执行 ? RequireInternalSlot (nf , [[InitializedNumberFormat]] )。 令 x 为 ? ToIntlMathematicalValue (value )。 返回 FormatNumericToParts (nf , x )。
16.3.7 Intl.NumberFormat.prototype [ %Symbol.toStringTag% ]
%Symbol.toStringTag% 属性的初始值为字符串 "Intl.NumberFormat" 。
该属性具有 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : true } 属性。
16.4 Intl.NumberFormat 实例的属性
Intl.NumberFormat 实例是普通对象,继承自 %Intl.NumberFormat.prototype% 。
Intl.NumberFormat 实例具有 [[InitializedNumberFormat]] 内部槽。
Intl.NumberFormat 实例还具有多个内部槽,这些槽由 Intl.NumberFormat 构造函数 计算:
[[Locale]] 是一个字符串,表示用于格式化的语言标签 。
[[LocaleData]] 是一个记录,表示实现用于格式化的数据。它是 %Intl.NumberFormat% .[[LocaleData]] 中与 [[Locale]] 或其前缀相关的条目的值。
[[NumberingSystem]] 是一个字符串,表示用于格式化的 Unicode 数字系统标识符 。
[[Style]] 是字符串 "decimal" 、"currency" 、"percent" 或 "unit" 之一,标识所测量的数量类型。
[[Currency]] 是一个字符串,表示货币代码,仅在 [[Style]] 为 "currency" 时使用。
[[CurrencyDisplay]] 是字符串 "code" 、"symbol" 、"narrowSymbol" 或 "name" 之一,指定货币显示方式,仅在 [[Style]] 为
"currency" 时使用。
[[CurrencySign]] 是字符串 "standard" 或 "accounting" 之一,指定负数是否以会计格式显示,仅在 [[Style]] 为 "currency" 且
[[SignDisplay]] 不为 "never" 时使用。
[[Unit]] 是核心单位标识符 ,仅在 [[Style]] 为 "unit" 时使用。
[[UnitDisplay]] 是字符串 "short" 、"narrow" 或 "long" 之一,指定单位显示方式,仅在 [[Style]] 为 "unit" 时使用。
[[MinimumIntegerDigits]] 是非负整数,表示最小整数位数。必要时会用前导零填充。
[[MinimumFractionDigits]] 和 [[MaximumFractionDigits]] 是非负整数,表示最小和最大小数位数。必要时会四舍五入或用尾随零填充。仅在 [[RoundingType]] 为
fraction-digits 、more-precision 或 less-precision 时使用。
[[MinimumSignificantDigits]] 和 [[MaximumSignificantDigits]] 是正整数,表示最小和最大有效位数。若存在,格式化器会显示所需的有效位数。仅在
[[RoundingType]] 为 significant-digits 、more-precision 或 less-precision 时使用。
[[UseGrouping]] 是布尔值或字符串,表示何时使用分组分隔符。分组分隔符的位置及是否显示由实现定义。"always" 表示尽可能显示分组分隔符;"min2" 表示至少有2位时显示;"auto"
表示按区域偏好显示。false 禁用分组分隔符。
[[RoundingType]] 是 fraction-digits 、significant-digits 、more-precision 或 less-precision
之一,表示舍入策略。fraction-digits 按 [[MinimumFractionDigits]] 和 [[MaximumFractionDigits]] 舍入;significant-digits 按
[[MinimumSignificantDigits]] 和 [[MaximumSignificantDigits]] 舍入;more-precision 或 less-precision
使用上述四项并按特定规则区分。[[RoundingType]] 由 "roundingPriority" 选项派生。
[[ComputedRoundingPriority]] 是字符串 "auto" 、"morePrecision" 或 "lessPrecision" 之一,仅用于 16.3.2 将 [[RoundingType]] 转换回有效的
"roundingPriority" 选项。
[[Notation]] 是字符串 "standard" 、"scientific" 、"engineering" 或 "compact" 之一,指定格式化数字的显示方式。
[[CompactDisplay]] 是字符串 "short" 或 "long" 之一,指定紧凑记法的显示方式,仅在 [[Notation]] 为 "compact" 时使用。
[[SignDisplay]] 是字符串 "auto" 、"always" 、"never" 、"exceptZero" 或 "negative" 之一,指定何时显示符号(非 "auto"
时分别对应总是显示、从不显示、仅非零显示、仅负数非零显示)。
在科学记法中,该槽影响尾数的符号显示,不影响指数。
[[RoundingIncrement]] 是一个整数,能整除 10、100、1000 或 10000,表示舍入增量。例如 [[MaximumFractionDigits]] 为 2 且
[[RoundingIncrement]] 为 5,则数字舍入到最接近的 0.05。
[[RoundingMode]] 是 舍入模式 ,为 Table 29 的 Identifier 列中的字符串之一。
[[TrailingZeroDisplay]] 是字符串 "auto" 或 "stripIfInteger" 之一,表示是否在格式化为整数时去除尾随零。
Table 29: Intl.NumberFormat 的舍入模式
Identifier
描述
示例:舍入到 0 小数位
-1.5
0.4
0.5
0.6
1.5
"ceil"
向正无穷舍入
⬆️ [-1]
⬆️ [1]
⬆️ [1]
⬆️ [1]
⬆️ [2]
"floor"
向负无穷舍入
⬇️ [-2]
⬇️ [0]
⬇️ [0]
⬇️ [0]
⬇️ [1]
"expand"
远离零舍入
⬇️ [-2]
⬆️ [1]
⬆️ [1]
⬆️ [1]
⬆️ [2]
"trunc"
向零舍入
⬆️ [-1]
⬇️ [0]
⬇️ [0]
⬇️ [0]
⬇️ [1]
"halfCeil"
中间值向正无穷舍入
⬆️ [-1]
⬇️ [0]
⬆️ [1]
⬆️ [1]
⬆️ [2]
"halfFloor"
中间值向负无穷舍入
⬇️ [-2]
⬇️ [0]
⬇️ [0]
⬆️ [1]
⬇️ [1]
"halfExpand"
中间值远离零舍入
⬇️ [-2]
⬇️ [0]
⬆️ [1]
⬆️ [1]
⬆️ [2]
"halfTrunc"
中间值向零舍入
⬆️ [-1]
⬇️ [0]
⬇️ [0]
⬆️ [1]
⬇️ [1]
"halfEven"
中间值向偶数舍入增量倍数
⬇️ [-2]
⬇️ [0]
⬇️ [0]
⬆️ [1]
⬆️ [2]
Note 示例用于说明每个选项的独特行为。⬆️ 表示“向正无穷舍入”;⬇️ 表示“向负无穷舍入”。
最后,Intl.NumberFormat 实例具有 [[BoundFormat]] 内部槽,用于缓存 format 访问器 (16.3.3 ) 返回的函数。
16.5 NumberFormat 对象的抽象操作
16.5.1 CurrencyDigits ( currency )
The implementation-defined abstract operation CurrencyDigits takes argument currency (一个字符串) and returns 一个非负整数. It performs the following steps when called:
断言:IsWellFormedCurrencyCode (currency ) 为 true 。 返回一个非负整数,表示格式化该货币对应数量时使用的小数位数。如果没有可用的信息,则返回 2。
16.5.2 数字格式化函数
数字格式化函数是一个匿名内置函数,具有 [[NumberFormat]] 内部槽。
当数字格式化函数 F 被调用并传入可选参数 value 时,执行以下步骤:
令 nf 为 F .[[NumberFormat]] 。 断言:nf 是一个对象且具有 [[InitializedNumberFormat]] 内部槽。 如果未提供 value ,令 value 为 undefined 。 令 x 为 ? ToIntlMathematicalValue (value )。 返回 FormatNumeric (nf , x )。
数字格式化函数的 "length" 属性为 1 𝔽 。
16.5.3 FormatNumericToString ( intlObject , x )
The abstract operation FormatNumericToString takes arguments intlObject (一个对象) and x (一个数学值或 negative-zero ) and returns 一个包含 [[RoundedNumber]] (数学值或 negative-zero )和 [[FormattedString]] (字符串)的记录. It performs the following steps when called:
断言:intlObject 具有
[[RoundingMode]] 、[[RoundingType]] 、[[MinimumSignificantDigits]] 、[[MaximumSignificantDigits]] 、[[MinimumIntegerDigits]] 、[[MinimumFractionDigits]] 、[[MaximumFractionDigits]] 、[[RoundingIncrement]]
和 [[TrailingZeroDisplay]] 内部槽。 如果 x 是 negative-zero ,则 令 sign 为 negative 。 设置 x 为 0。 否则, 断言:x 是一个数学值。 如果 x < 0,令 sign 为 negative ;否则令 sign 为 positive 。 如果 sign 为 negative ,则 设置 x 为 -x 。 令 unsignedRoundingMode 为 GetUnsignedRoundingMode (intlObject .[[RoundingMode]] , sign )。 如果 intlObject .[[RoundingType]] 为 significant-digits ,则 令 result 为 ToRawPrecision(x , intlObject .[[MinimumSignificantDigits]] ,
intlObject .[[MaximumSignificantDigits]] , unsignedRoundingMode )。 否则如果 intlObject .[[RoundingType]] 为 fraction-digits ,则 令 result 为 ToRawFixed(x , intlObject .[[MinimumFractionDigits]] , intlObject .[[MaximumFractionDigits]] ,
intlObject .[[RoundingIncrement]] , unsignedRoundingMode )。 否则, 令 sResult 为 ToRawPrecision(x , intlObject .[[MinimumSignificantDigits]] ,
intlObject .[[MaximumSignificantDigits]] , unsignedRoundingMode )。 令 fResult 为 ToRawFixed(x , intlObject .[[MinimumFractionDigits]] , intlObject .[[MaximumFractionDigits]] ,
intlObject .[[RoundingIncrement]] , unsignedRoundingMode )。 如果 fResult .[[RoundingMagnitude]] < sResult .[[RoundingMagnitude]] ,令 fixedIsMorePrecise 为 true ;否则令
fixedIsMorePrecise 为 false 。 如果 intlObject .[[RoundingType]] 为 more-precision 且 fixedIsMorePrecise 为 true ,则 令 result 为 fResult 。 否则如果 intlObject .[[RoundingType]] 为 less-precision 且 fixedIsMorePrecise 为 false ,则 令 result 为 fResult 。 否则, 令 result 为 sResult 。 设置 x 为 result .[[RoundedNumber]] 。 令 string 为 result .[[FormattedString]] 。 如果 intlObject .[[TrailingZeroDisplay]] 为 "stripIfInteger" 且 x modulo 1 = 0 ,则 令 i 为 StringIndexOf (string , "." , 0)。 如果 i 不为 not-found ,设置 string 为从 0 到 i 的子字符串。 令 int 为 result .[[IntegerDigitsCount]] 。 令 minInteger 为 intlObject .[[MinimumIntegerDigits]] 。 如果 int < minInteger ,则 令 forwardZeros 为 minInteger - int 个 0x0030(数字零)组成的字符串。 设置 string 为 forwardZeros 与 string 的字符串拼接。 如果 sign 为 negative ,则 如果 x 为 0,设置 x 为 negative-zero 。否则设置 x 为 -x 。 返回记录 { [[RoundedNumber]] : x , [[FormattedString]] : string }。
16.5.4 PartitionNumberPattern ( numberFormat , x )
The abstract operation PartitionNumberPattern takes arguments numberFormat (一个已初始化为 NumberFormat 的对象) and x (一个 Intl 数学值) and returns 一个包含 [[Type]] (字符串)和 [[Value]] (字符串)字段的记录列表. It performs the following steps when called:
令 exponent 为 0。 如果 x 是 not-a-number ,则 令 n 为表示 NaN 的 ILD 字符串值。 否则如果 x 是 positive-infinity ,则 令 n 为表示正无穷的 ILD 字符串值。 否则如果 x 是 negative-infinity ,则 令 n 为表示负无穷的 ILD 字符串值。 否则, 如果 x 不是 negative-zero ,则 断言:x 是一个数学值。 如果 numberFormat .[[Style]] 为 "percent" ,设置 x 为 100 × x 。 设置 exponent 为 ComputeExponent (numberFormat , x )。 设置 x 为 x × 10-exponent 。 令 formatNumberResult 为 FormatNumericToString (numberFormat , x )。 令 n 为 formatNumberResult .[[FormattedString]] 。 设置 x 为 formatNumberResult .[[RoundedNumber]] 。 令 pattern 为 GetNumberFormatPattern (numberFormat , x )。 令 result 为一个新的空列表。 令 patternParts 为 PartitionPattern (pattern )。 对每个记录 { [[Type]] , [[Value]] } patternPart 属于 patternParts ,执行 令 p 为 patternPart .[[Type]] 。 如果 p 为 "literal" ,则 向 result 添加记录 { [[Type]] : "literal" , [[Value]] : patternPart .[[Value]] }。 否则如果 p 为 "number" ,则 令 notationSubParts 为 PartitionNotationSubPattern (numberFormat , x , n , exponent )。 设置 result 为 result 与 notationSubParts 的列表拼接。 否则如果 p 为 "plusSign" ,则 令 plusSignSymbol 为表示加号的 ILND 字符串。 向 result 添加记录 { [[Type]] : "plusSign" , [[Value]] : plusSignSymbol }。 否则如果 p 为 "minusSign" ,则 令 minusSignSymbol 为表示减号的 ILND 字符串。 向 result 添加记录 { [[Type]] : "minusSign" , [[Value]] : minusSignSymbol }。 否则如果 p 为 "percentSign" 且 numberFormat .[[Style]] 为 "percent" ,则 令 percentSignSymbol 为表示百分号的 ILND 字符串。 向 result 添加记录 { [[Type]] : "percentSign" , [[Value]] : percentSignSymbol }。 否则如果 p 为 "unitPrefix" 且 numberFormat .[[Style]] 为 "unit" ,则 令 unit 为 numberFormat .[[Unit]] 。 令 unitDisplay 为 numberFormat .[[UnitDisplay]] 。 令 mu 为在 unitDisplay 形式下,表示 unit 在 x 前的 ILD 字符串值,可能依赖于语言的不同复数形式。 向 result 添加记录 { [[Type]] : "unit" , [[Value]] : mu }。 否则如果 p 为 "unitSuffix" 且 numberFormat .[[Style]] 为 "unit" ,则 令 unit 为 numberFormat .[[Unit]] 。 令 unitDisplay 为 numberFormat .[[UnitDisplay]] 。 令 mu 为在 unitDisplay 形式下,表示 unit 在 x 后的 ILD 字符串值,可能依赖于语言的不同复数形式。 向 result 添加记录 { [[Type]] : "unit" , [[Value]] : mu }。 否则如果 p 为 "currencyCode" 且 numberFormat .[[Style]] 为 "currency" ,则 令 currency 为 numberFormat .[[Currency]] 。 令 cd 为 currency 。 向 result 添加记录 { [[Type]] : "currency" , [[Value]] : cd }。 否则如果 p 为 "currencyPrefix" 且 numberFormat .[[Style]] 为 "currency" ,则 令 currency 为 numberFormat .[[Currency]] 。 令 currencyDisplay 为 numberFormat .[[CurrencyDisplay]] 。 令 cd 为在 currencyDisplay 形式下,表示 currency 在 x 前的 ILD 字符串值,可能依赖于语言的不同复数形式。 向 result 添加记录 { [[Type]] : "currency" , [[Value]] : cd }。 否则如果 p 为 "currencySuffix" 且 numberFormat .[[Style]] 为 "currency" ,则 令 currency 为 numberFormat .[[Currency]] 。 令 currencyDisplay 为 numberFormat .[[CurrencyDisplay]] 。 令 cd 为在 currencyDisplay 形式下,表示 currency 在 x 后的 ILD 字符串值,可能依赖于语言的不同复数形式。如果实现没有这样的表示,则使用 currency 本身。 向 result 添加记录 { [[Type]] : "currency" , [[Value]] : cd }。 否则, 令 unknown 为基于 x 和 p 的 ILND 字符串。 向 result 添加记录 { [[Type]] : "unknown" , [[Value]] : unknown }。 返回 result 。
16.5.5 PartitionNotationSubPattern ( numberFormat , x , n , exponent )
The abstract operation PartitionNotationSubPattern takes arguments numberFormat (一个 Intl.NumberFormat), x (一个 Intl 数学值), n (一个字符串), and exponent (一个整数) and returns 一个包含 [[Type]] (字符串)和 [[Value]] (字符串)字段的记录列表. It performs the following steps when called:
令 result 为一个新的空列表。 如果 x 是 not-a-number ,则 向 result 添加记录 { [[Type]] : "nan" , [[Value]] : n }。 否则如果 x 是 positive-infinity 或 negative-infinity ,则 向 result 添加记录 { [[Type]] : "infinity" , [[Value]] : n }。 否则, 令 notationSubPattern 为 GetNotationSubPattern (numberFormat , exponent )。 令 patternParts 为 PartitionPattern (notationSubPattern )。 对每个记录 { [[Type]] , [[Value]] } patternPart 属于 patternParts ,执行 令 p 为 patternPart .[[Type]] 。 如果 p 为 "literal" ,则 向 result 添加记录 { [[Type]] : "literal" , [[Value]] : patternPart .[[Value]] }。 否则如果 p 为 "number" ,则 如果 numberFormat .[[NumberingSystem]] 匹配下表 Numbering System 列中的某个值,则 令 digits 为该行 Digits 列指定的码点列表。 断言:digits 的长度为 10。 令 transliterated 为空字符串。 令 len 为 n 的长度。 令 position 为 0。 重复,直到 position < len , 令 c 为 n 中索引 position 的码元。 如果 0x0030 ≤ c ≤ 0x0039,则 注:c 是 ASCII 数字。 令 i 为 c - 0x0030。 设置 c 为 CodePointsToString (« digits [i ] »)。 设置 transliterated 为 transliterated 与 c 的字符串拼接。 设置 position 为 position + 1。 设置 n 为 transliterated 。 否则, 使用实现相关算法将 n 映射为该数字系统下的适当表示。 令 decimalSepIndex 为 StringIndexOf (n , "." , 0)。 如果 decimalSepIndex 不为 not-found 且 decimalSepIndex > 0,则 令 integer 为 n 从 0 到 decimalSepIndex 的子字符串。 令 fraction 为 n 从 decimalSepIndex + 1 开始的子字符串。 否则, 令 integer 为 n 。 令 fraction 为 undefined 。 如果 numberFormat .[[UseGrouping]] 为 false ,则 向 result 添加记录 { [[Type]] : "integer" , [[Value]] : integer }。 否则, 令 groupSepSymbol 为表示分组分隔符的 ILND 字符串。 令 groups 为一个列表,其元素为 integer 中由 ILND 集合定义的位置的子字符串,顺序为从左到右,可能依赖于 numberFormat .[[UseGrouping]] 的值。 断言:groups 列表的元素数量大于 0。 重复,直到 groups 列表为空, 移除第一个元素,令 integerGroup 为该元素的值。 向 result 添加记录 { [[Type]] : "integer" , [[Value]] : integerGroup }。 如果 groups 列表不为空,则 向 result 添加记录 { [[Type]] : "group" , [[Value]] : groupSepSymbol }。 如果 fraction 不为 undefined ,则 令 decimalSepSymbol 为表示小数分隔符的 ILND 字符串。 向 result 添加记录 { [[Type]] : "decimal" , [[Value]] : decimalSepSymbol }。 向 result 添加记录 { [[Type]] : "fraction" , [[Value]] : fraction }。 否则如果 p 为 "compactSymbol" ,则 令 compactSymbol 为表示 exponent 的短形式 ILD 字符串,可能依赖于语言的不同复数形式。实现必须能提供该字符串,否则模式不会有 "{compactSymbol}" 占位符。 向 result 添加记录 { [[Type]] : "compact" , [[Value]] : compactSymbol }。 否则如果 p 为 "compactName" ,则 令 compactName 为表示 exponent 的长形式 ILD 字符串,可能依赖于语言的不同复数形式。实现必须能提供该字符串,否则模式不会有 "{compactName}" 占位符。 向 result 添加记录 { [[Type]] : "compact" , [[Value]] : compactName }。 否则如果 p 为 "scientificSeparator" ,则 令 scientificSeparator 为表示指数分隔符的 ILND 字符串。 向 result 添加记录 { [[Type]] : "exponentSeparator" , [[Value]] : scientificSeparator }。 否则如果 p 为 "scientificExponent" ,则 如果 exponent < 0,则 令 minusSignSymbol 为表示减号的 ILND 字符串。 向 result 添加记录 { [[Type]] : "exponentMinusSign" , [[Value]] : minusSignSymbol }。 令 exponent 为 -exponent 。 令 exponentResult 为 ToRawFixed(exponent , 0, 0, 1, undefined )。 向 result 添加记录 { [[Type]] : "exponentInteger" , [[Value]] : exponentResult .[[FormattedString]] }。 否则, 令 unknown 为基于 x 和 p 的 ILND 字符串。 向 result 添加记录 { [[Type]] : "unknown" , [[Value]] : unknown }。 返回 result 。
Table 30: 具有简单数字映射的数字系统
数字系统
数字
adlm
U+1E950 到 U+1E959
ahom
U+11730 到 U+11739
arab
U+0660 到 U+0669
arabext
U+06F0 到 U+06F9
bali
U+1B50 到 U+1B59
beng
U+09E6 到 U+09EF
bhks
U+11C50 到 U+11C59
brah
U+11066 到 U+1106F
cakm
U+11136 到 U+1113F
cham
U+AA50 到 U+AA59
deva
U+0966 到 U+096F
diak
U+11950 到 U+11959
fullwide
U+FF10 到 U+FF19
gara
U+10D40 到 U+10D49
gong
U+11DA0 到 U+11DA9
gonm
U+11D50 到 U+11D59
gujr
U+0AE6 到 U+0AEF
gukh
U+16130 到 U+16139
guru
U+0A66 到 U+0A6F
hanidec
U+3007, U+4E00, U+4E8C, U+4E09, U+56DB, U+4E95, U+516D, U+4E03, U+516B, U+4E5D
hmng
U+16B50 到 U+16B59
hmnp
U+1E140 到 U+1E149
java
U+A9D0 到 U+A9D9
kali
U+A900 到 U+A909
kawi
U+11F50 到 U+11F59
khmr
U+17E0 到 U+17E9
knda
U+0CE6 到 U+0CEF
krai
U+16D70 到 U+16D79
lana
U+1A80 到 U+1A89
lanatham
U+1A90 到 U+1A99
laoo
U+0ED0 到 U+0ED9
latn
U+0030 到 U+0039
lepc
U+1C40 到 U+1C49
limb
U+1946 到 U+194F
mathbold
U+1D7CE 到 U+1D7D7
mathdbl
U+1D7D8 到 U+1D7E1
mathmono
U+1D7F6 到 U+1D7FF
mathsanb
U+1D7EC 到 U+1D7F5
mathsans
U+1D7E2 到 U+1D7EB
mlym
U+0D66 到 U+0D6F
modi
U+11650 到 U+11659
mong
U+1810 到 U+1819
mroo
U+16A60 到 U+16A69
mtei
U+ABF0 到 U+ABF9
mymr
U+1040 到 U+1049
mymrepka
U+116DA 到 U+116E3
mymrpao
U+116D0 到 U+116D9
mymrshan
U+1090 到 U+1099
mymrtlng
U+A9F0 到 U+A9F9
nagm
U+1E4F0 到 U+1E4F9
newa
U+11450 到 U+11459
nkoo
U+07C0 到 U+07C9
olck
U+1C50 到 U+1C59
onao
U+1E5F1 到 U+1E5FA
orya
U+0B66 到 U+0B6F
osma
U+104A0 到 U+104A9
outlined
U+1CCF0 到 U+1CCF9
rohg
U+10D30 到 U+10D39
saur
U+A8D0 到 U+A8D9
segment
U+1FBF0 到 U+1FBF9
shrd
U+111D0 到 U+111D9
sind
U+112F0 到 U+112F9
sinh
U+0DE6 到 U+0DEF
sora
U+110F0 到 U+110F9
sund
U+1BB0 到 U+1BB9
sunu
U+11BF0 到 U+11BF9
takr
U+116C0 到 U+116C9
talu
U+19D0 到 U+19D9
tamldec
U+0BE6 到 U+0BEF
telu
U+0C66 到 U+0C6F
thai
U+0E50 到 U+0E59
tibt
U+0F20 到 U+0F29
tirh
U+114D0 到 U+114D9
tnsa
U+16AC0 到 U+16AC9
tols
U+11DE0 到 U+11DE9
vaii
U+A620 到 U+A629
wara
U+118E0 到 U+118E9
wcho
U+1E2F0 到 U+1E2F9
Note 1
计算依赖于
ILD 和
ILND 字符串值,以及数字字符串中的位置,这些都取决于
numberFormat 的有效区域,或有效区域和数字系统。上述
ILD 和
ILND 字符串值(货币名称除外)不得包含 Unicode
标准中 General Category "Number, decimal digit" 的任何码点。
Note 2
推荐实现使用由 Common Locale Data Repository 提供的区域数据(可在
https://cldr.unicode.org/ 获取)。
16.5.6 FormatNumeric ( numberFormat , x )
The abstract operation FormatNumeric takes arguments numberFormat (一个 Intl.NumberFormat) and x (一个 Intl 数学值) and returns 一个字符串. It performs the following steps when called:
令 parts 为 PartitionNumberPattern (numberFormat , x )。 令 result 为空字符串。 对 parts 中每个记录 { [[Type]] , [[Value]] } part ,执行 设置 result 为 result 与 part .[[Value]] 的字符串拼接。 返回 result 。
16.5.7 FormatNumericToParts ( numberFormat , x )
The abstract operation FormatNumericToParts takes arguments numberFormat (一个 Intl.NumberFormat) and x (一个 Intl 数学值) and returns 一个数组. It performs the following steps when called:
令 parts 为 PartitionNumberPattern (numberFormat , x )。 令 result 为 ! ArrayCreate (0)。 令 n 为 0。 对 parts 中每个记录 { [[Type]] , [[Value]] } part ,执行 令 O 为 OrdinaryObjectCreate (%Object.prototype% )。 执行 ! CreateDataPropertyOrThrow (O , "type" , part .[[Type]] )。 执行 ! CreateDataPropertyOrThrow (O , "value" , part .[[Value]] )。 执行 ! CreateDataPropertyOrThrow (result , ! ToString (𝔽 (n )), O )。 将 n 增加 1。 返回 result 。
16.5.8
ToRawPrecision (
x : 一个非负数学值,
minPrecision : 1 到 21(含)的整数,
maxPrecision : 1 到 21(含)的整数,
unsignedRoundingMode : Table 31 的无符号舍入模式规范类型,或
undefined ,
): 一个包含 [[FormattedString]] (字符串)、[[RoundedNumber]] (数学值)、[[IntegerDigitsCount]] (整数)、[[RoundingMagnitude]] (整数)的记录
The abstract operation UNKNOWN takes UNPARSEABLE ARGUMENTS. It performs the following steps when called:
令 p 为 maxPrecision 。 如果 x = 0, 令 m 为由 p 个码元 0x0030(数字零)组成的字符串。 令 e 为 0。 令 xFinal 为 0。 否则, 令 n1 和 e1 均为整数,r1 为数学值,r1 = ToRawPrecisionFn(n1 , e1 ,
p ) ,满足 r1 ≤ x 且 r1 最大化。 令 n2 和 e2 均为整数,r2 为数学值,r2 = ToRawPrecisionFn(n2 , e2 ,
p ) ,满足 r2 ≥ x 且 r2 最小化。 令 xFinal 为 ApplyUnsignedRoundingMode(x , r1 , r2 , unsignedRoundingMode )。 如果 xFinal 是 r1 , 令 n 为 n1 。 令 e 为 e1 。 否则, 令 n 为 n2 。 令 e 为 e2 。 令 m 为 n 的十进制表示的数字字符串(顺序,无前导零)。 如果 e ≥ (p - 1), 设置 m 为 m 与 e - p + 1 个码元 0x0030(数字零)的字符串拼接。 令 int 为 e + 1。 否则如果 e ≥ 0, 设置 m 为前 e + 1 个码元、码元 0x002E(句点)、剩余 p - (e + 1) 个码元的字符串拼接。 令 int 为 e + 1。 否则, 断言:e < 0。 设置 m 为 "0." 、-(e + 1) 个码元 0x0030(数字零)、m 的字符串拼接。 令 int 为 1。 如果 m 包含码元 0x002E(句点)且 maxPrecision > minPrecision , 令 cut 为 maxPrecision - minPrecision 。 重复,直到 cut > 0 且 m 的最后一个码元为 0x0030(数字零), 移除 m 的最后一个码元。 设置 cut 为 cut - 1。 如果 m 的最后一个码元为 0x002E(句点), 移除 m 的最后一个码元。 返回记录 { [[FormattedString]] : m , [[RoundedNumber]] : xFinal , [[IntegerDigitsCount]] : int ,
[[RoundingMagnitude]] : e –p +1 }。
16.5.9
ToRawFixed (
x : 一个非负数学值,
minFraction : 0 到 100(含)的整数,
maxFraction : 0 到 100(含)的整数,
roundingIncrement : 一个整数,
unsignedRoundingMode : Table 31 的无符号舍入模式规范类型,或
undefined ,
): 一个包含 [[FormattedString]] (字符串)、[[RoundedNumber]] (数学值)、[[IntegerDigitsCount]] (整数)、[[RoundingMagnitude]] (整数)的记录
The abstract operation UNKNOWN takes UNPARSEABLE ARGUMENTS. It performs the following steps when called:
令 f 为 maxFraction 。 令 n1 为整数,r1 为数学值,r1 = ToRawFixedFn(n1 , f ) ,满足 n1
modulo roundingIncrement = 0 ,r1 ≤ x 且 r1 最大化。 令 n2 为整数,r2 为数学值,r2 = ToRawFixedFn(n2 , f ) ,满足 n2
modulo roundingIncrement = 0 ,r2 ≥ x 且 r2 最小化。 令 xFinal 为 ApplyUnsignedRoundingMode(x , r1 , r2 , unsignedRoundingMode )。 如果 xFinal 是 r1 ,令 n 为 n1 。否则令 n 为 n2 。 如果 n = 0,令 m 为 "0" 。否则令 m 为 n 的十进制表示的数字字符串(顺序,无前导零)。 如果 f ≠ 0, 令 k 为 m 的长度。 如果 k ≤ f , 令 z 为 f + 1 - k 个码元 0x0030(数字零)组成的字符串。 设置 m 为 z 与 m 的字符串拼接。 设置 k 为 f + 1。 令 a 为 m 的前 k - f 个码元,b 为剩余 f 个码元。 设置 m 为 a 、"." 、b 的字符串拼接。 令 int 为 a 的长度。 否则, 令 int 为 m 的长度。 令 cut 为 maxFraction - minFraction 。 重复,直到 cut > 0 且 m 的最后一个码元为 0x0030(数字零), 移除 m 的最后一个码元。 设置 cut 为 cut - 1。 如果 m 的最后一个码元为 0x002E(句点), 移除 m 的最后一个码元。 返回记录 { [[FormattedString]] : m , [[RoundedNumber]] : xFinal , [[IntegerDigitsCount]] : int ,
[[RoundingMagnitude]] : –f }。
16.5.10 UnwrapNumberFormat ( nf )
The abstract operation UnwrapNumberFormat takes argument nf (一个 ECMAScript 语言值) and returns 正常完成包含 ECMAScript 语言值或抛出完成. It performs the following steps when called:
如果 nf 不是对象,抛出 TypeError 异常。 如果 nf 没有 [[InitializedNumberFormat]] 内部槽且 ?
OrdinaryHasInstance (%Intl.NumberFormat% , nf ) 是 true , 返回 ? Get (nf , %Intl% .[[FallbackSymbol]] )。 返回 nf 。
16.5.11 GetNumberFormatPattern ( numberFormat , x )
The abstract operation GetNumberFormatPattern takes arguments numberFormat (一个 Intl.NumberFormat) and x (一个 Intl 数学值) and returns 一个字符串. It performs the following steps when called:
令 resolvedLocaleData 为 numberFormat .[[LocaleData]] 。 令 patterns 为 resolvedLocaleData .[[patterns]] 。 断言:patterns 是一个记录(见 16.2.3 )。 令 style 为 numberFormat .[[Style]] 。 如果 style 是 "percent" , 设置 patterns 为 patterns .[[percent]] 。 否则如果 style 是 "unit" , 令 unit 为 numberFormat .[[Unit]] 。 令 unitDisplay 为 numberFormat .[[UnitDisplay]] 。 设置 patterns 为 patterns .[[unit]] 。 如果 patterns 没有字段 [[<unit >]], 设置 unit 为 "fallback" 。 设置 patterns 为 patterns .[[<unit >]]。 设置 patterns 为 patterns .[[<unitDisplay >]]。 否则如果 style 是 "currency" , 令 currency 为 numberFormat .[[Currency]] 。 令 currencyDisplay 为 numberFormat .[[CurrencyDisplay]] 。 令 currencySign 为 numberFormat .[[CurrencySign]] 。 设置 patterns 为 patterns .[[currency]] 。 如果 patterns 没有字段 [[<currency >]], 设置 currency 为 "fallback" 。 设置 patterns 为 patterns .[[<currency >]]。 设置 patterns 为 patterns .[[<currencyDisplay >]]。 设置 patterns 为 patterns .[[<currencySign >]]。 否则, 断言:style 是 "decimal" 。 设置 patterns 为 patterns .[[decimal]] 。 如果 x 是 negative-infinity , 令 category 为 negative-non-zero 。 否则如果 x 是 negative-zero , 令 category 为 negative-zero 。 否则如果 x 是 not-a-number , 令 category 为 positive-zero 。 否则如果 x 是 positive-infinity , 令 category 为 positive-non-zero 。 否则, 断言:x 是一个数学值。 如果 x < 0, 令 category 为 negative-non-zero 。 否则如果 x > 0, 令 category 为 positive-non-zero 。 否则, 令 category 为 positive-zero 。 令 signDisplay 为 numberFormat .[[SignDisplay]] 。 如果 signDisplay 是 "never" , 令 pattern 为 patterns .[[zeroPattern]] 。 否则如果 signDisplay 是 "auto" , 如果 category 是 positive-non-zero 或 positive-zero , 令 pattern 为 patterns .[[zeroPattern]] 。 否则, 令 pattern 为 patterns .[[negativePattern]] 。 否则如果 signDisplay 是 "always" , 如果 category 是 positive-non-zero 或 positive-zero , 令 pattern 为 patterns .[[positivePattern]] 。 否则, 令 pattern 为 patterns .[[negativePattern]] 。 否则如果 signDisplay 是 "exceptZero" , 如果 category 是 positive-zero 或 negative-zero , 令 pattern 为 patterns .[[zeroPattern]] 。 否则如果 category 是 positive-non-zero , 令 pattern 为 patterns .[[positivePattern]] 。 否则, 令 pattern 为 patterns .[[negativePattern]] 。 否则, 断言:signDisplay 是 "negative" 。 如果 category 是 negative-non-zero , 令 pattern 为 patterns .[[negativePattern]] 。 否则, 令 pattern 为 patterns .[[zeroPattern]] 。 返回 pattern 。
16.5.12 GetNotationSubPattern ( numberFormat , exponent )
The abstract operation GetNotationSubPattern takes arguments numberFormat (一个 Intl.NumberFormat) and exponent (一个整数) and returns 一个字符串. It performs the following steps when called:
令 resolvedLocaleData 为 numberFormat .[[LocaleData]] 。 令 notationSubPatterns 为 resolvedLocaleData .[[notationSubPatterns]] 。 断言:notationSubPatterns 是一个记录(见 16.2.3 )。 令 notation 为 numberFormat .[[Notation]] 。 如果 notation 是 "scientific" 或 notation 是 "engineering" , 返回 notationSubPatterns .[[scientific]] 。 否则如果 exponent 不为 0, 断言:notation 是 "compact" 。 令 compactDisplay 为 numberFormat .[[CompactDisplay]] 。 令 compactPatterns 为 notationSubPatterns .[[compact]] .[[<compactDisplay >]]。 返回 compactPatterns .[[<exponent >]]。 否则, 返回 "{number}" 。
16.5.13 ComputeExponent ( numberFormat , x )
The abstract operation ComputeExponent takes arguments numberFormat (一个 Intl.NumberFormat) and x (一个数学值) and returns 一个整数. It performs the following steps when called:
如果 x = 0, 返回 0。 如果 x < 0, 令 x = -x 。 令 magnitude 为 floor (log10 (x ))。 令 exponent 为 ComputeExponentForMagnitude (numberFormat , magnitude )。 令 x 为 x × 10-exponent 。 令 formatNumberResult 为 FormatNumericToString (numberFormat , x )。 如果 formatNumberResult .[[RoundedNumber]] = 0, 返回 exponent 。 令 newMagnitude 为 floor (log10 (formatNumberResult .[[RoundedNumber]] ))。 如果 newMagnitude 是 magnitude - exponent , 返回 exponent 。 返回 ComputeExponentForMagnitude (numberFormat , magnitude + 1)。
16.5.14 ComputeExponentForMagnitude ( numberFormat , magnitude )
The abstract operation ComputeExponentForMagnitude takes arguments numberFormat (一个 Intl.NumberFormat) and magnitude (一个整数) and returns 一个整数. It performs the following steps when called:
令 notation 为 numberFormat .[[Notation]] 。 如果 notation 是 "standard" , 返回 0。 否则如果 notation 是 "scientific" , 返回 magnitude 。 否则如果 notation 是 "engineering" , 令 thousands 为不大于 magnitude / 3 的最大整数。 返回 thousands × 3。 否则, 断言:notation 是 "compact" 。 令 exponent 为当前区域紧凑记法下缩放给定数量级的 ILD 整数。 返回 exponent 。
16.5.15 运行时语义:StringIntlMV
The syntax-directed operation 运行时语义:StringIntlMV takes no arguments.
Note
It is defined piecewise over the following productions:
StringNumericLiteral ::: StrWhiteSpace opt
返回 0。
StringNumericLiteral :::
StrWhiteSpace opt
StrNumericLiteral
StrWhiteSpace opt
返回 StrNumericLiteral 的 StringIntlMV。
StrNumericLiteral ::: NonDecimalIntegerLiteral
返回 NonDecimalIntegerLiteral 的 MV。
StrDecimalLiteral :::
-
StrUnsignedDecimalLiteral
令 a 为 StrUnsignedDecimalLiteral 的 StringIntlMV。 如果 a 是 0,返回 negative-zero 。 如果 a 是 positive-infinity ,返回 negative-infinity 。 返回 -a 。
StrUnsignedDecimalLiteral ::: Infinity
返回 positive-infinity 。
StrUnsignedDecimalLiteral :::
DecimalDigits
.
DecimalDigits opt
ExponentPart opt
令 a 为第一个 DecimalDigits 的 MV。 如果第二个 DecimalDigits 存在, 令 b 为第二个 DecimalDigits 的 MV。 令 n 为第二个 DecimalDigits 的码点数。 否则, 令 b 为 0。 令 n 为 0。 如果 ExponentPart 存在,令 e 为 ExponentPart 的 MV。否则令 e 为 0。 返回 (a + (b × 10-n )) × 10e 。
StrUnsignedDecimalLiteral :::
.
DecimalDigits
ExponentPart opt
令 b 为 DecimalDigits 的 MV。 如果 ExponentPart 存在,令 e 为 ExponentPart 的 MV。否则令 e 为 0。 令 n 为 DecimalDigits 的码点数。 返回 b × 10e - n 。
StrUnsignedDecimalLiteral :::
DecimalDigits
ExponentPart opt
令 a 为 DecimalDigits 的 MV。 如果 ExponentPart 存在,令 e 为 ExponentPart 的 MV。否则令 e 为 0。 返回 a × 10e 。
16.5.16 ToIntlMathematicalValue ( value )
The abstract operation ToIntlMathematicalValue takes argument value (一个 ECMAScript 语言值) and returns 正常完成包含一个 Intl 数学值或抛出完成. It performs the following steps when called:
令 primValue 为 ? ToPrimitive (value , number )。 如果 primValue 是 BigInt,返回 ℝ (primValue )。 如果 primValue 是字符串, 令 str 为 primValue 。 否则, 令 x 为 ? ToNumber (primValue )。 如果 x 是 -0 𝔽 ,返回 negative-zero 。 令 str 为 Number::toString (x , 10)。 令 text 为 StringToCodePoints (str )。 令 literal 为 ParseText (text , StringNumericLiteral )。 如果 literal 是错误列表,返回 not-a-number 。 令 intlMV 为 literal 的 StringIntlMV。 如果 intlMV 是数学值, 令 rounded 为 RoundMVResult (abs (intlMV ))。 如果 rounded 是 +∞ 𝔽 且 intlMV < 0,返回 negative-infinity 。 如果 rounded 是 +∞ 𝔽 ,返回 positive-infinity 。 如果 rounded 是 +0 𝔽 且 intlMV < 0,返回 negative-zero 。 如果 rounded 是 +0 𝔽 ,返回 0。 返回 intlMV 。
16.5.17 GetUnsignedRoundingMode ( roundingMode , sign )
The abstract operation GetUnsignedRoundingMode takes arguments roundingMode (一个舍入模式 ) and sign (negative 或 positive ) and returns Table 31 的无符号舍入模式 规范类型. It performs the following steps when called:
返回 Table 31 的 Unsigned Rounding Mode 列中,Identifier 列为
roundingMode 且 Sign 列为 sign 的规范类型。
Table 31: 舍入模式 到无符号舍入模式 的转换
Identifier
Sign
Unsigned Rounding Mode
"ceil"
positive
infinity
negative
zero
"floor"
positive
zero
negative
infinity
"expand"
positive
infinity
negative
infinity
"trunc"
positive
zero
negative
zero
"halfCeil"
positive
half-infinity
negative
half-zero
"halfFloor"
positive
half-zero
negative
half-infinity
"halfExpand"
positive
half-infinity
negative
half-infinity
"halfTrunc"
positive
half-zero
negative
half-zero
"halfEven"
positive
half-even
negative
half-even
16.5.18
ApplyUnsignedRoundingMode (
x : 一个数学值,
r1 : 一个数学值,
r2 : 一个数学值,
unsignedRoundingMode : Table 31 的无符号舍入模式规范类型,或
undefined ,
): 一个数学值
The abstract operation UNKNOWN takes UNPARSEABLE ARGUMENTS. It performs the following steps when called:
如果 x 是 r1 ,返回 r1 。 断言:r1 < x < r2 。 断言:unsignedRoundingMode 不为 undefined 。 如果 unsignedRoundingMode 是 zero ,返回 r1 。 如果 unsignedRoundingMode 是 infinity ,返回 r2 。 令 d1 为 x – r1 。 令 d2 为 r2 – x 。 如果 d1 < d2 ,返回 r1 。 如果 d2 < d1 ,返回 r2 。 断言:d1 等于 d2 。 如果 unsignedRoundingMode 是 half-zero ,返回 r1 。 如果 unsignedRoundingMode 是 half-infinity ,返回 r2 。 断言:unsignedRoundingMode 是 half-even 。 令 cardinality 为 (r1 / (r2 – r1 )) modulo 2 。 如果 cardinality 是 0,返回 r1 。 返回 r2 。
16.5.19 PartitionNumberRangePattern ( numberFormat , x , y )
The abstract operation PartitionNumberRangePattern takes arguments numberFormat (一个 Intl.NumberFormat), x (一个 Intl 数学值), and y (一个 Intl 数学值) and returns 正常完成包含字段 [[Type]] (字符串)、[[Value]] (字符串)、[[Source]] (字符串)的记录列表或抛出完成. It performs the following steps when called:
如果 x 是 not-a-number 或 y 是 not-a-number ,抛出 RangeError 异常。 令 xResult 为 PartitionNumberPattern (numberFormat , x )。 令 yResult 为 PartitionNumberPattern (numberFormat , y )。 如果 FormatNumeric (numberFormat , x ) 等于 FormatNumeric (numberFormat , y ), 令 appxResult 为 FormatApproximately (numberFormat , xResult )。 对 appxResult 中每个元素 r ,执行 设置 r .[[Source]] 为 "shared" 。 返回 appxResult 。 令 result 为新的空列表。 对 xResult 中每个元素 r ,执行 将记录 { [[Type]] : r .[[Type]] , [[Value]] : r .[[Value]] , [[Source]] : "startRange" } 添加到 result 。 令 rangeSeparator 为用于分隔两个数字的 ILND 字符串值。 将记录 { [[Type]] : "literal" , [[Value]] : rangeSeparator , [[Source]] : "shared" } 添加到 result 。 对 yResult 中每个元素 r ,执行 将记录 { [[Type]] : r .[[Type]] , [[Value]] : r .[[Value]] , [[Source]] : "endRange" } 添加到 result 。 返回 CollapseNumberRange (numberFormat , result )。
16.5.20 FormatApproximately ( numberFormat , result )
The abstract operation FormatApproximately takes arguments numberFormat (一个 Intl.NumberFormat) and result (字段 [[Type]] (字符串)、[[Value]] (字符串)的记录列表) and returns 字段 [[Type]] (字符串)、[[Value]] (字符串)的记录列表. It performs the following steps when called:
令 approximatelySign 为用于表示数字近似的 ILND 字符串值。 如果 approximatelySign 非空,在 result 的 ILND 索引处插入记录 { [[Type]] : "approximatelySign" , [[Value]] :
approximatelySign }。例如,若 numberFormat 的 [[Locale]] 为 "en-US" 、[[NumberingSystem]] 为 "latn" 、[[Style]] 为
"decimal" ,新记录可能插入到 result 的第一个元素前。 返回 result 。
16.5.21 CollapseNumberRange ( numberFormat , result )
The implementation-defined abstract operation CollapseNumberRange takes arguments numberFormat (一个 Intl.NumberFormat) and result (字段 [[Type]] (字符串)、[[Value]] (字符串)、[[Source]] (字符串)的记录列表) and returns 字段 [[Type]] (字符串)、[[Value]] (字符串)、[[Source]] (字符串)的记录列表.
例如,实现可移除区间分隔符后的货币符号,将 "$3–$5" 转为 "$3–5" 。
实现也可为语法正确性修改记录 [[Value]] 字段,如将 "0.5 miles–1 mile" 转为 "0.5–1 miles" 。
返回 result 未修改保证是 CollapseNumberRange 的正确实现。
16.5.22 FormatNumericRange ( numberFormat , x , y )
The abstract operation FormatNumericRange takes arguments numberFormat (一个 Intl.NumberFormat), x (一个 Intl 数学值), and y (一个 Intl 数学值) and returns 正常完成包含一个字符串或抛出完成. It performs the following steps when called:
令 parts 为 ? PartitionNumberRangePattern (numberFormat , x , y )。 令 result 为空字符串。 对 parts 中每个元素 part ,执行 设置 result 为 result 与 part .[[Value]] 的字符串拼接。 返回 result 。
16.5.23 FormatNumericRangeToParts ( numberFormat , x , y )
The abstract operation FormatNumericRangeToParts takes arguments numberFormat (一个 Intl.NumberFormat), x (一个 Intl 数学值), and y (一个 Intl 数学值) and returns 正常完成包含一个数组或抛出完成. It performs the following steps when called:
令 parts 为 ? PartitionNumberRangePattern (numberFormat , x , y )。 令 result 为 ! ArrayCreate (0)。 令 n 为 0。 对 parts 中每个元素 part ,执行 令 O 为 OrdinaryObjectCreate (%Object.prototype% )。 执行 ! CreateDataPropertyOrThrow (O , "type" , part .[[Type]] )。 执行 ! CreateDataPropertyOrThrow (O , "value" , part .[[Value]] )。 执行 ! CreateDataPropertyOrThrow (O , "source" , part .[[Source]] )。 执行 ! CreateDataPropertyOrThrow (result , ! ToString (𝔽 (n )), O )。 将 n 增加 1。 返回 result 。
17 PluralRules 对象
17.1 Intl.PluralRules 构造函数
Intl.PluralRules 构造函数:
是 %Intl.PluralRules% 。
是 Intl 对象 的 "PluralRules" 属性的初始值。
Intl 对象 所有服务构造属性的通用行为在 9.1 中指定。
17.1.1 Intl.PluralRules ( [ locales [ , options ] ] )
当 Intl.PluralRules 函数以可选参数 locales 和 options 调用时,执行以下步骤:
如果 NewTarget 是 undefined ,抛出 TypeError 异常。 令 pluralRules 为 ? OrdinaryCreateFromConstructor (NewTarget, "%Intl.PluralRules.prototype%" , «
[[InitializedPluralRules]] , [[Locale]] , [[Type]] , [[Notation]] , [[CompactDisplay]] , [[MinimumIntegerDigits]] ,
[[MinimumFractionDigits]] , [[MaximumFractionDigits]] , [[MinimumSignificantDigits]] ,
[[MaximumSignificantDigits]] , [[RoundingType]] , [[RoundingIncrement]] , [[RoundingMode]] ,
[[ComputedRoundingPriority]] , [[TrailingZeroDisplay]] »)。 令 optionsResolution 为 ? ResolveOptions (%Intl.PluralRules% , %Intl.PluralRules% .[[LocaleData]] , locales ,
options , « coerce-options »)。 设置 options 为 optionsResolution .[[Options]] 。 令 r 为 optionsResolution .[[ResolvedLocale]] 。 设置 pluralRules .[[Locale]] 为 r .[[Locale]] 。 令 t 为 ? GetOption (options , "type" , string , « "cardinal" , "ordinal" », "cardinal" )。 设置 pluralRules .[[Type]] 为 t 。 令 notation 为 ? GetOption (options , "notation" , string , « "standard" , "scientific" ,
"engineering" , "compact" », "standard" )。 设置 pluralRules .[[Notation]] 为 notation 。 令 compactDisplay 为 ? GetOption (options , "compactDisplay" , string , « "short" , "long" »,
"short" )。 如果 notation 是 "compact" ,则 设置 pluralRules .[[CompactDisplay]] 为 compactDisplay 。 执行 ? SetNumberFormatDigitOptions (pluralRules , options , 0, 3, notation )。 返回 pluralRules 。
17.2 Intl.PluralRules 构造函数的属性
Intl.PluralRules 构造函数:
17.2.1 Intl.PluralRules.prototype
Intl.PluralRules.prototype 的值为 %Intl.PluralRules.prototype% 。
该属性具有 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : false } 属性。
17.2.2 Intl.PluralRules.supportedLocalesOf ( locales [ , options ] )
当 supportedLocalesOf 方法以参数 locales 和 options 调用时,执行以下步骤:
令 availableLocales 为 %Intl.PluralRules% .[[AvailableLocales]] 。 令 requestedLocales 为 ? CanonicalizeLocaleList (locales )。 返回 ? FilterLocales (availableLocales , requestedLocales , options )。
17.2.3 内部槽
[[AvailableLocales]] 内部槽的值在 9.1 所述约束范围内由实现定义。
[[RelevantExtensionKeys]] 内部槽的值为 « »。
[[ResolutionOptionDescriptors]] 内部槽的值为 « »。
Note 1
[[LocaleData]] 内部槽的值在 9.1 所述约束范围内由实现定义。
Note 2
建议实现使用由 Common Locale Data Repository 提供的区域数据(可在
https://cldr.unicode.org/ 获取)。
17.3 Intl.PluralRules 原型对象的属性
Intl.PluralRules 原型对象 :
是 %Intl.PluralRules.prototype% 。
是一个普通对象。
不是 Intl.PluralRules 实例,不具有 [[InitializedPluralRules]] 内部槽或任何 Intl.PluralRules 实例对象的其他内部槽。
具有 [[Prototype]] 内部槽,其值为 %Object.prototype% 。
17.3.1 Intl.PluralRules.prototype.constructor
Intl.PluralRules.prototype.constructor 的初始值为 %Intl.PluralRules% 。
17.3.2 Intl.PluralRules.prototype.resolvedOptions ( )
此函数提供在对象初始化期间计算的区域和选项的访问。
令 pr 为 this 值。 执行 ? RequireInternalSlot (pr , [[InitializedPluralRules]] )。 令 options 为 OrdinaryObjectCreate (%Object.prototype% )。 令 pluralCategories 为包含所选区域 pr .[[Locale]] 的 PluralRuleSelect 所有可能结果的字符串列表,按如下顺序排序:"zero" , "one" , "two" ,
"few" , "many" , "other" 。 对 Table 32 的每一行(除表头行外),按表顺序执行 令 p 为当前行的 Property 值。 如果 p 是 "pluralCategories" ,则 令 v 为 CreateArrayFromList (pluralCategories )。 否则, 令 v 为 pr 的内部槽,其名称为当前行的 Internal Slot 值。 如果 v 不是 undefined ,则 如果当前行有 Conversion 值,则 断言:当前行的 Conversion 值为 number 。 设置 v 为 𝔽 (v )。 执行 ! CreateDataPropertyOrThrow (options , p , v )。 返回 options 。
Table 32: PluralRules 实例的已解析选项
Internal Slot
Property
Conversion
[[Locale]]
"locale"
[[Type]]
"type"
[[Notation]]
"notation"
[[CompactDisplay]]
"compactDisplay"
[[MinimumIntegerDigits]]
"minimumIntegerDigits"
number
[[MinimumFractionDigits]]
"minimumFractionDigits"
number
[[MaximumFractionDigits]]
"maximumFractionDigits"
number
[[MinimumSignificantDigits]]
"minimumSignificantDigits"
number
[[MaximumSignificantDigits]]
"maximumSignificantDigits"
number
"pluralCategories"
[[RoundingIncrement]]
"roundingIncrement"
number
[[RoundingMode]]
"roundingMode"
[[ComputedRoundingPriority]]
"roundingPriority"
[[TrailingZeroDisplay]]
"trailingZeroDisplay"
17.3.3 Intl.PluralRules.prototype.select ( value )
当 select 方法以参数 value 调用时,执行以下步骤:
令 pr 为 this 值。 执行 ? RequireInternalSlot (pr , [[InitializedPluralRules]] )。 令 n 为 ? ToIntlMathematicalValue (value )。 返回 ResolvePlural (pr , n ).[[PluralCategory]] 。
17.3.4 Intl.PluralRules.prototype.selectRange ( start , end )
当 selectRange 方法以参数 start 和 end 调用时,执行以下步骤:
令 pr 为 this 值。 执行 ? RequireInternalSlot (pr , [[InitializedPluralRules]] )。 如果 start 是 undefined 或 end 是 undefined ,抛出 TypeError 异常。 令 x 为 ? ToIntlMathematicalValue (start )。 令 y 为 ? ToIntlMathematicalValue (end )。 返回 ? ResolvePluralRange (pr , x , y )。
17.3.5 Intl.PluralRules.prototype [ %Symbol.toStringTag% ]
%Symbol.toStringTag% 属性的初始值为字符串 "Intl.PluralRules" 。
该属性具有 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : true } 属性。
17.4 Intl.PluralRules 实例的属性
Intl.PluralRules 实例是普通对象,继承自 %Intl.PluralRules.prototype% 的属性。
Intl.PluralRules 实例具有 [[InitializedPluralRules]] 内部槽。
Intl.PluralRules 实例还具有由 Intl.PluralRules 构造函数 计算的多个内部槽:
[[Locale]] 是字符串值,表示用于复数规则的区域语言标签 。
[[Type]] 是字符串值 "cardinal" 或 "ordinal" ,标识所用复数规则。
[[Notation]] 是字符串值 "standard" 、"scientific" 、"engineering" 或 "compact" ,标识所用记法。
[[CompactDisplay]] 是字符串值 "short" 或 "long" ,指定在使用 "compact" 记法格式化时是否以短形式("5K")或长形式("5
thousand")显示紧凑记法附加词,这在某些情况下会影响复数形式选择。仅当 [[Notation]] 为 "compact" 时使用。
[[MinimumIntegerDigits]] 是非负整数,表示要使用的最小整数位数。
[[MinimumFractionDigits]] 和 [[MaximumFractionDigits]] 是非负整数,表示要使用的最小和最大小数位数。数字会被舍入或填充尾随零(如有必要)。
[[MinimumSignificantDigits]] 和 [[MaximumSignificantDigits]]
是正整数,表示要使用的最小和最大有效数字。要么都不存在,要么都存在;如果存在,则覆盖最小和最大整数及小数位数。
[[RoundingType]] 是 fraction-digits 、significant-digits 、more-precision 或 less-precision ,表示要使用的舍入策略,如
16.4 所述。
[[ComputedRoundingPriority]] 是字符串值 "auto" 、"morePrecision" 或 "lessPrecision" 。仅在 17.3.2 中用于将 [[RoundingType]] 转换回有效的
"roundingPriority" 选项。
[[RoundingIncrement]] 是整数,可将 10、100、1000 或 10000 均分为十分、五分、四分或二分。表示相对于计算的舍入幅度应进行舍入的增量。例如,如果
[[MaximumFractionDigits]] 为 2 且 [[RoundingIncrement]] 为 5,则格式化数字会舍入到最接近的 0.05(“镍舍入”)。
[[RoundingMode]] 标识要使用的舍入模式 。
[[TrailingZeroDisplay]] 是字符串值 "auto" 或 "stripIfInteger" ,表示如果格式化数字为整数(即没有非零小数位),是否去除尾随零。
17.5 PluralRules 对象的抽象操作
17.5.1 PluralRuleSelect ( locale , type , notation , compactDisplay , s )
The implementation-defined abstract operation PluralRuleSelect takes arguments locale (语言标签 ), type ("cardinal" 或 "ordinal" ), notation (字符串), compactDisplay ("short" 或 "long" ), and s (十进制字符串) and returns "zero" , "one" , "two" , "few" , "many" , 或 "other" .
17.5.2 ResolvePlural ( pluralRules , n )
The abstract operation ResolvePlural takes arguments pluralRules (一个 Intl.PluralRules) and n (一个 Intl 数学值) and returns 一个包含字段 [[PluralCategory]] ("zero" , "one" , "two" , "few" , "many" , 或 "other" ) 和. It performs the following steps when called:
如果 n 是 not-a-number ,则 令 s 为表示 NaN 值的 ILD 字符串值。 返回记录 { [[PluralCategory]] : "other" , [[FormattedString]] : s }。 如果 n 是 positive-infinity ,则 令 s 为表示正无穷的 ILD 字符串值。 返回记录 { [[PluralCategory]] : "other" , [[FormattedString]] : s }。 如果 n 是 negative-infinity ,则 令 s 为表示负无穷的 ILD 字符串值。 返回记录 { [[PluralCategory]] : "other" , [[FormattedString]] : s }。 令 res 为 FormatNumericToString (pluralRules , n )。 令 s 为 res .[[FormattedString]] 。 令 locale 为 pluralRules .[[Locale]] 。 令 type 为 pluralRules .[[Type]] 。 令 notation 为 pluralRules .[[Notation]] 。 令 compactDisplay 为 pluralRules .[[CompactDisplay]] 。 令 p 为 PluralRuleSelect (locale , type , notation , compactDisplay , s )。 返回记录 { [[PluralCategory]] : p , [[FormattedString]] : s }。
17.5.3 PluralRuleSelectRange ( locale , type , notation , compactDisplay , xp , yp )
The implementation-defined abstract operation PluralRuleSelectRange takes arguments locale (语言标签 ), type ("cardinal" 或 "ordinal" ), notation (字符串), compactDisplay ("short" 或 "long" ), xp ("zero" , "one" , "two" , "few" , "many" , 或 "other" ), and yp ("zero" , "one" , "two" , "few" , "many" , 或 "other" ) and returns "zero" , "one" , "two" , "few" , "many" , 或 "other" .
17.5.4 ResolvePluralRange ( pluralRules , x , y )
The abstract operation ResolvePluralRange takes arguments pluralRules (一个 Intl.PluralRules), x (一个 Intl 数学值), and y (一个 Intl 数学值) and returns 正常完成,包含 "zero" , "one" , "two" , "few" , "many" , 或 "other" ,或抛出完成. It performs the following steps when called:
如果 x 是 not-a-number 或 y 是 not-a-number ,抛出 RangeError 异常。 令 xp 为 ResolvePlural (pluralRules , x )。 令 yp 为 ResolvePlural (pluralRules , y )。 如果 xp .[[FormattedString]] 等于 yp .[[FormattedString]] ,则 返回 xp .[[PluralCategory]] 。 令 locale 为 pluralRules .[[Locale]] 。 令 type 为 pluralRules .[[Type]] 。 令 notation 为 pluralRules .[[Notation]] 。 令 compactDisplay 为 pluralRules .[[CompactDisplay]] 。 返回 PluralRuleSelectRange (locale , type , notation , compactDisplay , xp .[[PluralCategory]] ,
yp .[[PluralCategory]] )。
18 RelativeTimeFormat 对象
18.1 Intl.RelativeTimeFormat 构造函数
Intl.RelativeTimeFormat 构造函数:
是 %Intl.RelativeTimeFormat% 。
是 Intl 对象 的 "RelativeTimeFormat" 属性的初始值。
Intl 对象 所有服务构造属性的通用行为在 9.1 中指定。
18.1.1 Intl.RelativeTimeFormat ( [ locales [ , options ] ] )
当调用 Intl.RelativeTimeFormat 函数并传入可选参数 locales 和 options 时,执行以下步骤:
如果 NewTarget 是 undefined ,抛出 TypeError 异常。 令 relativeTimeFormat 为 ? OrdinaryCreateFromConstructor (NewTarget, "%Intl.RelativeTimeFormat.prototype%" ,
« [[InitializedRelativeTimeFormat]] , [[Locale]] , [[LocaleData]] , [[Style]] , [[Numeric]] , [[NumberFormat]] ,
[[NumberingSystem]] , [[PluralRules]] »)。 令 optionsResolution 为 ? ResolveOptions (%Intl.RelativeTimeFormat% , %Intl.RelativeTimeFormat% .[[LocaleData]] ,
locales , options , « coerce-options »)。 设置 options 为 optionsResolution .[[Options]] 。 令 r 为 optionsResolution .[[ResolvedLocale]] 。 令 locale 为 r .[[Locale]] 。 设置 relativeTimeFormat .[[Locale]] 为 locale 。 设置 relativeTimeFormat .[[LocaleData]] 为 r .[[LocaleData]] 。 设置 relativeTimeFormat .[[NumberingSystem]] 为 r .[[nu]] 。 令 style 为 ? GetOption (options , "style" , string , « "long" , "short" , "narrow" », "long" )。 设置 relativeTimeFormat .[[Style]] 为 style 。 令 numeric 为 ? GetOption (options , "numeric" , string , « "always" , "auto" », "always" )。 设置 relativeTimeFormat .[[Numeric]] 为 numeric 。 令 nfOptions 为 OrdinaryObjectCreate (null )。 执行 ! CreateDataPropertyOrThrow (nfOptions , "numberingSystem" , relativeTimeFormat .[[NumberingSystem]] )。 令 relativeTimeFormat .[[NumberFormat]] 为 ! Construct (%Intl.NumberFormat% , « locale , nfOptions »)。 令 relativeTimeFormat .[[PluralRules]] 为 ! Construct (%Intl.PluralRules% , « locale »)。 返回 relativeTimeFormat 。
18.2 Intl.RelativeTimeFormat 构造函数的属性
Intl.RelativeTimeFormat 构造函数:
18.2.1 Intl.RelativeTimeFormat.prototype
Intl.RelativeTimeFormat.prototype 的值为 %Intl.RelativeTimeFormat.prototype% 。
该属性具有 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : false } 属性。
18.2.2 Intl.RelativeTimeFormat.supportedLocalesOf ( locales [ , options ] )
当调用 supportedLocalesOf 方法并传入参数 locales 和 options 时,执行以下步骤:
令 availableLocales 为 %Intl.RelativeTimeFormat% .[[AvailableLocales]] 。 令 requestedLocales 为 ? CanonicalizeLocaleList (locales )。 返回 ? FilterLocales (availableLocales , requestedLocales , options )。
18.2.3 内部槽
[[AvailableLocales]] 内部槽的值在 9.1 所述约束范围内由实现定义。
[[RelevantExtensionKeys]] 内部槽的值为 « "nu" »。
[[ResolutionOptionDescriptors]] 内部槽的值为 « { [[Key]] : "nu" , [[Property]] : "numberingSystem" } »。
Note 1
Unicode 技术标准 #35 第1部分核心,第3.6.1节 键和值定义
描述了与相对时间格式相关的一个区域扩展键:
"nu" 表示数字系统(格式化数字)。
[[LocaleData]] 内部槽的值在 9.1 所述约束范围内由实现定义,并且对于所有区域值
locale ,还需满足以下附加约束:
[[LocaleData]] .[[<locale >]] 包含字段 "second" , "minute" , "hour" , "day" , "week" , "month" ,
"quarter" , 和 "year" 。还可以存在以上述名称与 "-narrow" 或 "-short" 字符串拼接而成的附加字段。对应这些字段的值是包含以下两类字段的记录:
"future" 和 "past" 字段,这些字段是记录,包含与 locale 相关的每个复数类别的字段。对应这些字段的值是模式,可以包含 "{0}" ,用于替换为格式化数字。
可选地,附加字段,其键为 Number 的 ToString 结果,值为不作为模板处理的字面字符串。
[[LocaleData]] 的任何区域字段的 "nu" 字段的值列表不得包含 "native" 、"traditio" 或 "finance" 。
Note 2
推荐实现使用由 Common Locale Data Repository 提供的区域数据(可在
https://cldr.unicode.org/ 获取)。
18.3 Intl.RelativeTimeFormat 原型对象的属性
Intl.RelativeTimeFormat 原型对象 :
是 %Intl.RelativeTimeFormat.prototype% 。
是一个普通对象。
不是 Intl.RelativeTimeFormat 实例,不具有 [[InitializedRelativeTimeFormat]] 内部槽或任何 Intl.RelativeTimeFormat 实例对象的其他内部槽。
具有 [[Prototype]] 内部槽,其值为 %Object.prototype% 。
18.3.1 Intl.RelativeTimeFormat.prototype.constructor
Intl.RelativeTimeFormat.prototype.constructor 的初始值为 %Intl.RelativeTimeFormat% 。
18.3.2 Intl.RelativeTimeFormat.prototype.resolvedOptions ( )
此函数提供对对象初始化时计算的区域和选项的访问。
令 relativeTimeFormat 为 this 值。 执行 ? RequireInternalSlot (relativeTimeFormat , [[InitializedRelativeTimeFormat]] )。 令 options 为 OrdinaryObjectCreate (%Object.prototype% )。 对 Table 33 的每一行(除表头行外),按表顺序执行 令 p 为当前行的 Property 值。 令 v 为 relativeTimeFormat 的内部槽,其名称为当前行的 Internal Slot 值。 断言:v 不为 undefined 。 执行 ! CreateDataPropertyOrThrow (options , p , v )。 返回 options 。
Table 33: RelativeTimeFormat 实例的已解析选项
内部槽
属性
[[Locale]]
"locale"
[[Style]]
"style"
[[Numeric]]
"numeric"
[[NumberingSystem]]
"numberingSystem"
18.3.3 Intl.RelativeTimeFormat.prototype.format ( value , unit )
当调用 format 方法并传入参数 value 和 unit 时,执行以下步骤:
令 relativeTimeFormat 为 this 值。 执行 ? RequireInternalSlot (relativeTimeFormat , [[InitializedRelativeTimeFormat]] )。 令 value 为 ? ToNumber (value )。 令 unit 为 ? ToString (unit )。 返回 ? FormatRelativeTime (relativeTimeFormat , value , unit )。
18.3.4 Intl.RelativeTimeFormat.prototype.formatToParts ( value , unit )
当调用 formatToParts 方法并传入参数 value 和 unit 时,执行以下步骤:
令 relativeTimeFormat 为 this 值。 执行 ? RequireInternalSlot (relativeTimeFormat , [[InitializedRelativeTimeFormat]] )。 令 value 为 ? ToNumber (value )。 令 unit 为 ? ToString (unit )。 返回 ? FormatRelativeTimeToParts (relativeTimeFormat , value , unit )。
18.3.5 Intl.RelativeTimeFormat.prototype [ %Symbol.toStringTag% ]
%Symbol.toStringTag% 属性的初始值为字符串 "Intl.RelativeTimeFormat" 。
该属性具有 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : true } 属性。
18.4 Intl.RelativeTimeFormat 实例的属性
Intl.RelativeTimeFormat 实例是普通对象,继承自 %Intl.RelativeTimeFormat.prototype% 的属性。
Intl.RelativeTimeFormat 实例具有 [[InitializedRelativeTimeFormat]] 内部槽。
Intl.RelativeTimeFormat 实例还具有由 Intl.RelativeTimeFormat 构造函数
计算的多个内部槽:
[[Locale]] 是一个字符串值,表示用于格式化的区域语言标签 。
[[LocaleData]] 是一个记录,表示实现用于格式化的可用数据。它是 %Intl.RelativeTimeFormat% .[[LocaleData]] 中与 [[Locale]] 的值或其前缀相关的条目的值。
[[Style]] 是字符串值 "long" 、"short" 或 "narrow" ,标识使用的相对时间格式样式。
[[Numeric]] 是字符串值 "always" 或 "auto" ,标识是否始终使用数字描述,或仅在没有更具体版本时使用(如 "1 day ago" 与 "yesterday")。
[[NumberFormat]] 是用于格式化的 Intl.NumberFormat 对象。
[[NumberingSystem]] 是用于格式化的 Unicode
数字系统标识符 的字符串值。
[[PluralRules]] 是用于格式化的 Intl.PluralRules 对象。
18.5 RelativeTimeFormat 对象的抽象操作
18.5.1 SingularRelativeTimeUnit ( unit )
The abstract operation SingularRelativeTimeUnit takes argument unit (一个字符串) and returns 返回字符串的正常完成或抛出完成. It performs the following steps when called:
如果 unit 是 "seconds" ,返回 "second" 。 如果 unit 是 "minutes" ,返回 "minute" 。 如果 unit 是 "hours" ,返回 "hour" 。 如果 unit 是 "days" ,返回 "day" 。 如果 unit 是 "weeks" ,返回 "week" 。 如果 unit 是 "months" ,返回 "month" 。 如果 unit 是 "quarters" ,返回 "quarter" 。 如果 unit 是 "years" ,返回 "year" 。 如果 unit 不是 "second" 、"minute" 、"hour" 、"day" 、"week" 、"month" 、"quarter" 或 "year" ,抛出 RangeError
异常。 返回 unit 。
18.5.2 PartitionRelativeTimePattern ( relativeTimeFormat , value , unit )
The abstract operation PartitionRelativeTimePattern takes arguments relativeTimeFormat (一个 Intl.RelativeTimeFormat), value (一个数字), and unit (一个字符串) and returns 返回包含字段 [[Type]] (字符串)、[[Value]] (字符串)、[[Unit]] (字符串或 empty )的记录列表的正常完成或抛出完成. It performs the following steps when called:
如果 value 是 NaN 、+∞ 𝔽 或 -∞ 𝔽 ,抛出 RangeError 异常。 令 unit 为 ? SingularRelativeTimeUnit (unit )。 令 fields 为 relativeTimeFormat .[[LocaleData]] 。 令 patterns 为 fields .[[<unit >]]。 令 style 为 relativeTimeFormat .[[Style]] 。 如果 style 是 "short" 或 "narrow" ,则 令 key 为 unit 与 "-" 和 style 的字符串拼接。 如果 fields 有字段 [[<key >]],则设置 patterns 为 fields .[[<key >]]。 如果 relativeTimeFormat .[[Numeric]] 是 "auto" ,则 令 valueString 为 ! ToString (value )。 如果 patterns 有字段 [[<valueString >]],则 令 result 为 patterns .[[<valueString >]]。 返回包含记录 { [[Type]] : "literal" , [[Value]] : result , [[Unit]] : empty } 的列表。 如果 value 是 -0 𝔽 或 value < -0 𝔽 ,则 令 tl 为 "past" 。 否则, 令 tl 为 "future" 。 令 po 为 patterns .[[<tl >]]。 令 fv 为 PartitionNumberPattern (relativeTimeFormat .[[NumberFormat]] , ℝ (value ))。 令 pr 为 ResolvePlural (relativeTimeFormat .[[PluralRules]] , value ).[[PluralCategory]] 。 令 pattern 为 po .[[<pr >]]。 返回 MakePartsList (pattern , unit , fv )。
18.5.3 MakePartsList ( pattern , unit , parts )
The abstract operation MakePartsList takes arguments pattern (一个模式字符串 ), unit (一个字符串), and parts (表示格式化数字的记录列表) and returns 返回包含字段 [[Type]] (字符串)、[[Value]] (字符串)、[[Unit]] (字符串或 empty )的记录列表. It performs the following steps when called:
令 patternParts 为 PartitionPattern (pattern )。 令 result 为新的空列表。 对每个记录 { [[Type]] , [[Value]] } patternPart 属于 patternParts ,执行 如果 patternPart .[[Type]] 是 "literal" ,则 将记录 { [[Type]] : "literal" , [[Value]] : patternPart .[[Value]] , [[Unit]] : empty } 添加到 result 。 否则, 断言:patternPart .[[Type]] 是 "0" 。 对每个记录 { [[Type]] , [[Value]] } part 属于 parts ,执行 将记录 { [[Type]] : part .[[Type]] , [[Value]] : part .[[Value]] , [[Unit]] : unit } 添加到 result 。 返回 result 。
Note
示例:
返回 MakePartsList ("AA{0}BB" , "hour" , « 记录 { [[Type]] : "integer" , [[Value]] : "15" } »)。
将返回如下记录列表
«
{ [[Type]] : "literal" , [[Value]] : "AA" , [[Unit]] : empty },
{ [[Type]] : "integer" , [[Value]] : "15" , [[Unit]] : "hour" },
{ [[Type]] : "literal" , [[Value]] : "BB" , [[Unit]] : empty }
»
18.5.4 FormatRelativeTime ( relativeTimeFormat , value , unit )
The abstract operation FormatRelativeTime takes arguments relativeTimeFormat (一个 Intl.RelativeTimeFormat), value (一个数字), and unit (一个字符串) and returns 返回字符串的正常完成或抛出完成. It performs the following steps when called:
令 parts 为 ? PartitionRelativeTimePattern (relativeTimeFormat , value , unit )。 令 result 为空字符串。 对每个记录 { [[Type]] , [[Value]] , [[Unit]] } part 属于 parts ,执行 设置 result 为 result 与 part .[[Value]] 的字符串拼接。 返回 result 。
18.5.5 FormatRelativeTimeToParts ( relativeTimeFormat , value , unit )
The abstract operation FormatRelativeTimeToParts takes arguments relativeTimeFormat (一个 Intl.RelativeTimeFormat), value (一个数字), and unit (一个字符串) and returns 返回数组的正常完成或抛出完成. It performs the following steps when called:
令 parts 为 ? PartitionRelativeTimePattern (relativeTimeFormat , value , unit )。 令 result 为 ! ArrayCreate (0)。 令 n 为 0。 对每个记录 { [[Type]] , [[Value]] , [[Unit]] } part 属于 parts ,执行 令 O 为 OrdinaryObjectCreate (%Object.prototype% )。 执行 ! CreateDataPropertyOrThrow (O , "type" , part .[[Type]] )。 执行 ! CreateDataPropertyOrThrow (O , "value" , part .[[Value]] )。 如果 part .[[Unit]] 不为 empty ,则 执行 ! CreateDataPropertyOrThrow (O , "unit" , part .[[Unit]] )。 执行 ! CreateDataPropertyOrThrow (result , ! ToString (𝔽 (n )), O )。 n 增加 1。返回 result 。
19 分词器对象
19.1 Intl.Segmenter 构造函数
Intl.Segmenter 构造函数:
是 %Intl.Segmenter% 。
是 Intl 对象 的 "Segmenter" 属性的初始值。
Intl 对象 所有服务构造属性的通用行为在 9.1 中指定。
19.1.1 Intl.Segmenter ( [ locales [ , options ] ] )
当以可选参数 locales 和 options 调用 Intl.Segmenter 函数时,执行以下步骤:
如果 NewTarget 是 undefined ,抛出 TypeError 异常。 令 internalSlotsList 为 « [[InitializedSegmenter]] , [[Locale]] , [[SegmenterGranularity]] »。 令 segmenter 为 ? OrdinaryCreateFromConstructor (NewTarget, "%Intl.Segmenter.prototype%" ,
internalSlotsList )。 令 optionsResolution 为 ? ResolveOptions (%Intl.Segmenter% , %Intl.Segmenter% .[[LocaleData]] , locales ,
options )。 设置 options 为 optionsResolution .[[Options]] 。 令 r 为 optionsResolution .[[ResolvedLocale]] 。 设置 segmenter .[[Locale]] 为 r .[[Locale]] 。 令 granularity 为 ? GetOption (options , "granularity" , string , « "grapheme" , "word" , "sentence" »,
"grapheme" )。 设置 segmenter .[[SegmenterGranularity]] 为 granularity 。 返回 segmenter 。
19.2 Intl.Segmenter 构造函数的属性
Intl.Segmenter 构造函数:
19.2.1 Intl.Segmenter.prototype
Intl.Segmenter.prototype 的值为 %Intl.Segmenter.prototype% 。
该属性具有 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : false } 属性。
19.2.2 Intl.Segmenter.supportedLocalesOf ( locales [ , options ] )
当以参数 locales 和 options 调用 supportedLocalesOf 方法时,执行以下步骤:
令 availableLocales 为 %Intl.Segmenter% .[[AvailableLocales]] 。 令 requestedLocales 为 ? CanonicalizeLocaleList (locales )。 返回 ? FilterLocales (availableLocales , requestedLocales , options )。
19.2.3 Intl.Segmenter 构造函数的内部槽
[[AvailableLocales]] 内部槽的值在 9.1 描述的约束范围内由实现定义。
[[RelevantExtensionKeys]] 内部槽的值为 « »。
[[ResolutionOptionDescriptors]] 内部槽的值为 « »。
Note
Intl.Segmenter 没有任何相关扩展键。
[[LocaleData]] 内部槽的值在 9.1 描述的约束范围内由实现定义。
19.3 Intl.Segmenter 原型对象的属性
Intl.Segmenter 原型对象 :
是 %Intl.Segmenter.prototype% 。
是一个普通对象。
不是 Intl.Segmenter 实例,不具有 [[InitializedSegmenter]] 内部槽或任何 Intl.Segmenter 实例对象的其他内部槽。
拥有 [[Prototype]] 内部槽,其值为 %Object.prototype% 。
19.3.1 Intl.Segmenter.prototype.constructor
Intl.Segmenter.prototype.constructor 的初始值为 %Intl.Segmenter% 。
19.3.2 Intl.Segmenter.prototype.resolvedOptions ( )
此函数提供对对象初始化期间计算的 locale 和 options 的访问。
令 segmenter 为 this 值。 执行 ? RequireInternalSlot (segmenter , [[InitializedSegmenter]] )。 令 options 为 OrdinaryObjectCreate (%Object.prototype% )。 对 Table 34 的每一行(除表头行外),按表顺序执行 令 p 为当前行的 Property 值。 令 v 为 segmenter 的内部槽,其名称为当前行的 Internal Slot 值。 断言:v 不为 undefined 。 执行 ! CreateDataPropertyOrThrow (options , p , v )。 返回 options 。
Table 34: 分词器实例的已解析选项
内部槽
属性
[[Locale]]
"locale"
[[SegmenterGranularity]]
"granularity"
19.3.3 Intl.Segmenter.prototype.segment ( string )
Intl.Segmenter.prototype.segment 方法在 Intl.Segmenter 实例上以参数 string 调用,用于根据该实例的 locale 和 options 为字符串创建
Segments 实例 。执行以下步骤:
令 segmenter 为 this 值。 执行 ? RequireInternalSlot (segmenter , [[InitializedSegmenter]] )。 令 string 为 ? ToString (string )。 返回 CreateSegmentsObject (segmenter , string )。
19.3.4 Intl.Segmenter.prototype [ %Symbol.toStringTag% ]
%Symbol.toStringTag% 属性的初始值为字符串 "Intl.Segmenter" 。
该属性具有 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : true } 属性。
19.4 Intl.Segmenter 实例的属性
Intl.Segmenter 实例是普通对象,继承自 %Intl.Segmenter.prototype% 的属性。
Intl.Segmenter 实例具有 [[InitializedSegmenter]] 内部槽。
Intl.Segmenter 实例还具有由 Intl.Segmenter 构造函数 计算的内部槽:
[[Locale]] 是字符串值,表示用于分词的本地化语言标签 。
[[SegmenterGranularity]] 是字符串值 "grapheme" 、"word" 或 "sentence" ,用于标识要分割的文本元素类型。
19.5 Segments 对象
Segments 实例 是一个对象,表示特定字符串的分段,受其构造 Intl.Segmenter 实例的 locale 和 options 影响。
19.5.1 CreateSegmentsObject ( segmenter , string )
The abstract operation CreateSegmentsObject takes arguments segmenter (一个 Intl.Segmenter) and string (一个字符串) and returns 一个 Segments 实例 . It performs the following steps when called:
令 internalSlotsList 为 « [[SegmentsSegmenter]] , [[SegmentsString]] »。 令 segments 为 OrdinaryObjectCreate (%IntlSegmentsPrototype% , internalSlotsList )。 设置 segments .[[SegmentsSegmenter]] 为 segmenter 。 设置 segments .[[SegmentsString]] 为 string 。 返回 segments 。
19.5.2 %IntlSegmentsPrototype% 对象
%IntlSegmentsPrototype% 对象:
19.5.2.1 %IntlSegmentsPrototype%.containing ( index )
containing 方法在 Segments 实例 上以参数 index 调用,返回一个 Segment Data 对象 ,描述字符串中包含指定索引处代码单元的分段,根据 Segments 实例 的构造
Intl.Segmenter 实例的 locale 和 options。执行以下步骤:
令 segments 为 this 值。 执行 ? RequireInternalSlot (segments , [[SegmentsSegmenter]] )。 令 segmenter 为 segments .[[SegmentsSegmenter]] 。 令 string 为 segments .[[SegmentsString]] 。 令 len 为 string 的长度。 令 n 为 ? ToIntegerOrInfinity (index )。 如果 n < 0 或 n ≥ len ,返回 undefined 。 令 startIndex 为 FindBoundary (segmenter , string , n , before )。 令 endIndex 为 FindBoundary (segmenter , string , n , after )。 返回 CreateSegmentDataObject (segmenter , string , startIndex , endIndex )。
19.5.2.2 %IntlSegmentsPrototype% [ %Symbol.iterator% ] ( )
%Symbol.iterator% 方法在 Segments 实例 上调用,用于根据其构造 Intl.Segmenter 实例的 locale 和 options 创建一个 Segment
Iterator 。执行以下步骤:
令 segments 为 this 值。 执行 ? RequireInternalSlot (segments , [[SegmentsSegmenter]] )。 令 segmenter 为 segments .[[SegmentsSegmenter]] 。 令 string 为 segments .[[SegmentsString]] 。 返回 CreateSegmentIterator (segmenter , string )。
此函数的 "name" 属性值为 "[Symbol.iterator]" 。
19.5.3 Segments 实例的属性
Segments 实例 是普通对象,继承自 %IntlSegmentsPrototype% 的属性。
Segments 实例 具有 [[SegmentsSegmenter]] 内部槽,引用其构造的 Intl.Segmenter 实例。
Segments 实例 具有 [[SegmentsString]] 内部槽,引用其所暴露分段的字符串值。
19.6 Segment Iterator 对象
Segment Iterator 是一个对象,表示对特定字符串分段的迭代。
19.6.1 CreateSegmentIterator ( segmenter , string )
The abstract operation CreateSegmentIterator takes arguments segmenter (一个 Intl.Segmenter) and string (一个字符串) and returns 一个 Segment Iterator . It performs the following steps when called:
令 internalSlotsList 为 « [[IteratingSegmenter]] , [[IteratedString]] ,
[[IteratedStringNextSegmentCodeUnitIndex]] »。 令 iterator 为 OrdinaryObjectCreate (%IntlSegmentIteratorPrototype% , internalSlotsList )。 设置 iterator .[[IteratingSegmenter]] 为 segmenter 。 设置 iterator .[[IteratedString]] 为 string 。 设置 iterator .[[IteratedStringNextSegmentCodeUnitIndex]] 为 0。 返回 iterator 。
19.6.2 %IntlSegmentIteratorPrototype% 对象
%IntlSegmentIteratorPrototype% 对象:
19.6.2.1 %IntlSegmentIteratorPrototype%.next ( )
next 方法在 Segment Iterator 实例上调用,用于向前推进一个分段并返回一个 IteratorResult 对象,描述新分段或声明迭代结束。执行以下步骤:
令 iterator 为 this 值。 执行 ? RequireInternalSlot (iterator , [[IteratingSegmenter]] )。 令 segmenter 为 iterator .[[IteratingSegmenter]] 。 令 string 为 iterator .[[IteratedString]] 。 令 startIndex 为 iterator .[[IteratedStringNextSegmentCodeUnitIndex]] 。 令 len 为 string 的长度。 如果 startIndex ≥ len ,则 返回 CreateIteratorResultObject (undefined , true )。 令 endIndex 为 FindBoundary (segmenter , string , startIndex , after )。 设置 iterator .[[IteratedStringNextSegmentCodeUnitIndex]] 为 endIndex 。 令 segmentData 为 CreateSegmentDataObject (segmenter , string , startIndex , endIndex )。 返回 CreateIteratorResultObject (segmentData , false )。
19.6.2.2 %IntlSegmentIteratorPrototype% [ %Symbol.toStringTag% ]
%Symbol.toStringTag% 属性的初始值为字符串 "Segmenter String Iterator" 。
该属性具有 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : true } 属性。
19.6.3 Segment Iterator 实例的属性
Segment Iterator 实例是普通对象,继承自 %SegmentIteratorPrototype% 的属性。Segment Iterator 实例最初创建时具有 Table 35 描述的内部槽。
Table 35: Segment Iterator 实例的内部槽
内部槽
描述
[[IteratingSegmenter]]
用于迭代的 Intl.Segmenter 实例。
[[IteratedString]]
正在迭代的字符串值。
[[IteratedStringNextSegmentCodeUnitIndex]]
正在迭代的字符串值中下一个分段起始的代码单元索引。
19.7 Segment Data 对象
Segment Data 对象 是一个对象,表示字符串中的特定分段。
19.7.1 CreateSegmentDataObject ( segmenter , string , startIndex , endIndex )
The abstract operation CreateSegmentDataObject takes arguments segmenter (一个 Intl.Segmenter), string (一个字符串), startIndex (一个非负整数), and endIndex (一个非负整数) and returns 一个 Segment Data 对象 . It performs the following steps when called:
令 len 为 string 的长度。 断言:endIndex ≤ len 。 断言:startIndex < endIndex 。 令 result 为 OrdinaryObjectCreate (%Object.prototype% )。 令 segment 为 string 从 startIndex 到 endIndex 的子字符串。 执行 ! CreateDataPropertyOrThrow (result , "segment" , segment )。 执行 ! CreateDataPropertyOrThrow (result , "index" , 𝔽 (startIndex ))。 执行 ! CreateDataPropertyOrThrow (result , "input" , string )。 令 granularity 为 segmenter .[[SegmenterGranularity]] 。 如果 granularity 为 "word",则 令 isWordLike 为布尔值,指示 string 中的 segment 是否根据 locale segmenter .[[Locale]] 是“类词”。 执行 ! CreateDataPropertyOrThrow (result , "isWordLike" , isWordLike )。 返回 result 。
Note 分段是否“类词”由实现决定,建议实现采用本地化裁剪。一般来说,仅由空格和/或标点组成的分段(如 ICU [International Components for Unicode, 文档见
https://unicode-org.github.io/icu-docs/ ] 的 "WORD_NONE"
边界终止的分段)不被视为“类词”。
19.8 分词器对象的抽象操作
19.8.1 FindBoundary ( segmenter , string , startIndex , direction )
The abstract operation FindBoundary takes arguments segmenter (一个 Intl.Segmenter), string (一个字符串), startIndex (一个非负整数), and direction (before 或 after ) and returns 一个非负整数. It performs the following steps when called:
令 len 为 string 的长度。 断言:startIndex < len 。 令 locale 为 segmenter .[[Locale]] 。 令 granularity 为 segmenter .[[SegmenterGranularity]] 。 如果 direction 是 before ,则 在 string 中查找距离起始最多 startIndex 个代码单元的最后一个分段边界,使用 locale locale 和文本元素粒度 granularity 。 如果找到边界,返回 string 中在其之前的代码单元数量。 返回 0。 断言:direction 是 after 。 在 string 中查找紧随 startIndex 处代码单元之后的第一个分段边界,使用 locale locale 和文本元素粒度 granularity 。 如果找到边界,返回 string 中在其之前的代码单元数量。 返回 len 。
Note 边界判定由实现决定,但一般默认算法见
Unicode Standard Annex #29 。建议实现采用如
Common Locale Data Repository (
https://cldr.unicode.org )
提供的本地化裁剪。
20 ECMAScript语言规范的区域敏感函数
ECMA-262 描述了若干区域敏感函数。实现本规范的ECMAScript实现应按照此处描述实现这些函数。
Note
在本条款的算法中创建的Collator、NumberFormat或DateTimeFormat对象仅在这些算法内部使用。它们不会被ECMAScript代码直接访问,且实现中不需要实际存在这些对象。
20.1 String原型对象的属性
20.1.1 String.prototype.localeCompare ( that [ , locales [ , options ] ] )
本定义取代ECMA-262 中提供的定义,22.1.3.12 。
当调用localeCompare方法并传入参数that 及可选参数locales 和options 时,按以下步骤执行:
令O 为 ? RequireObjectCoercible (this value)。 令S 为 ? ToString (O )。 令thatValue 为 ? ToString (that )。 令collator 为 ? Construct (%Intl.Collator% , « locales , options »)。 返回 CompareStrings (collator , S , thatValue )。
该函数的"length" 属性为1 𝔽 。
Note 1
localeCompare方法本身不适合作为Array.prototype.sort的参数,因为后者要求一个带有两个参数的函数。
Note 2
localeCompare函数有意设计为通用;它不要求其this 值为String对象。因此,可以将其转移到其他类型对象上作为方法使用。
20.1.2 String.prototype.toLocaleLowerCase ( [ locales ] )
本定义取代ECMA-262 中提供的定义,22.1.3.26 。
该函数将String值解释为一系列代码点,如ECMA-262 中所述,6.1.4 。按以下步骤执行:
令O 为 ? RequireObjectCoercible (this value)。 令S 为 ? ToString (O )。 返回 ? TransformCase (S , locales , lower )。
Note
toLocaleLowerCase函数有意设计为通用;它不要求其this 值为String对象。因此,可以将其转移到其他类型对象上作为方法使用。
20.1.2.1 TransformCase ( S , locales , targetCase )
The abstract operation TransformCase takes arguments S (一个字符串), locales (一个ECMAScript语言值), and targetCase (lower 或upper ). 它将S 解释为一系列UTF-16编码的代码点,如ECMA-262 中所述,6.1.4 ,并返回将其ILD 转换为targetCase 后的新字符串值。 It performs the following steps when called:
令requestedLocales 为 ? CanonicalizeLocaleList (locales )。 如果requestedLocales 不是空列表,则 令requestedLocale 为requestedLocales [0]。 否则, 令requestedLocale 为 DefaultLocale ()。
1.
令availableLocales 为一个可用语言环境列表 ,其中包含Unicode字符数据库中具有语言敏感大小写映射的语言标签 。如果实现支持额外的区域敏感大小写映射,availableLocales 还应包含其对应的语言标签 。 令match 为 LookupMatchingLocaleByPrefix (availableLocales , « requestedLocale »)。 如果match 不是undefined ,令locale 为match .[[locale]] ;否则令locale 为"und" 。 令codePoints 为 StringToCodePoints (S )。 如果targetCase 为lower ,则 令newCodePoints 为一个列表,其元素是根据实现自定义算法使用locale 或Unicode默认大小写转换算法对codePoints 进行小写转换的结果。 否则, 断言:targetCase 为upper 。 令newCodePoints 为一个列表,其元素是根据实现自定义算法使用locale 或Unicode默认大小写转换算法对codePoints 进行大写转换的结果。 返回 CodePointsToString (newCodePoints )。
代码点映射可根据Unicode标准的默认大小写转换算法的定制版本得出。实现可以使用Unicode字符数据库的SpecialCasing.txt 文件中定义的区域敏感定制,以及/或CLDR,以及/或任何其他自定义定制。无论定制如何,符合规范的实现的大小写转换算法在给定相同输入代码点、区域和目标大小写时必须始终产生相同结果。
Note
某些代码点的大小写映射可能产生多个代码点,因此结果长度可能与输入不同。由于toLocaleUpperCase和toLocaleLowerCase都具有上下文敏感行为,这些函数并非对称。换句话说,s.toLocaleUpperCase().toLocaleLowerCase()不一定等于s.toLocaleLowerCase(),s.toLocaleLowerCase().toLocaleUpperCase()也不一定等于s.toLocaleUpperCase()。
20.1.3 String.prototype.toLocaleUpperCase ( [ locales ] )
本定义取代ECMA-262 中提供的定义,22.1.3.27 。
该函数将String值解释为一系列代码点,如ECMA-262 中所述,6.1.4 。按以下步骤执行:
令O 为 ? RequireObjectCoercible (this value)。 令S 为 ? ToString (O )。 返回 ? TransformCase (S , locales , upper )。
Note
toLocaleUpperCase函数有意设计为通用;它不要求其this 值为String对象。因此,可以将其转移到其他类型对象上作为方法使用。
20.2 Number原型对象的属性
以下定义引用了ECMA-262 中定义的抽象操作thisNumberValue,21.1.3 。
20.2.1 Number.prototype.toLocaleString ( [ locales [ , options ] ] )
本定义取代ECMA-262 中提供的定义,21.1.3.4 。
当调用toLocaleString方法并传入可选参数locales 和options 时,按以下步骤执行:
令x 为 ? ThisNumberValue (this value)。 令numberFormat 为 ? Construct (%Intl.NumberFormat% , « locales , options »)。 返回 FormatNumeric (numberFormat , ! ToIntlMathematicalValue (x ))。
20.3 BigInt原型对象的属性
以下定义引用了ECMA-262 中定义的抽象操作thisBigIntValue,21.2.3 。
20.3.1 BigInt.prototype.toLocaleString ( [ locales [ , options ] ] )
本定义取代ECMA-262 中提供的定义,21.2.3.2 。
当调用toLocaleString方法并传入可选参数locales 和options 时,按以下步骤执行:
令x 为 ? ThisBigIntValue (this value)。 令numberFormat 为 ? Construct (%Intl.NumberFormat% , « locales , options »)。 返回 FormatNumeric (numberFormat , ℝ (x ))。
20.4 Date原型对象的属性
以下定义引用了ECMA-262 中定义的抽象操作thisTimeValue,21.4.4 。
20.4.1 Date.prototype.toLocaleString ( [ locales [ , options ] ] )
本定义取代ECMA-262 中提供的定义,21.4.4.39 。
当调用toLocaleString方法并传入可选参数locales 和options 时,按以下步骤执行:
令dateObject 为this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令x 为dateObject .[[DateValue]] 。 如果x 为NaN ,返回"Invalid Date" 。 令dateFormat 为 ? CreateDateTimeFormat (%Intl.DateTimeFormat% , locales , options , any , all )。 返回 ! FormatDateTime (dateFormat , x )。
20.4.2 Date.prototype.toLocaleDateString ( [ locales [ , options ] ] )
本定义取代Date.prototype.toLocaleDateString ( [ reserved1 [ , reserved2 ] ] ) 中提供的定义。
当调用toLocaleDateString方法并传入可选参数locales 和options 时,按以下步骤执行:
令dateObject 为this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令x 为dateObject .[[DateValue]] 。 如果x 为NaN ,返回"Invalid Date" 。 令dateFormat 为 ? CreateDateTimeFormat (%Intl.DateTimeFormat% , locales , options , date , date )。 返回 ! FormatDateTime (dateFormat , x )。
20.4.3 Date.prototype.toLocaleTimeString ( [ locales [ , options ] ] )
本定义取代Date.prototype.toLocaleTimeString ( [ reserved1 [ , reserved2 ] ] ) 中提供的定义。
当调用toLocaleTimeString方法并传入可选参数locales 和options 时,按以下步骤执行:
令dateObject 为this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令x 为dateObject .[[DateValue]] 。 如果x 为NaN ,返回"Invalid Date" 。 令timeFormat 为 ? CreateDateTimeFormat (%Intl.DateTimeFormat% , locales , options , time , time )。 返回 ! FormatDateTime (timeFormat , x )。
20.5 Array原型对象的属性
20.5.1 Array.prototype.toLocaleString ( [ locales [ , options ] ] )
本定义取代Array.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] ) 中提供的定义。
当调用toLocaleString方法并传入可选参数locales 和options 时,按以下步骤执行:
令array 为 ? ToObject (this value)。 令len 为 ? LengthOfArrayLike (array )。 令separator 为适用于主机环境当前区域的实现定义列表分隔符字符串值(如", " )。 令R 为空字符串。 令k 为0。 当k < len 时重复, 如果k > 0,则 将R 设为R 与separator 的字符串连接。 令nextElement 为 ? Get (array , ! ToString (𝔽 (k )))。 如果nextElement 不是undefined 或null ,则 令S 为 ? ToString (? Invoke (nextElement , "toLocaleString" , « locales , options »))。 将R 设为R 与S 的字符串连接。 将k 设为k + 1。 返回R 。
Note 1
此算法的步骤与
Array.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] ) 中的步骤一致,唯一不同的是
Invoke (
nextElement ,
"toLocaleString" )现在接受
locales 和
options 作为参数。
Note 2
数组元素通过其toLocaleString方法转换为字符串,然后这些字符串被连接,并以实现定义的区域敏感分隔符字符串分隔。此函数类似于toString,但旨在生成与主机环境当前区域惯例相对应的区域敏感结果。
Note 3
toLocaleString函数有意设计为通用;它不要求其this 值为Array对象。因此可以将其转移到其他类型对象上作为方法使用。
Annex A (informative) 实现依赖行为
本规范中的以下方面是实现依赖的:
在所有功能中:
在 Collator 中:
对 Unicode 扩展键 "kf" 、"kn" 及并行选项属性 "caseFirst" 、"numeric" 的支持 (10.1.1 )
每个区域除默认排序外支持的 "co" 键值(排序)集合 (10.2.3 )
每个区域支持的 "kf" 键值(大小写顺序)集合 (10.2.3 )
每个区域支持的 "kn" 键值(数字排序)集合 (10.2.3 )
每个区域的默认搜索敏感度 (10.2.3 )
每个区域的默认忽略标点符号值 (10.2.3 )
每个支持的区域及选项组合的排序顺序 (10.3.3.1 )
在 DateTimeFormat 中:
在 DisplayNames 中:
在 DurationFormat 中:
每个区域支持的 "nu" 键值(数字系统)集合 (13.2.3 )
数字格式配置(以 "numeric" 样式使用两位小时及数字小时、分钟、秒的分隔符)每个区域 (13.2.3 )
在 ListFormat 中:
在 Locale 中:
在 NumberFormat 中:
在 PluralRules 中:
每个区域复数选择可能结果的字符串列表及其对应顺序 (17.1.1 )
在 RelativeTimeFormat 中:
在 Segmenter 中:
Annex B (informative) 引入与先前版本不兼容的新增与变更
Annex C (informative) 跋
本规范在GitHub 上以一种名为Ecmarkup 的纯文本源格式编写。Ecmarkup 是一种 HTML 和 Markdown 方言,为以纯文本编写 ECMAScript
规范并处理规范为完整 HTML 渲染提供了框架和工具集,遵循本文件的编辑惯例。Ecmarkup 构建并集成了多种其他格式和技术,包括用于定义语法的Grammarkdown 和用于编写算法步骤的Ecmarkdown 。本规范的 PDF 渲染使用打印样式表,利用 CSS Paged Media 规范,并通过PrinceXML 进行转换。
本规范的早期版本使用 Word 编写——构成本版基础的 Ecmarkup 源文本是通过自动转换工具将 ECMAScript 2015 Word 文档转换为 Ecmarkup 得到的。
Copyright & Software License
Ecma International
Rue du Rhone 114
CH-1204 Geneva
Tel: +41 22 849 6000
Fax: +41 22 849 6001
Web: https://ecma-international.org/
Copyright Notice
COPYRIGHT NOTICE
© 2026 Ecma International
By obtaining and/or copying this work, you (the licensee) agree that you have read, understood, and will comply with the following terms and conditions.
This document may be copied, published and distributed to others, and certain derivative works of it may be prepared, copied, published, and distributed, in whole or in part, provided that the above copyright notice and this Copyright License and Disclaimer are included on all such copies and derivative works. The only derivative works that are permissible under this Copyright License and Disclaimer are:
(i) works which incorporate all or portion of this document for the purpose of providing commentary or explanation (such as an annotated version of the document),
(ii) works which incorporate all or portion of this document for the purpose of incorporating features that provide accessibility,
(iii) translations of this document into languages other than English and into different formats and
(iv) works by making use of this specification in standard conformant products by implementing (e.g. by copy and paste wholly or partly) the functionality therein.
However, the content of this document itself may not be modified in any way, including by removing the copyright notice or references to Ecma International, except as required to translate it into languages other than English or into a different format.
The official version of an Ecma International document is the English language version on the Ecma International website. In the event of discrepancies between a translated version and the official version, the official version shall govern.
The limited permissions granted above are perpetual and will not be revoked by Ecma International or its successors or assigns.
This document and the information contained herein is provided on an “AS IS” basis and ECMA INTERNATIONAL DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY OWNERSHIP RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
Software License
All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT https://ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.