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.
31 lines
838 B
31 lines
838 B
import { type NextRequest, NextResponse } from "next/server";
|
|
import { defaultLocale, isLocale } from "@/i18n/config";
|
|
|
|
function getPreferredLocale(request: NextRequest) {
|
|
const acceptLanguage = request.headers.get("accept-language") ?? "";
|
|
|
|
if (acceptLanguage.toLowerCase().includes("fa")) {
|
|
return "fa";
|
|
}
|
|
|
|
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).*)"],
|
|
};
|