我正在React项目中导入带翻译的json,以便组织代码。但是useTranslation()挂钩似乎没有读取这些导入的名称空间。
我这样做的例子:
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>
}
它显示:
文本
file2.text
编辑:我已经添加了诸如useTranslation(“ file1”)之类的命名空间,但仍然无法正常工作。
调用挂钩时,您缺少名称空间。应该是:
useTranslation(["file1", "file2"])