Exploring JavaScript's Internationalization Objects.
Choose a locale to see how the following examples change.
Formats numbers according to the conventions of a specified locale.
const number = 123456.789; new Intl.NumberFormat('de-DE').format(number); // '123.456,789'
Input Number: 123456.789
Formats dates and times according to the locale's rules.
const date = new Date(); new Intl.DateTimeFormat('en-US').format(date);
Input Date:
Formats a list of strings into a human-readable format, respecting locale-specific conjunctions and punctuation.
const list = ['apples', 'oranges', 'bananas']; new Intl.ListFormat('en').format(list); // 'apples, oranges, and bananas'
Input List: ["apples", "oranges", "bananas"]
Formats a relative time statement (e.g., "in 3 days", "1 month ago").
const rtf = new Intl.RelativeTimeFormat('en', { style: 'long' }); rtf.format(3, 'days'); // 'in 3 days'
Input: 2 days ago
Determines the plural category for a number in a given locale (e.g., one, few, many, other).
const pr = new Intl.PluralRules('en-US'); pr.select(1); // 'one' pr.select(2); // 'other'
Plural Categories for numbers 0-5 and 100:
Number | Category |
---|
Translates language, region, script, or currency display names into a specific language.
const dn = new Intl.DisplayNames(['en'], { type: 'language' }); dn.of('fr'); // 'French'
Input: Country code `FR` (France) and language code `ja` (Japanese)
Breaks a string into meaningful segments like words, grapheme clusters (characters), or sentences.
const segmenter = new Intl.Segmenter('ja-JP', { granularity: 'word' }); const segments = segmenter.segment('こんにちは世界');
Input Text: "The quick brown fox."