| import { NextResponse, type NextRequest } from "next/server"; |
| import { defaultLocale, locales } from "@/i18n/config"; |
|
|
| const PUBLIC_FILE = /\.(.*)$/; |
|
|
| function pickLocale(request: NextRequest): string { |
| const cookie = request.cookies.get("locale")?.value; |
| if (cookie && (locales as readonly string[]).includes(cookie)) return cookie; |
| const header = request.headers.get("accept-language") ?? ""; |
| for (const part of header.split(",")) { |
| const code = part.split(";")[0].trim().toLowerCase().split("-")[0]; |
| if ((locales as readonly string[]).includes(code)) return code; |
| } |
| return defaultLocale; |
| } |
|
|
| export function proxy(request: NextRequest) { |
| const { pathname } = request.nextUrl; |
| if ( |
| pathname.startsWith("/_next") || |
| pathname.startsWith("/api") || |
| pathname === "/favicon.ico" || |
| PUBLIC_FILE.test(pathname) |
| ) { |
| return; |
| } |
| const hasLocale = (locales as readonly string[]).some( |
| (l) => pathname === `/${l}` || pathname.startsWith(`/${l}/`) |
| ); |
| if (hasLocale) return; |
| const locale = pickLocale(request); |
| const url = request.nextUrl.clone(); |
| url.pathname = `/${locale}${pathname === "/" ? "" : pathname}`; |
| return NextResponse.redirect(url); |
| } |
|
|
| export const config = { |
| matcher: ["/((?!_next|api|.*\\..*).*)"], |
| }; |
|
|