TypeScript 93.3%
JavaScript 4.4%
CSS 2.3%
1/**2 * Author: Simon-Pierre Boucher3 * Contact: contact@spboucher.ai4 * Project: Hilmacorp.ai — Web Platform5 * File: middleware.ts6 * Description: Locale-prefix middleware — redirects unprefixed paths to /fr or /en7 * based on the NEXT_LOCALE cookie or the Accept-Language header8 */910import { NextRequest, NextResponse } from "next/server";1112const LOCALES = ["fr", "en"];13const DEFAULT_LOCALE = "fr";1415function detectLocale(request: NextRequest): string {16 const cookie = request.cookies.get("NEXT_LOCALE")?.value;17 if (cookie && LOCALES.includes(cookie)) return cookie;18 const header = request.headers.get("accept-language") ?? "";19 for (const part of header.split(",")) {20 const code = part.split(";")[0].trim().slice(0, 2).toLowerCase();21 if (LOCALES.includes(code)) return code;22 }23 return DEFAULT_LOCALE;24}2526export function middleware(request: NextRequest) {27 const { pathname } = request.nextUrl;28 const hasLocale = LOCALES.some(29 (locale) => pathname === `/${locale}` || pathname.startsWith(`/${locale}/`)30 );31 if (hasLocale) return NextResponse.next();3233 const locale = detectLocale(request);34 const url = request.nextUrl.clone();35 url.pathname = `/${locale}${pathname === "/" ? "" : pathname}`;36 return NextResponse.redirect(url);37}3839export const config = {40 matcher: ["/((?!api|_next|favicon.ico|icon.svg|robots.txt|sitemap.xml|.*\\..*).*)"],41};42