我在我的 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/