react-i18next 忽略选项参数
react-i18next ignores options parameter
我想更好地组织我的项目的语言翻译文件,不得不在语言文件中嵌套对象。
- 没有嵌套一切正常
- 即使是嵌套对象,我也可以通过在 t() 函数中传递属性目录来访问它们(即:t('section.subsection.text')
- 文档的解决方案对我不起作用,我不知道为什么
我已经尝试打印 useTranslation 实例以查看它是否正在接收“keyPrefix”值,但它总是在 t 或 fixedT 函数上显示 'undefined'。
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。
我想更好地组织我的项目的语言翻译文件,不得不在语言文件中嵌套对象。
- 没有嵌套一切正常
- 即使是嵌套对象,我也可以通过在 t() 函数中传递属性目录来访问它们(即:t('section.subsection.text')
- 文档的解决方案对我不起作用,我不知道为什么
我已经尝试打印 useTranslation 实例以查看它是否正在接收“keyPrefix”值,但它总是在 t 或 fixedT 函数上显示 'undefined'。
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。