Trans Component

Important note

While <Trans> gives you a lot of power by letting you interpolate or translate complex React elements, the truth is: in most cases you don't even need it.

As long you have no React/HTML nodes integrated into a cohesive sentence (text formatting like strong, em, link components, maybe others), you won't need it - most of the times you will be using the good old t function.

You may be looking directly for the Trans props.

import React from 'react';
import { Trans, useTranslation } from 'react-i18next'

function MyComponent() {
  const { t } = useTranslation('myNamespace');

  return <Trans t={t}>Hello World</Trans>;
}

Have a look at the i18next documentation for details on the the t function:

Samples

Using with React components

So you learned there is no need to use the Trans component everywhere (the plain t function will just do fine in most cases).

This component enables you to nest any React content to be translated as one cohesive string. It supports both plural and interpolation. The <Trans> component will automatically use 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 Trans component just change it to:

Your en.json (translation strings) will look like:

saveMissing will send a valid defaultValue based on the component children. Also, The i18nKey is optional, in case you already use text as translation keys.

Alternative usage which lists the components (v11.6.0)

This format is useful if you want to interpolate the same node multiple times. Another advantage is the simpler named tags, which avoids the trouble with index guessing - however, this can also be achieved with transSupportBasicHtmlNodes, see the next section.

Make sure you also adapt your translation resources to include the named tags (<italic>) instead of the indexed tags (<0>)!

Overriding React component props (v11.5.0)

In some cases you may want to override the props of a given component based on the active language.

This can be achieved by providing prop values inside of your translations. Such values will override whatever has been passed to the component present in the components prop of the Trans component.

In the example below we want our custom link component to have a different href value based on the active language. This is how our custom link component is being used:

with the following being our translation message:

This setup will render the following JSX:

This approach also works with listed components:

With this then making up our translation message:

Usage with simple HTML elements like <br /> and others (v10.4.0)

There are two options that allow you to have basic HTML tags inside your translations, instead of numeric indexes. However, this only works for elements without additional attributes (like className), having none or a single text children.

Examples of elements that will be readable in translation strings:

  • <br/>

  • <strong>bold</strong>

  • <p>some paragraph</p>

Examples that will be converted to indexed nodes:

  • <i className="icon-gear" />: no attributes allowed

  • <strong title="something">{{name}}</strong>: only text nodes allowed

  • <b>bold <i>italic</i></b>: no nested elements, even simple ones

Here is what can be configured in i18next.options.react that affect this behaviour:

Option
Default
Description

transSupportBasicHtmlNodes

true

Enables keeping the name of simple nodes (e.g. <br/>) in translations instead of indexed keys

transKeepBasicHtmlNodesFor

['br', 'strong', 'i', 'p']

Which nodes are allowed to be kept in translations during defaultValue generation of <Trans>.

transWrapTextNodes (v11.10.0)

''

Wrap text nodes in a user-specified element. e.g. set it to span. By default, text nodes are not wrapped. Can be used to work around a well-known Google Translate issue with React apps. See facebook/react#11538.

Interpolation

You can pass in variables to get interpolated into the translation string by using objects containing those key:values.

Plural

You will need to pass the count prop:

Using with lists (v10.5.0)

You can still use Array.map() to turn dynamic content into nodes, using an extra option on a wrapping element:

Setting i18nIsDynamicList on the parent element will assert the nodeToString function creating the string for saveMissing will not contain children.

Alternative usage (components array)

Some use cases, such as the ICU format, might be simpler by just passing content as props:

<0> -> 0 is the index of the component in the components array

E.g. this format is needed when using ICU as translation format as it is not possible to have the needed syntax as children (invalid jsx).

How to get the correct translation string?

Guessing replacement tags (<0></0>) of your component is rather difficult. There are four options to get those translations directly generated by i18next:

  1. use React Developer Tools to inspect the <Trans> component instance and look at the props.children array for array index of the tag in question.

  2. use debug = true in i18next.init() options and watch your console for the missing key output

  3. use the saveMissing feature of i18next to get those translations pushed to your backend or handled by a custom function.

  4. understand how those numbers get generated from child index:

Sample JSX:

Resulting translation string:

The complete the node tree:

Rules:

  • child is a string: nothing to wrap; just take the string

  • child is an object: nothing to do; it's used for interpolation

  • child is an element: wrap it's children in <x></x> where x is the index of that element's position in the children list; handle its children with the same rules (starting element.children index at 0 again)

Trans props

All properties are optional, although you'll need to use i18nKey if you're not using natural language keys (text-based).

name

type (default)

description

i18nKey

string (undefined)

If you prefer to use text as keys you can omit this, and the translation will be used as key. Can contain the namespace by prepending it in the form 'ns:key' (depending on i18next.options.nsSeparator)

But this is not recommended when used in combination with natural language keys, better use the dedicated ns parameter: <Trans i18nKey="myKey" ns="myNS"></Trans>

ns

string (undefined)

Namespace to use. May also be embedded in i18nKey but not recommended when used in combination with natural language keys, see above.

t

function (undefined)

t function to use instead of the global i18next.t() or the t() function provided by the nearest provider.

count

integer (undefined)

Numeric value for pluralizable strings

context

string (undefined)

Value used for the context feature.

tOptions

object (undefined)

Extra options to pass to t() (e.g. context, postProcessor, ...)

parent

node (undefined)

A component to wrap the content into (can be globally set on i18next.init). Required for React < v16

i18n

object (undefined)

i18next instance to use if not provided by context

defaults

string (undefined)

Use this instead of using the children as default translation value (useful for ICU)

values

object (undefined)

Interpolation values if not provided in children

components

array[nodes] (undefined)

Components to interpolate based on index of tag

shouldUnescape

boolean (false)

HTML encoded tags like: &lt; &amp; &gt; should be unescaped, to become: < & >

i18next options

Last updated