How to use Trans Macro or "t" tag within a map function? Also how to preserve line break styling? #1491
-
For example, I am using Nextjs. I have a FAQ array, and both of the questions and answers are strings or ReactNodes. I am doing this:
But One more issue is that if I use the Trans or t macro in the array, the macro would remove the line breaks inside ``, which makes the styling disappear. Originally I don't have to put line break character inside ``. But after adding Trans macro seems I can only add back '\n' to resolve this styling. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Translating from variables is not going to work // Wrong!
<>
<div><Trans>{v}</Trans></div>
<div><Trans>{answers[i]}</Trans></div>
</> Extractor, not executing your code, it only analyzing it during compilation. So all messages should be marked for translation in place where they are defined. Extractor has no idea what is
Normalize them to only react nodes (or if it's possible to only strings): const FAQs = [
{
q: <Trans>question1</Trans>,
a: <Trans>ans1</Trans>,
},
{
q: <Trans>question2</Trans>,
a: <>
<div><Link><Trans>Click me</Trans></Link></div>
<Trans>...</Trans>
<div><Link><Trans>Also click me</Trans></Link></div>
</>,
},
{
q: <Trans>question3</Trans>,
a: <>
<div><Link><Trans>Click me</Trans></Link></div>
<Trans>...</Trans>
<div><Link><Trans>Also click me</Trans></Link></div>
</>,
},
];
return (
<>{FAQs.map(({ q, a }) =>
<>
<div className="question">{q}</div>
<div className="answer">{a}</div>
</>
)}
</>
);
Something like this is possible, and it's intended. you also can use |
Beta Was this translation helpful? Give feedback.
Translating from variables is not going to work
Extractor, not executing your code, it only analyzing it during compilation. So all messages should be marked for translation in place where they are defined. Extractor has no idea what is
{v}
or{answers[i]}
when it analyzes<Trans>
macro.Normalize them to only react nodes (or if it's possible to only strings):