简体   繁体   English

下一个翻译找不到模块'./locales/en/common.json'

[英]next-translate Cannot find module './locales/en/common.json'

I am trying to integrate the next-translate library into my Next app, however I am getting an error when configuring the loadLocaleFrom function我正在尝试将next-translate库集成到我的 Next 应用程序中,但是在配置loadLocaleFrom function 时出现错误

This is what my i18n.js file looks like:这是我的 i18n.js 文件的样子:

module.exports = {
  locales: ["en", "fr", "es", "ru", "ar", "zh", "hi", "sw"],
  defaultLocale: "en",
  pages: {
    "*": ["common"],
    "/": ["home"],
  },
  loadLocaleFrom: async (lang, ns) => {
    try {
      const m = await import(`./locales/${lang}/${ns}.json`);

      return {
        ...m.default,
      };
    } catch (error) {
      console.log(error);
    }
  },
};

And my next.config file:还有我的 next.config 文件:

const withPlugins = require("next-compose-plugins");
const withImages = require("next-images");
const nextTranslate = require("next-translate");

module.exports = withPlugins([withImages, nextTranslate], {
  reactStrictMode: true,
  images: {
    disableStaticImages: true,
  },
});

package.json version: package.json版本:

next-translate: ^1.5.0
next: 12.1.6
react: 17.0.2

Even though my directory has both, the common.json and the home.json files in the correct folder structure, the loadLocaleFrom function still throws an error that looks like this:即使我的目录两者都有,common.json 和 home.json 文件在正确的文件夹结构中, loadLocaleFrom function 仍然引发如下错误:

Error: Cannot find module './locales/en/common.json'
    at webpackEmptyContext (D:\projects\mk\mk-academy\.next\server\pages\index.js:529:10)
    at eval (webpack-internal:///./i18n.js:64:89)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Object.loadLocaleFrom (webpack-internal:///./i18n.js:63:23)
    at async Promise.all (index 0) {
  code: 'MODULE_NOT_FOUND'
}

I did try using the appWithI18n hoc in _app.js but that doesn't solve it too.我确实尝试在 _app.js 中使用 appWithI18n hoc,但这也不能解决它。 I also did try moving the locales to a different directory under src but that shouldn't make a difference.我也确实尝试将语言环境移动到 src 下的不同目录,但这不应该有所作为。 The image shows my directory structure该图显示了我的目录结构在此处输入图像描述

require worked for me instead of import require为我工作而不是import

await require(`./locales/${lang}/${ns}.json`)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM