Intl.DisplayNames【国際化表示名】オブジェクト

メモ

概要

  • 国際化対応表示名 を操作 (下記に対応)
    • 言語
    • 地域
    • 文字体系
    • 通貨
    • 日時フィールド

基本操作

関連

外部リンク

コンストラクタ・メソッド

コンストラクタ説明
new Intl.DisplayNames ( locales, options ) コンストラクタ
メソッド説明
Intl.DisplayNames.prototype.of ( code )表示名取得
Intl.DisplayNames.prototype.resolvedOptions ( )ロケール・オプション取得
Intl.DisplayNames.supportedLocalesOf ( locales [, options ] ) サポート ロケール取得

プロパティ

プロパティ説明
Intl.DisplayNames.prototype[ @@toStringTag ] タグ (デフォルト:'Intl.DisplayNames')
実装:DisplayNames[ Symbol.toStringTag ]
Intl.DisplayNames.prototype.constructorコンストラクタ定義
Intl.DisplayNames.prototypeプロトタイプ
const names = new Intl.DisplayNames("ja", { type: 'language'});

// [ @@toStringTag ]
console.log(names[Symbol.toStringTag]);
// 出力例:Intl.DisplayNames

console.log(names.constructor);
// 出力例:function DisplayNames()

console.log(Intl.DisplayNames.prototype);
// 出力例:
// Object { … }
//   constructor: function DisplayNames()
//   of: function of()
//   resolvedOptions: function resolvedOptions()
//   Symbol(Symbol.toStringTag): "Intl.DisplayNames"
//   <prototype>: Object { … }

new Intl.DisplayNames【コンストラクタ】

メモ

構文

new Intl.DisplayNames(locales, options)

localesロケール (BCP 47 の言語タグ等)〔実装依存〕
     文字列:1ロケール指定
     文字列の配列:複数指定可 (適切な1ロケールを自動選択)
optionsオプション の組合せ

locales (ロケール) 実装依存

BCP 47 の言語タグ (一例)
備考
ja日本語
ja-JP日本語 (日本)
en-US英語 (アメリカ)
en-GB英語 (イギリス)
de-DEドイツ語 (ドイツ)
fr-FRフランス語 (フランス)
ISO 639-1・639-2 (言語コード) 一例
ISO 639-1ISO 639-2ISO 639-3備考
jajpnjpn日本語
enengeng英語
dedeu
ger
deuドイツ語
frfra
fre
fraフランス語

options (オプション)

オプション値 (太字:デフォルト値)説明
fallback 'code':コード
'none':なし
代替名 (表示名がない場合)
languageDisplay 'dialect':日用語
'standard':標準
言語表示
(type【タイプ】が'language'【言語】の場合)
localeMatcher 'lookup':Lookupアルゴリズム
'best fit':最適アルゴリズム
ロケールマッチングアルゴリズム
style'narrow':縮小形式
'short':短い形式
'long':長い形式
スタイル
type (必須) 'calendar':暦
'currency':通貨
'dateTimeField':日時フィールド
'language':言語
'region':地域
'script':文字体系
タイプ

例 (1/2)

of【表示名取得】の 例 も参照

// calendar:暦
let namesJ = new Intl.DisplayNames('ja', { type: 'calendar' });
let namesE = new Intl.DisplayNames('en', { type: 'calendar' });
for (let code of ['gregory', 'japanese', 'iso8601']) {
  console.log(namesJ.of(code), '/', namesE.of(code));
}
// 出力例:
// 西暦(グレゴリオ暦) / Gregorian Calendar
// 和暦 / Japanese Calendar
// ISO-8601 / ISO-8601 Calendar

// currency:通貨
namesJ = new Intl.DisplayNames('ja', { type: 'currency' });
namesE = new Intl.DisplayNames('en', { type: 'currency' });
for (let code of ['JPY', 'USD', 'EUR']) {
  console.log(namesJ.of(code), '/', namesE.of(code));
}
// 出力例:
// 日本円 / Japanese Yen
// 米ドル / US Dollar
// ユーロ / Euro

