Fai*_*ire 14 reactjs react-i18next
我在一个小型网站上使用 ReactJS。我决定使用 i18next 进行国际化并且它有效 - 除非我使用嵌套引用作为翻译键。
在下面的示例中,显示了 intro1 和 intro2 键,但没有找到welcome.headtitle(控制台中的错误“missingKey”)。
应用程序.js:
...
<p><Trans i18nKey='intro1'/></p>
<p><Trans i18nKey='intro2'/></p>
<p><Trans i18nKey='welcome.headtitle'/></p>
...
Run Code Online (Sandbox Code Playgroud)
翻译.json:
{
"welcome": {
"headtitle": ...
...
},
"intro1": ...,
"intro2": ...,
}
Run Code Online (Sandbox Code Playgroud)
我知道 i18next 允许嵌套的 JSON 翻译对象。我究竟做错了什么?我检查了文档和示例,没有发现任何错误。
San*_*nda 32
虽然使用“welcome.name”等的答案是有效的用法,但对于我的用例,我实际上需要使用结构化键,以便我可以更好地构建我的翻译。
使嵌套值对我有用的是keySeparator: false
从i18n.init
函数中删除。
代码将是:
i18n.use(initReactI18next).init({
resources: {
en: {translation: EN},
fr: {translation: FR},
},
lng: 'en',
fallbackLng: 'en',
// keySeparator: false, // this was the line that I've had to remove to make it work
interpolation: {
escapeValue: false,
},
});
Run Code Online (Sandbox Code Playgroud)
我的 JSON 看起来像:
{
"nested": {
"value": "Trying a nested value"
}
}
Run Code Online (Sandbox Code Playgroud)
我的 HTML(我的反应组件中的 div):
<div>{t("nested.value")}</div>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5830 次 |
最近记录: |