diff --git a/src/components/internationalization/middleware.ts b/src/components/internationalization/middleware.ts deleted file mode 100644 index 59cd5c4..0000000 --- a/src/components/internationalization/middleware.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { match } from '@formatjs/intl-localematcher'; -import Negotiator from 'negotiator'; -import { type NextRequest, NextResponse } from 'next/server'; -import { i18n } from './config'; - -function getLocale(request: NextRequest) { - // 1. Check cookie first for user preference - const cookieLocale = request.cookies.get('NEXT_LOCALE')?.value; - if (cookieLocale && i18n.locales.includes(cookieLocale as any)) { - return cookieLocale; - } - - // 2. Get Accept-Language header - const headers = { - 'accept-language': request.headers.get('accept-language') ?? '', - }; - - // Use negotiator to parse preferred languages - const languages = new Negotiator({ headers }).languages(); - - // Match against supported locales - return match(languages, i18n.locales, i18n.defaultLocale); -} - -export function localizationMiddleware(request: NextRequest) { - const { pathname } = request.nextUrl; - - // Check if pathname already has a locale - const pathnameHasLocale = i18n.locales.some( - (locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}` - ); - - // If locale exists in URL, continue - if (pathnameHasLocale) { - return NextResponse.next(); - } - - // Get best matching locale - const locale = getLocale(request); - - // Redirect to localized URL - request.nextUrl.pathname = `/${locale}${pathname}`; - const response = NextResponse.redirect(request.nextUrl); - - // Set cookie for future visits - response.cookies.set('NEXT_LOCALE', locale, { - maxAge: 365 * 24 * 60 * 60, // 1 year - sameSite: 'lax', - secure: process.env.NODE_ENV === 'production', - }); - - return response; -} \ No newline at end of file diff --git a/validate-i18n.js b/validate-i18n.js index 10d6349..c74e84d 100644 --- a/validate-i18n.js +++ b/validate-i18n.js @@ -13,11 +13,6 @@ const checks = [ path: 'src/components/internationalization/config.ts', required: true }, - { - name: 'V2.0 Middleware', - path: 'src/components/internationalization/middleware.ts', - required: true - }, { name: 'V2.0 Dictionaries', path: 'src/components/internationalization/dictionaries.ts', @@ -44,8 +39,8 @@ const checks = [ required: true }, { - name: 'Root Middleware', - path: 'middleware.ts', + name: 'Root Proxy (Next 16)', + path: 'proxy.ts', required: true }, { @@ -87,16 +82,18 @@ try { allPassed = false; } -// Check middleware content +// Check proxy content (Next 16 replaces middleware.ts) try { - const middlewarePath = path.join(process.cwd(), 'middleware.ts'); - const middlewareContent = fs.readFileSync(middlewarePath, 'utf8'); - - const usesNewStructure = middlewareContent.includes('./src/components/internationalization/middleware'); - console.log(`${usesNewStructure ? '✅' : '❌'} Root middleware uses new structure`); - + const proxyPath = path.join(process.cwd(), 'proxy.ts'); + const proxyContent = fs.readFileSync(proxyPath, 'utf8'); + + const importsConfig = proxyContent.includes("./src/components/internationalization/config"); + const exportsProxy = /export\s+function\s+proxy\b/.test(proxyContent); + console.log(`${importsConfig ? '✅' : '❌'} proxy.ts imports from internationalization/config`); + console.log(`${exportsProxy ? '✅' : '❌'} proxy.ts exports a named proxy() function`); + } catch (error) { - console.log('❌ Could not validate middleware content'); + console.log('❌ Could not validate proxy content'); allPassed = false; }