-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmiddleware.ts
More file actions
145 lines (132 loc) · 3.74 KB
/
middleware.ts
File metadata and controls
145 lines (132 loc) · 3.74 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { supportedLanguages, defaultLanguage, type Language } from "./app/lib/i18n";
// Browser language to supported language mapping
const browserLangMap: Record<string, Language> = {
// Russian Federation - 26%
ru: "ru",
"ru-RU": "ru",
// Japan - 17%
ja: "ja",
"ja-JP": "ja",
// USA / English - 15%
en: "en",
"en-US": "en",
"en-GB": "en",
"en-AU": "en",
"en-CA": "en",
// South Korea - 11%
ko: "ko",
"ko-KR": "ko",
// Netherlands - 7%
nl: "nl",
"nl-NL": "nl",
"nl-BE": "nl",
// Germany - 3%
de: "de",
"de-DE": "de",
"de-AT": "de",
"de-CH": "de",
// Chinese
zh: "zh",
"zh-CN": "zh",
"zh-TW": "zh",
"zh-HK": "zh",
// French
fr: "fr",
"fr-FR": "fr",
"fr-CA": "fr",
"fr-BE": "fr",
"fr-CH": "fr",
// Spanish
es: "es",
"es-ES": "es",
"es-MX": "es",
"es-AR": "es",
// Italian
it: "it",
"it-IT": "it",
// Portuguese
pt: "pt",
"pt-BR": "pt",
"pt-PT": "pt",
// Arabic
ar: "ar",
"ar-SA": "ar",
"ar-AE": "ar",
"ar-EG": "ar",
// Hindi
hi: "hi",
"hi-IN": "hi",
};
function getPreferredLanguage(request: NextRequest): Language {
// Check Accept-Language header
const acceptLanguage = request.headers.get("accept-language");
if (acceptLanguage) {
const languages = acceptLanguage
.split(",")
.map((lang) => {
const [code, priority] = lang.trim().split(";q=");
return {
code: code.trim(),
priority: priority ? parseFloat(priority) : 1,
};
})
.sort((a, b) => b.priority - a.priority);
for (const { code } of languages) {
// Try exact match first
if (browserLangMap[code]) {
return browserLangMap[code];
}
// Try language code without region
const langCode = code.split("-")[0];
if (browserLangMap[langCode]) {
return browserLangMap[langCode];
}
}
}
return defaultLanguage;
}
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Skip for static files, API routes, and Next.js internals
if (
pathname.startsWith("/_next") ||
pathname.startsWith("/api") ||
pathname.includes(".") ||
pathname === "/favicon.ico" ||
pathname === "/robots.txt" ||
pathname === "/sitemap.xml"
) {
return NextResponse.next();
}
// Check if pathname already has a valid language prefix
const pathnameSegments = pathname.split("/").filter(Boolean);
const firstSegment = pathnameSegments[0];
if (firstSegment && supportedLanguages.includes(firstSegment as Language)) {
// Language is already in the path, continue
return NextResponse.next();
}
// Handle legacy query parameter URLs (redirect to new path-based URLs)
const langParam = request.nextUrl.searchParams.get("lang");
if (langParam && supportedLanguages.includes(langParam as Language)) {
const newUrl = new URL(`/${langParam}`, request.url);
return NextResponse.redirect(newUrl, { status: 301 });
}
// Root path or path without language - redirect to appropriate language
if (pathname === "/" || !firstSegment) {
const preferredLanguage = getPreferredLanguage(request);
const newUrl = new URL(`/${preferredLanguage}`, request.url);
return NextResponse.redirect(newUrl, { status: 307 });
}
// For other paths without language prefix, redirect to default language
const preferredLanguage = getPreferredLanguage(request);
const newUrl = new URL(`/${preferredLanguage}${pathname}`, request.url);
return NextResponse.redirect(newUrl, { status: 307 });
}
export const config = {
matcher: [
// Match all paths except static files and API
"/((?!_next/static|_next/image|favicon.ico|.*\\..*|api).*)",
],
};