在运行时提供 ICU 数据
【Providing ICU data at runtime】
如果使用了 small-icu 选项,仍然可以在运行时提供额外的区域设置数据,以使 JS 方法能够适用于所有 ICU 区域设置。假设数据文件存储在 /runtime/directory/with/dat/file,可以通过以下方式之一使其对 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. Assuming the
data file is stored at /runtime/directory/with/dat/file, it can be made
available to ICU through either:】
-
--with-icu-default-data-dir配置选项:./configure --with-icu-default-data-dir=/runtime/directory/with/dat/file --with-intl=small-icu这只会将默认的数据目录路径嵌入到二进制文件中。实际的数据文件将在运行时从该目录路径加载。
-
NODE_ICU_DATA环境变量:env NODE_ICU_DATA=/runtime/directory/with/dat/file node -
--icu-data-dirCLI 参数:node --icu-data-dir=/runtime/directory/with/dat/file
当指定多个选项时,--icu-data-dir 命令行参数具有最高优先级,其次是 NODE_ICU_DATA 环境变量,再次是 --with-icu-default-data-dir 配置选项。
【When more than one of them is specified, the --icu-data-dir CLI parameter has
the highest precedence, then the NODE_ICU_DATA environment variable, then
the --with-icu-default-data-dir configure option.】
ICU能够自动查找和加载多种数据格式,但数据必须适用于ICU的版本,并且文件命名正确。数据文件最常见的名称是 icudtX[bl].dat,其中 X 表示目标ICU版本,b 或 l 表示系统的字节序。如果无法从指定目录读取预期的数据文件,Node.js 将无法加载。可以根据当前Node.js版本计算出对应的数据文件名称:
【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.
The most common name for the data file is icudtX[bl].dat, where X denotes
the intended ICU version, and b or l indicates the system's endianness.
Node.js would fail to load if the expected data file cannot be read from the
specified directory. The name of the data file corresponding to the current
Node.js version can be computed with:】
`icudt${process.versions.icu.split('.')[0]}${os.endianness()[0].toLowerCase()}.dat`; 请查阅 ICU 用户指南中的 重症监护病房数据 文章,以了解其他支持的格式以及有关 ICU 数据的一般详细信息。
【Check "ICU Data" article in the ICU User Guide for other supported formats and more details on ICU data in general.】
完整 ICU npm 模块可以通过检测正在运行的 node 可执行文件的 ICU 版本并下载相应的数据文件,从而大大简化 ICU 数据的安装。在通过 npm i full-icu 安装该模块后,数据文件将位于 ./node_modules/full-icu。然后可以将此路径传递给 NODE_ICU_DATA 或如上所示的 --icu-data-dir,以启用完整的 Intl 支持。
【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. After installing the module through npm i full-icu,
the data file will be available at ./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.】