i18next-http-backend 在生产环境下不起作用

huangapple go评论108阅读模式
英文:

i18next-http-backend not working on production

问题

I am using i18next for translation with i18next-http-backend to get the translation files. My codes work on development but not on production, it's a Vite application. I have tried a couple of suggestions by using different i18next-http-backend versions like v1.2.4, but it's still the same as suggested here link.

The debug is showing:

i18next-http-backend 在生产环境下不起作用

My config file:

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

i18n
  .use(initReactI18next)
  .use(backend)
  .init({
    fallbackLng: "en",
    lng: "en",
    backend: {
      loadPath: "src/i18n/locales/{{lng}}.json",
      addPath: "src/i18n/locales/add/{{lng}}",
    },
    interpolation: { escapeValue: false },
    debug: true,
  });

export default i18n;

And your App component:

function App() {
  return (
    <Suspense fallback={<Spinner/>}>
      <Routes>
        <Route path="/" element={<LandingPage />} />
      </Routes>
    </Suspense>
  );
}

export default App;

(Note: I have provided the code in a readable format, not as HTML entities as in your original text.)

英文:

I am using i18next for transalation with i18next-http-backend to get the translation files. My codes works on development but not on production, it's a vite application.
i have tried a couple of suggestion by using different i18next-http-backend version like the v1.2.4 but still same as suggested here link

The debug is showing
i18next-http-backend 在生产环境下不起作用

My config file

import i18n from &quot;i18next&quot;;
import { initReactI18next } from &quot;react-i18next&quot;;
import backend from &quot;i18next-http-backend&quot;;

i18n
  .use(initReactI18next)
  .use(backend)
  .init({
    fallbackLng: &quot;en&quot;,
    lng: &quot;en&quot;,
    backend: {
      loadPath: &quot;src/i18n/locales/{{lng}}.json&quot;,
      addPath: &quot;src/i18n/locales/add/{{lng}}&quot;,
    },
    interpolation: { escapeValue: false },
    debug: true,
  });

export default i18n;
function App() {
  return (
    &lt;Suspense fallback={&lt;Spinner/&gt;}&gt;
      &lt;Routes&gt;
        &lt;Route path=&quot;/&quot; element={&lt;LandingPage /&gt;} /&gt;
       
      &lt;/Routes&gt;
    &lt;/Suspense&gt;
  );
}

export default App;

答案1

得分: 0

检查你的翻译文件的路由。你在控制台中有404错误。请使用公共文件夹。

文件应该位于public/src/i18n/locales/{{lng}}.json

以及public/src/i18n/locales/add/{{lng}}.json

但也许更正确的是将其更改为public/i18n/locales/{{lng}}.json。

配置将是:

loadPath: "i18n/locales/{{lng}}.json",
addPath: "i18n/locales/add/{{lng}}",

https://react.i18next.com/legacy-v9/step-by-step-guide#a-add-an-additional-language-file

英文:

Check routes of your translations files. You have 404 eroror in console. Use public folder.

Files should be at public/src/i18n/locales/{{lng}}.json

and public/src/i18n/locales/add/{{lng}}.json

But maybe will be correctly to change it to public/i18n/locales/{{lng}}.json.

Config will be:

loadPath: &quot;i18n/locales/{{lng}}.json&quot;,
addPath: &quot;i18n/locales/add/{{lng}}&quot;,

https://react.i18next.com/legacy-v9/step-by-step-guide#a-add-an-additional-language-file

huangapple
  • 本文由 发表于 2023年2月18日 16:00:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定