// dateTimeField:日時フィールド
namesJ = new Intl.DisplayNames('ja', { type: 'dateTimeField' });
namesE = new Intl.DisplayNames('en', { type: 'dateTimeField' });
for (let code of ['era', 'year', 'quarter', 'month', 'weekOfYear', 'weekday', 'day', 'dayPeriod', 'hour', 'minute', 'second', 'timeZoneName']) {
  console.log(namesJ.of(code), '/', namesE.of(code));
}
// 出力例:
// 時代 / era
// 年 / year
// 四半期 / quarter
// 月 / month
// 週 / week
// 曜日 / day of the week
// 日 / day
// 午前/午後 / AM/PM
// 時 / hour
// 分 / minute
// 秒 / second
// タイムゾーン / time zone

// language:言語
namesJ = new Intl.DisplayNames('ja', { type: 'language' });
namesE = new Intl.DisplayNames('en', { type: 'language' });
for (let code of ['ja', 'ja-JP', 'en-US', 'en-GB']) {
  console.log(namesJ.of(code), '/', namesE.of(code));
}
// 出力例:
// 日本語 / Japanese
// 日本語 (日本) / Japanese (Japan)
// アメリカ英語 / American English
// イギリス英語 / British English

// region:地域
namesJ = new Intl.DisplayNames('ja', { type: 'region' });
namesE = new Intl.DisplayNames('en', { type: 'region' });
for (let code of ['JP', 'US', 'GB']) {
  console.log(namesJ.of(code), '/', namesE.of(code));
}
// 出力例:
// 日本 / Japan
// アメリカ合衆国 / United States
// イギリス / United Kingdom

// script:文字体系
namesJ = new Intl.DisplayNames('ja', { type: 'script' });
namesE = new Intl.DisplayNames('en', { type: 'script' });
for (let code of ['Hani', 'Hira', 'Kana']) {
  console.log(namesJ.of(code), '/', namesE.of(code));
}
// 出力例:
// 漢字 / Han
// ひらがな / Hiragana
// カタカナ / Katakana

例 (2/2)

// ISO 639-2
const namesJ = new Intl.DisplayNames(['ng', 'jpn', 'eng'], { type: 'region' });
const namesE = new Intl.DisplayNames(['ng', 'eng', 'jpn'], { type: 'region' });
for (let code of ['JP', 'US', 'GB']) {
  console.log(namesJ.of(code), '/', namesE.of(code));
}
// 出力例:
// 日本 / Japan
// アメリカ合衆国 / United States
// イギリス / United Kingdom

Intl.DisplayNames.prototype.of【表示名取得】

メモ

構文

displayNames.of(code)

表示名 (実装依存)
codeタイプ別コード

RangeError 例外code (タイプ別コード)が不正

code (タイプ別コード)

コンストラクタ
options (オプション)の type
code (タイプ別コード) 例 (実装依存)
'calendar'
(暦)
暦アルゴリズム
(calendar.xml)
'gregory':西暦(グレゴリオ暦)
'japanese':和暦
'iso8601':ISO-8601
'currency'
(通貨)
ISO 通貨コード (3文字)
(currency.xml)
'JPY':日本円
'USD':米ドル
'GBP':英国ポンド
'EUR':ユーロ
'dateTimeField'
(日時フィールド)
下記のコード
コード備考
'era'時代
'year'
'quarter'四半期
'month'
'weekOfYear'
'weekday'曜日
'day'
'dayPeriod'午前/午後
'hour'
'minute'
'second'
'timeZoneName'タイムゾーン
'year':年
'month':月
'day':日
'weekday':曜日
'language'
(言語)
unicode_language_subtag
[-unicode_script_subtag]
[-unicode_region_subtag]
[-unicode_variant_subtag] (複数指定可)
(unicode_language_id)
'ja':日本語
'ain':アイヌ語
'ja-JP':日本語 (日本)
'ja-Kana-JP':日本語 (カタカナ、日本)
'ja-392-hepburn':日本語 (日本、ヘボン式ローマ字)
'en':英語
'en-US':アメリカ英語
'en-GB':イギリス英語
'region'
(地域)
英字2文字 (ISO 3166-1)
3桁の地域コード (UN M.49)〔実装依存〕
(unicode_region_subtag)
'JP':日本
'US':アメリカ
'GB':イギリス
'script'
(文字体系)
英字4文字 (ISO 15924)
(unicode_script_subtag)
'Hani':漢字 (Han:漢王朝)
'Hira':ひらがな
'Kana':カタカナ
'Hrkt':仮名 (ひらがな+カタカナ)
'Jpan':日本語の文字 (漢字+ひらがな+カタカナ)
'Zsye':絵文字
'Latn':ラテン文字

