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

问题描述 投票:1回答:1

我正在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”)之类的命名空间,但仍然无法正常工作。

reactjs namespaces internationalization react-hooks i18next
1个回答
0
投票

调用挂钩时,您缺少名称空间。应该是:

useTranslation(["file1", "file2"])
© www.soinside.com 2019 - 2024. All rights reserved.