-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
31 lines (24 loc) · 954 Bytes
/
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
import { NextRequest, NextResponse } from "next/server";
import { match } from "@formatjs/intl-localematcher";
import Negotiator from "negotiator";
let defaultLocale = "pt";
let locales = ["pt", "en", "es"];
function getLocale(request: NextRequest) {
const acceptedLanguage = request.headers.get("accept-language") ?? undefined;
let headers = { "accept-language": acceptedLanguage };
let languages = new Negotiator({ headers }).languages();
return match(languages, locales, defaultLocale);
}
export function middleware(request: NextRequest) {
const pathname = request.nextUrl.pathname;
const langMatch = pathname.match(/^\/(pt|en|es)/);
const lang = langMatch ? langMatch[1] : null;
if (!lang) {
const locale = getLocale(request);
const newUrl = new URL(`/${locale}${pathname}`, request.url);
return NextResponse.redirect(newUrl);
}
}
export const config = {
matcher: ["/((?!api|assets|docs|.*\\..*|_next).*)"],
};