Sub topic 2
Translating Text in React Components
Assume you know React function components, hooks, TypeScript or JavaScript modules, and basic async/await. This chapter uses react-i18next and the browser’s Intl APIs.
Translating Text in Components
A translation catalog maps stable keys to messages in each locale. Components request messages by key instead of storing English text directly.
import { useTranslation } from 'react-i18next';
export function Welcome({ name }: { name: string }) {
const { t } = useTranslation();
return <h1>{t('greeting', { name })}</h1>;
}With these catalog entries:
{
"greeting": "Hello, {{name}}!"
}{
"greeting": "Hallo, {{name}}!"
}The same component renders the active language.
Variables and Plural Forms
Use interpolation for values inside a message. Do not concatenate translated fragments:
<p>{t('items', { count })}</p>Concatenation assumes every language uses the same word order. Interpolation lets translators move the variable where it belongs.
Plural rules also differ between languages. Give i18next a count and define plural variants:
{
"items_one": "{{count}} item in your cart",
"items_other": "{{count}} items in your cart"
}export function Cart({ count }: { count: number }) {
const { t } = useTranslation();
return <p>{t('items', { count })}</p>;
}i18next selects the appropriate key using the active locale’s plural rules. Avoid manually checking count === 1; some languages have more than two plural categories.
Locale-Aware Formatting
Use Intl for values whose presentation depends on the locale:
const locale = 'de-DE';
const price = new Intl.NumberFormat(locale, {
style: 'currency',
currency: 'EUR',
}).format(1234.5);
const date = new Intl.DateTimeFormat(locale, {
dateStyle: 'long',
}).format(new Date(2026, 11, 2));
const products = new Intl.ListFormat(locale, {
type: 'conjunction',
}).format(['Äpfel', 'Birnen', 'Kirschen']);Intl handles separators, currency placement, date order, and list conjunctions. Pass the active locale instead of hard-coding a format.
Exercise
Why should a component pass count to the translation function instead of choosing _one or _other itself?
Show answer
The translation library applies the active language's plural rules, which may contain categories beyond singular and plural.