如何在github页面上使用i18next

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

我使用react-i18next库开发了具有国际化的react应用程序。我对此具有通常的配置(如lib的自述文件):

import i18n from 'i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import Backend from 'i18next-xhr-backend';
import { initReactI18next } from 'react-i18next';

i18n
  .use(Backend)
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    fallbackLng: "en",
    interpolation: {
      escapeValue: false
    }
  });

  export default i18n;

并且我的翻译文件被放置在public/locales/de/translation.json(而不是de文件夹中,与文档中相同。

它在开发模式下工作。我将应用程序部署到了heroku,在那里也可以正常工作。但是我需要它在github页面站点上工作,并且我的json(translation.json文件)未加载到那里,并且从浏览器控制台中得到了这些错误:

Failed to load http://hereIsMyUsername.github.io/locales/en/translation.json resource: the server responded with a status of 404 ()

构建React应用之后,我的build文件夹具有以下结构:

build
- locales
- static
  - css
  - js
  - media

因此,我的应用通常无法在github页面上正常运行,但无法加载json。我在文档中读到它是因为github页面试图从根目录中加载这些json,但在本地找不到它们,因为它们存储在locales / {lang} /translation.json中。因此建议的方法是将所有json和其他文件都放到根目录。但是由于我有很多不同的translation.json文件,所以这不是管理它的方法。

所以我的问题是如何使github页面能够使用它?或如何配置我的i18n以使其适用于github页面?

reactjs github-pages i18next react-i18next
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.