question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trans component doesn't work with nested tags

See original GitHub issue

Occurs in react-i18next version react-i18next@10.12.2

To Reproduce

Render:

<Trans i18nKey="myLangText">
	Please click on <span onClick={this.toggleTipsModal}><u>here</u></span> for further information.
</Trans>

Lang:

"myLangText": "Please click on <1><2>here</2></1> for further information.",

Expected behaviour

Please click on <span><u>here</u></span> for further information.

Current behaviour

Please click on <span>here</span> for further information.

OS (please complete the following information):

  • Browser: Latest Chrome for Windows

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

11reactions
adraicommented, Aug 21, 2019

In theory if you keep everything like you had in the beginning, but change the translation value to:

...
myLangText: "Please click on <1><0>here</0></1> for further information."
...

it should work too.

So just change your <2> tag to <0> tag

Can you try this @Saibamen?

4reactions
callmemarvcommented, Aug 11, 2021

For those who are still wondering about how to translate their nested tags correctly here is another example. As @adrai mentioned the children of a translated tag are indexed starting with 0 again so in order to reference the anchor element inside paragraph 2 we need to use index 1 because the paragraph has three children: 0: “This is paragraph 2 with” 1: <a> 2: .

Component

<Trans i18nKey="EXAMPLE.TEXT">
    Ignore this text.
    <p>This is paragraph 1.</p>
    <p>This is paragraph 2 with <a href="https://github.com/i18next/react-i18next/issues/928">a link</a>.</p>
    <p>This is paragraph 3.</p>
    Ignore this text as well.
</Trans>

Translation JSON

  "EXAMPLE": {
    "TEXT": "<1>Translated paragraph 1.</1><2>Translated paragraph 2 with <1>translated link</1>.</2><3>Translated paragraph 3.</3>"
  }

Outcome Translated paragraph 1. Translated paragraph 2 with translated link. Translated paragraph 3.

In our team we only use the tags as building blocks in our components so we would achieve the same outcome like this:

Component

<Trans i18nKey="EXAMPLE.TEXT">
    <p/>
    <p><a href="#"/></p>
    <p/>
</Trans>

Translation JSON

"EXAMPLE": {
  "TEXT": "<0>Translated paragraph 1.</0><1>Translated paragraph 2 with <0>translated link</0>.</1><2>Translated paragraph 3.</2>"
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

React-i18n Trans Component with translations that contain ...
The only way I managed to make it work was to use Trans like this: <Trans i18nKey="readOnlyField" // the key in your JSON...
Read more >
Trans Component - react-i18next documentation
This component enables you to nest any React content to be translated as one ... Existing self-closing HTML tag names are reserved keys...
Read more >
Use i18next Trans Component to Translate Complex React ...
The react-i18next library has a Trans component that we can use to interpolate or translate complex react element. In most cases, we don't...
Read more >
@types/react-i18next | Yarn - Package Manager
0.0 Trans component per default won't add a parent div around content passed as children. It will just return it's children. You still...
Read more >
@lingui/macro - Reference — LinguiJS documentation
Trans ¶ · both custom React components and built-in HTML tags are supported · change of component props doesn't break the translation ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found