diff --git a/src/middleware.ts b/src/middleware.ts index 49667d6..60d5455 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -14,8 +14,28 @@ function getLocale(request: NextRequest): string { negotiatorHeaders[key] = value; }); - const languages = new Negotiator({ headers: negotiatorHeaders }).languages(); - return match(languages, i18n.locales, i18n.defaultLocale); + try { + const languages = new Negotiator({ headers: negotiatorHeaders }).languages(); + // Filter out invalid locale values that would cause Intl.getCanonicalLocales to fail + const validLanguages = languages.filter((lang) => { + try { + Intl.getCanonicalLocales(lang); + return true; + } catch { + return false; + } + }); + + if (validLanguages.length === 0) { + return i18n.defaultLocale; + } + + return match(validLanguages, i18n.locales, i18n.defaultLocale); + } catch (error) { + // Fallback to default locale if locale negotiation fails + console.error('Locale negotiation error:', error); + return i18n.defaultLocale; + } } export function middleware(request: NextRequest) {