-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.js
61 lines (55 loc) · 1.98 KB
/
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
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
import { match } from "@formatjs/intl-localematcher";
import { NextResponse } from "next/server";
import { i18n } from "./i18n-config";
let { locales, defaultLocale } = i18n;
// Get the preferred locale, similar to above or using a library
function getLocale() {
// These 2 lines are there to select automatically the language from browser lamnguage
// but we don't want that
// let languages = new Negotiator({
// headers: { "accept-language": request.headers.get("accept-language") },
// }).languages();
return match([], locales, defaultLocale); // -> 'en-US'
}
export function middleware(request) {
// Check if there is any supported locale in the pathname
const pathname = request.nextUrl.pathname;
const pathnameIsMissingLocale = locales.every((locale) => {
return !pathname.startsWith(`/${locale}/`) && pathname !== `/${locale}`;
});
// const pathnameLocale = pathname.split("/")[1];
// Redirect if there is no or wrong locale
if (pathnameIsMissingLocale) {
const locale = getLocale(request);
const paths = pathname.split("/");
const pathLocale = paths[1];
// Redirect to default langague when a wrong locale code is used
if (
pathLocale &&
/^[A-Za-z]{2,4}([_-][A-Za-z]{4})?([_-]([A-Za-z]{2}|[0-9]{3}))?$/.test(
pathLocale,
) &&
pathLocale !== "blog"
) {
paths.splice(1, 1);
const newPathname = paths.join("/");
// redirect to german if the locale path doesn'exist
return NextResponse.redirect(
new URL(`/${locale}${newPathname ? newPathname : "/"}`, request.url),
);
}
// e.g. incoming request is /products
// The new URL is now /en-US/products
return NextResponse.redirect(
new URL(`/${locale}/${pathname}`, request.url),
);
}
}
export const config = {
matcher: [
// Skip all internal paths (_next)
"/((?!api|_next/static|_next/image|favicon.ico|admin|sitemap|images|config.yml).*)",
// Optional: only run on root (/) URL
// '/'
],
};