0 Tage Serie
javascript / intermediate
Snippet

Internationalisierte Listenformatierung

Das Intl.ListFormat-Objekt ermöglicht eine sprachenabhängige Listenformatierung und verarbeitet automatisch Trennzeichen und Konjunktionen wie 'und' oder 'oder' basierend auf dem Gebietsschema.

snippet.js
javascript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const items = ['HTML', 'CSS', 'JavaScript'];
const enFormatter = new Intl.ListFormat('en', {
style: 'long',
type: 'conjunction'
});
const deFormatter = new Intl.ListFormat('de', {
style: 'short',
type: 'disjunction'
});
console.log(enFormatter.format(items)); // "HTML, CSS, and JavaScript"
console.log(deFormatter.format(items)); // "HTML, CSS oder JavaScript"
Erklärung
1
new Intl.ListFormat('en', ...)
Initialisiert einen Formatierer für die englische Sprache mit spezifischem Styling.
2
type: 'disjunction'
Gibt an, dass die Listenelemente Alternativen sind (verwendet 'oder' statt 'und').