Sub topic 4
Setting up react-i18next
This chapter assumes you know React function components and hooks, TypeScript, ES modules, and a bundler such as Vite. It also assumes you know what a locale and a message catalog are from earlier chapters. Here you install the library, create the i18n instance, and render your first translated component.
What the two packages do
i18next is the framework-independent engine. It stores catalogs, resolves keys, interpolates variables and picks plural forms. react-i18next is the React binding. It supplies the useTranslation hook, the Trans component and the initReactI18next plugin, which connects the engine to React so components re-render when the language changes.
Install both:
npm install i18next react-i18nextCreate the i18n instance
Put the setup in its own module, for example src/i18n.ts. The code below registers two in-memory catalogs, sets English as the starting and fallback language, and uses the _one and _other suffixes for plural keys.
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
const resources = {
en: {
translation: {
greeting: 'Hello, {{name}}!',
items_one: '{{count}} item in your cart',
items_other: '{{count}} items in your cart',
},
},
de: {
translation: {
greeting: 'Hallo, {{name}}!',
items_one: '{{count}} Artikel in Ihrem Warenkorb',
items_other: '{{count}} Artikel in Ihrem Warenkorb',
},
},
};
i18n.use(initReactI18next).init({
resources,
lng: 'en',
fallbackLng: 'en',
interpolation: { escapeValue: false },
});
export default i18n;The options that matter here:
| Option | Purpose |
|---|---|
| `resources` | Catalogs keyed by language code, then by namespace. `translation` is the default namespace. |
| `lng` | The language to use at startup. |
| `fallbackLng` | The language searched when a key is missing in the active one. |
| `interpolation.escapeValue` | Set to `false` because React already escapes rendered values. Leaving it `true` would escape them twice. |
Load the instance before rendering
The module only does its work when it is imported. Import it once, before the first component renders, in your entry file:
import React from 'react';
import { createRoot } from 'react-dom/client';
import './i18n';
import { Cart } from './Cart';
createRoot(document.getElementById('root')!).render(
<Cart name="Ana" count={3} />
);No provider component is required, because initReactI18next registers the instance globally.
Translate in a component
useTranslation returns t. It looks up a key in the active language, fills in variables, and picks a plural form from count.
import { useTranslation } from 'react-i18next';
export function Cart({ name, count }: { name: string; count: number }) {
const { t } = useTranslation();
return (
<section>
<h1>{t('greeting', { name })}</h1>
<p>{t('items', { count })}</p>
</section>
);
}Note that the code asks for items, but the catalog has items_one and items_other. i18next appends the plural suffix itself, using Intl.PluralRules for the active language. With count={3} in English this renders "3 items in your cart".
Common errors
- The page shows raw keys such as
greeting. The instance was never imported, the key is misspelled, or the namespace is wrong. Check the import in your entry file first. - A plural key is never used. The catalog has
itemsinstead ofitems_oneanditems_other, orcountwas not passed as an option. - Values appear double-escaped, such as
&.escapeValueis stilltrue. - A missing translation is not noticed. The fallback hides it. Turn on
debug: truein development to log missing keys.
Exercise
A component calls t('items', { count: 1 }) and renders the text "items". The catalog contains the keys itemsone and itemsother under en.translation. Name one likely cause and how to check it.
Show answer
Likely causes are that ./i18n was never imported before rendering, so the instance is not initialised, or that the active language or namespace has no such catalog. Check that the entry file imports './i18n' and that init was called with resources. Setting debug: true logs the missing-key lookup in the console.