Intl.DisplayNames object is a constructor for objects that enables the consistent translation of language, region and script display names.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

构造函数

Intl.DisplayNames()
创建新的 Intl.DisplayNames 对象。

静态方法

Intl.DisplayNames.supportedLocalesOf()

Returns an array containing those of the provided locales that are supported without having to fall back to the runtime's default locale.

实例方法

Intl.DisplayNames.prototype.of()
This method receives a code and returns a string based on the locale and options provided when instantiating Intl.DisplayNames .
Intl.DisplayNames.prototype.resolvedOptions()

Returns a new object with properties reflecting the locale and formatting options computed during initialization of the object.

范例

Region Code Display Names

To create an Intl.DisplayNames for a locale and get the display name for a region code.

// Get display names of region in English
let regionNames = new Intl.DisplayNames(['en'], {type: 'region'});
regionNames.of('419'); // "Latin America"
regionNames.of('BZ');  // "Belize"
regionNames.of('US');  // "United States"
regionNames.of('BA');  // "Bosnia & Herzegovina"
regionNames.of('MM');  // "Myanmar (Burma)"
// Get display names of region in Traditional Chinese
regionNames = new Intl.DisplayNames(['zh-Hant'], {type: 'region'});
regionNames.of('419'; // "拉丁美洲"
regionNames.of('BZ'); // "貝里斯"
regionNames.of('US'); // "美國"
regionNames.of('BA'); // "波士尼亞與赫塞哥維納"
regionNames.of('MM'); // "緬甸"
					

Language Display Names

To create an Intl.DisplayNames for a locale and get the display name for a language-script-region sequence.

// Get display names of language in English
let languageNames = new Intl.DisplayNames(['en'], {type: 'language'});
languageNames.of('fr');      // "French"
languageNames.of('de');      // "German"
languageNames.of('fr-CA');   // "Canadian French"
languageNames.of('zh-Hant'); // "Traditional Chinese"
languageNames.of('en-US');   // "American English"
languageNames.of('zh-TW');   // "Chinese (Taiwan)"]
// Get display names of language in Traditional Chinese
languageNames = new Intl.DisplayNames(['zh-Hant'], {type: 'language'});
languageNames.of('fr'); // "法文"
languageNames.of('zh'); // "中文"
languageNames.of('de'); // "德文"
					

Script Code Display Names

To create an Intl.DisplayNames for a locale and get the display name for a script code.

// Get display names of script in English
let scriptNames = new Intl.DisplayNames(['en'], {type: 'script'});
// Get script names
scriptNames.of('Latn'); // "Latin"
scriptNames.of('Arab'); // "Arabic"
scriptNames.of('Kana'); // "Katakana"
// Get display names of script in Traditional Chinese
scriptNames = new Intl.DisplayNames(['zh-Hant'], {type: 'script'});
scriptNames.of('Latn'); // "拉丁文"
scriptNames.of('Arab'); // "阿拉伯文"
scriptNames.of('Kana'); // "片假名"
					

Currency Code Display Names

To create an Intl.DisplayNames for a locale and get the display name for currency code.

// Get display names of currency code in English
let currencyNames = new Intl.DisplayNames(['en'], {type: 'currency'});
// Get currency names
currencyNames.of('USD'); // "US Dollar"
currencyNames.of('EUR'); // "Euro"
currencyNames.of('TWD'); // "New Taiwan Dollar"
currencyNames.of('CNY'); // "Chinese Yuan"
// Get display names of currency code in Traditional Chinese
currencyNames = new Intl.DisplayNames(['zh-Hant'], {type: 'currency'});
currencyNames.of('USD'); // "美元"
currencyNames.of('EUR'); // "歐元"
currencyNames.of('TWD'); // "新台幣"
currencyNames.of('CNY'); // "人民幣"
					

规范

规范
Intl.DisplayNames
The definition of 'DisplayNames' in that specification.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
DisplayNames Chrome 81 Edge 81 Firefox No IE No Opera 68 Safari No WebView Android 81 Chrome Android 81 Firefox Android No Opera Android 58 Safari iOS No Samsung Internet Android No nodejs 14.0.0
DisplayNames() 构造函数 Chrome 81 Edge 81 Firefox No IE No Opera 68 Safari No WebView Android 81 Chrome Android 81 Firefox Android No Opera Android 58 Safari iOS No Samsung Internet Android No nodejs 14.0.0
of Chrome 81 Edge 81 Firefox No IE No Opera 68 Safari No WebView Android 81 Chrome Android 81 Firefox Android No Opera Android 58 Safari iOS No Samsung Internet Android No nodejs 14.0.0
resolvedOptions Chrome 81 Edge 81 Firefox No IE No Opera 68 Safari No WebView Android 81 Chrome Android 81 Firefox Android No Opera Android 58 Safari iOS No Samsung Internet Android No nodejs 14.0.0
supportedLocalesOf Chrome 81 Edge 81 Firefox No IE No Opera 68 Safari No WebView Android 81 Chrome Android 81 Firefox Android No Opera Android 58 Safari iOS No Samsung Internet Android No nodejs 14.0.0

图例

完整支持

完整支持

不支持

不支持

实现进度

The following table provides a daily implementation status for new features that has not yet reached cross-browser stability. The data is generated by running the relevant feature tests in Test262 , the standard test suite of JavaScript, in the nightly build, or latest release of each browser's JavaScript engine.

另请参阅

元数据

  • 最后修改: