
JavaScript Intl Namespace
๐ Make Your App International with Intl
You're building an app that needs to support customers from around the world.
Dates need to look right in Tokyo, London, and New York.
Currencies need the correct symbols and formats.
Lists need proper separators based on language.
JavaScript comes with a built-in Intl namespace that helps you achieve this.
โ Intl.DateTimeFormat โ Intl.NumberFormat โ Intl.ListFormat โ Intl.RelativeTimeFormat โ Intl.Collator โ Intl.PluralRules โ Intl.Segmenter
#javascript #intl #localization #i18n #formatting #dates #numbers #webdev #frontend #coding #tips
The Intl namespace provides a set of constructors for language-sensitive formatting and comparison.
| Constructor | Purpose |
|---|---|
Intl.DateTimeFormat | Format dates and times for a locale. |
Intl.NumberFormat | Format numbers, currencies, and units. |
Intl.ListFormat | Format arrays into localized lists. |
Intl.RelativeTimeFormat | Format relative time expressions. |
Intl.Collator | Compare strings for sorting. |
Intl.PluralRules | Determine plural forms for a locale. |
Intl.Segmenter | Segment text by grapheme, word, or sentence. |
Intl.DateTimeFormat
Format dates and times for a specific locale.
const date = new Date("2024-07-12T10:30:00");
new Intl.DateTimeFormat("en-US").format(date); // "7/12/2024"
new Intl.DateTimeFormat("en-GB").format(date); // "12/07/2024"
new Intl.DateTimeFormat("ja-JP").format(date); // "2024/07/12"With formatting options.
const options = {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
hour: "2-digit",
minute: "2-digit",
};
new Intl.DateTimeFormat("en-US", options).format(date);
// "Friday, July 12, 2024 at 10:30 AM"Intl.NumberFormat
Format numbers, currencies, percentages, and units.
const number = 1234567.89;
new Intl.NumberFormat("en-US").format(number); // "1,234,567.89"
new Intl.NumberFormat("de-DE").format(number); // "1.234.567,89"
new Intl.NumberFormat("ja-JP").format(number); // "1,234,567.89"Currency
new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
}).format(1234.5); // "$1,234.50"
new Intl.NumberFormat("ja-JP", {
style: "currency",
currency: "JPY",
}).format(1234); // "๏ฟฅ1,234"Units
new Intl.NumberFormat("en-US", {
style: "unit",
unit: "kilometer-per-hour",
}).format(90); // "90 km/h"
new Intl.NumberFormat("en-US", {
style: "unit",
unit: "celsius",
}).format(25); // "25ยฐC"Percentages
new Intl.NumberFormat("en-US", {
style: "percent",
}).format(0.85); // "85%"Intl.ListFormat
Format arrays into localized lists.
const items = ["HTML", "CSS", "JavaScript"];
new Intl.ListFormat("en-US", { type: "conjunction" }).format(items);
// "HTML, CSS, and JavaScript"
new Intl.ListFormat("en-US", { type: "disjunction" }).format(items);
// "HTML, CSS, or JavaScript"
new Intl.ListFormat("en-US", { type: "unit" }).format(items);
// "HTML, CSS, JavaScript"Intl.RelativeTimeFormat
Format relative time expressions like "2 days ago" or "in 3 hours".
const rtf = new Intl.RelativeTimeFormat("en-US", { numeric: "auto" });
rtf.format(-1, "day"); // "yesterday"
rtf.format(0, "day"); // "today"
rtf.format(1, "day"); // "tomorrow"
rtf.format(-3, "month"); // "3 months ago"
rtf.format(5, "minute"); // "in 5 minutes"Intl.Collator
Compare strings for sorting in a locale-aware way.
const words = ["Z", "a", "z", "รค"];
words.sort(new Intl.Collator("de").compare);
// ["a", "รค", "z", "Z"]
words.sort(new Intl.Collator("sv").compare);
// ["a", "z", "Z", "รค"]Case-insensitive comparison.
new Intl.Collator("en", { sensitivity: "base" }).compare("resume", "Resume"); // 0Intl.PluralRules
Determine the plural category for a number.
const pr = new Intl.PluralRules("en-US");
pr.select(0); // "other"
pr.select(1); // "one"
pr.select(2); // "other"Useful for choosing the right message string.
function pluralize(count, singular, plural) {
const rules = new Intl.PluralRules("en-US");
return rules.select(count) === "one" ? singular : plural;
}
pluralize(1, "apple", "apples"); // "apple"
pluralize(3, "apple", "apples"); // "apples"Intl.Segmenter
Segment text by grapheme, word, or sentence.
const segmenter = new Intl.Segmenter("en", { granularity: "word" });
const segments = Array.from(segmenter.segment("Hello world"));
segments.map((s) => s.segment); // ["Hello", " ", "world"]Grapheme segmentation.
const graphemes = new Intl.Segmenter("en", { granularity: "grapheme" });
Array.from(graphemes.segment("๐จโ๐ฉโ๐งโ๐ฆ")).length; // 1Full-Stack AI Developer Roadmap
From HTML & CSS to working with AI models, all in one structured roadmap.