Closed chhornponleu closed 1 year ago
I experience the same behaviour with dynamic segments and next-translate-plugin@2.0.2
:
// i18n.js
module.exports = {
locales: ['en', 'pl'],
defaultLocale: 'en',
defaultNS: 'dashboard',
pages: {
'*': ['dashboard', 'auth', 'elements'],
'/login': ['auth'],
},
};
// src/app/clients/[id]/page.tsx
import { loadTenant } from '../../../app-utils/server-utils';
import { loadClient } from '../../../app-utils/api';
import { ClientView } from '../../../app-pages/ClientView';
export default async function ClientPage({ params }: { params: { id: string } }) {
const tenant = await loadTenant();
const client = await loadClient(params.id, tenant?.token ?? '');
return <ClientView client={client} />;
}
When using next-translate-plugin@2.0.2
I get 404 error when trying to render /clients/some-client-id
route
Thanks to report it. I'm going to prioritize it
BTW; try to use the next-translate & next-translate-plugin in the same version. The last version of the next-translate-plugin is 2.0.2
I can confirm that the error does also happen with next-translate
and next-translate-plugin
both in version 2.0.2
I realize that without next-translate is not working either. With i18n
routing and appDir
:
next.config.js
module.exports = {
experimental: { appDir: true },
i18n: {
locales: ['en', 'ca', 'es'],
defaultLocale: 'en'
}
}
Removing the i18n
"works"... 🤔
module.exports = {
experimental: { appDir: true },
- i18n: {
- locales: ['en', 'ca', 'es'],
- defaultLocale: 'en'
- }
}
Related with this issue https://github.com/vercel/next.js/issues/46841
As a workaround meanwhile Next.js issue is not fixed, you can add this the middleware:
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import i18n from "./i18n";
export function middleware(request: NextRequest) {
const locale = request.nextUrl.locale || i18n.defaultLocale;
request.nextUrl.searchParams.set("lang", locale);
+ request.nextUrl.href = request.nextUrl.href.replace(`/${locale}`, "");
return NextResponse.rewrite(request.nextUrl);
}
Probably related (or maybe not) to this https://github.com/aralroca/next-translate/issues/993 and this https://github.com/aralroca/next-translate/issues/1005
I have made a PR to support /[lang]
dynamic routing in an easy way.
You can try it with these versions:
next-translate
- version 2.3.0-canary.3
next-translate-plugin
- version 2.3.0-canary.6
Things to keep in mind:
i18n.js
will have to be updated by adding the /[lang]
in front of them./app
pages inside /app/[lang]
.I updated the example of next-translate working with /app/[lang]
and Next.js 13.4:
both ways now are supported (?lang=
or /[lang]
), but the way using ?lang=
needs middleware to do the rewrite and as of Next.js 13.3 it no longer works well, so I've added a second option for people to use until the issue is resolved by the Next.js team.
Solved in 2.3
What version of this package are you using?
What operating system, Node.js, and npm version?
What happened?
I create dynamic url
[userId]
, and cannot access neither/use123
or/user123/profile
. It redirect to error 404 page.Checkout the repo I extracted from with-app-directory example
https://github.com/chhornponleu/next-translate-app-dir-demo