Intl.DisplayNames() コンストラクター
Baseline
広く利用可能
この機能は広く実装されており、多くのバージョンの端末やブラウザーで動作します。2021年4月以降、すべてのブラウザーで利用可能です。
Intl.DisplayNames() コンストラクターは、 Intl.DisplayNames オブジェクトを生成します。
試してみましょう
const regionNamesInEnglish = new Intl.DisplayNames(["en"], { type: "region" });
const regionNamesInTraditionalChinese = new Intl.DisplayNames(["zh-Hant"], {
type: "region",
});
console.log(regionNamesInEnglish.of("US"));
// 予想される結果: "United States"
console.log(regionNamesInTraditionalChinese.of("US"));
// 予想される結果: "美國"
構文
new Intl.DisplayNames(locales, options)
引数
locales-
BCP 47 言語タグを持つ文字列または
Intl.Localeインスタンス、またはそのようなロケール識別子の配列。undefinedが渡された場合や、指定したロケール識別子に対応していない場合は、ランタイムのデフォルトロケールが使用されます。locales引数の一般的な形式や 解釈については、Intlメインページの引数の説明を参照してください。 options-
以下のプロパティの一部またはすべてを持つオブジェクトです。
localeMatcher省略可-
使用するロケール照合アルゴリズムです。利用可能な値は、
"lookup"と"best fit"です。デフォルト値は"best fit"です。このオプションについての情報は、ロケール識別子とネゴシエーションを参照してください。 style省略可-
使用する書式化スタイルです。取りうる値は
"narrow","short","long"です。デフォルト値は"long"です。 type-
The
of()から返すための表示名の種類です。取りうる値は"language","region","script","currency","calendar","dateTimeField"です。 fallback省略可-
入力値が構造的には有効だが、一致する表示名がない場合に
of()から返す値です。取りうる値は次の通りです。"code"(デフォルト値)-
入力コード自体を返します。
"none"-
undefinedを返します。
languageDisplay省略可-
言語名をどのように表示するかです。
type: "language"と共にのみ使用可能です。取りうる値は次の通りです。"dialect"(デフォルト値)-
特別な地域の方言を、その名前を使用して表示します。例えば、
"nl-BE"は"Flemish"と表示されます。 "standard"-
標準形式を使用してすべての言語を表示します。例:
"nl-BE"は"Dutch (Belgium)"と表示されます。
例外
TypeError-
options.typeが提供されなかった場合に発生します。 RangeError-
localesまたはoptionsの値が不正であった場合に発生します。
例
>基本的な使用法
ロケールを指定しない基本的な使用法では、デフォルトのロケールとデフォルトのオプションで書式化された文字列が返されます。
console.log(new Intl.DisplayNames([], { type: "language" }).of("US"));
// 'us'
dateTimeField 型の使用
入力型オプションとして dateTimeField を使用する例では、ローカライズされた日付時刻の文字列を返します。
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'
calendar 型の使用
型オプションとして calendar を使用する例では、ローカライズされたカレンダーの名前の文字列を返します。
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'
language 型を languageDisplay 付きで使用
language を languageDisplay オプション付きで使用する例です。
// `dialect` オプションの使用
const dnDialect = new Intl.DisplayNames("en", {
type: "language",
languageDisplay: "dialect",
});
console.log(dnDialect.of("en-GB")); // 'British English'
// `standard` オプションの使用
const dnStd = new Intl.DisplayNames("en", {
type: "language",
languageDisplay: "standard",
});
console.log(dnStd.of("en-GB")); // 'English (United Kingdom)'
仕様書
| 仕様書 |
|---|
| ECMAScript® 2027 Internationalization API Specification> # sec-intl-displaynames-constructor> |