JavaScript's Intl (Internationalization) object provides a way to localize text and numbers in web applications. It provides several methods to format numbers, dates, and currencies according to the conventions of a specific locale.
The most commonly used methods are:
- Intl.NumberFormat: This method formats a number according to the conventions of a specific locale. For example, the following code formats the number 12345.67 as a currency in the en-US locale:
- Intl.DateTimeFormat: This method formats a date and time according to the conventions of a specific locale. For example, the following code formats the date September 20, 2022, as a full date in the fr-FR locale:
- Intl.Collator: This method provides language-sensitive string comparison. For example, the following code sorts an array of words in the es-ES locale:
- Intl.PluralRules: This method provides information about plural forms of a language. For example, the following code gives the plural form of a number in the en-US locale:
- Intl.RelativeTimeFormat: This method formats a relative time according to the conventions of a specific locale. For example, the following code formats the time -1 day as a relative time in the en-US locale:
- Intl.ListFormat: This method formats a list of items according to the conventions of a specific locale. The items can be any type, including numbers, strings, and objects. For example, the following code formats a list of fruits in the en-US locale: