Localize UI messages and pluralization
Separate language, locale and stored data
Internationalization (i18n) means designing an application so it can support different locales. Localization (l10n) adapts its messages, formatting and conventions for a specific audience. A locale such as en-GB expresses a language and region preference, not a currency, citizenship or location. Our fictional museum ticket desk supports en-US and en-GB with English messages only. The numeric ticket count, price in USD cents and booking timestamp remain unchanged when the locale changes. Keep the locale explicit in the application state; validate it against the supported set before passing it to formatters. A server-rendered app must agree with the client on initial locale, timezone and message resources.
const catalogs = { 'en-US': {title: 'Visitor center', tickets: {one: '{n} ticket', other: '{n} tickets'}}, 'en-GB': {title: 'Visitor centre', tickets: {one: '{n} ticket', other: '{n} tickets'}}};export function selectLocale(requested) { return Object.hasOwn(catalogs, requested) ? requested : 'en-US';}export function titleFor(requested) { return catalogs[selectLocale(requested)].title;}export function ticketMessage(requested, count) { if (!Number.isSafeInteger(count) || count < 0) throw new RangeError('Invalid count'); const locale = selectLocale(requested); const category = new Intl.PluralRules(locale).select(count); const forms = catalogs[locale].tickets; return (forms[category] ?? forms.other).replace('{n}', new Intl.NumberFormat(locale).format(count));}The title changes from center to centre; 1 produces “1 ticket” and 0 produces “0 tickets”. A count of 2000 is formatted before substitution. The message key is stable, while the entire sentence can be edited per locale. This small catalog implements an explicit fallback for unsupported locale tags, not a general BCP 47 negotiation algorithm. It supports nonnegative integer ticket counts only. Languages can have more than two cardinal categories; do not extend this English example by simply appending an s. In a real catalog validate matching keys and required plural categories for every supported language. Do not construct grammatical sentences by concatenating separately translated fragments.
Format amounts and times at the display boundary
import {selectLocale} from './messages.mjs';export function priceText(locale, cents, currency = 'USD') { if (!Number.isSafeInteger(cents) || cents < 0 || cents > 100000000) throw new RangeError('Invalid price'); if (!['USD', 'GBP'].includes(currency)) throw new RangeError('Unsupported currency'); return new Intl.NumberFormat(selectLocale(locale), {style:'currency', currency}).format(cents / 100);}export function bookingDate(locale, iso) { const date = new Date(iso); if (!Number.isFinite(date.getTime())) throw new RangeError('Invalid date'); return new Intl.DateTimeFormat(selectLocale(locale), { year:'numeric', month:'short', day:'numeric', timeZone:'UTC' }).format(date);}The same USD 1250 cents represents 12.50 dollars in both locales. A British locale can display a different currency symbol or ordering without converting the amount. This model assumes USD or GBP (two minor-unit digits), a bounded price and a valid ISO timestamp with an explicit offset supplied by the data layer. It is not a general money or date parser. An instant near midnight can display a different day in another timezone; UTC here makes the fixture predictable. Exact punctuation and spacing can vary across ICU/CLDR versions. Test semantic values and required formatting options rather than freezing every formatted string across engines.
Make locale selection a React input
import React, {useState} from 'react';import {titleFor, ticketMessage} from './messages.mjs';import {priceText, bookingDate} from './formats.mjs';export default function TicketDesk() { const [locale, setLocale] = useState('en-US'); return <section lang={locale} dir="ltr"> <h2>{titleFor(locale)}</h2> <label>English region <select value={locale} onChange={e => setLocale(e.target.value)}> <option value="en-US">United States</option><option value="en-GB">United Kingdom</option> </select></label> <p>{ticketMessage(locale, 2)}</p> <p>{priceText(locale, 1250, 'USD')}</p> <p>{bookingDate(locale, '2026-03-01T00:15:00Z')}</p> </section>;}Changing the select triggers a render using the same data and a new locale. No Effect is needed to synchronize derived strings. This section sets local language metadata; a full application should also supply matching document-level metadata. react-i18next provides useTranslation and a configurable message/fallback system; FormatJS React Intl provides IntlProvider, FormattedMessage and useIntl with ICU-style messages. Framework integrations can select locale from routes and deliver resources on the server. These are comparison candidates, not installed dependencies. Verify package versions, SSR behavior, missing-key policy and extraction workflows before adoption. Keep preference storage minimal and do not infer identity from locale. Practice: add an English message for a closed desk and define its missing-key behavior.