在运行时提供 ICU 数据


【Providing ICU data at runtime】

如果使用 small-icu 选项,仍然可以在运行时提供额外的区域设置数据,以便 JS 方法能适用于所有 ICU 区域设置。假设数据文件存储在 /some/directory,可以通过以下方式之一提供给 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 /some/directory, it can be made available to ICU through either:】

  • NODE_ICU_DATA 环境变量:

    env NODE_ICU_DATA=/some/directory node 
  • --icu-data-dir CLI 参数:

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

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

ICU 能够自动查找并加载各种数据格式,但数据必须适用于 ICU 版本,并且文件命名正确。数据文件最常见的名称是 icudt6X[bl].dat,其中 6X 表示目标 ICU 版本,bl 表示系统的字节序。请参阅 ICU 用户指南中的 重症监护病房数据 文章,了解其他支持的格式以及有关 ICU 数据的更多详细信息。

【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.】

完整 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.】