Intl.ListFormat【国際化リスト要素フォーマット】オブジェクト
メモ
- 国際化対応のリスト要素フォーマット オブジェクト
- 基本的な操作
- ロケールとリスト要素の列挙タイプを指定して new ListFormat【コンストラクタ】 でオブジェクトを生成 (必要に応じてスタイル等も指定)
- format【フォーマット】メソッドでリスト要素の列挙を取得
関連
外部リンク
- ECMA-402 (英語)
ECMA-402:ListFormat Objects ES2021 Intl (8) ES2020 Intl (7) ES2019 Intl (6) ES2018 Intl (5) ES2017 Intl (4) ES2016 Intl (3) ES2015 Intl (2) ES Intl (1)
コンストラクタ・メソッド
メソッド | 説明 |
---|---|
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【コンストラクタ】
メモ
- Intl.ListFormat【国際化リスト要素フォーマット】 オブジェクトを生成
- 関連
- 外部リンク
- ECMA-402 (英語)
ECMA-402:Intl.ListFormat ( [ locales [ , options ] ] ) ES2021 Intl (8) ES2020 Intl (7) ES2019 Intl (6) ES2018 Intl (5) ES2017 Intl (4) ES2016 Intl (3) ES2015 Intl (2) ES Intl (1) - ISO 639-1コード一覧
- BCP 47 (Best Current Practice) [英語]
- ECMA-402 (英語)
構文
new Intl.ListFormat([locales[, options]])
戻り値Intl.ListFormat【国際化リスト要素フォーマット】オブジェクト
localesロケール (BCP 47 の言語タグ)
文字列:1ロケール指定
文字列の配列:複数指定可 (適切なロケールを自動選択)
optionsオプション の組合せ
locales (ロケール)
BCP 47 の言語タグ (一例)
値 | 説明 |
---|---|
ja | 日本語 |
ja-JP | 日本語 (日本) |
en | 英語 |
en-US | 英語 (アメリカ) |
en-GB | 英語 (イギリス) |
de-DE | ドイツ語 (ドイツ) |
fr-FR | フランス語 (フランス) |
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【フォーマット】
メモ
- リストの要素を列挙タイプ(AND・OR・単位付き)でフォーマット
- 関連
- 外部リンク (英語)
Intl.ListFormat.prototype.format ( list ) ES2021 Intl (8) ES2020 Intl (7) ES2019 Intl (6) ES2018 Intl (5) ES2017 Intl (4) ES2016 Intl (3) ES2015 Intl (2) ES Intl (1)
構文
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【フォーマットパーツ取得】
メモ
- フォーマットされたリストをパーツに分解
- パーツを編集し組み合わせることで、独自フォーマットが可能
- 関連
- 外部リンク (英語)
Intl.ListFormat.prototype.formatToParts ( list ) ES2021 Intl (8) ES2020 Intl (7) ES2019 Intl (6) ES2018 Intl (5) ES2017 Intl (4) ES2016 Intl (3) ES2015 Intl (2) ES Intl (1)
構文
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【オプション取得】
メモ
- ロケール・オプションを取得
- 以下のプロパティを持ったオプション オブジェクトを取得
(new Intl.ListFormat() 【コンストラクタ】も参照) - 関連
- 外部リンク (英語)
Intl.ListFormat.prototype.resolvedOptions () ES2021 Intl (8) ES2020 Intl (7) ES2019 Intl (6) ES2018 Intl (5) ES2017 Intl (4) ES2016 Intl (3) ES2015 Intl (2) ES Intl (1)
構文
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 ]) ES2021 Intl (8) ES2020 Intl (7) ES2019 Intl (6) ES2018 Intl (5) ES2017 Intl (4) ES2016 Intl (3) ES2015 Intl (2) ES Intl (1)
構文
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" ]