检测国际化支持
要验证是否启用了 ICU(system-icu、small-icu 或 full-icu),只需检查 Intl 是否存在就足够了:
const hasICU = typeof Intl === 'object';或者,检查 process.versions.icu,一个仅在启用 ICU 时定义的属性,也可以工作:
const hasICU = typeof process.versions.icu === 'string';要检查对非英语语言环境(即 full-icu 或 system-icu)的支持,Intl.DateTimeFormat 可能是一个很好的区分因素:
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 支持的更详细的测试,以下资源可能会有所帮助:
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';Alternatively, checking for process.versions.icu, a property defined only
when ICU is enabled, works too:
const hasICU = typeof process.versions.icu === 'string';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;
}
})();For more verbose tests for Intl support, the following resources may be found
to be helpful: