Options for building Node.js构建Node.js的选项#

To control how ICU is used in Node.js, four configure options are available during compilation. 为了控制如何在Node.js中使用ICU,编译期间有四个configure选项可用。Additional details on how to compile Node.js are documented in BUILDING.md.BUILDING.md中记录了有关如何编译Node.js的其他详细信息。

An overview of available Node.js and JavaScript features for each configure option:每个configure选项的可用Node.js和JavaScript功能概述:

Featurenonesystem-icusmall-icufull-icu
String.prototype.normalize()none (function is no-op)fullfullfull
String.prototype.to*Case()fullfullfullfull
Intlnone (object does not exist)partial/full (depends on OS)partial (English-only)full
String.prototype.localeCompare()partial (not locale-aware)fullfullfull
String.prototype.toLocale*Case()partial (not locale-aware)fullfullfull
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 Parserpartial (no IDN support)fullfullfull
WHATWG URL Parserpartial (no IDN support)fullfullfull
require('node:buffer').transcode()none (function does not exist)fullfullfull
REPLpartial (inaccurate line editing)fullfullfull
require('node:util').TextDecoderpartial (basic encodings support)partial/full (depends on OS)partial (Unicode-only)full
RegExp Unicode Property Escapesnone (invalid RegExp error)fullfullfull

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()的相同。

Disable all internationalization features (none)禁用所有国际化功能(none#

If this option is chosen, ICU is disabled and most internationalization features mentioned above will be unavailable in the resulting node binary.如果选择此选项,ICU将被禁用,并且上面提到的大多数国际化功能将在生成的node二进制文件中不可用

Build with a pre-installed ICU (system-icu)使用预先安装的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 String.prototype.normalize() and the WHATWG URL parser, are fully supported under system-icu. 系统ICU完全支持只需要ICU库本身的功能,例如String.prototype.normalize()WHATWG URL解析器Features that require ICU locale data in addition, such as Intl.DateTimeFormat may be fully or partially supported, depending on the completeness of the ICU data installed on the system.根据系统上安装的ICU数据的完整性,还可能完全或部分支持需要ICU区域设置数据的功能,例如Intl.DateTimeFormat

Embed a limited set of ICU data (small-icu)嵌入一组有限的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 node executable.此选项使生成的二进制链接静态地与ICU库相关联,并在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 Intl.DateTimeFormat, generally only work with the English locale:此外,还需要ICU语言环境数据的功能(如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.此模式提供了功能和二进制大小之间的平衡。

Providing ICU data at runtime在运行时提供ICU数据#

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:

(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版本,bl表示系统的端序。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 node executable and downloading the appropriate data file. full-icuu npm模块可以通过检测运行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-icuThis 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支持。

Embed the entire ICU (full-icu)嵌入整个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.官方二进制文件也是在这种模式下构建的。