site/src/middleware.ts
Dariusz Niemczyk 9a6d46a826
Cebula.
2025-02-09 00:47:59 +01:00

34 lines
1 KiB
TypeScript

import { match } from "@formatjs/intl-localematcher";
import Negotiator from "negotiator";
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
const locales = ["en", "pl"];
const defaultLocale = "pl";
function getLocale(request: NextRequest) {
const headers = {
"accept-language": request.headers.get("accept-language") || "",
};
const languages = new Negotiator({ headers }).languages();
return match(languages, locales, defaultLocale);
}
export function middleware(request: NextRequest) {
const pathname = request.nextUrl.pathname;
const pathnameIsMissingLocale = locales.every(
(locale) => !pathname.startsWith(`/${locale}/`) && pathname !== `/${locale}`
);
if (pathnameIsMissingLocale) {
const locale = getLocale(request);
return NextResponse.redirect(new URL(`/${locale}${pathname}`, request.url));
}
}
export const config = {
matcher: "/((?!api|static|.*\\..*|_next).*)",
// matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};