-
Notifications
You must be signed in to change notification settings - Fork 15
/
i18nRouter.ts
165 lines (134 loc) · 4.11 KB
/
i18nRouter.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import defaultLocaleDetector from './localeDetector';
import validateConfig from './validateConfig';
import { NextRequest, NextResponse } from 'next/server';
import { Config } from './types';
function i18nRouter(request: NextRequest, config: Config): NextResponse {
if (!request) {
throw new Error(`i18nRouter requires a request argument.`);
}
if (!config) {
throw new Error(`i18nRouter requires a config argument`);
}
const {
locales,
defaultLocale,
localeCookie = 'NEXT_LOCALE',
localeDetector = defaultLocaleDetector,
prefixDefault = false,
basePath = '',
serverSetCookie = 'always',
noPrefix = false,
cookieOptions = {
path: request.nextUrl.basePath || undefined,
sameSite: 'strict',
maxAge: 31536000 // one year
}
} = config;
validateConfig(config);
const pathname = request.nextUrl.pathname;
const basePathTrailingSlash = basePath.endsWith('/');
const responseOptions = {
request: {
headers: new Headers(request.headers)
}
};
let response = NextResponse.next(responseOptions);
let cookieLocale;
// check cookie for locale
if (localeCookie) {
const cookieValue = request.cookies.get(localeCookie)?.value;
if (cookieValue && config.locales.includes(cookieValue)) {
cookieLocale = cookieValue;
}
}
const pathLocale = noPrefix
? undefined
: locales.find(
locale =>
pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
);
if (!pathLocale) {
let locale = cookieLocale;
// if no cookie, detect locale with localeDetector
if (!locale) {
if (localeDetector === false) {
locale = defaultLocale;
} else {
locale = localeDetector(request, config);
}
}
if (!locales.includes(locale)) {
console.warn(
'The localeDetector callback must return a locale included in your locales array. Reverting to using defaultLocale.'
);
locale = defaultLocale;
}
let newPath = `${locale}${pathname}`;
newPath = `${basePath}${basePathTrailingSlash ? '' : '/'}${newPath}`;
if (request.nextUrl.search) {
newPath += request.nextUrl.search;
}
if (noPrefix) {
response = NextResponse.rewrite(
new URL(newPath, request.url),
responseOptions
);
} else if (prefixDefault || locale !== defaultLocale) {
return NextResponse.redirect(new URL(newPath, request.url));
} else {
// prefixDefault is false and using default locale
response = NextResponse.rewrite(
new URL(newPath, request.url),
responseOptions
);
}
} else {
if (cookieLocale && cookieLocale !== pathLocale) {
// if always, do not redirect to cookieLocale
if (serverSetCookie !== 'always') {
let newPath = pathname.replace(`/${pathLocale}`, `/${cookieLocale}`);
if (request.nextUrl.search) {
newPath += request.nextUrl.search;
}
if (basePathTrailingSlash) {
newPath = newPath.slice(1);
}
newPath = `${basePath}${newPath}`;
response = NextResponse.redirect(new URL(newPath, request.url));
}
}
// If /default, redirect to /
if (!prefixDefault && pathLocale === defaultLocale) {
let pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) || '/';
if (basePathTrailingSlash) {
pathWithoutLocale = pathWithoutLocale.slice(1);
}
if (request.nextUrl.search) {
pathWithoutLocale += request.nextUrl.search;
}
response = NextResponse.redirect(
new URL(`${basePath}${pathWithoutLocale}`, request.url)
);
}
const setCookie = () => {
response.cookies.set(localeCookie, pathLocale, cookieOptions);
};
if (serverSetCookie !== 'never') {
if (
cookieLocale &&
cookieLocale !== pathLocale &&
serverSetCookie === 'always'
) {
setCookie();
} else if (!cookieLocale) {
setCookie();
}
}
}
response.headers.set(
'x-next-i18n-router-locale',
pathLocale || defaultLocale
);
return response;
}
export default i18nRouter;