-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
73 lines (62 loc) · 2.09 KB
/
middleware.ts
File metadata and controls
73 lines (62 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { defaultLocale, locales, type Locale } from './lib/i18n'
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
// 检查路径是否以语言代码开头
const pathnameHasLocale = locales.some(
(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
)
// 如果路径已经包含语言前缀,直接通过
if (pathnameHasLocale) {
return NextResponse.next()
}
// 排除静态文件和 API 路由
if (
pathname.startsWith('/_next') ||
pathname.startsWith('/api') ||
pathname.startsWith('/static') ||
pathname.includes('.') // 文件扩展名
) {
return NextResponse.next()
}
// 尝试从 cookie 或 Accept-Language header 获取语言偏好
let locale: Locale = defaultLocale
// 首先检查 cookie
const cookieLocale = request.cookies.get('locale')?.value
if (cookieLocale && locales.includes(cookieLocale as Locale)) {
locale = cookieLocale as Locale
} else {
// 检查 Accept-Language header
const acceptLanguage = request.headers.get('accept-language')
if (acceptLanguage) {
// 简单检测:如果包含 'zh' 则使用中文,否则使用英文
if (acceptLanguage.toLowerCase().includes('zh')) {
locale = 'zh'
}
}
}
// 重定向到带语言前缀的路径
const newUrl = request.nextUrl.clone()
newUrl.pathname = `/${locale}${pathname === '/' ? '' : pathname}`
// 设置 cookie 以记住用户偏好
const response = NextResponse.redirect(newUrl)
response.cookies.set('locale', locale, {
path: '/',
maxAge: 60 * 60 * 24 * 365, // 1 year
})
return response
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - static files (images, etc.)
*/
'/((?!api|_next/static|_next/image|favicon.ico|.*\\.).*)',
],
}