Skip to content

Internationalization support

Node.js 和基础 V8 引擎使用 Unicode 国际组件 (ICU) 在本机 C/C++ 代码中实现这些功能

构建Node.js的选项

为了控制 ICU 在Node.js中的使用方式,在编译过程中提供了四个 configure 选项

  • --with-intl=none/--without-intl
  • --with-intl=system-icu
  • --with-intl=small-icu
  • --with-intl=full-icu(默认)

检测国际化支持

要验证 ICU 是否完全启用( system-icu , small-icu , 或 full-icu ),只需检查是否存在就足够了 Intl

js
const hasICU = typeof Intl === 'object';

或者,检查 process.versions.icu ,仅在启用 ICU 时定义的属性也有效

js
const hasICU = typeof process.versions.icu === 'string';

要检查是否支持非英语区域设置( full-icu 即 或 system-icu ), Intl.DateTimeFormat 可能是一个很好的区分因素:

js
const hasFullICU = (() => {
  try {
    const january = new Date(9e8);
    const spanish = new Intl.DateTimeFormat('es', { month: 'long' });
    return spanish.format(january) === 'enero';
  } catch (err) {
    return false;
  }
})();

Released under the MIT License.