Trans component doesn't work with nested tags
See original GitHub issueOccurs 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:
- Created 4 years ago
- Comments:9 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
In theory if you keep everything like you had in the beginning, but change the translation value to:
it should work too.
So just change your <2> tag to <0> tag
Can you try this @Saibamen?
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
Translation JSON
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
Translation JSON