例 (calendar:暦)

const locales = ['ja', 'en'];
let codes = ['gregory', 'japanese', 'iso8601'];
const styles = ['narrow', 'short', 'long'];

for (let locale of locales) {
  for (let code of codes) {
    let str = '';
    for (let style of styles) {
      displayNames = new Intl.DisplayNames(locale, { type: 'calendar', style: style });
      name = displayNames.of(code);
      str += ' [' + name + ']';
    }
    console.log(locale + " of('" + code + "')" + str);
  }
}
// 出力例:
// ja of('gregory') [西暦(グレゴリオ暦)] [西暦(グレゴリオ暦)] [西暦(グレゴリオ暦)]
// ja of('japanese') [和暦] [和暦] [和暦]
// ja of('iso8601') [ISO-8601] [ISO-8601] [ISO-8601]
// en of('gregory') [Gregorian Calendar] [Gregorian Calendar] [Gregorian Calendar]
// en of('japanese') [Japanese Calendar] [Japanese Calendar] [Japanese Calendar]
// en of('iso8601') [ISO-8601 Calendar] [ISO-8601 Calendar] [ISO-8601 Calendar]

例 (currency:通貨)

for (let locales of ['ja', 'en']) {
  for (let code of ['JPY', 'USD', 'GBP', 'EUR']) {
    let str = '';
    for (let style of ['narrow', 'short', 'long']) {
      displayNames = new Intl.DisplayNames(locales, { type: 'currency', style: style });
      name = displayNames.of(code);
      str += ' [' + name + ']';
    }
    console.log(locales + " of('" + code + "')" + str);
  }
}
// 出力例:
// ja of('JPY') [¥] [¥] [日本円]
// ja of('USD') [$] [$] [米ドル]
// ja of('GBP') [£] [£] [英国ポンド]
// ja of('EUR') [€] [€] [ユーロ]
// en of('JPY') [JPY] [¥] [Japanese Yen]
// en of('USD') [USD] [$] [US Dollar]
// en of('GBP') [GBP] [£] [British Pound]
// en of('EUR') [EUR] [€] [Euro]

例 (dateTimeField:日時フィールド)

const locales = ['ja', 'en'];
const codes = ['era', 'year', 'quarter', 'month', 'weekOfYear', 'weekday', 'day', 'dayPeriod', 'hour', 'minute', 'second', 'timeZoneName'];
const styles = ['narrow', 'short', 'long'];

for (let locale of locales) {
  for (let code of codes) {
    let str = '';
    for (let style of styles) {
      displayNames = new Intl.DisplayNames(locale, { type: 'dateTimeField', style: style });
      name = displayNames.of(code);
      str += ' [' + name + ']';
    }
    console.log(locale + " of('" + code + "')" + str);
  }
}
// 出力例:
// ja of('era') [時代] [時代] [時代]
// ja of('year') [年] [年] [年]
// ja of('quarter') [四半期] [四半期] [四半期]
// ja of('month') [月] [月] [月]
// ja of('weekOfYear') [週] [週] [週]
// ja of('weekday') [曜日] [曜日] [曜日]
// ja of('day') [日] [日] [日]
// ja of('dayPeriod') [午前/午後] [午前/午後] [午前/午後]
// ja of('hour') [時] [時] [時]
// ja of('minute') [分] [分] [分]
// ja of('second') [秒] [秒] [秒]
// ja of('timeZoneName') [タイムゾーン] [タイムゾーン] [タイムゾーン]
// en of('era') [era] [era] [era]
// en of('year') [yr.] [yr.] [year]
// en of('quarter') [qtr.] [qtr.] [quarter]
// en of('month') [mo.] [mo.] [month]
// en of('weekOfYear') [wk.] [wk.] [week]
// en of('weekday') [day of wk.] [day of wk.] [day of the week]
// en of('day') [day] [day] [day]
// en of('dayPeriod') [AM/PM] [AM/PM] [AM/PM]
// en of('hour') [hr.] [hr.] [hour]
// en of('minute') [min.] [min.] [minute]
// en of('second') [sec.] [sec.] [second]
// en of('timeZoneName') [zone] [zone] [time zone]

