首页 > 解决方案 > 带有下一个 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

标签: reactjsnext.jsi18next

解决方案


推荐阅读