在运行时提供 ICU 数据


如果使用 small-icu 选项,则仍然可以在运行时提供额外的语言环境数据,以便 JS 方法适用于所有 ICU 语言环境。 假设数据文件存储在 /some/directory,它可以通过以下任一方式提供给 ICU:

  • NODE_ICU_DATA 环境变量:

    env NODE_ICU_DATA=/some/directory node
  • --icu-data-dir 命令行参数:

    node --icu-data-dir=/some/directory

(如果两者都指定,则 --icu-data-dir 命令行参数优先。)

ICU 能够自动查找和加载多种数据格式,但数据必须适合 ICU 版本,并且文件命名正确。 数据文件最常见的名称是 icudt6X[bl].dat,其中 6X 表示预期的 ICU 版本,而 bl 表示系统的字节序。 查看 ICU 用户指南中的 "ICU 数据"文章以了解其他支持的格式以及有关 ICU 数据的更多详细信息。

full-icu npm 模块通过检测运行中的 node 可执行文件的 ICU 版本并下载相应的数据文件,可以大大简化 ICU 数据安装。 通过 npm i full-icu 安装模块后,数据文件将在 ./node_modules/full-icu 可用。 然后可以将此路径传给 NODE_ICU_DATA--icu-data-dir,如上所示以启用完整的 Intl 支持。

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 /some/directory, it can be made available to ICU through either:

  • The NODE_ICU_DATA environment variable:

    env NODE_ICU_DATA=/some/directory node
  • The --icu-data-dir CLI parameter:

    node --icu-data-dir=/some/directory

(If both are specified, the --icu-data-dir CLI parameter takes precedence.)

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 icudt6X[bl].dat, where 6X denotes the intended ICU version, and b or l indicates the system's endianness. Check "ICU Data" article in the ICU User Guide for other supported formats and more details on ICU data in general.

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.