例 (language:言語)

for (let locales of ['ja', 'en']) {
  for (let code of ['ja', 'ain', 'ja-JP', 'ja-Kana-JP', 'ja-392-hepburn', 'en', 'en-US', 'en-GB', 'eo']) {
    let str = '';
    for (let style of ['narrow', 'short', 'long']) {
      displayNames = new Intl.DisplayNames(locales, { type: 'language', style: style });
      name = displayNames.of(code);
      str += ' [' + name + ']';
    }
    console.log(locales + " of('" + code + "')" + str);
  }
}
// 出力例:
// ja of('ja') [日本語] [日本語] [日本語]
// ja of('ain') [アイヌ語] [アイヌ語] [アイヌ語]
// ja of('ja-JP') [日本語 (日本)] [日本語 (日本)] [日本語 (日本)]
// ja of('ja-Kana-JP') [日本語 (カタカナ、日本)] [日本語 (カタカナ、日本)] [日本語 (カタカナ、日本)]
// ja of('ja-392-hepburn') [日本語 (日本、ヘボン式ローマ字)] [日本語 (日本、ヘボン式ローマ字)] [日本語 (日本、ヘボン式ローマ字)]
// ja of('en') [英語] [英語] [英語]
// ja of('en-US') [英語(米国)] [英語(米国)] [アメリカ英語]
// ja of('en-GB') [英語(英国)] [英語(英国)] [イギリス英語]
// ja of('eo') [エスペラント語] [エスペラント語] [エスペラント語]
// en of('ja') [Japanese] [Japanese] [Japanese]
// en of('ain') [Ainu] [Ainu] [Ainu]
// en of('ja-JP') [Japanese (Japan)] [Japanese (Japan)] [Japanese (Japan)]
// en of('ja-Kana-JP') [Japanese (Katakana, Japan)] [Japanese (Katakana, Japan)] [Japanese (Katakana, Japan)]
// en of('ja-392-hepburn') [Japanese (Japan, Hepburn romanization)] [Japanese (Japan, Hepburn romanization)] [Japanese (Japan, Hepburn romanization)]
// en of('en') [English] [English] [English]
// en of('en-US') [US English] [US English] [American English]
// en of('en-GB') [UK English] [UK English] [British English]
// en of('eo') [Esperanto] [Esperanto] [Esperanto]

for (let locales of ['ja', 'en']) {
  for (let code of ['en-US', 'en-GB']) {
    let str = '';
    for (let style of ['narrow', 'short', 'long']) {
      str += '\n';
      for (let languageDisplay of ['dialect', 'standard']) {
        displayNames = new Intl.DisplayNames(
          locales,
          { type: 'language', style: style, languageDisplay: languageDisplay });
        name = displayNames.of(code);
        str += ' [' + name + ']';
      }
    }
    console.log(locales + " of('" + code + "')" + str);
  }
}
// 出力例:
// ja of('en-US')
//  [英語(米国)] [英語 (アメリカ)]
//  [英語(米国)] [英語 (アメリカ)]
//  [アメリカ英語] [英語 (アメリカ合衆国)]
// ja of('en-GB')
//  [英語(英国)] [英語 (英国)]
//  [英語(英国)] [英語 (英国)]
//  [イギリス英語] [英語 (イギリス)]
// en of('en-US')
//  [US English] [English (US)]
//  [US English] [English (US)]
//  [American English] [English (United States)]
// en of('en-GB')
//  [UK English] [English (UK)]
//  [UK English] [English (UK)]
//  [British English] [English (United Kingdom)]

例 (region:地域)

for (let locales of ['ja', 'en']) {
  for (let code of ['JP', 'US', 'GB', 392, 840, 826]) {
    let str = '';
    for (let style of ['narrow', 'short', 'long']) {
      displayNames = new Intl.DisplayNames(locales, { type: 'region', style: style });
      name = displayNames.of(code);
      str += ' [' + name + ']';
    }
    console.log(locales + " of('" + code + "')" + str);
  }
}
// 出力例:
// ja of('JP') [日本] [日本] [日本]
// ja of('US') [アメリカ] [アメリカ] [アメリカ合衆国]
// ja of('GB') [英国] [英国] [イギリス]
// ja of('392') [日本] [日本] [日本]
// ja of('840') [アメリカ] [アメリカ] [アメリカ合衆国]
// ja of('826') [英国] [英国] [イギリス]
// en of('JP') [Japan] [Japan] [Japan]
// en of('US') [US] [US] [United States]
// en of('GB') [UK] [UK] [United Kingdom]
// en of('392') [Japan] [Japan] [Japan]
// en of('840') [US] [US] [United States]
// en of('826') [UK] [UK] [United Kingdom]

