Internationalisierung
Daten internationalisieren
Wir können Datumsobjekte internationalisieren, indem wir die eingebaute Intl-API von JavaScript nutzen:
const now = new Date();
console.log(new Intl.DateTimeFormat('en-US').format(now));
console.log(new Intl.DateTimeFormat('en-GB').format(now));
console.log(new Intl.DateTimeFormat('de-DE').format(now));
console.log(new Intl.DateTimeFormat('ar-SY').format(now));Eine komplette Tabelle dieser Codes findest du hier.
Wir können auch weitere Optionen mitgeben, um z.B. auch die Zeit anzuzeigen:
const options = {
hour: 'numeric',
minute: 'numeric',
day: 'numeric',
month: 'numeric',
year: 'numeric'
};
console.log(new Intl.DateTimeFormat('en-US', options).format(now));
console.log(new Intl.DateTimeFormat('en-GB', options).format(now));
console.log(new Intl.DateTimeFormat('de-DE', options).format(now));
console.log(new Intl.DateTimeFormat('ar-SY', options).format(now); Sprache / Locale vom Browser
Oftmals macht es Sinn die Sprache, bzw. das Locale vom Browser zu bekommen. Deshalb können wir einfach auf ein Property zugreifen (navigator.language)
Zahlen internationalisieren
Um Zahlen zu internationalisieren nutzen wir die Intl-API von JavaScript. Bei dieser API gibt es die statische Method NumberFormat(), mit welcher wir arbeiten können:
const num = 3884764.23;
console.log(new Intl.NumberFormat('en-US').format(num));
console.log(new Intl.NumberFormat('de-DE').format(num));
console.log(new Intl.NumberFormat('de-CH').format(num));
console.log(new Intl.NumberFormat('ar-SY').format(num));Wie wir sehen, sind die Trennzeichen ganz anders, sowie die "Kommas" für die Dezimalstellen der Zahl.
Weiter können wir Optionen mitgeben, welche uns die Formatierung noch weiter vereinfacht:
const options = {
style: 'unit',
unit: 'mile-per-hour'
};
console.log(new Intl.NumberFormat('en-US', options).format(velocity));
console.log(new Intl.NumberFormat('de-DE', options).format(velocity));
console.log(new Intl.NumberFormat('ar-SY', options).format(velocity));
const currency = 12321.23;
options = {
style: 'currency',
currency: 'USD'
};
console.log(new Intl.NumberFormat('en-US', options).format(currency));
console.log(new Intl.NumberFormat('de-DE', options).format(currency));
console.log(new Intl.NumberFormat('ar-SY', options).format(currency));Last updated