Published · tagged with Intl

Web applications that reach a global audience need to show the display names of languages, regions, scripts, and currencies in many different languages. The translations of those names require data, which is available in the Unicode CLDR. Packaging the data as part of the application incurs a cost on developer time. Users are likely to prefer consistent translations of language and region names, and keeping that data up to date with the world's geopolitical happenings requires constant maintenance.

Luckily, most JavaScript runtimes already ship and keep up-to-date that very same translation data. The new Intl.DisplayNames API gives JavaScript developers direct access to those translations, allowing applications to more easily display localized names.

Usage examples #

The following example shows how to create an Intl.DisplayNames object to get region names in English using ISO-3166 2-letter country codes.

const regionNames = new Intl.DisplayNames(['en'], { type: 'region' });
// → 'United States'
// → 'Bosnia & Herzegovina'
// → 'Myanmar (Burma)'

The following example gets language names in Traditional Chinese using Unicode's language identifier grammar.

const languageNames = new Intl.DisplayNames(['zh-Hant'], { type: 'language' });
// → '法文'
// → '中文'
// → '德文'

The following example gets currency names in Simplified Chinese using ISO-4217 3-letter currency codes. In languages that have distinct singular and plural forms, the currency names are singular. For plural forms, Intl.NumberFormat may be used.

const currencyNames = new Intl.DisplayNames(['zh-Hans'], {type: 'currency'});
// → '美元'
// → '欧元'
// → '日元'
// → '人民币'

The following example shows the final supported type, scripts, in English, using ISO-15924 4-letter script codes.

const scriptNames = new Intl.DisplayNames(['en'], { type: 'script' });
// → 'Latin'
// → 'Arabic'
// → 'Katakana'

For more advanced usage, the second options parameter also supports the style property. The style property corresponds to the width of the display name and may be either "long", "short", or "narrow". The values for different styles do not always differ. The default is "long".

const longLanguageNames = new Intl.DisplayNames(['en'], { type: 'language' });
// → 'American English'
const shortLanguageNames = new Intl.DisplayNames(['en'], { type: 'language', style: 'short' });
// → 'US English'
const narrowLanguageNames = new Intl.DisplayNames(['en'], { type: 'language', style: 'narrow' });
// → 'US English'

Full API #

The full API for Intl.DisplayNames is as follows.

Intl.DisplayNames([ locales [ , options ]])
Intl.DisplayNames.prototype.of( code )

The constructor is consistent with other Intl APIs. Its first argument is a list of locales, and its second parameter is an options parameter that takes localeMatcher, type, and style properties.

The "localeMatcher" property is treated the same as in other Intl APIs. The type property may be "region", "language", "currency", or "script". The style property may be "long", "short", or "narrow", with "long" being the default.

Intl.DisplayNames.prototype.of( code ) expects the following formats depending on the type of how the instance is constructed.

Conclusion #

Like other Intl APIs, as Intl.DisplayNames become more widely available, libraries and applications will opt to drop packaging and shipping their own translation data in favor of using the native functionality.

Intl.DisplayNames support #