IcuTrans Component
IcuTrans, the Trans variant for ICU message format (i18next-icu): nest React components in an ICU message, the props reference and Server Component usage.
Important note
IcuTrans is the Trans counterpart for the ICU message format. Use it when your i18next instance uses i18next-icu and a translation contains React elements (text formatting like strong, em, link components) that belong to one cohesive sentence.
Trans builds its default string from JSX children, and JSX reserves {curly brackets} for expressions, so ICU syntax like {count, plural, ...} cannot be written there. IcuTrans takes the message as a plain string instead (defaultTranslation) and a list of component declarations (content) that replace the numbered tags <0>, <1>, ... in it.
As long as you have no React/HTML nodes integrated into a cohesive sentence, you won't need it: most of the time you will be using the good old t function.
import React from 'react';
import { IcuTrans, useTranslation } from 'react-i18next';
function MyComponent() {
// this loads the namespace (and suspends until it is loaded)
const { t } = useTranslation('myNamespace');
return <IcuTrans i18nKey="helloWorld" defaultTranslation="Hello World" content={[]} t={t} />;
}Samples
Using with React components
This component enables you to nest any React content to be translated as one cohesive string. Plurals, selects and formatting go into the ICU message itself. IcuTrans uses the most relevant t() function (from the context instance or the global instance), unless overridden via the i18n or t props.
Let's say you want to create following HTML output:
Hello Arthur, you have 42 unread messages. Go to messages.
Before: Your untranslated React code would have looked something like:
After: With the IcuTrans component change it to:
Your en.json (translation strings) will look like:
Result with count = 42:
The index of a declaration in content is the number of its tag: content[0] replaces <0>...</0>, content[1] replaces <1>...</1>. The text between the tags becomes the children of that element. There is one key per message: the plural forms live inside the ICU message, not in _one/_other suffixed keys.
Nested elements
Declarations can have their own children declarations. Tags inside a tag are numbered again from 0 and refer to props.children of the outer declaration:
HTML entities in the translation (&, <, , ©, numeric entities, ...) are decoded to their characters. IcuTrans renders a fragment, without a wrapping element.
Usage in React Server Components
Since v18, import { IcuTrans } from 'react-i18next' works in a Server Component: the package's react-server export condition gives Server Components the variant without React context (IcuTransWithoutContext). Pass i18n explicitly when you have an instance per request:
Without i18n it uses the global instance set by initReactI18next, which is the wrong one with an instance per request: pass that instance as i18n (t is optional; a t from getFixedT carries no instance). In development and with debug: true it warns when i18n is missing.
Before v18 (since v17.0.21), import IcuTransWithoutContext from react-i18next/IcuTransWithoutContext instead: that entry point does not create a React context when it is imported, the same way react-i18next/TransWithoutContext works for Trans. Importing from the main react-i18next entry point in a Server Component can fail there with createContext is not a function. These entry points keep working in v18.
TypeScript
With parseInterpolation: 'icu' in your CustomTypeOptions (i18next v26.5.0 and react-i18next v17.1.0 or later), values is typed from the message of i18nKey, the same way the options of t are. A misspelled or missing argument, or a value of the wrong type, is a type error:
values itself stays optional, as on Trans. See Using with ICU format for the setup.
IcuTrans props
defaultTranslation and content are required properties, all others are optional. IcuTrans and IcuTransWithoutContext take the same props.
name
type (default)
description
i18nKey
string (undefined)
The key to look up. If omitted, defaultTranslation is used as the key (natural language keys). Can contain the namespace in the form 'ns:key' (depending on i18next.options.nsSeparator), but the dedicated ns prop is clearer. The TypeScript types currently require this prop.
defaultTranslation
string
The message in ICU format with numbered tags (<0>...</0>). Used when the key has no translation yet, and as the key when i18nKey is omitted.
content
array
Component declarations, one per numbered tag: { type, props }, where type is a DOM element name ('strong', 'a') or a component (Link), and the optional props are passed to it, for example { type: 'a', props: { href: '/somewhere' } }. For tags nested inside a tag, put their declarations in props.children, see Nested elements.
values
object ({})
Values for the arguments of the ICU message, including the number for plural and selectordinal, for example values={{ name, count }}. These override the same names in interpolation.defaultVariables. With parseInterpolation: 'icu', the TypeScript type is derived from the message, see TypeScript.
ns
string | string[] (undefined)
Namespace to use. Falls back to the namespace of t, then the defaultNS of the provider, then i18next.options.defaultNS.
t
function (undefined)
t function to use instead of i18n.t(), for example the one returned by useTranslation.
i18n
object (undefined)
i18next instance to use. Falls back to the instance of the nearest provider (IcuTrans only), then to the global instance set by initReactI18next.
Unlike Trans, there are no count, context, tOptions, parent, shouldUnescape or components props, and children are ignored. Put plurals and selects into the ICU message and pass their values in values. The react options for Trans in i18next.init (transWrapTextNodes, transKeepBasicHtmlNodesFor, defaultTransParent, ...) do not apply to IcuTrans.
Last updated