Sub topic 6
Variables in messages instead of string concatenation
This chapter assumes you have set up react-i18next as in the earlier chapters. You should be able to call useTranslation and have a catalog with at least English loaded.
Why concatenation breaks
A common first attempt at translating a greeting looks like this:
import { useTranslation } from 'react-i18next';
export function BadGreeting({ name }: { name: string }) {
const { t } = useTranslation();
return <h1>{t('hello')} {name}!</h1>;
}This hard-codes the word order. English puts the name last. Other languages may put a name or number in the middle, or need a different punctuation mark. Translators see only the fragment Hello and cannot move the name. Splitting a sentence across several keys also removes the context a translator needs.
The fix is to keep the whole sentence in one message and mark the changing parts with placeholders.
Placeholders in the catalog
i18next marks a variable with double curly braces. The catalog holds the full sentence:
const resources = {
en: {
translation: {
greeting: 'Hello, {{name}}!',
shipped: 'Order {{id}} shipped to {{city}}.',
},
},
de: {
translation: {
greeting: 'Hallo, {{name}}!',
shipped: 'Bestellung {{id}} wurde nach {{city}} versandt.',
},
},
};The two shipped messages place the variables at different positions. The component code does not need to know that.
Passing values with t
The second argument of t is an options object. Each property name must match a placeholder name.
import { useTranslation } from 'react-i18next';
export function OrderStatus({ id, city }: { id: string; city: string }) {
const { t } = useTranslation();
return <p>{t('shipped', { id, city })}</p>;
}The component passes data and the catalog decides the sentence shape.
Escaping and missing values
i18next escapes interpolated values by default. In React this would escape twice, so a name like Tom & Jerry would show as Tom & Jerry. That is why the setup chapter sets interpolation: { escapeValue: false }. React already escapes anything it renders as text.
If you forget a value, i18next does not throw. It leaves the placeholder text visible, so the user sees Hello, {{name}}!. Check for this in review and tests.
Common errors
| Symptom | Likely cause |
|---|---|
| `{{name}}` shows on screen | The option key does not match the placeholder name, or the value was left out |
| `&` or `'` in text | `escapeValue` is still true while React renders the result |
| Translator asks for context | The sentence was split across several keys |
| Numbers look wrong in some locales | The value was pre-formatted in code instead of using `Intl` |
Variables carry data, not wording. Do not pass in a translated word such as a month name unless it also comes from the catalog or from Intl. Numbers and dates are covered in the formatting chapter.
Exercise
A teammate wrote {t('welcome')} {user.name}, {t('youHave')} {count} {t('messages')}. Give two problems with this and show the better catalog entry and call.
Show answer
It fixes the word order, so languages that need a different order cannot be translated correctly, and it splits one sentence into three keys, which hides the context from translators. It also ignores plural rules for count. Better: the catalog entry "inbox": "Welcome {{name}}, you have {{count}} new messages." and the call t('inbox', { name: user.name, count }). The plural variants are handled in the next chapter.