UNB/ CS/ David Bremner/ teaching/ cs2613/ books/ mdn/ Reference/ Global Objects/ Intl/ DisplayNames/ Intl.DisplayNames() constructor

The Intl.DisplayNames() constructor creates Intl.DisplayNames objects.

Syntax

new Intl.DisplayNames(locales, options)

Note: Intl.DisplayNames() can only be constructed with new. Attempting to call it without new throws a TypeError.

Parameters

Exceptions

Examples

Basic usage

In basic use without specifying a locale, a formatted string in the default locale and with default options is returned.

console.log(new Intl.DisplayNames([], { type: "language" }).of("US"));
// 'us'

Using type dateTimeField

Example using dateTimeField as a type option, will return the localized date time names strings.

const dn = new Intl.DisplayNames("pt", { type: "dateTimeField" });
console.log(dn.of("era")); // 'era'
console.log(dn.of("year")); // 'ano'
console.log(dn.of("month")); // 'mês'
console.log(dn.of("quarter")); // 'trimestre'
console.log(dn.of("weekOfYear")); // 'semana'
console.log(dn.of("weekday")); // 'dia da semana'
console.log(dn.of("dayPeriod")); // 'AM/PM'
console.log(dn.of("day")); // 'dia'
console.log(dn.of("hour")); // 'hora'
console.log(dn.of("minute")); // 'minuto'
console.log(dn.of("second")); // 'segundo'

Using type calendar

Example using calendar as a type option, will return the localized calendar names strings.

const dn = new Intl.DisplayNames("en", { type: "calendar" });
console.log(dn.of("roc")); // 'Minguo Calendar'
console.log(dn.of("gregory")); // 'Gregorian Calendar'
console.log(dn.of("chinese")); // 'Chinese Calendar'

Using type language with languageDisplay

Example using language as a type with languageDisplay options.

// Using `dialect` option
const dnDialect = new Intl.DisplayNames("en", {
  type: "language",
  languageDisplay: "dialect",
});
console.log(dnDialect.of("en-GB")); // 'British English'

// Using `standard` option
const dnStd = new Intl.DisplayNames("en", {
  type: "language",
  languageDisplay: "standard",
});
console.log(dnStd.of("en-GB")); // 'English (United Kingdom)'

Specifications

Browser compatibility

See also