-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.js
More file actions
58 lines (48 loc) · 1.65 KB
/
Copy pathproxy.js
File metadata and controls
58 lines (48 loc) · 1.65 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
import { NextResponse } from "next/server";
const locales = ['tr', 'en', 'es', 'fr', 'de', 'pt', 'it', 'ru', 'ja', 'ar', 'id'];
const defaultLocale = 'en';
export function proxy(request) {
// Check if there is any supported locale in the pathname
const { pathname } = request.nextUrl;
// Exclude API routes, static files, and Next.js internal paths
if (
pathname.startsWith('/api/') ||
pathname.startsWith('/_next/') ||
pathname.includes('.')
) {
return;
}
const pathnameHasLocale = locales.some(
(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
);
if (pathnameHasLocale) return;
// Get locale from cookie if exists
let locale = defaultLocale;
const cookieLocale = request.cookies.get('oxiron_lang')?.value;
if (cookieLocale && locales.includes(cookieLocale)) {
locale = cookieLocale;
} else {
// Basic accept-language matching
const acceptLanguage = request.headers.get('accept-language') || '';
const preferredLocales = acceptLanguage.split(',').map(lang => lang.split(';')[0].split('-')[0].trim());
for (const prefLocale of preferredLocales) {
if (locales.includes(prefLocale)) {
locale = prefLocale;
break;
}
}
}
// Redirect if there is no locale
request.nextUrl.pathname = `/${locale}${pathname === '/' ? '' : pathname}`;
const response = NextResponse.redirect(request.nextUrl);
// Optional: Set cookie if it was automatically detected
if (!cookieLocale) {
response.cookies.set('oxiron_lang', locale);
}
return response;
}
export const config = {
matcher: [
'/((?!api|_next/static|_next/image|favicon.ico).*)',
],
};