♻️ Switch to turborepo (#532)

This commit is contained in:
Luke Vella 2023-03-01 14:10:06 +00:00 committed by GitHub
parent 41ef81aa75
commit 0a836aeec7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
419 changed files with 2300 additions and 2504 deletions

View file

@ -0,0 +1,57 @@
import languageParser from "accept-language-parser";
import { NextRequest, NextResponse } from "next/server";
const supportedLocales = [
"ca",
"cs",
"da",
"de",
"en",
"es",
"fi",
"fr",
"hu",
"hr",
"it",
"ko",
"nl",
"pl",
"pt-BR",
"pt",
"ru",
"sk",
"sv",
"zh",
];
export function middleware({ headers, cookies, nextUrl }: NextRequest) {
const newUrl = nextUrl.clone();
// Check if locale is specified in cookie
const localeCookie = cookies.get("NEXT_LOCALE");
if (localeCookie && supportedLocales.includes(localeCookie.value)) {
newUrl.pathname = `/${localeCookie.value}${newUrl.pathname}`;
return NextResponse.rewrite(newUrl);
} else {
// Check if locale is specified in header
const acceptLanguageHeader = headers.get("accept-language");
if (acceptLanguageHeader) {
const locale = languageParser.pick(
supportedLocales,
acceptLanguageHeader,
);
if (locale) {
newUrl.pathname = `/${locale}${newUrl.pathname}`;
return NextResponse.rewrite(newUrl);
}
}
}
return NextResponse.next();
}
export const config = {
matcher: ["/admin/:id*", "/demo", "/p/:id*", "/profile", "/new", "/login"],
};