Dieser Inhalt wurde automatisch aus dem Englischen übersetzt, und kann Fehler enthalten. Erfahre mehr über dieses Experiment.

View in English Always switch to English

Intl.ListFormat

Baseline
Weitgehend verfügbar

Diese Funktion ist gut etabliert und funktioniert auf vielen Geräten und in vielen Browserversionen. Sie ist seit April 2021 browserübergreifend verfügbar.

Das Objekt Intl.ListFormat ermöglicht die sprachabhängige Formatierung von Listen.

Probieren Sie es aus

const vehicles = ["Motorcycle", "Bus", "Car"];

const formatter = new Intl.ListFormat("en", {
  style: "long",
  type: "conjunction",
});
console.log(formatter.format(vehicles));
// Expected output: "Motorcycle, Bus, and Car"

const formatter2 = new Intl.ListFormat("de", {
  style: "short",
  type: "disjunction",
});
console.log(formatter2.format(vehicles));
// Expected output: "Motorcycle, Bus oder Car"

const formatter3 = new Intl.ListFormat("en", { style: "narrow", type: "unit" });
console.log(formatter3.format(vehicles));
// Expected output: "Motorcycle Bus Car"

Konstruktor

Intl.ListFormat()

Erstellt ein neues Intl.ListFormat-Objekt.

Statische Methoden

Intl.ListFormat.supportedLocalesOf()

Gibt ein Array mit den übergebenen Locales zurück, die unterstützt werden, ohne dass auf die Standard-Locale der Laufzeitumgebung zurückgegriffen werden muss.

Instanzeigenschaften

Diese Eigenschaften sind auf Intl.ListFormat.prototype definiert und werden von allen Intl.ListFormat-Instanzen gemeinsam genutzt.

Intl.ListFormat.prototype.constructor

Die Konstruktorfunktion, die das Instanzobjekt erstellt hat. Für Intl.ListFormat-Instanzen ist der Anfangswert der Konstruktor Intl.ListFormat.

Intl.ListFormat.prototype[Symbol.toStringTag]

Der Anfangswert der Eigenschaft [Symbol.toStringTag] ist die Zeichenfolge "Intl.ListFormat". Diese Eigenschaft wird in Object.prototype.toString() verwendet.

Instanzmethoden

Intl.ListFormat.prototype.format()

Gibt eine sprachabhängig formatierte Zeichenfolge zurück, die die Elemente der Liste darstellt.

Intl.ListFormat.prototype.formatToParts()

Gibt ein Array von Objekten zurück, die die verschiedenen Komponenten darstellen, mit denen sich eine Liste von Werten unter Berücksichtigung der Locale formatieren lässt.

Intl.ListFormat.prototype.resolvedOptions()

Gibt ein neues Objekt mit Eigenschaften zurück, die die bei der Erstellung des aktuellen Intl.ListFormat-Objekts ermittelten Optionen für Locale und Formatierungsstil widerspiegeln.

Beispiele

Verwendung von format

Das folgende Beispiel zeigt, wie Sie einen Listenformatierer für die englische Sprache erstellen.

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

Verwendung von formatToParts

Das folgende Beispiel zeigt, wie Sie einen Listenformatierer erstellen, der formatierte Bestandteile zurückgibt.

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" } ];

Spezifikationen

Spezifikation
ECMAScript® 2027 Internationalization API Specification
# listformat-objects

Browser-Kompatibilität

Siehe auch