You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1.1 KiB
36 lines
1.1 KiB
import { type NextRequest, NextResponse } from "next/server";
|
|
import { defaultLocale, isLocale } from "@/translations/config";
|
|
|
|
function getPreferredLocale(request: NextRequest) {
|
|
const cookieLocale =
|
|
request.cookies.get("HABIB_LANGUAGE")?.value ??
|
|
request.cookies.get("habib_language")?.value;
|
|
const acceptLanguage = request.headers.get("accept-language") ?? "";
|
|
const acceptedLocale = acceptLanguage.split(",")[0]?.split("-")[0];
|
|
|
|
for (const locale of [cookieLocale, acceptedLocale]) {
|
|
const normalizedLocale = locale?.trim().toLowerCase();
|
|
if (isLocale(normalizedLocale)) return normalizedLocale;
|
|
}
|
|
|
|
return defaultLocale;
|
|
}
|
|
|
|
export function proxy(request: NextRequest) {
|
|
const { pathname } = request.nextUrl;
|
|
const pathnameHasLocale = isLocale(pathname.split("/")[1]);
|
|
|
|
if (pathnameHasLocale) {
|
|
return NextResponse.next();
|
|
}
|
|
|
|
const locale = getPreferredLocale(request);
|
|
const url = request.nextUrl.clone();
|
|
url.pathname = `/${locale}${pathname === "/" ? "" : pathname}`;
|
|
|
|
return NextResponse.redirect(url);
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ["/((?!api|_next|favicon.ico|assets|fonts).*)"],
|
|
};
|