Intl DisplayNames Localizes Language Codes

Intl.DisplayNames turns language and region codes into localized labels for the chosen locale and returns unknown codes unchanged instead of throwing.

JavascriptData Integrity

Code

javascriptsnippet
21 lines
// Intl.DisplayNames turns language/region codes into display names for the current locale -
// language switchers and region lists render directly, no bundled translation table needed
const regionNames = new Intl.DisplayNames(['zh-CN'], { type: 'region' });
console.log('US:', regionNames.of('US'), '| JP:', regionNames.of('JP'));

const langNames = new Intl.DisplayNames(['en'], { type: 'language' });
console.log('zh-CN:', langNames.of('zh-CN'), '| de:', langNames.of('de'));

// The same code gets different labels under different locales
const enRegions = new Intl.DisplayNames(['en'], { type: 'region' });
console.log('DE in zh:', regionNames.of('DE'), '| DE in en:', enRegions.of('DE'));

// Subtags resolve too: zh-Hans becomes the variant name instead of throwing
console.log('zh-Hans:', langNames.of('zh-Hans'));

// Rendering a picker: map each code in the list to a label for the current locale
const codes = ['US', 'JP', 'DE'];
console.log(codes.map((c) => c + '=' + regionNames.of(c)).join(' | '));

// Invalid or unknown codes come back as the code itself rather than throwing
console.log('unknown:', regionNames.of('XX'));

Output

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

US: 美国 | JP: 日本
zh-CN: Chinese (China) | de: German
DE in zh: 德国 | DE in en: Germany
zh-Hans: Simplified Chinese
US=美国 | JP=日本 | DE=德国
unknown: XX

How the snippet works

Top-level bindings regionNames, langNames, enRegions, codes hold the demo state — nothing sits inside a function scope.

Using it

Look for the US:, zh-CN:, DE in zh: prefixes when the file runs; they tie each line to its step.

Key statements

const regionNames = new Intl.DisplayNames(['zh-CN'], { type: 'region' });
const langNames = new Intl.DisplayNames(['en'], { type: 'language' });
const enRegions = new Intl.DisplayNames(['en'], { type: 'region' });

Interactive Tools for This Topic

FAQ

Any setup needed beyond the language runtime?
None — the language runtime is the only requirement. Its state lives in module-level bindings: `regionNames`, `langNames`, `enRegions`.