import { NextResponse } from "next/server"; import type { NextRequest } from "next/server"; import { resolvePublicPath, toInternalPath, toPublicFromInternal, type Locale } from "./i18n/routes"; function localeFromPath(pathname: string): Locale { if (pathname === "/en" || pathname.startsWith("/en/")) return "en"; const resolved = resolvePublicPath(pathname); if (resolved) return resolved.locale; if (pathname === "/de" || pathname.startsWith("/de/")) return "de"; return "de"; } function withLocale(response: NextResponse, pathname: string) { response.headers.set("x-locale", localeFromPath(pathname)); return response; } export function proxy(request: NextRequest) { const { pathname } = request.nextUrl; if (pathname === "/de" || pathname.startsWith("/de/")) { const publicPath = toPublicFromInternal(pathname); if (publicPath && publicPath !== pathname) { const url = request.nextUrl.clone(); url.pathname = publicPath; return withLocale(NextResponse.redirect(url), publicPath); } } const internal = toInternalPath(pathname); if (internal && internal !== pathname) { const url = request.nextUrl.clone(); url.pathname = internal; return withLocale(NextResponse.rewrite(url), pathname); } return withLocale(NextResponse.next(), pathname); } export const config = { matcher: [ "/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp|ico|txt|xml)$).*)", ], };