-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
42 lines (33 loc) · 1.28 KB
/
middleware.ts
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
import Negotiator from 'negotiator'
import { type NextRequest, NextResponse } from 'next/server';
import { supportedLangs } from '@app/config';
import { matchLocale, splitPath } from '@lib/router';
const getLocale = (req: NextRequest) => {
const headers = { 'accept-language': req.headers.get('accept-language') || '' };
const languages = new Negotiator({ headers }).languages();
return matchLocale(languages);
};
export function middleware(request: NextRequest) {
// Check for supported language code in path
const { pathname } = request.nextUrl;
const path = splitPath(pathname);
const foundLang = supportedLangs.find((lang) => path.locale === lang);
const locale = getLocale(request);
if (foundLang) {
return;
}
// Detect unknown language code
if (/^[a-z]{2}$/.test(path.locale)) {
// request.nextUrl.pathname = '/en/language';
// request.nextUrl.search = `?lang=${pageLang}`;
request.nextUrl.pathname = `/${locale}${path.page}`;
} else {
request.nextUrl.pathname = `/${locale}${pathname}`;
}
return NextResponse.redirect(request.nextUrl);
};
export const config = {
matcher: [
'/((?!_next/images/|_next/static/|images/|api/|brandbook|revalidate|favicon.ico|apple-icon.png|robots.txt|humans.txt|sitemap.xml|manifest.webmanifest).*)',
],
};