例 (script:文字体系)

for (let locales of ['ja', 'en']) {
  for (let code of ['Hani', 'Hira', 'Kana', 'Hrkt', 'Jpan', 'Zsye', 'Latn']) {
    let str = '';
    for (let style of ['narrow', 'short', 'long']) {
      displayNames = new Intl.DisplayNames(locales, { type: 'script', style: style });
      name = displayNames.of(code);
      str += ' [' + name + ']';
    }
    console.log(locales + " of('" + code + "')" + str);
  }
}
// 出力例:
// ja of('Hani') [漢字] [漢字] [漢字]
// ja of('Hira') [ひらがな] [ひらがな] [ひらがな]
// ja of('Kana') [カタカナ] [カタカナ] [カタカナ]
// ja of('Hrkt') [仮名] [仮名] [仮名]
// ja of('Jpan') [日本語の文字] [日本語の文字] [日本語の文字]
// ja of('Zsye') [絵文字] [絵文字] [絵文字]
// ja of('Latn') [ラテン文字] [ラテン文字] [ラテン文字]
// en of('Hani') [Han] [Han] [Han]
// en of('Hira') [Hiragana] [Hiragana] [Hiragana]
// en of('Kana') [Katakana] [Katakana] [Katakana]
// en of('Hrkt') [Japanese syllabaries] [Japanese syllabaries] [Japanese syllabaries]
// en of('Jpan') [Japanese] [Japanese] [Japanese]
// en of('Zsye') [Emoji] [Emoji] [Emoji]
// en of('Latn') [Latin] [Latin] [Latin]

Intl.DisplayNames.prototype.resolvedOptions【ロケール・オプション取得】

メモ

構文

displayNames.resolvedOptions( )

オブジェクト (下記プロパティ有効)
プロパティ説明
localeロケール
styleスタイル
typeタイプ
fallback代替名

let names = new Intl.DisplayNames('ja', { type: 'language' });
console.log(names.resolvedOptions());
// 出力例:Object { locale: "ja", style: "long", type: "language", fallback: "code", languageDisplay: "dialect" }

names = new Intl.DisplayNames('en', { type: 'region', style: 'narrow' });
console.log(names.resolvedOptions());
// 出力例:Object { locale: "en", style: "narrow", type: "region", fallback: "code" }

Intl.DisplayNames.supportedLocalesOf【サポート ロケール取得】

メモ

構文

Intl.DisplayNames.supportedLocalesOf( locales [, options]) 

サポートされるロケールの配列
localesBCP 47 言語タグの文字列 または その配列
(new DisplayNames【コンストラクタ】ロケール詳細 を参照)
optionsマッチングオプション (localeMatcher【ロケールマッチングアルゴリズム】)
(new DisplayNames【コンストラクタ】オプション詳細 を参照)

console.log(Intl.DisplayNames.supportedLocalesOf('ja'));
// 出力例:Array [ "ja" ]

console.log(Intl.DisplayNames.supportedLocalesOf('ng'));
// 出力例:Array []

let locales = ['ng', 'ja', 'ja-JP', 'en', 'en-US', 'en-GB'];
console.log(Intl.DisplayNames.supportedLocalesOf(locales));
// 出力例:Array(5) [ "ja", "ja-JP", "en", "en-US", "en-GB" ]

locales = ['ng', 'jpn', 'jpn-JP', 'eng', 'eng-US', 'eng-GB'];
console.log(Intl.DisplayNames.supportedLocalesOf(locales));
// 出力例:Array(5) [ "ja", "ja-JP", "en", "en-US", "en-GB" ]

console.log(Intl.DisplayNames.supportedLocalesOf(locales, { localeMatcher: 'lookup' }));
// 出力例:Array(5) [ "ja", "ja-JP", "en", "en-US", "en-GB" ]