intl done without libraries

This commit is contained in:
dqnid committed 2024-06-05 12:50:30 +02:00
1 parent 16ab570cb2
commit 315008a245
7 files changed
+81

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
import { NextRequest, NextResponse } from "next/server";
export const locales = ["es", "en", "fr"];
// Get the preferred locale, similar to the above or using a library
function getLocale() {
return locales[0];
}
export function middleware(request: NextRequest) {
// Check if there is any supported locale in the pathname
const { pathname } = request.nextUrl;
const pathnameHasLocale = locales.some(
(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`,
);
console.log(pathname);
if (pathname === "/") return;
if (pathnameHasLocale) return;
// Redirect if there is no locale
const locale = getLocale();
request.nextUrl.pathname = `/${locale}${pathname}`;
return NextResponse.redirect(request.nextUrl);
}
export const config = {
matcher: [
// Skip all internal paths (_next)
"/((?!_next).*)",
],
};