检测国际化支持


【Detecting internationalization support】

要验证 ICU 是否已启用(system-icusmall-icufull-icu),只需检查 Intl 是否存在即可:

【To verify that ICU is enabled at all (system-icu, small-icu, or full-icu), simply checking the existence of Intl should suffice:】

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

或者,检查 process.versions.icu(仅在启用 ICU 时定义的属性)也可以起作用:

【Alternatively, checking for process.versions.icu, a property defined only when ICU is enabled, works too:】

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

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

【To check for support for a non-English locale (i.e. full-icu or system-icu), Intl.DateTimeFormat can be a good distinguishing factor:】

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;
  }
})(); 

对于关于 Intl 支持的更详细测试,以下资源可能会有所帮助:

【For more verbose tests for Intl support, the following resources may be found to be helpful:】

  • btest402:通常用于检查 Node.js 是否正确构建了 Intl 支持。
  • Test262:ECMAScript 的官方一致性测试套件包含一个专门针对 ECMA-402 的部分。