react-i18next出现错误尝试导入错误

时间:2020-06-17 13:17:50

标签: reactjs i18next react-i18next

我尝试使用https://react.i18next.com/ 对于本地化,我遇到错误:尝试导入错误:'react-i18next'未导出'initReactI18next'。

这是我的i18n.js文件:

import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';

import Backend from 'i18next-http-backend';
import LanguageDetector from 'i18next-browser-languagedetector';
// not like to use this?
// have a look at the Quick start guide 
// for passing in lng and translations on init

i18n
  // load translation using http -> see /public/locales (i.e. https://github.com/i18next/react-i18next/tree/master/example/react/public/locales)
  // learn more: https://github.com/i18next/i18next-http-backend
  .use(Backend)
  // detect user language
  // learn more: https://github.com/i18next/i18next-browser-languageDetector
  .use(LanguageDetector)
  // pass the i18n instance to react-i18next.
  .use(initReactI18next)
  // init i18next
  // for all options read: https://www.i18next.com/overview/configuration-options
  .init({
    fallbackLng: 'en',
    debug: true,

    interpolation: {
      escapeValue: false, // not needed for react as it escapes by default
    }
  });


export default i18n;

我的package.json

{“ name”:“ heatmann”,“ version”:“ 0.1.0”,“ private”:true,
“依赖关系”:{ “ axios”:“ ^ 0.19.0”, “ i18next”:“ ^ 19.4.5”, “ i18next-browser-languagedetector”:“ ^ 4.3.0”, “ i18next-http-backend”:“ ^ 1.0.15”, “ i18next-xhr-backend”:“ ^ 3.2.2”, “ prop-types”:“ ^ 15.7.2”, “ proptypes”:“ ^ 1.1.0”, “反应”:“ ^ 16.12.0”, “ react-alice-carousel”:“ ^ 1.17.2”, “ react-dom”:“ ^ 16.12.0”, “ react-i18next”:“ ^ 9.0.10”, “ react-loader-spinner”:“ ^ 3.1.5”, “ react-native-gesture-handler”:“ ^ 1.5.2”, “反应导航”:“ ^ 4.0.10”, “ react-navigation-stack”:“ ^ 1.10.3”, “ react-router-dom”:“ ^ 5.1.2”, “ react-scripts”:“ ^ 3.3.0”},“ scripts”:{ “ start”:“反应脚本开始”, “ build”:“反应脚本建立”, “ test”:“反应脚本测试”, “ eject”:“反应脚本弹出”},“ eslintConfig”:{ “ extends”:“ react-app”},“ browserslist”:{ “生产”: [ “> 0.2%”, “没死”, “不是全部op_mini” ], “发展”:[ “最后1个chrome版本”, “最新的1个Firefox版本”, “最后1个野生动物园版本” ]},“ devDependencies”:{ “ @ babel / plugin-proposal-export-default-from”:“ ^ 7.8.3”, “ css-loader”:“ ^ 3.4.2”}}

2 个答案:

答案 0 :(得分:1)

使用package.json删除了i18next个条目。
删除了所有的node_modules文件夹
重新安装了npm install
npm install react-i18next i18next --save //当需要检测用户语言并加载翻译时
npm install i18next-http-backend i18next-browser-languagedetector --save

这解决了我的问题。

答案 1 :(得分:0)

npm install react-i18next i18next --save

yarn add react-i18next i18next