将Siserverside翻译添加到我的Next.js应用程序时出错

发布于 2025-02-11 18:47:56 字数 759 浏览 1 评论 0原文

在将应用程序部署到Vercel时,我会遇到错误,但是我的应用程序在本地服务器上的工作正常。 这是我的功能日志看起来像

TypeError: Cannot read properties of undefined (reading '__NEXT_DATA__')
    at Document (/var/task/.next/server/pages/_document.js:24:26)

Error: ENOENT: no such file or directory, scandir '/var/task/public/locales/en'

我的_document.js

export default function Document(props) {
  const locale = props.__NEXT_DATA__.locale;
  console.log(props.__NEXT_DATA__);
  const dir = locale === "ar" ? "rtl" : "ltr";
  return (
    <Html dir={dir} lang={locale}>
      <Head />
      <body className={locale === "ar" ? "rtl" : null}>
        <Main />
        <NextScript />
      </body>
    </Html>
  );
}

I am getting an error when deploying my app to vercel, but my app work perfectly fine on local server.
Here is what my function logs looks like

TypeError: Cannot read properties of undefined (reading '__NEXT_DATA__')
    at Document (/var/task/.next/server/pages/_document.js:24:26)

Error: ENOENT: no such file or directory, scandir '/var/task/public/locales/en'

my _document.js

export default function Document(props) {
  const locale = props.__NEXT_DATA__.locale;
  console.log(props.__NEXT_DATA__);
  const dir = locale === "ar" ? "rtl" : "ltr";
  return (
    <Html dir={dir} lang={locale}>
      <Head />
      <body className={locale === "ar" ? "rtl" : null}>
        <Main />
        <NextScript />
      </body>
    </Html>
  );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明媚如初 2025-02-18 18:47:58

我找到了答案,我必须在配置文件中指定我的Locales文件夹,这样

const path = require("path");
module.exports = {
  i18n: {
    localePath: path.resolve("./public/locales"),
  },
  trailingSlash: true,
};

I found the answer, I had to specify my locales folder in my config file like so

const path = require("path");
module.exports = {
  i18n: {
    localePath: path.resolve("./public/locales"),
  },
  trailingSlash: true,
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文