JavaScript Internationalization: A Guide
Learn how to work with internationalization in JavaScript with the help of the powerful Intl object. The JavaScript Internationalization API provides access to several properties that enable language-sensitive string comparison, date and time formatting, number formatting, plural formatting, and relative time formatting. In addition, the API offers the Intl.getCanonicalLocales() method to check if a locale is valid and return the correct formatting for it.
Intl.Collator
The Intl.Collator property allows for language-sensitive string comparison. To initialize a Collator object, use new Intl.Collator() with the desired locale. The object’s compare() method returns a positive value if the first argument comes after the second, a negative value if it’s the reverse, and zero if they are the same:
1 | const collator = new Intl.Collator('it-IT'); |
This property is useful for sorting arrays of characters.
Intl.DateTimeFormat
The Intl.DateTimeFormat property allows for language-sensitive date and time formatting. To initialize a DateTimeFormat object, use new Intl.DateTimeFormat() with the desired locale. Pass a date to the object’s format() method to format it according to the locale’s preferences:
1 | const date = new Date(); |
The formatToParts() method returns an array with all the date parts:
1 | const date = new Date(); |
You can also format the time. For more options, refer to the MDN reference.
Intl.NumberFormat
The Intl.NumberFormat property allows for language-sensitive number formatting, including currency formatting. Suppose you have a number like 10 representing the price of something. You want to format it as $10.00. If the number has more than 3 digits, it should be displayed differently, such as $1,000.00, following the conventions of different countries.
To achieve this, use new Intl.NumberFormat() and pass the desired locale along with additional options, such as style, currency, and minimum fraction digits:
1 | const formatter = new Intl.NumberFormat('en-US', { |
The minimumFractionDigits property ensures that the fraction part always has at least 2 digits. Check the NumberFormat MDN page for other available parameters.
Intl.PluralRules
The Intl.PluralRules property enables language-sensitive plural formatting and plural language rules. Consider the example of ordering numbers: 0th, 1st, 2nd, 3rd, 4th, 5th, and so on. The Intl.PluralRules property is perfect for handling such cases.
To use it, create a PluralRules object using new Intl.PluralRules() and specify the desired locale along with the type as ordinal. Then, use the object’s select() method to get the corresponding plural category for a given number:
1 | const pr = new Intl.PluralRules('en-US', { |
In this example, we translate other to th, one to st, two to nd, and few to rd. You can use an object as an associative array to create a mapping:
1 | const suffixes = { |
Create a formatting function that retrieves the corresponding value from the object and returns a string containing the original number and its suffix:
1 | const format = (number) => `${number}${suffixes[pr.select(number)]}`; |
Now, you can use it like this:
1 | format(0); // 0th |
Please note that while Chrome and Opera currently support Intl.RelativeTimeFormat and Intl.ListFormat, other browsers may not have implemented them yet.
Tags: javascript, internationalization, localization, date and time formatting, number formatting, string comparison, plural formatting, browser API