英文:
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:
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
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;
function App() {
return (
<Suspense fallback={<Spinner/>}>
<Routes>
<Route path="/" element={<LandingPage />} />
</Routes>
</Suspense>
);
}
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: "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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论