i18n错误(错误:您正在传递未定义的模块!请检查要传递给i18next.use()的对象)

时间:2020-07-11 12:39:41

标签: gatsby react-i18next

我正在尝试在Gatsby项目中设置i18n。

我一直在逐步遵循本教程:

https://www.gatsbyjs.org/blog/2017-10-17-building-i18n-with-gatsby/

首先,我下载所需的软件包:

npm i -S i18next i18next-xhr-backend i18next-browser-languagedetector react-i18next

然后我设置了i18n组件

import i18n from "i18next"
import Backend from "i18next-xhr-backend"
import LanguageDetector from "i18next-browser-languagedetector"
import { reactI18nextModule } from "react-i18next"

i18n
.use(Backend)
.use(LanguageDetector)
.use(reactI18nextModule)
.init({
    fallbackLng: "en",

    // have a common namespace used around the full app
    ns: ["translations"],
    defaultNS: "translations",

    debug: true,

    interpolation: {
    escapeValue: false, // not needed for react!!
    },

    react: {
    wait: true,
    },
})

export default i18n

将其导入到我的布局组件中:

import React from "react"
import "./layout.scss"
import NavMenu from "./NavMenu/navMenu"
import i18n from './i18n/i18n'

export default function Layout({ children }) {
  return (
    <div className="container">
      <NavMenu />
      {children}
    </div>
  )
}

但是,当我运行我的应用程序时,出现以下错误:

enter image description here

有人知道可能是什么问题吗?

1 个答案:

答案 0 :(得分:1)

react-i18next的导入是错误的。应该是

import { initReactI18next } from "react-i18next"

.use(initReactI18next)

如果您尝试导入未导出的内容,则该值是不确定的,然后use会给您显示的消息。

reactI18nextModule的早期版本中似乎已将其称为react-i18next,这就是为什么您可以使用它来查找教程的原因。

我做了一个demonstration of the exports in RunKit