|
1 | | -import { createI18nMiddleware } from 'fumadocs-core/i18n/middleware'; |
| 1 | +import { NextRequest, NextResponse } from 'next/server'; |
| 2 | +import Negotiator from 'negotiator'; |
2 | 3 | import { i18n } from '@/lib/i18n'; |
3 | 4 |
|
| 5 | +const LOCALE_COOKIE = 'FD_LOCALE'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Language code mapping |
| 9 | + * Maps browser language codes to our supported language codes |
| 10 | + */ |
| 11 | +const LANGUAGE_MAPPING: Record<string, string> = { |
| 12 | + 'zh': 'cn', // Chinese -> cn |
| 13 | + 'zh-CN': 'cn', // Chinese (China) -> cn |
| 14 | + 'zh-TW': 'cn', // Chinese (Taiwan) -> cn |
| 15 | + 'zh-HK': 'cn', // Chinese (Hong Kong) -> cn |
| 16 | +}; |
| 17 | + |
| 18 | +/** |
| 19 | + * Normalize language code to match our supported languages |
| 20 | + */ |
| 21 | +function normalizeLanguage(lang: string): string { |
| 22 | + // Check direct mapping first |
| 23 | + if (LANGUAGE_MAPPING[lang]) { |
| 24 | + return LANGUAGE_MAPPING[lang]; |
| 25 | + } |
| 26 | + |
| 27 | + // Check if the base language (without region) is mapped |
| 28 | + const baseLang = lang.split('-')[0]; |
| 29 | + if (LANGUAGE_MAPPING[baseLang]) { |
| 30 | + return LANGUAGE_MAPPING[baseLang]; |
| 31 | + } |
| 32 | + |
| 33 | + return lang; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Get the preferred language from the request |
| 38 | + */ |
| 39 | +function getPreferredLanguage(request: NextRequest): string { |
| 40 | + // Check cookie first |
| 41 | + const cookieLocale = request.cookies.get(LOCALE_COOKIE)?.value; |
| 42 | + if (cookieLocale && (i18n.languages as readonly string[]).includes(cookieLocale)) { |
| 43 | + return cookieLocale; |
| 44 | + } |
| 45 | + |
| 46 | + // Then check Accept-Language header |
| 47 | + const negotiatorHeaders: Record<string, string> = {}; |
| 48 | + request.headers.forEach((value, key) => { |
| 49 | + negotiatorHeaders[key] = value; |
| 50 | + }); |
| 51 | + |
| 52 | + const negotiator = new Negotiator({ headers: negotiatorHeaders }); |
| 53 | + const browserLanguages = negotiator.languages(); |
| 54 | + |
| 55 | + // Normalize browser languages to match our supported languages |
| 56 | + const normalizedLanguages = browserLanguages.map(normalizeLanguage); |
| 57 | + |
| 58 | + // Find the first match |
| 59 | + for (const lang of normalizedLanguages) { |
| 60 | + if ((i18n.languages as readonly string[]).includes(lang)) { |
| 61 | + return lang; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return i18n.defaultLanguage; |
| 66 | +} |
| 67 | + |
4 | 68 | /** |
5 | 69 | * Middleware for automatic language detection and redirection |
6 | 70 | * |
7 | 71 | * This middleware: |
8 | 72 | * - Detects the user's preferred language from browser settings or cookies |
9 | 73 | * - Redirects users to the appropriate localized version |
| 74 | + * - For default language (en): keeps URL as "/" (with internal rewrite) |
| 75 | + * - For other languages (cn): redirects to "/cn/" |
10 | 76 | * - Stores language preference as a cookie |
11 | 77 | */ |
12 | | -export default createI18nMiddleware(i18n); |
| 78 | +export default function middleware(request: NextRequest) { |
| 79 | + const { pathname } = request.nextUrl; |
| 80 | + |
| 81 | + // Check if the pathname already has a locale |
| 82 | + const pathnameHasLocale = i18n.languages.some( |
| 83 | + (locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}` |
| 84 | + ); |
| 85 | + |
| 86 | + if (pathnameHasLocale) { |
| 87 | + // Extract the locale from the pathname |
| 88 | + const locale = pathname.split('/')[1]; |
| 89 | + |
| 90 | + // If it's the default locale and hideLocale is 'default-locale', redirect to remove locale prefix |
| 91 | + if (locale === i18n.defaultLanguage && i18n.hideLocale === 'default-locale') { |
| 92 | + const url = new URL(request.url); |
| 93 | + url.pathname = pathname.replace(`/${i18n.defaultLanguage}`, '') || '/'; |
| 94 | + const response = NextResponse.redirect(url); |
| 95 | + response.cookies.set(LOCALE_COOKIE, locale); |
| 96 | + return response; |
| 97 | + } |
| 98 | + |
| 99 | + return NextResponse.next(); |
| 100 | + } |
| 101 | + |
| 102 | + // Pathname doesn't have a locale, determine preferred language |
| 103 | + const preferredLanguage = getPreferredLanguage(request); |
| 104 | + |
| 105 | + // If preferred language is the default, rewrite internally (keep URL clean) |
| 106 | + if (preferredLanguage === i18n.defaultLanguage && i18n.hideLocale === 'default-locale') { |
| 107 | + const url = new URL(request.url); |
| 108 | + url.pathname = `/${i18n.defaultLanguage}${pathname}`; |
| 109 | + return NextResponse.rewrite(url); |
| 110 | + } |
| 111 | + |
| 112 | + // For non-default languages, redirect to the localized path |
| 113 | + const url = new URL(request.url); |
| 114 | + url.pathname = `/${preferredLanguage}${pathname}`; |
| 115 | + const response = NextResponse.redirect(url); |
| 116 | + response.cookies.set(LOCALE_COOKIE, preferredLanguage); |
| 117 | + return response; |
| 118 | +} |
13 | 119 |
|
14 | 120 | export const config = { |
15 | 121 | // Match all routes except: |
|
0 commit comments