Sub topic 2
Locales and language tags
This chapter assumes you can write React function components and hooks, and that you know basic TypeScript and ES modules. It builds on the previous chapter's split between internationalisation and localisation. Here you learn how to name the language and region a user wants, because every later step depends on that name.
What a locale is
A locale is a language plus the regional conventions that go with it: number separators, date order, currency, sorting rules. English in the United States and English in the United Kingdom share a language but differ in date order and spelling. German in Germany and German in Switzerland differ in the thousands separator.
Browsers and libraries identify locales with BCP 47 language tags. A tag is a series of subtags joined by hyphens.
| Subtag | Example | Rule |
|---|---|---|
| Language | `de` | Required. Lowercase, usually two letters. |
| Script | `Hant` | Optional. Four letters, first one capitalised. |
| Region | `TW` | Optional. Two uppercase letters or three digits. |
So en, en-GB, sr-Latn and zh-Hant-TW are all valid. Include a script only when one language is written in several, as with Chinese (Hans and Hant) or Serbian.
Inspecting tags with Intl
Intl.Locale parses a tag and Intl.getCanonicalLocales normalises its casing. Both reject malformed input with a RangeError.
const tag = new Intl.Locale('zh-hant-tw');
console.log(tag.language); // 'zh'
console.log(tag.script); // 'Hant'
console.log(tag.region); // 'TW'
console.log(Intl.getCanonicalLocales('EN-us')); // [ 'en-US' ]
try {
new Intl.Locale('en_US');
} catch (error) {
console.log(error instanceof RangeError); // true
}
const amount = 1234567.89;
console.log(new Intl.NumberFormat('en-US').format(amount)); // 1,234,567.89
console.log(new Intl.NumberFormat('de-DE').format(amount)); // 1.234.567,89
console.log(new Intl.NumberFormat('en-IN').format(amount)); // 12,34,567.89The last three lines show why the region matters. The language alone does not decide the output.
Common mistakes
- Underscores.
en_USis a POSIX locale name, not a BCP 47 tag. Useen-US. - Wrong language codes. Japanese is
ja, notjp.jpis a country code. - Treating language and country as the same thing. A user in Switzerland may want French, German or Italian.
- Case-sensitive comparisons. Tags are case-insensitive, so normalise before comparing.
Choosing a fallback
You rarely ship a catalog for every region. A user who requests de-AT should get de if you have it, and your default language if you do not. This is a chain of progressively less specific tags.
The following function applies this rule to the browser's preferred languages.
const SUPPORTED = ['en', 'de'];
export function pickLocale(requested: readonly string[], fallback = 'en'): string {
for (const tag of requested) {
try {
const { language } = new Intl.Locale(tag);
if (SUPPORTED.includes(language)) return language;
} catch {
// Ignore malformed tags and try the next preference.
}
}
return fallback;
}
console.log(pickLocale(['de-AT', 'en-GB'])); // 'de'
console.log(pickLocale(['fr-CA'])); // 'en'navigator.languages is ordered by user preference, so the first match wins. Later chapters pass this result to i18n.changeLanguage. Keep the full requested tag (de-AT) for Intl formatting even when you load the shorter catalog (de). The user then sees Austrian number formats with German text.
Exercise
A user's browser reports ['pt-BR', 'en-US'], and your app ships only en and pt-PT catalogs. With the pickLocale approach above (SUPPORTED set to ['en', 'pt']), what is returned, and why might you prefer to keep pt-BR for number and date formatting?
Show answer
It returns 'pt', because the language subtag of pt-BR matches a supported language. Keeping pt-BR for Intl formatting preserves Brazilian conventions, such as currency and date style, even though the text comes from the shared Portuguese catalog.