I have a dynamically routed page in Nextjs (v10.1.3) /my-translation/[id], I'd like to use next-i18next (v8.1.3) package to translate this page.
I tried using 2 folder structure in Nextjs, they both give the same error that I can not grasp.
- pages/translated-page/[id]/index.tsx
- pages/translated-page/[id].tsx
However, if I change dynamic routing to static, the translation works.
Example for working folder structure:
- 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;
I get the error below for the dynamic route, I could not grasp from the provided link what I did wrong.
Server Error Error: getStaticPaths is required for dynamic SSG pages and is missing for '/translated-page/[id]'. Read more: https://nextjs.org/docs/messages/invalid-getstaticpaths-value This error happened while generating the page. Any console logs will be displayed in the terminal window. Call Stack 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 processTicksAndRejections internal/process/task_queues.js (93:5) async DevServer.renderToHTML file:///C:/projects/my-project.io/web-apps/my-project/node_modules/next/dist/next-server/server/next-server.js (138:923) async DevServer.renderToHTML file:///C:/projects/my-project.io/web-apps/my-project/node_modules/next/dist/server/next-dev-server.js (35:578) async DevServer.render file:///C:/projects/my-project.io/web-apps/my-project/node_modules/next/dist/next-server/server/next-server.js (75:236) async Object.fn file:///C:/projects/my-project.io/web-apps/my-project/node_modules/next/dist/next-server/server/next-server.js (59:580) async Router.execute file:///C:/projects/my-project.io/web-apps/my-project/node_modules/next/dist/next-server/server/router.js (25:67) async DevServer.run file:///C:/projects/my-project.io/web-apps/my-project/node_modules/next/dist/next-server/server/next-server.js (69:1042) async DevServer.handleRequest file:///C:/projects/my-project.io/web-apps/my-project/node_modules/next/dist/next-server/server/next-server.js (34:504)
I got it working by adding getStaticPaths function
export const getStaticProps = async ({ locale }) => ({
props: {
...(await serverSideTranslations(locale, ["my-translation"])),
},
});
export const getStaticPaths = async () => {
return {
paths: ["/my-translation/id"],
fallback: true,
};
};