To control how ICU is used in Node.js, four 为了控制如何在Node.js中使用ICU,编译期间有四个configure
options are available during compilation. configure
选项可用。Additional details on how to compile Node.js are documented in BUILDING.md.BUILDING.md中记录了有关如何编译Node.js的其他详细信息。
--with-intl=none
/--without-intl
--with-intl=system-icu
--with-intl=small-icu
--with-intl=full-icu
(default)An overview of available Node.js and JavaScript features for each 每个configure
option:configure
选项的可用Node.js和JavaScript功能概述:
Feature | none | system-icu | small-icu | full-icu |
---|---|---|---|---|
String.prototype.normalize() | none (function is no-op) | full | full | full |
String.prototype.to*Case() | full | full | full | full |
Intl | none (object does not exist) | partial/full (depends on OS) | partial (English-only) | full |
String.prototype.localeCompare() | partial (not locale-aware) | full | full | full |
String.prototype.toLocale*Case() | partial (not locale-aware) | full | full | full |
Number.prototype.toLocaleString() | partial (not locale-aware) | partial/full (depends on OS) | partial (English-only) | full |
Date.prototype.toLocale*String() | partial (not locale-aware) | partial/full (depends on OS) | partial (English-only) | full |
Legacy URL Parser | partial (no IDN support) | full | full | full |
WHATWG URL Parser | partial (no IDN support) | full | full | full |
require('node:buffer').transcode() | none (function does not exist) | full | full | full |
REPL | partial (inaccurate line editing) | full | full | full |
require('node:util').TextDecoder | partial (basic encodings support) | partial/full (depends on OS) | partial (Unicode-only) | full |
RegExp Unicode Property Escapes | none (invalid RegExp error) | full | full | full |
The "(not locale-aware)" designation denotes that the function carries out its operation just like the non-“(不支持区域设置)”指定表示该函数执行其操作,就像该函数的非Locale
version of the function, if one exists. Locale
设置版本(如果存在)一样。For example, under 例如,在none
mode, Date.prototype.toLocaleString()
's operation is identical to that of Date.prototype.toString()
.none
模式下,Date.prototype.toLocaleString()
的操作与Date.prototype.toString()
的相同。
none
)none
)#If this option is chosen, ICU is disabled and most internationalization features mentioned above will be unavailable in the resulting 如果选择此选项,ICU将被禁用,并且上面提到的大多数国际化功能将在生成的node
binary.node
二进制文件中不可用。
system-icu
)system-icu
)构建#Node.js can link against an ICU build already installed on the system. Node.js可以链接到系统上已经安装的ICU构建。In fact, most Linux distributions already come with ICU installed, and this option would make it possible to reuse the same set of data used by other components in the OS.事实上,大多数Linux发行版已经安装了ICU,这个选项可以重用操作系统中其他组件使用的同一组数据。
Functionalities that only require the ICU library itself, such as 系统ICU完全支持只需要ICU库本身的功能,例如String.prototype.normalize()
and the WHATWG URL parser, are fully supported under system-icu
. String.prototype.normalize()
和WHATWG URL解析器。Features that require ICU locale data in addition, such as 根据系统上安装的ICU数据的完整性,还可能完全或部分支持需要ICU区域设置数据的功能,例如Intl.DateTimeFormat
may be fully or partially supported, depending on the completeness of the ICU data installed on the system.Intl.DateTimeFormat
。
small-icu
)small-icu
)#This option makes the resulting binary link against the ICU library statically, and includes a subset of ICU data (typically only the English locale) within the 此选项使生成的二进制链接静态地与ICU库相关联,并在node
executable.node
可执行文件中包含ICU数据的子集(通常只有英文区域设置)。
Functionalities that only require the ICU library itself, such as String.prototype.normalize()
and the WHATWG URL parser, are fully supported under small-icu
. small-icu
完全支持只需要ICU库本身的功能,例如String.prototype.normalize()
和WHATWG URL解析器。Features that require ICU locale data in addition, such as 此外,还需要ICU语言环境数据的功能(如Intl.DateTimeFormat
, generally only work with the English locale:Intl.DateTimeFormat
)通常仅适用于英语语言环境:
const january = new Date(9e8);
const english = new Intl.DateTimeFormat('en', { month: 'long' });
const spanish = new Intl.DateTimeFormat('es', { month: 'long' });
console.log(english.format(january));
// Prints "January"
console.log(spanish.format(january));
// Prints either "M01" or "January" on small-icu, depending on the user’s default locale
// Should print "enero"
This mode provides a balance between features and binary size.此模式提供了功能和二进制大小之间的平衡。
If the 如果使用了small-icu
option is used, one can still provide additional locale data at runtime so that the JS methods would work for all ICU locales. small-icu
选项,则仍可以在运行时提供额外的区域设置数据,以便JS方法适用于所有icu区域设置。Assuming the data file is stored at 假设数据文件存储在/some/directory
, it can be made available to ICU through either:/some/directory
中,可以通过以下任一方式将其提供给ICU:
The NODE_ICU_DATA
environment variable:NODE_ICU_DATA
环境变量:
env NODE_ICU_DATA=/some/directory node
The --icu-data-dir
CLI parameter:--icu-data-dir
CLI参数:
node --icu-data-dir=/some/directory
(If both are specified, the (如果同时指定了这两个参数,则--icu-data-dir
CLI parameter takes precedence.)--icu-data-dir
CLI参数优先。)
ICU is able to automatically find and load a variety of data formats, but the data must be appropriate for the ICU version, and the file correctly named. ICU能够自动查找和加载各种数据格式,但数据必须适合ICU版本,并且文件名称正确。The most common name for the data file is 数据文件最常见的名称是icudt6X[bl].dat
, where 6X
denotes the intended ICU version, and b
or l
indicates the system's endianness. icudt6X[bl].dat
,其中6X
表示预期的ICU版本,b
或l
表示系统的端序。Check "ICU Data" article in the ICU User Guide for other supported formats and more details on ICU data in general.查看《ICU用户指南》中的“ICU数据”一文,了解其他支持的格式以及有关ICU数据的更多详细信息。
The full-icu npm module can greatly simplify ICU data installation by detecting the ICU version of the running full-icuu npm模块可以通过检测运行node
executable and downloading the appropriate data file. node
可执行文件的icu版本并下载适当的数据文件来大大简化icu数据安装。After installing the module through 通过npm i full-icu
, the data file will be available at ./node_modules/full-icu
. npm i full-icu
安装模块后,数据文件将在上提供./node_modules/full-icu
。This path can be then passed either to 然后可以将此路径传递到NODE_ICU_DATA
or --icu-data-dir
as shown above to enable full Intl
support.NODE_ICU_DATA
或--icu-data-dir
,如上图所示,以启用完整的Intl
支持。
full-icu
)full-icu
)#This option makes the resulting binary link against ICU statically and include a full set of ICU data. 此选项使生成的二进制链接与ICU静态链接,并包含完整的ICU数据集。A binary created this way has no further external dependencies and supports all locales, but might be rather large. 以这种方式创建的二进制文件没有进一步的外部依赖关系,并且支持所有区域设置,但可能相当大。This is the default behavior if no 如果不传递--with-intl
flag is passed. --with-intl
标志,这是默认行为。The official binaries are also built in this mode.官方二进制文件也是在这种模式下构建的。