Internationalization (i18n) — adapting the application for different languages and regions.
Why it matters: If your application targets users in multiple countries, i18n is essential. It handles not just translations but also date formats, number formatting, pluralization rules, right-to-left (RTL) layouts, and currency display. Without proper i18n, your app may display awkward formatting like "1 items" or dates in the wrong order.
react-intl (FormatJS) — the most popular library:
1import { IntlProvider, FormattedMessage, FormattedNumber } from "react-intl";23// 1. Messages by language4const messages = {5 en: {6 greeting: "Hello, {name}!",7 items: "{count, plural, one {# item} other {# items}}",8 price: "{value, number, ::currency/USD}",9 },10 ru: {11 greeting: "Привет, {name}!",12 items: "{count, plural, one {# товар} few {# товара} many {# товаров} other {# товаров}}",13 price: "{value, number, ::currency/USD}",14 },15};1617// 2. Component with localization18function Welcome({ name, itemCount, price }) {19 return (20 <div>21 <h1><FormattedMessage id="greeting" values={{ name }} /></h1>22 <p><FormattedMessage id="items" values={{ count: itemCount }} /></p>23 <p><FormattedNumber value={price} style="currency" currency="USD" /></p>24 </div>25 );26}2728// 3. Provider29function App() {30 const [locale, setLocale] = useState("en");3132 return (33 <IntlProvider locale={locale} messages={messages[locale]}>34 <Welcome name="Alice" itemCount={5} price={99.99} />35 <button onClick={() => setLocale(locale === "en" ? "ru" : "en")}>36 Switch language37 </button>38 </IntlProvider>39 );40}
Key concepts to understand:
navigator.language, user settings, or URL parameter.dir="rtl" on the <html> element for languages like Arabic or Hebrew.How locale switching works:
<FormattedMessage> components automatically re-render with the new text.<FormattedNumber>, <FormattedDate> adapt to the new locale formatting rules.Alternatives: