feat(i18n): enhance localization support with new languages(zh-tw/ja)

This commit is contained in:
beilunyang
2025-10-21 23:53:58 +08:00
parent 0c7a4d84a5
commit 7398b73f3f
20 changed files with 915 additions and 97 deletions

View File

@@ -1,6 +1,6 @@
import { auth } from "@/lib/auth"
import { NextResponse } from "next/server"
import { i18n } from "@/i18n/config"
import { i18n, type Locale } from "@/i18n/config"
import { PERMISSIONS } from "@/lib/permissions"
import { checkPermission } from "@/lib/auth"
import { Permission } from "@/lib/permissions"
@@ -63,7 +63,9 @@ export async function middleware(request: Request) {
const hasLocalePrefix = i18n.locales.includes(maybeLocale as any)
if (!hasLocalePrefix) {
const cookieLocale = request.headers.get('Cookie')?.match(/NEXT_LOCALE=([^;]+)/)?.[1]
const targetLocale = (cookieLocale && i18n.locales.includes(cookieLocale as any)) ? cookieLocale : i18n.defaultLocale
const acceptLanguage = request.headers.get('Accept-Language')
const preferredLocale = resolvePreferredLocale(cookieLocale, acceptLanguage)
const targetLocale = preferredLocale ?? i18n.defaultLocale
const redirectURL = new URL(`/${targetLocale}${pathname}${url.search}`, request.url)
return NextResponse.redirect(redirectURL)
}
@@ -71,6 +73,61 @@ export async function middleware(request: Request) {
return NextResponse.next()
}
function resolvePreferredLocale(cookieLocale: string | undefined, acceptLanguageHeader: string | null): Locale | null {
if (cookieLocale && i18n.locales.includes(cookieLocale as Locale)) {
return cookieLocale as Locale
}
if (!acceptLanguageHeader) return null
const candidates = parseAcceptLanguage(acceptLanguageHeader)
for (const lang of candidates) {
const match = matchLocale(lang)
if (match) {
return match
}
}
return null
}
function parseAcceptLanguage(header: string): string[] {
return header
.split(',')
.map((part) => {
const [lang, ...params] = part.trim().split(';')
const qualityParam = params.find((param) => param.trim().startsWith('q='))
const quality = qualityParam ? parseFloat(qualityParam.split('=')[1]) : 1
return { lang: lang.toLowerCase(), quality: isNaN(quality) ? 1 : quality }
})
.sort((a, b) => b.quality - a.quality)
.map((entry) => entry.lang)
}
function matchLocale(lang: string): Locale | null {
const exactMatch = i18n.locales.find((locale) => locale.toLowerCase() === lang)
if (exactMatch) return exactMatch
const base = lang.split('-')[0]
// Handle Chinese variants with explicit regions or scripts
if (base === 'zh') {
if (lang.includes('tw') || lang.includes('hk') || lang.includes('mo') || lang.includes('hant')) {
return 'zh-TW'
}
if (lang.includes('cn') || lang.includes('sg') || lang.includes('hans')) {
return 'zh-CN'
}
// default Chinese fallback
return 'zh-CN'
}
const baseMatch = i18n.locales.find((locale) => locale.toLowerCase().split('-')[0] === base)
if (baseMatch) return baseMatch
return null
}
export const config = {
matcher: [
'/((?!_next|.*\\..*).*)', // all pages excluding static assets
@@ -80,4 +137,4 @@ export const config = {
'/api/config/:path*',
'/api/api-keys/:path*',
]
}
}