我正在尝试在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>
)
}
但是,当我运行我的应用程序时,出现以下错误:
有人知道可能是什么问题吗?
答案 0 :(得分:1)
从react-i18next
的导入是错误的。应该是
import { initReactI18next } from "react-i18next"
和
.use(initReactI18next)
如果您尝试导入未导出的内容,则该值是不确定的,然后use
会给您显示的消息。
在reactI18nextModule
的早期版本中似乎已将其称为react-i18next
,这就是为什么您可以使用它来查找教程的原因。