繁体   English   中英

react-i18next 忽略选项参数

[英]react-i18next ignores options parameter

我想更好地组织我的项目的语言翻译文件,必须在语言文件中嵌套对象。

  • 没有嵌套一切正常
  • 即使是嵌套对象,我也可以通过在 t() function 中传递属性目录来访问它们(即:t('section.subsection.text')
  • 文档的解决方案对我不起作用,我不知道为什么

我已经尝试打印 useTranslation 实例以查看它是否正在接收“keyPrefix”值,但它总是在 t 或 fixedT function 上显示“未定义”。

lang文件是这样的:

export const en = {
  section:{
    subsection:{
      text:'my text',
    }
  }
}

我在其中使用 useTranslation 挂钩的文件:

const { t } = useTranslation('section', { keyPrefix: 'subsection' });

<h1>{t('text')}</h1>

这是从文档中获取的

// having resources like this:
/*{
  "translation": {
    "very": {
      "deeply": {
        "nested": {
          "key": "here"
        }
      }
    }
  }
}*/
// you can define a keyPrefix to be used for the resulting t function
const { t } = useTranslation('translation', { keyPrefix: 'very.deeply.nested' });
const text = t('key'); // "here"

我有一个旧的 node.js 版本。

更新它(nvm 是一个很好的工具),瞧

当我遇到这个问题时,我已将 Node.js 从 12.14.0 更新到 14.19.0。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM