Sub topic 5
Translating text in components
This chapter assumes you have react-i18next set up as in the previous chapter. That means an i18n.ts file that calls init with catalogs and a fallback language. It also assumes you know React function components and hooks.
From literal text to keys
A hard-coded string in JSX ties the component to one language. With i18next, the component holds a key and the catalog holds the text for each language. Here is a small catalog for English:
{
"nav": {
"home": "Home",
"settings": "Settings"
},
"settings": {
"title": "Account settings",
"save": "Save changes"
}
}Nested objects become dotted keys, so nav.home finds "Home". The German catalog needs the same key paths with German values.
The useTranslation hook
useTranslation returns t, the lookup function. It also subscribes the component to language changes, so calling i18n.changeLanguage re-renders every component that uses the hook.
import { useTranslation } from 'react-i18next';
import './i18n';
export function SettingsPage() {
const { t } = useTranslation();
return (
<main>
<nav aria-label={t('nav.home')}>
<a href="/">{t('nav.home')}</a>
<a href="/settings">{t('nav.settings')}</a>
</nav>
<h1>{t('settings.title')}</h1>
<button type="submit">{t('settings.save')}</button>
</main>
);
}Translate every user-visible string, including attributes such as aria-label, placeholder and title. Screen reader users hear those too.
What t does when a key is missing
t never throws. It looks in the active language first, then in each fallback language, and finally returns the key itself.
If you see settings.save on screen, the key is missing in every language you configured. If you see English text in a German session, the German entry is missing and the fallback was used.
Scoping keys with keyPrefix
When a component uses many keys from one section, set a prefix once:
import { useTranslation } from 'react-i18next';
export function SettingsHeader() {
const { t } = useTranslation('translation', { keyPrefix: 'settings' });
return <h1>{t('title')}</h1>;
}The first argument is the namespace. translation is the default one, which is where the earlier catalogs live.
Common errors
- Calling
tat module level. A constant such asconst LABEL = i18n.t('nav.home')runs once at import time and never updates. Calltinside the component, or store keys in the constant and translate when rendering. - Building keys dynamically.
t(status.${value})works, but extraction tools cannot see the full key. If you must do this, list the possible keys in a typed object. - Forgetting to load the setup file. If no module imports
i18n.ts,useTranslationwarns that no instance exists and returns the keys. - Joining translated fragments.
t('hello') + ' ' + namebreaks in languages with a different word order. The next chapter covers variables in messages.
Exercise
A German user sees the text settings.save on a button, while English users see "Save changes". Which of these is the likely cause: the key is missing from the German catalog only, or the key is missing from every catalog? Explain.
Show answer
The key is missing from every catalog, including the fallback. If only the German catalog lacked it, t would fall back to the English entry and show "Save changes". Returning the raw key means the lookup failed in the German catalog and in the fallback language. English users see the right text, so check that the German session's fallbackLng is set and that the key path matches exactly, including the namespace.