在运行时提供 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 配置选项:

    ¥The --with-icu-default-data-dir configure option:

    ./configure --with-icu-default-data-dir=/runtime/directory/with/dat/file --with-intl=small-icu 

    这仅将默认数据目录路径嵌入到二进制文件中。实际的数据文件将在运行时从此目录路径加载。

    ¥This only embeds the default data directory path into the binary. The actual data file is going to be loaded at runtime from this directory path.

  • NODE_ICU_DATA 环境变量:

    ¥The NODE_ICU_DATA environment variable:

    env NODE_ICU_DATA=/runtime/directory/with/dat/file node 
  • --icu-data-dir 命令行参数:

    ¥The --icu-data-dir CLI parameter:

    node --icu-data-dir=/runtime/directory/with/dat/file 

当指定多个变量时,--icu-data-dir CLI 参数具有最高优先级,然后是 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 版本,而 bl 表示系统的字节序。如果无法从指定目录读取预期的数据文件,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 数据" 文章,了解其他支持的格式以及有关 ICU 数据的更多详细信息。

¥Check "ICU Data" article in the ICU User Guide for other supported formats and more details on ICU data in general.

full-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.