Intl.ListFormat【国際化リスト要素フォーマット】オブジェクト

メモ

概要

  • 国際化対応リスト要素フォーマット オブジェクト

    基本操作

    関連

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

    メソッド説明
    new Intl.ListFormat( [ locales [ , options ] ] ) コンストラクタ
    メソッド説明
    Intl.ListFormat.prototype. format ( list ) フォーマット
    Intl.ListFormat.prototype. formatToParts ( list ) フォーマットパーツ取得
    Intl.ListFormat.prototype. resolvedOptions ( ) オプション取得
    Intl.ListFormat. supportedLocalesOf ( locales [, options ] ) サポート ロケール取得

    プロパティ

    プロパティ説明
    Intl.ListFormat.prototype[ @@toStringTag ] タグ (デフォルト:'Intl.ListFormat')
    実装:ListFormat[ Symbol.toStringTag ]
    Intl.ListFormat.prototype.constructorコンストラクタ定義
    Intl.ListFormat.prototypeプロトタイプ
    const formatter = new Intl.ListFormat("ja");
    
    // [ @@toStringTag ]
    console.log(formatter[Symbol.toStringTag]);
    // 出力例:Intl.ListFormat
    
    console.log(formatter.constructor);
    // 出力例:function ListFormat()
    
    console.log(Intl.ListFormat.prototype);
    // 出力例:
    // Object { … }
    //   constructor: function ListFormat()
    //   format: function format()
    //   formatToParts: function formatToParts()
    //   resolvedOptions: function resolvedOptions()
    //   Symbol(Symbol.toStringTag): "Intl.ListFormat"
    //   ​<prototype>: Object { … }
    

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

    メモ

    構文

    new Intl.ListFormat([locales[, options]])
    
    Intl.ListFormat【国際化リスト要素フォーマット】オブジェクト
    localesロケール (BCP 47 の言語タグ等)〔実装依存〕
         省略:デフォルトのロケール〔実装依存〕
         文字列: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 (オプション)

    オプション値 (太字:デフォルト値)説明
    localeMatcher'lookup':Lookupアルゴリズム
    'best fit':最適アルゴリズム
    ロケールマッチングアルゴリズム
    style'narrow':縮小形式
    'short':短い形式
    'long':長い形式
    スタイル
    type'conjunction':AND
    'disjunction':OR
    'unit':単位付き
    列挙タイプ

    format【フォーマット】も参照

    const listJ = ['ネズミ', 'ネコ', 'イヌ'];
    const listE = ['Mouse', 'Cat', 'Dog'];
    
    // AND
    let formatterJ = new Intl.ListFormat('ja', { type: 'conjunction' });
    console.log(formatterJ.format(listJ));
    // 出力例:ネズミ、ネコ、イヌ
    let formatterE = new Intl.ListFormat('en', { type: 'conjunction' });
    console.log(formatterE.format(listE));
    // 出力例:Mouse, Cat, and Dog
    
    // OR
    formatterJ = new Intl.ListFormat('ja', { type: 'disjunction' });
    console.log(formatterJ.format(listJ));
    // 出力例:ネズミ、ネコ、またはイヌ
    formatterE = new Intl.ListFormat('en', { type: 'disjunction' });
    console.log(formatterE.format(listE));
    // 出力例:Mouse, Cat, or Dog
    
    // 単位付き
    const listJU = ['1メートル', '2センチ', '3ミリ'];
    const listEU = ['1 yard', '2 feet', '3 inches'];
    formatterJ = new Intl.ListFormat('ja', { type: 'unit' });
    console.log(formatterJ.format(listJU));
    // 出力例:1メートル 2センチ 3ミリ
    formatterE = new Intl.ListFormat('en', { type: 'unit' });
    console.log(formatterE.format(listEU));
    // 出力例:1 yard, 2 feet, 3 inches
    

    Intl.ListFormat.prototype.format【フォーマット】

    メモ

    構文

    ListFormat.format ( list )
    
    フォーマット文字列
    list要素のリスト
    

    new ListFormat【コンストラクタ】も参照

    const styles = ['narrow', 'short', 'long'];
    const listJ = ['ネズミ', 'ネコ', 'イヌ'];
    const listE = ['Mouse', 'Cat', 'Dog'];
    
    // AND
    for (let style of styles) {
      let formatterJ = new Intl.ListFormat('ja', { type: 'conjunction', style: style });
      console.log(formatterJ.format(listJ), '(' + style + ')');
    }
    // 出力例:ネズミ、ネコ、イヌ (narrow)
    // 出力例:ネズミ、ネコ、イヌ (short)
    // 出力例:ネズミ、ネコ、イヌ (long)
    
    for (let style of styles) {
      let formatterE = new Intl.ListFormat('en', { type: 'conjunction', style: style });
      console.log(formatterE.format(listE), '(' + style + ')');
    }
    // 出力例:Mouse, Cat, Dog (narrow)
    // 出力例:Mouse, Cat, & Dog (short)
    // 出力例:Mouse, Cat, and Dog (long)
    
    // OR
    for (let style of styles) {
      formatterJ = new Intl.ListFormat('ja', { type: 'disjunction', style: style });
      console.log(formatterJ.format(listJ), '(' + style + ')');
    }
    // 出力例:ネズミ、ネコ、またはイヌ (narrow)
    // 出力例:ネズミ、ネコ、またはイヌ (short)
    // 出力例:ネズミ、ネコ、またはイヌ (long)
    
    for (let style of styles) {
      formatterE = new Intl.ListFormat('en', { type: 'disjunction', style: style });
      console.log(formatterE.format(listE), '(' + style + ')');
    }
    // 出力例:Mouse, Cat, or Dog (narrow)
    // 出力例:Mouse, Cat, or Dog (short)
    // 出力例:Mouse, Cat, or Dog (long)
    
    // 単位付き
    const listJ2 = ['1メートル', '2センチ', '3ミリ'];
    const listE2 = ['1 yard', '2 feet', '3 inches'];
    for (let style of styles) {
      formatterJ = new Intl.ListFormat('ja', { type: 'unit', style: style });
      console.log(formatterJ.format(listJ2), '(' + style + ')');
    }
    // 出力例:1メートル2センチ3ミリ (narrow)
    // 出力例:1メートル 2センチ 3ミリ (short)
    // 出力例:1メートル 2センチ 3ミリ (long)
    
    for (let style of styles) {
      formatterE = new Intl.ListFormat('en', { type: 'unit', style: style });
      console.log(formatterE.format(listE2), '(' + style + ')');
    }
    // 出力例:1 yard 2 feet 3 inches (narrow)
    // 出力例:1 yard, 2 feet, 3 inches (short)
    // 出力例:1 yard, 2 feet, 3 inches (long)
    

    Intl.ListFormat.prototype.formatToParts【フォーマットパーツ取得】

    メモ

    構文

    ListFormat.formatToParts ( list )
    
     (Array)フォーマットのパーツ
    list要素のリスト
    

    const listJ = ['ネズミ', 'ネコ', 'イヌ'];
    const listE = ['Mouse', 'Cat', 'Dog'];
    
    // AND
    let formatterJ = new Intl.ListFormat('ja', { type: 'conjunction' });
    array = formatterJ.formatToParts(listJ);
    for (let i = 0; i < array.length; i++) {
      console.log(i, array[i]);
    }
    // 出力例:
    // 0 Object { type: "element", value: "ネズミ" }
    // 1 Object { type: "literal", value: "、" }
    // 2 Object { type: "element", value: "ネコ" }
    // 3 Object { type: "literal", value: "、" }
    // 4 Object { type: "element", value: "イヌ" }
    
    let formatterE = new Intl.ListFormat('en', { type: 'conjunction' });
    array = formatterE.formatToParts(listE);
    for (let i = 0; i < array.length; i++) {
      console.log(i, array[i]);
    }
    // 出力例:
    // 0 Object { type: "element", value: "Mouse" }
    // 1 Object { type: "literal", value: ", " }
    // 2 Object { type: "element", value: "Cat" }
    // 3 Object { type: "literal", value: ", and " }
    // 4 Object { type: "element", value: "Dog" }
    
    // OR
    formatterJ = new Intl.ListFormat('ja', { type: 'disjunction' });
    array = formatterJ.formatToParts(listJ);
    for (let i = 0; i < array.length; i++) {
      console.log(i, array[i]);
    }
    // 出力例:
    // 0 Object { type: "element", value: "ネズミ" }
    // 1 Object { type: "literal", value: "、" }
    // 2 Object { type: "element", value: "ネコ" }
    // 3 Object { type: "literal", value: "、または" }
    // 4 Object { type: "element", value: "イヌ" }
    
    formatterE = new Intl.ListFormat('en', { type: 'disjunction' });
    array = formatterE.formatToParts(listE);
    for (let i = 0; i < array.length; i++) {
      console.log(i, array[i]);
    }
    // 出力例:
    // 0 Object { type: "element", value: "Mouse" }
    // 1 Object { type: "literal", value: ", " }
    // 2 Object { type: "element", value: "Cat" }
    // 3 Object { type: "literal", value: ", or " }
    // 4 Object { type: "element", value: "Dog" }
    
    // 単位付き
    const listJ2 = ['1メートル', '2センチ', '3ミリ'];
    const listE2 = ['1 yard', '2 feet', '3 inches'];
    formatterJ = new Intl.ListFormat('ja', { type: 'unit' });
    array = formatterJ.formatToParts(listJ2);
    for (let i = 0; i < array.length; i++) {
      console.log(i, array[i]);
    }
    // 出力例:
    // 0 Object { type: "element", value: "1メートル" }
    // 1 Object { type: "literal", value: " " }
    // 2 Object { type: "element", value: "2センチ" }
    // 3 Object { type: "literal", value: " " }
    // 4 Object { type: "element", value: "3ミリ" }
    
    formatterE = new Intl.ListFormat('en', { type: 'unit' });
    array = formatterE.formatToParts(listE2);
    for (let i = 0; i < array.length; i++) {
      console.log(i, array[i]);
    }
    // 出力例:
    // 0 Object { type: "element", value: "1 yard" }
    // 1 Object { type: "literal", value: ", " }
    // 2 Object { type: "element", value: "2 feet" }
    // 3 Object { type: "literal", value: ", " }
    // 4 Object { type: "element", value: "3 inches" }
    

    Intl.ListFormat.prototype.resolvedOptions【オプション取得】

    メモ

    構文

    ListFormat.resolvedOptions ()
    
    オブジェクト (下記プロパティ有効)
    
    プロパティ説明
    localeロケール
    type列挙タイプ
    styleスタイル

    let formatter = new Intl.ListFormat('ja');
    console.log(formatter.resolvedOptions());
    // 出力例:Object { locale: "ja", type: "conjunction", style: "long" }
    
    formatter = new Intl.ListFormat('en', { type: 'disjunction', style: 'narrow' });
    console.log(formatter.resolvedOptions());
    // 出力例:Object { locale: "en", type: "disjunction", style: "narrow" }
    
    formatter = new Intl.ListFormat(['ja', 'en']);
    console.log(formatter.resolvedOptions());
    // 出力例:Object { locale: "ja", type: "conjunction", style: "long" }
    

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

    メモ

    構文

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

    console.log(Intl.ListFormat.supportedLocalesOf('ja'));
    // 出力例:Array [ "ja" ]
    
    console.log(Intl.ListFormat.supportedLocalesOf('ng'));
    // 出力例:Array []
    
    let locales = ['ng', 'ja', 'ja-JP', 'en', 'en-US', 'en-GB'];
    console.log(Intl.ListFormat.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.ListFormat.supportedLocalesOf(locales));
    // 出力例:Array(5) [ "ja", "ja-JP", "en", "en-US", "en-GB" ]
    
    console.log(Intl.ListFormat.supportedLocalesOf(locales, { localeMatcher: 'lookup' }));
    // 出力例:Array(5) [ "ja", "ja-JP", "en", "en-US", "en-GB" ]