【发布时间】:2020-12-22 19:13:12
【问题描述】:
我已经在 github 上部署了一个 react 项目,并在使用 github pages 进行查看时。我正在使用 i18next 为我的网络应用程序提供不同的翻译。当我通过 localhost 运行我的应用程序时,locales 文件夹被访问并正确翻译了我的应用程序的内容。但是,当使用 github 页面部署时,出现错误:
警告:
i18next::backendConnector: loading namespace translation for language en failed failed loading /locales/en/translation.json
错误:
request.js:60 GET https://user.github.io/locales/en/translation.json 404
所以我无法在我的语言环境文件夹中访问我的翻译。
我的文件夹结构(我的文件夹里面有更多的语言,而不是同级别的英语):public > locales > en > translation.json
来自以下文档的 i18next.js:
src > i18n.js
带代码:
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import Backend from 'i18next-http-backend';
import LanguageDetector from 'i18next-browser-languagedetector';
// don't want to use this?
// have a look at the Quick start guide
// for passing in lng and translations on init
const Languages = ['en' , 'gr']; //the languages I want
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,
whitelist:Languages,
interpolation: {
escapeValue: false, // not needed for react as it escapes by default
}
});
export default i18n;
感谢您的帮助
【问题讨论】: