【问题标题】:i18next not loading in file from public folderi18next 未从公用文件夹加载文件
【发布时间】:2021-01-20 00:00:39
【问题描述】:

我正在尝试加载多个翻译文件以响应应用程序。 到目前为止,我已经能够分离文件 在尝试附加两个不同翻译文件的名称时遇到问题 -- /locales/{{lng}}/{{ns}}.json?lng=en+es&ns=translation1+translation2)

我已经解决了这个问题,但现在它不会从公用文件夹中读取

我的配置文件如下所示

import i18n from 'i18next'
import Backend from 'i18next-http-backend'
import Http from 'i18next-http-backend'
import { initReactI18next } from 'react-i18next'
import BackendAdapter from 'i18next-multiload-backend-adapter'

const languages = ['en', 'es']

i18n
  .use(Backend)
  .use(initReactI18next)
  .init({
    defaultNS: 'menus',
    fallbackLng: 'en',
    debug: true,
    whitelist: languages,
    lng: 'en',

    backend: {
      backends:[
        new BackEndAdapter(null, {
          backend: new Http(null,{
            loadPath: `${process.env.PUBLIC_URL}/locales/{{lng}}/{{ns}}.json?lng={{lng}}&{{ns}}`,
            addPath: `${process.env.PUBLIC_URL}/locales/add/{{lng}}/{{ns}}`,
            allowMultiLoading: true
          })
        })
      ]    
    },

    interpolation: {
      escapeValue: false
    }
  })

export default i18n

package.json

{
   "i18next": "^19.5.1",
   "i18next-http-backend": "^1.0.15",
   "i18next-multiload-backend-adapter": "^1.0.0"
}

使用this 作为为什么需要多负载适配器的参考

【问题讨论】:

标签: reactjs i18next react-i18next


【解决方案1】:

应该是这样的:

import i18n from 'i18next';
import HttpBackend from 'i18next-http-backend';
import { initReactI18next } from 'react-i18next';

const languages = ['en', 'es'];

i18n
  .use(HttpBackend)
  .use(initReactI18next)
  .init({
    defaultNS: 'menus',
    fallbackLng: 'en',
    debug: true,
    whitelist: languages,
    lng: 'en',

    backend: {
      loadPath: `${process.env.PUBLIC_URL}/locales/{{lng}}/{{ns}}.json?lng={{lng}}&{{ns}}`,
      addPath: `${process.env.PUBLIC_URL}/locales/add/{{lng}}/{{ns}}`,
    },

    interpolation: {
      escapeValue: false,
    },
  });

export default i18n;

【讨论】:

    猜你喜欢
    • 2022-10-13
    • 2021-05-10
    • 1970-01-01
    • 2019-03-03
    • 2021-09-24
    • 1970-01-01
    • 2021-05-19
    • 2019-01-19
    • 1970-01-01
    相关资源
    最近更新 更多