mirror of
https://github.com/Steffo99/todocolors.git
synced 2024-11-25 17:54:18 +00:00
24 lines
893 B
TypeScript
24 lines
893 B
TypeScript
import {AVAILABLE_LOCALES} from "@/app/(i18n)/(locales)"
|
|
import Negotiator from "negotiator"
|
|
import {NextRequest, NextResponse} from "next/server"
|
|
|
|
|
|
export function middleware(request: NextRequest) {
|
|
// https://nextjs.org/docs/app/building-your-application/routing/internationalization
|
|
const pathname = request.nextUrl.pathname
|
|
const pathnameIsMissingLocale = AVAILABLE_LOCALES.every(
|
|
(locale) => !pathname.startsWith(`/${locale}/`) && pathname !== `/${locale}`
|
|
)
|
|
if(!pathnameIsMissingLocale) {
|
|
return NextResponse.next()
|
|
}
|
|
const negotiator = new Negotiator(request as any)
|
|
const bestLocale = negotiator.language(AVAILABLE_LOCALES)
|
|
return NextResponse.rewrite(`${request.nextUrl.protocol}//${request.nextUrl.host}/${bestLocale}${pathname}`)
|
|
}
|
|
|
|
export const config = {
|
|
matcher: [
|
|
'/((?!api|_next|manifest.json|logo-[nw]bg-[0-9]*.png|favicon-[nw]bg.ico).*)',
|
|
]
|
|
}
|