Sub topic 11
Embedding links and emphasis in translated messages
This chapter assumes you can write React function components and hooks. It also assumes you have react-i18next set up with a catalog, as in the earlier chapters, and that you know how t handles variables.
The problem with splitting sentences
Suppose a sentence contains a link: "I accept the terms of service, Ana." A common first attempt splits it into several keys and joins them in JSX.
// Avoid: word order is fixed by the code, not the translator
<p>
{t('accept_prefix')} <a href="/terms">{t('accept_link')}</a>, <strong>{name}</strong>.
</p>This breaks when a language needs a different word order. A translator also sees three unrelated fragments and cannot judge the grammar. The fix is to keep one full sentence in one key and mark the parts that need elements.
Named tags in the catalog
The Trans component reads a message that contains tags, then replaces each tag with a React element you supply. The catalog entry holds the whole sentence:
{
"terms": "I accept the <link>terms of service</link>, <bold>{{name}}</bold>."
}A German translator can move the tags freely:
{
"terms": "Ich akzeptiere die <link>Nutzungsbedingungen</link>, <bold>{{name}}</bold>."
}Using the Trans component
The components prop maps each tag name to an element. Children of the tag in the message become children of that element.
import { Trans } from 'react-i18next';
// catalog entry: "terms": "I accept the <link>terms of service</link>, <bold>{{name}}</bold>."
export function Terms({ name }: { name: string }) {
return (
<Trans
i18nKey="terms"
values={{ name }}
components={{ link: <a href="/terms" />, bold: <strong /> }}
/>
);
}Three props do the work:
i18nKeyis the catalog key, the same as the first argument tot.valuessupplies the{{name}}variables.componentsmaps<link>and<bold>to elements. The element you pass is a template. Its attributes, such ashref, are kept, and the translated text becomes its content.
The href stays in code, so translators cannot break the URL. If a link target differs per language, pass it as a variable or use a separate key.
Common errors
- Tag name mismatch. If the message says
<link>butcomponentshasLink, the tag is not replaced and may show as plain text. Names are case-sensitive. - Missing closing tag. Every tag in the message must be closed. A translator who deletes
</bold>breaks the sentence. - Using
Transfor plain text. If a message has no markup, uset.Transadds overhead and no benefit. - Hard-coded text inside the element. Do not write
<a href="/terms">terms</a>incomponents. The text comes from the catalog and replaces whatever you put there.
Exercise
Exercise
A catalog entry reads "Read our <policy>privacy policy</policy> before you continue." Write the Trans element that turns "policy" into a link to /privacy.
Show answer
<Trans i18nKey="privacyNotice" components={{ policy: <a href="/privacy" /> }} />. The key name is your choice. The tag name in components must match the tag in the message exactly, and the link text comes from the catalog.
Exercise
Why is joining three separate keys around a link in JSX a problem for translators?
Show answer
Each fragment has a fixed position in the code, so a language with a different word order cannot rearrange them. Translators also lose the full sentence, which makes correct grammar harder to judge. One key with tags solves both problems.