-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.js
32 lines (25 loc) · 977 Bytes
/
middleware.js
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
import { NextResponse } from "next/server";
let locales = ['en', 'jp'];
// Get the preferred locale from the request headers
function getLocale(request) {
const acceptLanguage = request.headers.get('accept-language');
if (!acceptLanguage) return locales[0]; // default to first locale if none provided
const preferredLocales = acceptLanguage.split(',').map((lang) => lang.split(';')[0]);
return preferredLocales.find((locale) => locales.includes(locale)) || locales[0];
}
export function middleware(request) {
const { pathname } = request.nextUrl;
const pathnameHasLocale = locales.some(
(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
);
if (pathnameHasLocale) return;
const locale = getLocale(request);
request.nextUrl.pathname = `/${locale}${pathname}`;
return NextResponse.redirect(request.nextUrl);
}
export const config = {
matcher:
[
'/((?!api|static|.*\\..*|_next|favicon.ico|robots.txt).*)'
]
};