← 中文资源

简体中文版本

Intl.Locale maximize 解析隐含子标签

用 maximize 与 minimize 补全或精简语言标签,并给 Locale 附加日历与数字系统选项。

阅读英文原文

运行结果

以下输出由构建管线在沙箱中真实运行该示例捕获。

tag: zh-HK
maximized: zh-Hant-HK
minimized: zh-HK
language: zh | region: HK
likely script: Hant
with options: en-u-ca-gregory-nu-latn
calendar: gregory | numbering: latn
tags sorted: zh-hans-cn,zh-hant-hk,zh-hk
base name: sr-Latn-RS | script: Latn
explicit maximized: en-Latn-US

实现说明

各个操作都建立在顶层绑定之上: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());