'多语言'
This commit is contained in:
28
proxy.ts
Normal file
28
proxy.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
const LOCALES = ["zh", "en"] as const;
|
||||
const DEFAULT_LOCALE = "zh";
|
||||
|
||||
function proxy(request: NextRequest) {
|
||||
const pathname = request.nextUrl.pathname;
|
||||
|
||||
// 检查路径是否已有 locale 前缀
|
||||
const hasLocale = LOCALES.some(
|
||||
(loc) => pathname === `/${loc}` || pathname.startsWith(`/${loc}/`)
|
||||
);
|
||||
|
||||
if (hasLocale) {
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
// 根路径或无效路径,重定向到默认 locale
|
||||
const url = request.nextUrl.clone();
|
||||
url.pathname = `/${DEFAULT_LOCALE}${pathname === "/" ? "" : pathname}`;
|
||||
return NextResponse.redirect(url);
|
||||
}
|
||||
|
||||
export { proxy };
|
||||
|
||||
export const config = {
|
||||
matcher: ["/((?!api|trpc|_next|_vercel|.*\\..*).*)"],
|
||||
};
|
||||
Reference in New Issue
Block a user