Open ivanzusko opened 1 month ago
@ivanzusko
Current Behavior As you've observed, setting the defaultLocale to "de" still results in a redirection to /en when accessing the root URL (http://localhost:3000/).
Explanation Next.js's internationalization (i18n) routing system is designed to prioritize the user's browser Accept-Language header over the defaultLocale. This means that if the browser is set to prefer English (en), Next.js will redirect to /en even if defaultLocale is set to another language like German (de).
This behavior ensures that users receive content in their preferred language whenever possible. However, we understand that there are scenarios where you might want to prioritize the defaultLocale regardless of the browser settings.
If you prefer to always redirect to the defaultLocale (e.g., "de") regardless of the user's browser settings, you can customize the middleware to achieve this behavior. Below is a sample implementation:
// middleware.ts
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { i18n } from "./i18n-config";
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// パス名に既にサポートされているロケールが含まれているか確認
if (i18n.locales.some(locale => pathname.startsWith(`/${locale}`))) {
return NextResponse.next();
}
// デフォルトロケールにリダイレクト
const locale = i18n.defaultLocale;
return NextResponse.redirect(new URL(`/${locale}${pathname}`, request.url));
}
export const config = {
// 公開アセットを除くすべてのルートにミドルウェアを適用
matcher: '/:path*',
};
@R1013-T
This behavior ensures that users receive content in their preferred language whenever possible. However, we understand that there are scenarios where you might want to prioritize the defaultLocale regardless of the browser settings.
I would say on one hand it makes total sense 👍
But at the same time it isn't clear from the very beginning. I guess it could be 2 types of behaviour:
defaultLocale
.defaultLocale
over the browser header.But if the 1st issue can be somehow reasoned, the second issue with not respecting user selection is a bit more critical I would say. If the user explicitly changes the locale, it must be respected and preserved.
Verify canary release
Provide environment information
Which example does this report relate to?
app-dir-i18n-routing
What browser are you using? (if relevant)
Chrome 128.0.6613.119
How are you deploying your application? (if relevant)
No response
Describe the Bug
dafaultLocale
is not being respected (setting this parameter to anything will still bring/en
in the url);/en
after changing it to something different(egde
) and navigating to the different page.Expected Behavior
defaultLocale
and i can see it by default.To Reproduce
Issue 1 (
defaultLocale
is not being respected)Step 1
Go to the https://github.com/vercel/next.js/blob/canary/examples/app-dir-i18n-routing/i18n-config.ts#L2 and change
defaultLocale
tode
orcs
.Step 2
Start application.
Step 3
You will see
http://localhost:3000/en
instead ofhttp://localhost:3000/de
.Issue 2 (locale isn't preserved after changing it and navigating to a different page)
Step1
Add new page under
[lang]
folder:Step 2
Add
Navigation
component:Step 3
Include
Navigation
to the main page:Step 4
Start the application
Step 5
Change locale from
en
tode
(orcs
)Step 6
In the navigation click on
johnny
Step 7
The redirection will be happen to http://localhost:3000/en/johnny (instead of http://localhost:3000/de/johnny).