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.

Nested HTML element within Trans seems like not working

See original GitHub issue

Describe the bug Nested HTML element within Trans seems like not working

Occurs in react-i18next version 10.11.3

To Reproduce Steps to reproduce the behavior:

i have this

<Trans i18nKey={"Are you sure you want to <0>delete</0> account  <0><1>{{name}}</1></0>  ?"} values={{name}} components={[<b key={0} />, <i key={1} />]} >
</Trans>               

{{name}} should be bold then italic, however, it becomes simply not shown.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
tisoapcommented, Oct 9, 2019

Oh, now I finally understand how the components prop works! For future readers reference, this is how I could have written my example above to get the expected results:

const keys = [
  'test1', // "<0>Text inside paragraph</0>"
  'test2', // "<0>Text inside paragraph with <0>bold text</0></0>",
  'test3', // "just some <1>bold text</1>",
  'test4' // "<0>Text inside paragraph with <1>a link</1> for the user</0>"
]

// ...

<ul>
  {keys.map(key => (
    <li key={key}>
      <Trans
        i18nKey={key}
        components={[
          // index 0
          <p>
            { /* New nesting level means the index starts over */ }
            <strong /> { /* index 0 */ }
            <a href='#' /> { /* index 1 */ }
          </p>,
          // index 1
          <strong />
        ]}
      />
    </li>
  ))}
</ul>
3reactions
tisoapcommented, Oct 9, 2019

But to be fair, this is a rather confusing API, that yields an awkward components prop declaration and hard to maintain translation texts like <0><0><0>this</0></0></0>.

I see there’s already some ongoing discussion at #833 to allow for named placeholders by passing an object to components prop, which would help a lot.

In my opinion, making the numbered placeholders match the equivalent position in the components array would be a more intuitive API than having to track nesting levels.

But more importantly, how to use <Trans> with nested components should definitely be in the documentation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problems when parsing nested html tags from string
It sounds like you want a recursive function that prints the textContent of itself, or of its children, if it has children:.
Read more >
Text in Nested Div Won't Style - HTML & CSS - SitePoint Forums
I've created a nested div and put some text into it with a h1 header assignation. I try and change the font family...
Read more >
Template Language - SparkPost Developers
The SparkPost API provides a powerful handlebars-style template language that you can use in the email subject, headers, text, HTML, and AMP HTML...
Read more >
Transforming XML Data with XSLT - Oracle Help Center
Here, you will create a simple test document using nested <SECT> elements, a few <PARA> elements, a <NOTE> element, a <LINK>, and a...
Read more >
Using HTML's translate attribute - W3C
The translate attribute in HTML5 indicates that the content of the ... is no , translation tools should protect the text of the...
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