使用 i18next 将变量替换为 ReactNode 元素
Using i18next to replace a variable with a ReactNode element
我有一个翻译 json 文件,翻译如下:
"pageNotFound": {
"description": "The page could not be found. Click {{link}} to return to the home page"
},
我想用 ReactRouter <Link>
替换 link 变量
我的 render
方法中有以下代码输出下面的图片。
public render() {
const { t } = this.props;
const message = t('pageNotFound.description', { link: <Link to="/">here</Link> });
return (
<div className="body-content">
<div>
{message}
</div>
</div>
);
}
我玩过 <Trans>
组件,我认为这可能是一种方式,但似乎您必须键入包括 <> 标签在内的全文,这对我的用例来说不是我想要的之后因为我希望所有文本都在翻译中 json 如果可能的话。
欢迎任何建议
您应该为此使用 Trans
组件。
"pageNotFound": {
"description": "The page could not be found. Click <0>here</0> to return to the home page"
},
public render() {
const { t } = this.props;
return (
<div className="body-content">
<div>
<Trans
t={t}
i18nKey="pageNotFound.description"
components={[
<Link key={0} to="/">
here
</Link>,
]}
/>
</div>
</div>
);
}
我有一个翻译 json 文件,翻译如下:
"pageNotFound": {
"description": "The page could not be found. Click {{link}} to return to the home page"
},
我想用 ReactRouter <Link>
我的 render
方法中有以下代码输出下面的图片。
public render() {
const { t } = this.props;
const message = t('pageNotFound.description', { link: <Link to="/">here</Link> });
return (
<div className="body-content">
<div>
{message}
</div>
</div>
);
}
我玩过 <Trans>
组件,我认为这可能是一种方式,但似乎您必须键入包括 <> 标签在内的全文,这对我的用例来说不是我想要的之后因为我希望所有文本都在翻译中 json 如果可能的话。
欢迎任何建议
您应该为此使用 Trans
组件。
"pageNotFound": {
"description": "The page could not be found. Click <0>here</0> to return to the home page"
},
public render() {
const { t } = this.props;
return (
<div className="body-content">
<div>
<Trans
t={t}
i18nKey="pageNotFound.description"
components={[
<Link key={0} to="/">
here
</Link>,
]}
/>
</div>
</div>
);
}