实现说明
各个操作都建立在顶层绑定之上:locale、configured、tags、sorted。
使用要点
- 每一步打印都带着自己的标签:
tag:、maximized:、minimized:。
常见问题
不安装任何包也能运行吗?
可以,开箱即跑——没有引用任何内建 API 之外的东西。 状态都挂在模块顶层:locale、configured、tags。
核心语句
const sorted = tags.sort((a, b) => new Intl.Collator('zh').compare(a, b));
const configured = new Intl.Locale('en', { calendar: 'gregory', numberingSystem: 'latn' });
const explicit = new Intl.Locale('en-Latn-US');
代码示例
const locale = new Intl.Locale('zh-hk');
console.log('tag:', locale.toString());
console.log('maximized:', locale.maximize().toString());
console.log('minimized:', locale.minimize().toString());
console.log('language:', locale.language, '| region:', locale.region);
console.log('likely script:', locale.maximize().script);
const configured = new Intl.Locale('en', { calendar: 'gregory', numberingSystem: 'latn' });
console.log('with options:', configured.toString());
console.log('calendar:', configured.calendar, '| numbering:', configured.numberingSystem);
// maximize resolves likely subtags such as the script for zh-HK
const tags = ['zh-hant-hk', 'zh-hans-cn', 'zh-hk'];
const sorted = tags.sort((a, b) => new Intl.Collator('zh').compare(a, b));
console.log('tags sorted:', sorted.join(','));
const base = new Intl.Locale('sr-Latn-RS');
console.log('base name:', base.baseName, '| script:', base.script);
// maximize 不改变显式写出的子标签,只补缺省部分
const explicit = new Intl.Locale('en-Latn-US');
console.log('explicit maximized:', explicit.maximize().toString());