Intl.Locale maximize Resolves Likely Subtags

Use Intl.Locale maximize and minimize to resolve likely subtags, then attach calendar and numbering options to the tag.

JavascriptConfig Management

Code

javascriptsnippet
22 lines
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());

Output

Captured from a sandboxed run of this snippet in the build pipeline.

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

How the snippet works

Top-level bindings locale, configured, tags, sorted hold the demo state — nothing sits inside a function scope.

Applying it

The run emits 10 labeled sections — tag:, maximized:, minimized:, language:, likely script:, with options:, calendar:, tags sorted:, base name:, explicit maximized: — one per step.

Statements that matter

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');

Interactive Tools for This Topic

FAQ

Will the snippet run without installing packages?
Yes, it runs as-is — nothing outside the built-in APIs is referenced. Its state lives in module-level bindings: `locale`, `configured`, `tags`.