Intl.DisplayNames() constructor
The Intl.DisplayNames()
constructor creates Intl.DisplayNames
objects.
Try it
Syntax
Parameters
locales
-
A string with a BCP 47 language tag, or an array of such strings. For the general form and interpretation of the
locales
argument, see the parameter description on theIntl
main page. The following Unicode extension key is allowed:nu
-
The numbering system to be used. Possible values include:
"arab"
,"arabext"
,"bali"
,"beng"
,"deva"
,"fullwide"
,"gujr"
,"guru"
,"hanidec"
,"khmr"
,"knda"
,"laoo"
,"latn"
,"limb"
,"mlym"
,"mong"
,"mymr"
,"orya"
,"tamldec"
,"telu"
,"thai"
,"tibt"
.
options
-
An object with some or all of the following properties:
localeMatcher
-
The locale matching algorithm to use. Possible values are
"lookup"
and"best fit"
; the default is"best fit"
. For information about this option, see the Intl page. style
-
The formatting style to use, the default is
"long"
."narrow"
"short"
"long"
type
-
The type to use.
"calendar"
"currency"
"dateTimeField"
"language"
"region"
"script"
languageDisplay
-
The
languageDisplay
it's only usable along with typelanguage
, defaults todialect
."dialect"
"standard"
fallback
-
The fallback to use, the default is
"code"
."code"
"none"
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
Specification |
---|
ECMAScript Internationalization API Specification # sec-intl-displaynames-constructor |
Browser compatibility
BCD tables only load in the browser