Intl.ListFormat
Baseline
広く利用可能
この機能は広く実装されており、多くのバージョンの端末やブラウザーで動作します。2021年4月以降、すべてのブラウザーで利用可能です。
Intl.ListFormat オブジェクトにより、言語を考慮したリストの書式化ができます。
試してみましょう
const vehicles = ["Motorcycle", "Bus", "Car"];
const formatter = new Intl.ListFormat("en", {
style: "long",
type: "conjunction",
});
console.log(formatter.format(vehicles));
// 予想される結果: "Motorcycle, Bus, and Car"
const formatter2 = new Intl.ListFormat("de", {
style: "short",
type: "disjunction",
});
console.log(formatter2.format(vehicles));
// 予想される結果: "Motorcycle, Bus oder Car"
const formatter3 = new Intl.ListFormat("en", { style: "narrow", type: "unit" });
console.log(formatter3.format(vehicles));
// 予想される結果: "Motorcycle Bus Car"
コンストラクター
Intl.ListFormat()-
新しい
Intl.ListFormatオブジェクトを作成します。
静的メソッド
Intl.ListFormat.supportedLocalesOf()-
指定されたロケールのうち、実行環境のデフォルトのロケールで代替されることなく対応するものを、配列に収めて返します。
インスタンスプロパティ
これらのプロパティは Intl.ListFormat.prototype で定義されており、すべての Intl.ListFormat インスタンスで共有されます。
Intl.ListFormat.prototype.constructor-
このインスタンスオブジェクトを作成したコンストラクター関数です。
Intl.ListFormatインスタンスの場合、初期値はIntl.ListFormatコンストラクターとなります。 Intl.ListFormat.prototype[Symbol.toStringTag]-
[Symbol.toStringTag]プロパティの初期値は、文字列"Intl.ListFormat"です。このプロパティはObject.prototype.toString()で使用されます。
インスタンスメソッド
Intl.ListFormat.prototype.format()-
リストの要素を表す、言語を考慮して書式化された文字列を返します。
Intl.ListFormat.prototype.formatToParts()-
ロケールを考慮した方法で値のリストを書式化するために使用できる、さまざまな部分を表すオブジェクトの配列を返します。
Intl.ListFormat.prototype.resolvedOptions()-
現在の
Intl.ListFormatオブジェクトの構築時に計算されたロケールおよびスタイルの書式化オプションを反映したプロパティを持つ、新しいオブジェクトを返します。
例
>format の使用
次の例は、英語を使用したリストフォーマッターの作成方法です。
js
const list = ["Motorcycle", "Bus", "Car"];
console.log(
new Intl.ListFormat("en-GB", { style: "long", type: "conjunction" }).format(
list,
),
);
// Motorcycle, Bus and Car
console.log(
new Intl.ListFormat("en-GB", { style: "short", type: "disjunction" }).format(
list,
),
);
// Motorcycle, Bus or Car
console.log(
new Intl.ListFormat("en-GB", { style: "narrow", type: "unit" }).format(list),
);
// Motorcycle Bus Car
formatToParts の使用
次の例では、整形済みの部分を返すリストフォーマッターを生成する方法を示します。
js
const list = ["Motorcycle", "Bus", "Car"];
console.log(
new Intl.ListFormat("en-GB", {
style: "long",
type: "conjunction",
}).formatToParts(list),
);
// [ { "type": "element", "value": "Motorcycle" },
// { "type": "literal", "value": ", " },
// { "type": "element", "value": "Bus" },
// { "type": "literal", "value": ", and " },
// { "type": "element", "value": "Car" } ];
仕様書
| 仕様書 |
|---|
| ECMAScript® 2027 Internationalization API Specification> # listformat-objects> |