【问题标题】:How to add next-i18next translation on a dynamically routed page in a nextjs application?如何在 nextjs 应用程序的动态路由页面上添加 next-i18next 翻译?
【发布时间】:2021-07-01 22:18:41
【问题描述】:

我在 Nextjs (v10.1.3) /my-translation/[id] 中有一个动态路由页面,我想使用 next-i18next (v8.1.3) 包来翻译这个页面。

我尝试在 Nextjs 中使用 2 个文件夹结构,它们都给出了我无法理解的相同错误。

  • pages/translated-page/[id]/index.tsx
  • pages/translated-page/[id].tsx

但是,如果我将动态路由更改为静态,则翻译工作正常。

工作文件夹结构示例:

  • pages/translated-page/id.tsx
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
import { useRouter } from "next/router";
import { useTranslation } from "next-i18next";

const TranslatedPage = () => {
  const { t } = useTranslation("my-translation");
  const router = useRouter();
  const { id } = router.query;

  return (
      <div>
         {t("existing-translation-key-from-my-translation-json")}
      </div>
  );
};

export const getStaticProps = async ({ locale }) => ({
  props: {
    fallback: true,
    paths: ["/translated-page/id", { params: { id: "" } }],
    ...(await serverSideTranslations(locale, ["my-translation"])),
  },
});

export default TranslatedPage;

我收到以下动态路线错误,我无法从提供的链接中了解我做错了什么。

服务器错误错误:动态 SSG 页面需要 getStaticPaths 并且缺少“/translated-page/[id]”。阅读更多: https://nextjs.org/docs/messages/invalid-getstaticpaths-value这个 生成页面时发生错误。任何控制台日志都将是 显示在终端窗口中。调用堆栈 renderToHTML file:///C:/projects/my-project.io/web-apps/my-project/node_modules/next/dist/next-server/server/render.js (21:2118) file:///C:/projects/my-project.io/web-apps/my-project/node_modules/next/dist/next-server/server/next-server.js (112:126) __wrapper file:///C:/projects/my-project.io/web-apps/my-project/node_modules/next/dist/lib/coalesced-function.js (1:341) file:///C:/projects/my-project.io/web-apps/my-project/node_modules/next/dist/lib/coalesced-function.js (1:377) DevServer.renderToHTMLWithComponents file:///C:/projects/my-project.io/web-apps/my-project/node_modules/next/dist/next-server/server/next-server.js (137:120) runMicrotasks 进程TicksAndRejections internal/process/task_queues.js (93:5) 异步 DevServer.renderToHTML file:///C:/projects/my-project.io/web-apps/my-project/node_modules/next/dist/next-server/server/next-server.js (138:923) 异步 DevServer.renderToHTML file:///C:/projects/my-project.io/web-apps/my-project/node_modules/next/dist/server/next-dev-server.js (35:578) 异步 DevServer.render file:///C:/projects/my-project.io/web-apps/my-project/node_modules/next/dist/next-server/server/next-server.js (75:236) 异步 Object.fn file:///C:/projects/my-project.io/web-apps/my-project/node_modules/next/dist/next-server/server/next-server.js (59:580) 异步 Router.execute file:///C:/projects/my-project.io/web-apps/my-project/node_modules/next/dist/next-server/server/router.js (25:67) 异步 DevServer.run file:///C:/projects/my-project.io/web-apps/my-project/node_modules/next/dist/next-server/server/next-server.js (69:1042) 异步 DevServer.handleRequest file:///C:/projects/my-project.io/web-apps/my-project/node_modules/next/dist/next-server/server/next-server.js (34:504)

我通过添加 getStaticPaths 函数使其工作

  export const getStaticProps = async ({ locale }) => ({
    props: {
      ...(await serverSideTranslations(locale, ["my-translation"])),
    },
  });
    
  export const getStaticPaths = async () => {
    return {
      paths: ["/my-translation/id"],
      fallback: true,
    };
  };

【问题讨论】:

  • 对于具有动态路由的页面,您需要使用getStaticPropsgetStaticPaths。我建议您阅读 getStaticPaths 文档。
  • 嗨,这个问题有什么进展吗?我的翻译也以同样的方式工作。但是我现在面临构建错误。这是我的主题stackoverflow.com/questions/67624322/…
  • 我不知道是否完全理解这个问题,但要在@juliomalves 的基础上构建,如果您通过语言环境并相应地调整路径,getStaticPaths 可以与 i18n 一起使用。见this topic
  • 感谢您的编辑。它有效

标签: reactjs next.js next-i18next


【解决方案1】:

我可以通过将语言环境添加到 getStaticPaths 函数来解决这个问题。

考虑到您的 id 是您的文件参数 ( [id].js ),我的工作方式如下所示:


import { serverSideTranslations } from "next-i18next/serverSideTranslations";
import { useRouter } from "next/router";
import { useTranslation } from "next-i18next";


const TranslatedPage = () => {
  const { t } = useTranslation("my-translation");
  const router = useRouter();
  const { id } = router.query;

  return (
      <div>
         {t("existing-translation-key-from-my-translation-json")}
      </div>
  );
};

export const getStaticPaths = async () => {
    return {
      paths: [
      { params: { type: "id" }, locale: "es" },
      { params: { type: "id" }, locale: "en" },
      ]
      fallback: true,
    };
  };

export async function getStaticProps(context) {
  return {
    props: {
      params: context.params,
      ...(await serverSideTranslations(context.locale, ["my-translation"])),
    },
  }
}

之后,您不需要有两个文件夹,只需保留 pages/translated-page/[id].tsx (考虑到您只有动态路由)。

如果您有很多路线或语言,请考虑运行一个小函数来简单地将 {locale:lang} 添加到您的所有路径中。

【讨论】:

    猜你喜欢
    • 2020-06-07
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 2020-04-25
    • 2021-08-14
    • 1970-01-01
    • 2020-09-01
    相关资源
    最近更新 更多