我正在使用react,i18next和react-i18next。我想要在文本中间插入带有HTML链接的可翻译文本,这些文本将在react中进行插值,就像这样:
This is my text with <a href="{{link}}">a beautiful link</a> in the middle of the text
下面的解决方案可行,但问题是我需要在react中插值链接,因此不能在标签文件中硬编码:
"my-label": "This is my text with <a href=\"http://google.com\">a beautiful link</a> in the middle of the text"
[...]
<Interpolate i18nKey="my-label" useDangerouslySetInnerHTML />
看起来这样会更好:
"my-label": "This is my text with {{link}} in the middle of the text",
"link" "a beautiful link"
[...]
const { url } = props;
const link = <a href={url}>{t('link')}</a>
<Interpolate i18nKey="my-label" link={link} />
这可能是一个解决方案,但应用程序已被翻译成许多语言,翻译的质量非常重要,因此我更喜欢将整个文本放在一行中以便于翻译(特别是对于具有格的语言而言)。
是否有任何方法可以使类似这样的内容正常工作(或者是否有完全不同的解决方法)?
"my-label": "This is my text with <a href=\"{{link}}\">a beautiful link</a> in the middle of the text"
[...]
const { url } = props;
<Interpolate i18nKey="my-label" useDangerouslySetInnerHTML link={url} />
X<a href>Y</a>Z
(真的是XYZ!)作为Trans
的子元素,并且它可以工作!在JSON中,我有实际的字符串(例如This is a text with <1>a link</1> inside
)。 - v.nivuahc