Trans doesn't work if the first element is a HTML tag
See original GitHub issueI’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> </div>
<div>
<div>Case 1: works!</div>
<Trans i18nKey='testTransKey1' count={numOfItems}>
{numOfItems} items matched.
</Trans>
</div>
<div> </div>
<div>
<div>Case 2: doesn't work!</div>
<Trans i18nKey='testTransKey2' count={numOfItems}>
<span className='matchCount'>{numOfItems}</span> items matched.
</Trans>
</div>
<div> </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:
- Created 6 years ago
- Comments:5 (4 by maintainers)
Top 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 >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 >
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

@jamuhl , it works well now! Thanks for the prompt fix for this! (also, thanks for the tip on the interpolation).
react-i18next@5.4.0 comes with the new ast parser and should fix this issue - please check if still encounter any issue. thx