reactjs - Hook useTranslation() 不读取导入的命名空间 - i18next

标签 reactjs namespaces internationalization react-hooks i18next

我在我的 React 项目中导入带有翻译的 jsons,以便我可以组织我的代码。但是 useTranslation() 钩子(Hook)似乎没有读取这些导入的命名空间。

我如何做的一个例子:

i18n.js:

import i18next from 'i18next';
import {
  file1, 
  file2
} from 'translations';

i18next.init({
  interpolation: {
    escapeValue: false
  },
  lng: 'en',
  resources: {
    en: {
      file1: file1,
      file2: file 2
}
export default i18next;

使用 useTanslator():
import React from 'react';
import { useTranslation } from 'react-i18next';

export function MyComponent() {
  const { t } = useTranslation();

  return
   <p>{t('file1:text')}</p>
   <p>{t('file2:file2.text')}</p>
}

表明:

文本

文件2.text

编辑:我添加了像 useTranslation("file1") 这样的命名空间,但仍然不起作用。

最佳答案

调用钩子(Hook)时缺少命名空间。它应该是:

useTranslation(["file1", "file2"])

关于reactjs - Hook useTranslation() 不读取导入的命名空间 - i18next,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61372946/

相关文章:

reactjs - React map 不是渲染数组而是 console.logs

javascript - React 中具有固定记录数的多行

reactjs - 如何更改material-ui slider 的拇指图标

visual-studio-2010 - 安装了来自 NuGet 的引用,但 VS2010 找不到命名空间

r - 同一 R 包中非导出函数的显式命名空间 - 最佳实践

.net - .NET:InvariantCulture和en-US之间有什么区别吗?

javascript - React DOM 创建 html 元素并将函数绑定(bind)到 setState

namespaces - 如何检查您的项目名称是否在 packagist.org 上可用

unicode - 为什么我们所做的一切都不是 Unicode?

python - 在嵌入式 Python 中使用 locale.setlocale 而不破坏 C 线程中的文件解析