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 doesn't work if the first element is a HTML tag

See original GitHub issue

I’m seeing that the <Trans> doesn’t work for some cases.

Component:

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

const TestTrans = () => {
  const numOfItems = 2;

  return (
    <div>
      <div>Test Trans</div>
      <div>&nbsp;</div>
      <div>
        <div>Case 1: works!</div>
        <Trans i18nKey='testTransKey1' count={numOfItems}>
          {numOfItems} items matched.
        </Trans>
      </div>
      <div>&nbsp;</div>
      <div>
        <div>Case 2: doesn't work!</div>
        <Trans i18nKey='testTransKey2' count={numOfItems}>
          <span className='matchCount'>{numOfItems}</span> items matched.
        </Trans>
      </div>
      <div>&nbsp;</div>
      <div>
        <div>Case 3: works!</div>
        <Trans i18nKey='testTransKey3' count={numOfItems}>
          Result: <span className='matchCount'>{numOfItems}</span> items matched.
        </Trans>
      </div>
    </div>
  );
};

export default translate('test')(TestTrans);

And, here’s the locale JSON -

{
  "testTransKey1": "<0>{numOfItems}</0> item matched.",
  "testTransKey1_plural": "<0>{numOfItems}</0> items matched.",

  "testTransKey2": "<0><0>{numOfItems}</0></0> item matched.",
  "testTransKey2_plural": "<0><0>{numOfItems}</0></0> items matched.",

  "testTransKey3": "Result: <1><0>{numOfItems}</0></1> item matched.",
  "testTransKey3_plural": "Result: <1><0>{numOfItems}</0></1> items matched."
}

And, here’s what I see on the browser -

Test Trans
 
Case 1: works!
2 items matched.
 
Case 2: doesn't work!
<0>{numOfItems}</0> items matched.
 
Case 3: works!
Result: 2 items matched.

As you can see, the “Case 2” doesn’t work. Am I missing something, or is it a bug?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
cooljykimcommented, Sep 18, 2017

@jamuhl , it works well now! Thanks for the prompt fix for this! (also, thanks for the tip on the interpolation).

0reactions
jamuhlcommented, Sep 16, 2017

react-i18next@5.4.0 comes with the new ast parser and should fix this issue - please check if still encounter any issue. thx

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why can't I translate element using transform: translateY()?
Transform doesn't work on inline elements. .first { display:inline-block; transform: translateY(40px); }.
Read more >
Using HTML's translate attribute
The translate attribute in HTML5 indicates that the content of the element should or should not be translated.
Read more >
first-of-type - CSS: Cascading Style Sheets - MDN Web Docs
The :first-of-type CSS pseudo-class represents the first element of its type among a group of sibling elements.
Read more >
Docs • Svelte
Complete documentation for Svelte.
Read more >
How to insert HTML tag with injectIntl formatMessage using ...
tagName prop is supposed to get a wrapper Tag, like a div, span or p. ... You have to add the br inside...
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