reactjs - 带有下一个 js 的 I18next 仅在家庭中无法用于生产
问题描述
我正在使用 nextI18next 并且在 DEV 中,我可以在除主页之外的所有页面和所有组件中看到翻译(真的很糟糕!!)。在构建阶段翻译未加载:(
这是我的依赖项:
"i18next": "^19.5.1",
"i18next-browser-languagedetector": "^5.0.0",
"i18next-http-middleware": "^3.0.0",
"next": "^9.4.4",
"next-i18next": "^4.5.0",
这是服务器
require('dotenv').config()
const express = require("express");
const next = require("next");
const nextI18NextMiddleware = require("next-i18next/middleware").default; // LINE 1
const nextI18next = require("./i18n");
const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();
async function start() {
try {
await app.prepare();
const server = express();
console.log("App loaded")
await nextI18next.initPromise;
console.log("Languages loaded")
server.use(nextI18NextMiddleware(nextI18next));
server.all("*", handle);
server.listen(port, '0.0.0.0', (err) => {
if (err) throw err;
console.log(`Ready on http://localhost:${port}`);
});
} catch (e) {
debugger;
throw e;
}
}
start();
这里是 i18n.js
const NextI18Next = require("next-i18next").default;
const { join } = require("path");
const I18Next = new NextI18Next({
ns: ["common"],
defaultNS: ["common"],
defaultLanguage: "en-GB",
otherLanguages: ["it-IT", "de-DE", "es-ES", "pt-PT", "fr-FR"],
load: ["en-GB", "it-IT"],
preload: ["en-GB", "it-IT"],
browserLanguageDetection: true,
serverLanguageDetection: true,
localePath: typeof window === "undefined" ? "public/locales" : "locales",
localeStructure: "{{lng}}/translation-{{lng}}",
saveMissing: true,
//debug: true,
initImmediate: true,
saveMissingTo: "all",
backend: {
loadPath: join(__dirname, typeof window === "undefined" ? "/public/locales/{{lng}}/translation-{{lng}}.json" : "/locales/{{lng}}/translation-{{lng}}.json"),
addPath: join(__dirname, typeof window === "undefined" ? "/public/locales_missing/{{lng}}_missing.json" : "/locales_missing/{{lng}}_missing.json"),
allowedAddOrUpdateHosts: ["localhost"],
},
});
module.exports = I18Next
你认为有什么问题?我快疯了……
编辑控制台说
i18next::backendConnector: loaded namespace for language it-IT {hi: "Ciao"}
i18next::backendConnector: loaded namespace for language en-GB {hi: "Hi"}
i18next::translator: missingKey it-IT undefined hi hi
i18next: languageChanged it-IT
i18next::translator: missingKey it-IT undefined hi hi
18next: languageChanged it-IT
i18next: initialized
i18next::translator: missingKey it-IT undefined hi hi
解决方案
推荐阅读
- laravel - 如何在 laravel 5.6 中找到不同用户的最大总和值?
- python - 查找数组中的最小数
- intellij-idea - Intellij:是否有使“运行任何东西”对话框内容更宽的选项或配置?
- tabs - AMP setState 在服务器中无法正常工作
- c# - 找不到与命令“dotnet-/app/Build\ClearPluginAssemblies.dll” Docker 匹配的可执行文件
- go - “i.(string)”在 golang 语法中的实际含义是什么?
- java - 在 ArrayList 中添加/删除方法与设置方法
- javascript - React 使用新项目更新列表,但 ID 可能会重复
- flutter - 从网络抖动加载图像的更好方法
- c++ - 在类的公共方法中调用私有方法