gt-next 5.2.8 → 5.2.9-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_routing.d.ts +3 -0
- package/dist/_routing.d.ts.map +1 -0
- package/dist/_routing.js +6 -0
- package/dist/_routing.js.map +1 -0
- package/dist/config-dir/TranslationManager.d.ts.map +1 -1
- package/dist/config-dir/TranslationManager.js.map +1 -1
- package/dist/config-dir/gt-router.d.ts +2 -0
- package/dist/config-dir/gt-router.d.ts.map +1 -0
- package/dist/config-dir/gt-router.js +8 -0
- package/dist/config-dir/gt-router.js.map +1 -0
- package/dist/config-dir/loadTranslation.d.ts.map +1 -1
- package/dist/config-dir/loadTranslation.js +1 -1
- package/dist/config-dir/loadTranslation.js.map +1 -1
- package/dist/config-dir/props/defaultWithGTConfigProps.d.ts +2 -2
- package/dist/config.d.ts +2 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +41 -14
- package/dist/config.js.map +1 -1
- package/dist/errors/createErrors.d.ts +1 -0
- package/dist/errors/createErrors.d.ts.map +1 -1
- package/dist/errors/createErrors.js +5 -1
- package/dist/errors/createErrors.js.map +1 -1
- package/dist/middleware-dir/createNextMiddleware.d.ts +6 -6
- package/dist/middleware-dir/createNextMiddleware.d.ts.map +1 -1
- package/dist/middleware-dir/createNextMiddleware.js +169 -76
- package/dist/middleware-dir/createNextMiddleware.js.map +1 -1
- package/dist/middleware-dir/utils.d.ts +43 -0
- package/dist/middleware-dir/utils.d.ts.map +1 -0
- package/dist/middleware-dir/utils.js +166 -0
- package/dist/middleware-dir/utils.js.map +1 -0
- package/dist/provider/ClientProviderWrapper.d.ts.map +1 -1
- package/dist/provider/ClientProviderWrapper.js +22 -1
- package/dist/provider/ClientProviderWrapper.js.map +1 -1
- package/dist/request/getLocale.d.ts.map +1 -1
- package/dist/request/getLocale.js +4 -1
- package/dist/request/getLocale.js.map +1 -1
- package/dist/server-dir/buildtime/T.d.ts.map +1 -1
- package/dist/server-dir/buildtime/T.js +4 -1
- package/dist/server-dir/buildtime/T.js.map +1 -1
- package/dist/server-dir/buildtime/getGT.d.ts.map +1 -1
- package/dist/server-dir/buildtime/getGT.js +18 -2
- package/dist/server-dir/buildtime/getGT.js.map +1 -1
- package/dist/utils/constants.d.ts +4 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/constants.js +7 -0
- package/dist/utils/constants.js.map +1 -0
- package/dist/variables/Currency.js +1 -1
- package/dist/variables/Currency.js.map +1 -1
- package/dist/variables/DateTime.js +12 -1
- package/dist/variables/DateTime.js.map +1 -1
- package/dist/variables/Num.js +12 -1
- package/dist/variables/Num.js.map +1 -1
- package/package.json +14 -5
- package/dist/translations/index.d.ts +0 -1
- package/dist/translations/index.d.ts.map +0 -1
- package/dist/translations/index.js +0 -2
- package/dist/translations/index.js.map +0 -1
|
@@ -5,16 +5,8 @@ var generaltranslation_1 = require("generaltranslation");
|
|
|
5
5
|
var internal_1 = require("generaltranslation/internal");
|
|
6
6
|
var createErrors_1 = require("../errors/createErrors");
|
|
7
7
|
var server_1 = require("next/server");
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
*
|
|
11
|
-
* @param {string} pathname - The pathname to extract from.
|
|
12
|
-
* @returns {string} The extracted locale.
|
|
13
|
-
*/
|
|
14
|
-
function extractLocale(pathname) {
|
|
15
|
-
var matches = pathname.match(/^\/([^\/]+)(?:\/|$)/);
|
|
16
|
-
return matches ? matches[1] : null;
|
|
17
|
-
}
|
|
8
|
+
var constants_1 = require("../utils/constants");
|
|
9
|
+
var utils_1 = require("./utils");
|
|
18
10
|
/**
|
|
19
11
|
* Middleware factory to create a Next.js middleware for i18n routing and locale detection.
|
|
20
12
|
*
|
|
@@ -23,17 +15,13 @@ function extractLocale(pathname) {
|
|
|
23
15
|
* If locale routing is enabled, it redirects to the localized pathname and
|
|
24
16
|
* updates the locale cookie.
|
|
25
17
|
*
|
|
26
|
-
* @param {Object} config - Configuration object for the middleware.
|
|
27
|
-
* @param {string} [config.defaultLocale='en'] - The default locale to use if no locale is detected.
|
|
28
|
-
* @param {string[]} [config.locales] - Array of supported locales. If provided, the locale will be validated against this list.
|
|
29
18
|
* @param {boolean} [config.localeRouting=true] - Flag to enable or disable automatic locale-based routing.
|
|
19
|
+
* @param {boolean} [config.prefixDefaultLocale=false] - Flag to enable or disable prefixing the default locale to the pathname, i.e., /en/about -> /about
|
|
30
20
|
* @returns {function} - A middleware function that processes the request and response.
|
|
31
21
|
*/
|
|
32
22
|
function createNextMiddleware(_a) {
|
|
33
|
-
var _b = _a === void 0 ? {
|
|
34
|
-
|
|
35
|
-
prefixDefaultLocale: false,
|
|
36
|
-
} : _a, _c = _b.localeRouting, localeRouting = _c === void 0 ? true : _c, _d = _b.prefixDefaultLocale, prefixDefaultLocale = _d === void 0 ? false : _d;
|
|
23
|
+
var _b = _a.localeRouting, localeRouting = _b === void 0 ? true : _b, _c = _a.prefixDefaultLocale, prefixDefaultLocale = _c === void 0 ? false : _c, _d = _a.pathConfig, pathConfig = _d === void 0 ? {} : _d;
|
|
24
|
+
// i18n config
|
|
37
25
|
var envParams;
|
|
38
26
|
if (process.env._GENERALTRANSLATION_I18N_CONFIG_PARAMS) {
|
|
39
27
|
try {
|
|
@@ -43,6 +31,9 @@ function createNextMiddleware(_a) {
|
|
|
43
31
|
console.error("gt-next middleware:", error);
|
|
44
32
|
}
|
|
45
33
|
}
|
|
34
|
+
// using gt services
|
|
35
|
+
var gtServicesEnabled = envParams === null || envParams === void 0 ? void 0 : envParams.gtServicesEnabled;
|
|
36
|
+
// i18n config
|
|
46
37
|
var defaultLocale = (envParams === null || envParams === void 0 ? void 0 : envParams.defaultLocale) || internal_1.libraryDefaultLocale;
|
|
47
38
|
var locales = (envParams === null || envParams === void 0 ? void 0 : envParams.locales) || [defaultLocale];
|
|
48
39
|
if (!(0, generaltranslation_1.isValidLocale)(defaultLocale))
|
|
@@ -51,6 +42,26 @@ function createNextMiddleware(_a) {
|
|
|
51
42
|
if (warningLocales.length)
|
|
52
43
|
console.warn((0, createErrors_1.createUnsupportedLocalesWarning)(warningLocales));
|
|
53
44
|
var approvedLocales = locales;
|
|
45
|
+
// ---------- PRE-PROCESSING PATHS ---------- //
|
|
46
|
+
// Standardize pathConfig paths
|
|
47
|
+
pathConfig = Object.entries(pathConfig).reduce(function (acc, _a) {
|
|
48
|
+
var sharedPath = _a[0], localizedPath = _a[1];
|
|
49
|
+
if (typeof localizedPath === 'string') {
|
|
50
|
+
acc[sharedPath] = localizedPath;
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
acc[sharedPath] = Object.entries(localizedPath).reduce(function (acc, _a) {
|
|
54
|
+
var locale = _a[0], localizedPath = _a[1];
|
|
55
|
+
acc[gtServicesEnabled ? (0, generaltranslation_1.standardizeLocale)(locale) : locale] =
|
|
56
|
+
localizedPath;
|
|
57
|
+
return acc;
|
|
58
|
+
}, {});
|
|
59
|
+
}
|
|
60
|
+
return acc;
|
|
61
|
+
}, {});
|
|
62
|
+
// Create the path mapping
|
|
63
|
+
var pathToSharedPath = (0, utils_1.createPathToSharedPathMap)(pathConfig);
|
|
64
|
+
// console.log(pathToSharedPath);
|
|
54
65
|
/**
|
|
55
66
|
* Processes the incoming request to determine the user's locale and sets a locale cookie.
|
|
56
67
|
* Optionally redirects the user based on the locale if locale-based routing is enabled.
|
|
@@ -61,79 +72,161 @@ function createNextMiddleware(_a) {
|
|
|
61
72
|
* - Sets a cookie with the detected or default locale.
|
|
62
73
|
* - Redirects to the correct locale route if locale routing is enabled.
|
|
63
74
|
*
|
|
64
|
-
* @param {
|
|
75
|
+
* @param {NextRequest} req - The incoming request object, containing URL and headers.
|
|
65
76
|
* @returns {NextResponse} - The Next.js response, either continuing the request or redirecting to the localized URL.
|
|
66
77
|
*/
|
|
67
78
|
function nextMiddleware(req) {
|
|
68
|
-
|
|
79
|
+
// console.log('--------------------------------');
|
|
69
80
|
var headerList = new Headers(req.headers);
|
|
70
|
-
var res = server_1.NextResponse.next(
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
candidates.push(pathnameLocale);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
// Check cookie locale
|
|
84
|
-
var cookieLocale = req.cookies.get(internal_1.localeCookieName);
|
|
85
|
-
if ((0, generaltranslation_1.isValidLocale)(cookieLocale === null || cookieLocale === void 0 ? void 0 : cookieLocale.value)) {
|
|
86
|
-
var resetCookieName = 'generaltranslation.locale.reset';
|
|
87
|
-
var resetCookie = req.cookies.get(resetCookieName);
|
|
88
|
-
if (resetCookie === null || resetCookie === void 0 ? void 0 : resetCookie.value) {
|
|
89
|
-
res.cookies.delete(resetCookieName);
|
|
90
|
-
candidates.unshift(cookieLocale.value);
|
|
91
|
-
}
|
|
92
|
-
else {
|
|
93
|
-
candidates.push(cookieLocale.value);
|
|
94
|
-
}
|
|
81
|
+
var res = server_1.NextResponse.next({
|
|
82
|
+
request: {
|
|
83
|
+
// New request headers
|
|
84
|
+
headers: headerList,
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
// routing
|
|
88
|
+
var routingConfig;
|
|
89
|
+
try {
|
|
90
|
+
routingConfig = require('gt-next/_routing');
|
|
95
91
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
var referer = headerList.get('referer');
|
|
99
|
-
if (referer && typeof referer === 'string') {
|
|
100
|
-
var refererLocale = extractLocale((_a = new URL(referer)) === null || _a === void 0 ? void 0 : _a.pathname);
|
|
101
|
-
if ((0, generaltranslation_1.isValidLocale)(refererLocale || ''))
|
|
102
|
-
candidates.push(refererLocale || '');
|
|
103
|
-
}
|
|
92
|
+
catch (e) {
|
|
93
|
+
console.error(e);
|
|
104
94
|
}
|
|
105
|
-
//
|
|
106
|
-
var
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
// Get default locale
|
|
110
|
-
candidates.push(defaultLocale);
|
|
111
|
-
// determine userLocale
|
|
112
|
-
var userLocale = (0, generaltranslation_1.standardizeLocale)((0, generaltranslation_1.determineLocale)(candidates.filter(generaltranslation_1.isValidLocale), approvedLocales) ||
|
|
113
|
-
defaultLocale);
|
|
95
|
+
// Check for rewrite flag in cookies
|
|
96
|
+
var rewriteFlag = req.headers.get(constants_1.middlewareLocaleRewriteFlagName) === 'true';
|
|
97
|
+
// ---------- LOCALE DETECTION ---------- //
|
|
98
|
+
var _a = (0, utils_1.getLocaleFromRequest)(req, defaultLocale, approvedLocales, localeRouting), userLocale = _a.userLocale, pathnameLocale = _a.pathnameLocale, unstandardizedPathnameLocale = _a.unstandardizedPathnameLocale;
|
|
114
99
|
res.headers.set(internal_1.localeHeaderName, userLocale);
|
|
100
|
+
if (userLocale) {
|
|
101
|
+
res.cookies.set(constants_1.middlewareLocaleName, userLocale);
|
|
102
|
+
}
|
|
115
103
|
if (localeRouting) {
|
|
104
|
+
// ---------- GET PATHS ---------- //
|
|
116
105
|
var pathname = req.nextUrl.pathname;
|
|
106
|
+
// Only strip off the locale if it's a valid locale (/fr/le-about -> /about), (/blog -> /blog)
|
|
107
|
+
var unprefixedPathname = pathnameLocale
|
|
108
|
+
? pathname.replace(new RegExp("^/".concat(unstandardizedPathnameLocale)), '')
|
|
109
|
+
: pathname;
|
|
117
110
|
var originalUrl = req.nextUrl;
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
111
|
+
// standardize pathname (ie, /tg/welcome -> /fil/welcome), (/blog -> /blog)
|
|
112
|
+
var standardizedPathname = pathnameLocale && pathnameLocale !== unstandardizedPathnameLocale
|
|
113
|
+
? pathname.replace(new RegExp("^/".concat(unstandardizedPathnameLocale)), "/".concat(userLocale))
|
|
114
|
+
: pathname;
|
|
115
|
+
// Get the shared path for the unprefixed pathname
|
|
116
|
+
var sharedPath = (0, utils_1.getSharedPath)(unprefixedPathname, pathToSharedPath);
|
|
117
|
+
// Localized path (/en-US/blog, /fr/le-about, /fr/dashboard/[id]/custom)
|
|
118
|
+
var localizedPath = sharedPath && (0, utils_1.getLocalizedPath)(sharedPath, userLocale, pathConfig);
|
|
119
|
+
// Combine localized path with dynamic parameters (/en-US/blog, /fr/le-about, /fr/dashboard/1/le-custom)
|
|
120
|
+
var localizedPathWithParameters = localizedPath &&
|
|
121
|
+
(0, utils_1.replaceDynamicSegments)(pathnameLocale
|
|
122
|
+
? standardizedPathname
|
|
123
|
+
: "/".concat(userLocale).concat(standardizedPathname), localizedPath);
|
|
124
|
+
// console.log('pathname', pathname);
|
|
125
|
+
// console.log('unprefixedPathname', unprefixedPathname);
|
|
126
|
+
// console.log('standardizedPathname', standardizedPathname);
|
|
127
|
+
// console.log('sharedPath', sharedPath);
|
|
128
|
+
// console.log('localizedPath', localizedPath);
|
|
129
|
+
// console.log('localizedPathWithParameters', localizedPathWithParameters);
|
|
130
|
+
// ---------- ROUTING LOGIC ---------- //
|
|
131
|
+
// BASE CASE: default locale, same path (/en-US/blog -> /en-US/blog), (/en-US/dashboard/1/custom -> /en-US/dashboard/1/custom)
|
|
132
|
+
if (localizedPathWithParameters &&
|
|
133
|
+
standardizedPathname === localizedPathWithParameters &&
|
|
134
|
+
userLocale === defaultLocale) {
|
|
135
|
+
// console.log(
|
|
136
|
+
// `[Middleware] Default locale path match: ${pathname} -> ${localizedPathWithParameters}`
|
|
137
|
+
// );
|
|
138
|
+
return res;
|
|
139
|
+
}
|
|
140
|
+
// BASE CASE: at localized path, which is the same as the shared path (/fil/blog -> /fil/blog)
|
|
141
|
+
if (pathname === localizedPathWithParameters &&
|
|
142
|
+
"/".concat(userLocale).concat(sharedPath) === localizedPathWithParameters) {
|
|
143
|
+
// console.log(
|
|
144
|
+
// `[Middleware] Localized path match: ${pathname} -> ${localizedPathWithParameters}`
|
|
145
|
+
// );
|
|
146
|
+
return res;
|
|
147
|
+
}
|
|
148
|
+
// If we've already rewritten this path, don't process it again
|
|
149
|
+
if (rewriteFlag) {
|
|
150
|
+
// console.log(
|
|
151
|
+
// `[Middleware] Already rewritten path: ${pathname} (skipping)`
|
|
152
|
+
// );
|
|
153
|
+
return res;
|
|
154
|
+
}
|
|
155
|
+
// REWRITE CASE: proxies a localized path, same locale (/fr/le-about => /fr/about) (/fr/dashboard/1/le-custom => /fr/dashboard/1/custom)
|
|
156
|
+
if (localizedPathWithParameters &&
|
|
157
|
+
standardizedPathname === localizedPathWithParameters) {
|
|
158
|
+
// convert to shared path with dynamic parameters
|
|
159
|
+
var rewritePath = (0, utils_1.replaceDynamicSegments)(localizedPathWithParameters, "/".concat(userLocale).concat(sharedPath));
|
|
160
|
+
var rewriteUrl = new URL(rewritePath, originalUrl);
|
|
161
|
+
rewriteUrl.search = originalUrl.search;
|
|
162
|
+
headerList.set(internal_1.localeHeaderName, userLocale);
|
|
163
|
+
var response = server_1.NextResponse.rewrite(rewriteUrl, {
|
|
164
|
+
headers: headerList,
|
|
165
|
+
});
|
|
166
|
+
response.headers.set(constants_1.middlewareLocaleRewriteFlagName, 'true');
|
|
167
|
+
if (userLocale) {
|
|
168
|
+
response.cookies.set(constants_1.middlewareLocaleName, userLocale);
|
|
169
|
+
}
|
|
170
|
+
// console.log(
|
|
171
|
+
// `[Middleware] Rewrite localized path: ${pathname} -> ${rewritePath}`
|
|
172
|
+
// );
|
|
173
|
+
return response;
|
|
174
|
+
}
|
|
175
|
+
// REWRITE CASE: no locale prefix
|
|
176
|
+
if (!pathnameLocale &&
|
|
177
|
+
!prefixDefaultLocale &&
|
|
178
|
+
(0, generaltranslation_1.isSameDialect)(userLocale, defaultLocale)) {
|
|
179
|
+
var rewritePath = "/".concat(userLocale).concat(pathname);
|
|
180
|
+
var rewriteUrl = new URL(rewritePath, originalUrl);
|
|
181
|
+
rewriteUrl.search = originalUrl.search;
|
|
182
|
+
var response = server_1.NextResponse.rewrite(rewriteUrl, {
|
|
183
|
+
headers: headerList,
|
|
184
|
+
});
|
|
185
|
+
response.headers.set(constants_1.middlewareLocaleRewriteFlagName, 'true');
|
|
186
|
+
// console.log(
|
|
187
|
+
// `[Middleware] Rewrite no locale prefix: ${pathname} -> ${rewritePath}`
|
|
188
|
+
// );
|
|
189
|
+
return response;
|
|
123
190
|
}
|
|
124
|
-
//
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
191
|
+
// REDIRECT CASE: non-i18n path
|
|
192
|
+
// 1. use customized path if it exists (/en-US/about -> /fr/le-about), (/about -> /fr/le-about)
|
|
193
|
+
// 2. otherwise, if pathname has locale prefix, replace it (/en-US/welcome -> /fr/welcome)
|
|
194
|
+
// 3. otherwise, prefix with locale (/welcome -> /fr/welcome)
|
|
195
|
+
if (unstandardizedPathnameLocale !== userLocale) {
|
|
196
|
+
// determine redirect path
|
|
197
|
+
var redirectPath = localizedPathWithParameters ||
|
|
198
|
+
(pathnameLocale
|
|
199
|
+
? pathname.replace(new RegExp("^/".concat(unstandardizedPathnameLocale)), "/".concat(userLocale))
|
|
200
|
+
: "/".concat(userLocale).concat(pathname));
|
|
201
|
+
var redirectUrl = new URL(redirectPath, originalUrl);
|
|
202
|
+
redirectUrl.search = originalUrl.search;
|
|
203
|
+
var response = server_1.NextResponse.redirect(redirectUrl);
|
|
204
|
+
if (userLocale) {
|
|
205
|
+
response.cookies.set(constants_1.middlewareLocaleName, userLocale);
|
|
206
|
+
}
|
|
207
|
+
// console.log(
|
|
208
|
+
// `[Middleware] Redirect non-i18n path: ${pathname} -> ${redirectPath}`
|
|
209
|
+
// );
|
|
210
|
+
return response;
|
|
131
211
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
212
|
+
// REDIRECT CASE: mismatched localized path (/fr/about -> /fr/le-about), mismatched dynamic path (/fr/dashboard/1/custom -> /fr/dashboard/1/le-custom)
|
|
213
|
+
if (localizedPathWithParameters) {
|
|
214
|
+
var redirectUrl = new URL(localizedPathWithParameters, originalUrl);
|
|
215
|
+
redirectUrl.search = originalUrl.search;
|
|
216
|
+
var response = server_1.NextResponse.redirect(redirectUrl);
|
|
217
|
+
if (userLocale) {
|
|
218
|
+
response.cookies.set(constants_1.middlewareLocaleName, userLocale);
|
|
219
|
+
}
|
|
220
|
+
// console.log(
|
|
221
|
+
// `[Middleware] Redirect mismatched path: ${pathname} -> ${localizedPathWithParameters}`
|
|
222
|
+
// );
|
|
223
|
+
return response;
|
|
135
224
|
}
|
|
225
|
+
// BASE CASE
|
|
226
|
+
// console.log(`[Middleware] No transformation needed: ${pathname}`);
|
|
227
|
+
return res;
|
|
136
228
|
}
|
|
229
|
+
// console.log(`[Middleware] No locale routing: ${req.nextUrl.pathname}`);
|
|
137
230
|
return res;
|
|
138
231
|
}
|
|
139
232
|
return nextMiddleware;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createNextMiddleware.js","sourceRoot":"","sources":["../../src/middleware-dir/createNextMiddleware.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"createNextMiddleware.js","sourceRoot":"","sources":["../../src/middleware-dir/createNextMiddleware.ts"],"names":[],"mappings":";;AAuCA,uCA4RC;AAnUD,yDAI4B;AAC5B,wDAIqC;AACrC,uDAAyE;AACzE,sCAAwD;AACxD,gDAI4B;AAC5B,iCAQiB;AAEjB;;;;;;;;;;;GAWG;AACH,SAAwB,oBAAoB,CAAC,EAQ5C;QAPC,qBAAoB,EAApB,aAAa,mBAAG,IAAI,KAAA,EACpB,2BAA2B,EAA3B,mBAAmB,mBAAG,KAAK,KAAA,EAC3B,kBAAe,EAAf,UAAU,mBAAG,EAAE,KAAA;IAMf,cAAc;IACd,IAAI,SAAS,CAAC;IACd,IAAI,OAAO,CAAC,GAAG,CAAC,sCAAsC,EAAE,CAAC;QACvD,IAAI,CAAC;YACH,SAAS,GAAG,IAAI,CAAC,KAAK,CACpB,OAAO,CAAC,GAAG,CAAC,sCAAsC,CACnD,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CA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C;YAClB,CAAC;YAED,sJAAsJ;YACtJ,IAAI,2BAA2B,EAAE,CAAC;gBAChC,IAAM,WAAW,GAAG,IAAI,GAAG,CAAC,2BAA2B,EAAE,WAAW,CAAC,CAAC;gBACtE,WAAW,CAAC,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC;gBACxC,IAAM,QAAQ,GAAG,qBAAY,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;gBACpD,IAAI,UAAU,EAAE,CAAC;oBACf,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,gCAAoB,EAAE,UAAU,CAAC,CAAC;gBACzD,CAAC;gBACD,eAAe;gBACf,2FAA2F;gBAC3F,KAAK;gBACL,OAAO,QAAQ,CAAC;YAClB,CAAC;YAED,YAAY;YACZ,qEAAqE;YACrE,OAAO,GAAG,CAAC;QACb,CAAC;QAED,0EAA0E;QAC1E,OAAO,GAAG,CAAC;IACb,CAAC;IAED,OAAO,cAAc,CAAC;AACxB,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { NextRequest } from 'next/server';
|
|
2
|
+
export type PathConfig = {
|
|
3
|
+
[key: string]: string | {
|
|
4
|
+
[key: string]: string;
|
|
5
|
+
};
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Extracts the locale from the given pathname.
|
|
9
|
+
*/
|
|
10
|
+
export declare function extractLocale(pathname: string): string | null;
|
|
11
|
+
/**
|
|
12
|
+
* Gets the shared path from a given pathname, handling both static and dynamic paths
|
|
13
|
+
*/
|
|
14
|
+
export declare function getSharedPath(pathname: string, pathToSharedPath: {
|
|
15
|
+
[key: string]: string;
|
|
16
|
+
}): string | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Extracts dynamic parameters from a path based on a shared path pattern
|
|
19
|
+
*/
|
|
20
|
+
export declare function extractDynamicParams(templatePath: string, path: string): string[];
|
|
21
|
+
/**
|
|
22
|
+
* Replaces dynamic segments in a path with their actual values
|
|
23
|
+
*/
|
|
24
|
+
export declare function replaceDynamicSegments(path: string, templatePath: string): string;
|
|
25
|
+
/**
|
|
26
|
+
* Gets the full localized path given a shared path and locale
|
|
27
|
+
*/
|
|
28
|
+
export declare function getLocalizedPath(sharedPath: string, locale: string, pathConfig: PathConfig): string | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Creates a map of localized paths to shared paths using regex patterns
|
|
31
|
+
*/
|
|
32
|
+
export declare function createPathToSharedPathMap(pathConfig: PathConfig): {
|
|
33
|
+
[key: string]: string;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Gets the locale from the request using various sources
|
|
37
|
+
*/
|
|
38
|
+
export declare function getLocaleFromRequest(req: NextRequest, defaultLocale: string, approvedLocales: string[], localeRouting: boolean): {
|
|
39
|
+
userLocale: string;
|
|
40
|
+
pathnameLocale: string | undefined;
|
|
41
|
+
unstandardizedPathnameLocale: string | null | undefined;
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/middleware-dir/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAmB1C,MAAM,MAAM,UAAU,GAAG;IACvB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;CACnD,CAAC;AAEF;;GAEG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAG7D;AAED;;GAEG;AACH,wBAAgB,aAAa,CAC3B,QAAQ,EAAE,MAAM,EAChB,gBAAgB,EAAE;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,GAC1C,MAAM,GAAG,SAAS,CAkBpB;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAClC,YAAY,EAAE,MAAM,EACpB,IAAI,EAAE,MAAM,GACX,MAAM,EAAE,CAcV;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CACpC,IAAI,EAAE,MAAM,EACZ,YAAY,EAAE,MAAM,GACnB,MAAM,CASR;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAC9B,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,MAAM,EACd,UAAU,EAAE,UAAU,GACrB,MAAM,GAAG,SAAS,CAapB;AAED;;GAEG;AACH,wBAAgB,yBAAyB,CAAC,UAAU,EAAE,UAAU,GAAG;IACjE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;CACvB,CAuBA;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAClC,GAAG,EAAE,WAAW,EAChB,aAAa,EAAE,MAAM,EACrB,eAAe,EAAE,MAAM,EAAE,EACzB,aAAa,EAAE,OAAO,GACrB;IACD,UAAU,EAAE,MAAM,CAAC;IACnB,cAAc,EAAE,MAAM,GAAG,SAAS,CAAC;IACnC,4BAA4B,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;CACzD,CA+DA"}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extractLocale = extractLocale;
|
|
4
|
+
exports.getSharedPath = getSharedPath;
|
|
5
|
+
exports.extractDynamicParams = extractDynamicParams;
|
|
6
|
+
exports.replaceDynamicSegments = replaceDynamicSegments;
|
|
7
|
+
exports.getLocalizedPath = getLocalizedPath;
|
|
8
|
+
exports.createPathToSharedPathMap = createPathToSharedPathMap;
|
|
9
|
+
exports.getLocaleFromRequest = getLocaleFromRequest;
|
|
10
|
+
var generaltranslation_1 = require("generaltranslation");
|
|
11
|
+
var internal_1 = require("generaltranslation/internal");
|
|
12
|
+
var constants_1 = require("../utils/constants");
|
|
13
|
+
/**
|
|
14
|
+
* Extracts the locale from the given pathname.
|
|
15
|
+
*/
|
|
16
|
+
function extractLocale(pathname) {
|
|
17
|
+
var matches = pathname.match(/^\/([^\/]+)(?:\/|$)/);
|
|
18
|
+
return matches ? matches[1] : null;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Gets the shared path from a given pathname, handling both static and dynamic paths
|
|
22
|
+
*/
|
|
23
|
+
function getSharedPath(pathname, pathToSharedPath) {
|
|
24
|
+
// Try exact match first
|
|
25
|
+
if (pathToSharedPath[pathname]) {
|
|
26
|
+
return pathToSharedPath[pathname];
|
|
27
|
+
}
|
|
28
|
+
// Try regex pattern match
|
|
29
|
+
for (var _i = 0, _a = Object.entries(pathToSharedPath); _i < _a.length; _i++) {
|
|
30
|
+
var _b = _a[_i], pattern = _b[0], sharedPath = _b[1];
|
|
31
|
+
if (pattern.includes('[^/]+')) {
|
|
32
|
+
// Convert the pattern to a strict regex that matches the exact path structure
|
|
33
|
+
var regex = new RegExp("^".concat(pattern.replace(/\//g, '\\/'), "$"));
|
|
34
|
+
if (regex.test(pathname)) {
|
|
35
|
+
return sharedPath;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Extracts dynamic parameters from a path based on a shared path pattern
|
|
43
|
+
*/
|
|
44
|
+
function extractDynamicParams(templatePath, path) {
|
|
45
|
+
if (!templatePath.includes('['))
|
|
46
|
+
return [];
|
|
47
|
+
var params = [];
|
|
48
|
+
var pathSegments = path.split('/');
|
|
49
|
+
var sharedSegments = templatePath.split('/');
|
|
50
|
+
sharedSegments.forEach(function (segment, index) {
|
|
51
|
+
if (segment.startsWith('[') && segment.endsWith(']')) {
|
|
52
|
+
params.push(pathSegments[index]);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
return params;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Replaces dynamic segments in a path with their actual values
|
|
59
|
+
*/
|
|
60
|
+
function replaceDynamicSegments(path, templatePath) {
|
|
61
|
+
if (!templatePath.includes('['))
|
|
62
|
+
return templatePath;
|
|
63
|
+
var params = extractDynamicParams(templatePath, path);
|
|
64
|
+
var paramIndex = 0;
|
|
65
|
+
var result = templatePath.replace(/\[([^\]]+)\]/g, function (match) {
|
|
66
|
+
return params[paramIndex++] || match;
|
|
67
|
+
});
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Gets the full localized path given a shared path and locale
|
|
72
|
+
*/
|
|
73
|
+
function getLocalizedPath(sharedPath, locale, pathConfig) {
|
|
74
|
+
var localizedPath = pathConfig[sharedPath];
|
|
75
|
+
var path;
|
|
76
|
+
if (typeof localizedPath === 'string') {
|
|
77
|
+
path = "/".concat(locale).concat(localizedPath);
|
|
78
|
+
}
|
|
79
|
+
else if (typeof localizedPath === 'object') {
|
|
80
|
+
path = localizedPath[locale]
|
|
81
|
+
? "/".concat(locale).concat(localizedPath[locale])
|
|
82
|
+
: "/".concat(locale).concat(sharedPath);
|
|
83
|
+
}
|
|
84
|
+
return path;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Creates a map of localized paths to shared paths using regex patterns
|
|
88
|
+
*/
|
|
89
|
+
function createPathToSharedPathMap(pathConfig) {
|
|
90
|
+
return Object.entries(pathConfig).reduce(function (acc, _a) {
|
|
91
|
+
var sharedPath = _a[0], localizedPath = _a[1];
|
|
92
|
+
// Add the shared path itself, converting to regex pattern if it has dynamic segments
|
|
93
|
+
if (sharedPath.includes('[')) {
|
|
94
|
+
var pattern = sharedPath.replace(/\[([^\]]+)\]/g, '[^/]+');
|
|
95
|
+
acc[pattern] = sharedPath;
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
acc[sharedPath] = sharedPath;
|
|
99
|
+
}
|
|
100
|
+
if (typeof localizedPath === 'object') {
|
|
101
|
+
Object.values(localizedPath).forEach(function (localizedPath) {
|
|
102
|
+
// Convert the localized path to a regex pattern
|
|
103
|
+
// Replace [param] with [^/]+ to match any non-slash characters
|
|
104
|
+
var pattern = localizedPath.replace(/\[([^\]]+)\]/g, '[^/]+');
|
|
105
|
+
acc[pattern] = sharedPath;
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
return acc;
|
|
109
|
+
}, {});
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Gets the locale from the request using various sources
|
|
113
|
+
*/
|
|
114
|
+
function getLocaleFromRequest(req, defaultLocale, approvedLocales, localeRouting) {
|
|
115
|
+
var _a, _b;
|
|
116
|
+
var headerList = new Headers(req.headers);
|
|
117
|
+
var candidates = [];
|
|
118
|
+
// Check pathname locales
|
|
119
|
+
var pathnameLocale, unstandardizedPathnameLocale;
|
|
120
|
+
var pathname = req.nextUrl.pathname;
|
|
121
|
+
if (localeRouting) {
|
|
122
|
+
unstandardizedPathnameLocale = extractLocale(pathname);
|
|
123
|
+
var extractedLocale = (0, generaltranslation_1.standardizeLocale)(unstandardizedPathnameLocale || '');
|
|
124
|
+
if ((0, generaltranslation_1.isValidLocale)(extractedLocale)) {
|
|
125
|
+
pathnameLocale = extractedLocale;
|
|
126
|
+
candidates.push(pathnameLocale);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
// Check cookie locale
|
|
130
|
+
var cookieLocale = req.cookies.get(internal_1.localeCookieName);
|
|
131
|
+
if ((cookieLocale === null || cookieLocale === void 0 ? void 0 : cookieLocale.value) && (0, generaltranslation_1.isValidLocale)(cookieLocale === null || cookieLocale === void 0 ? void 0 : cookieLocale.value)) {
|
|
132
|
+
var resetCookieName = constants_1.middlewareLocaleResetFlagName;
|
|
133
|
+
var resetCookie = req.cookies.get(resetCookieName);
|
|
134
|
+
if (resetCookie === null || resetCookie === void 0 ? void 0 : resetCookie.value) {
|
|
135
|
+
candidates.unshift(cookieLocale.value);
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
candidates.push(cookieLocale.value);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
// Check referer locale
|
|
142
|
+
var refererLocale;
|
|
143
|
+
if (localeRouting) {
|
|
144
|
+
var referer = headerList.get('referer');
|
|
145
|
+
if (referer && typeof referer === 'string') {
|
|
146
|
+
refererLocale = extractLocale((_a = new URL(referer)) === null || _a === void 0 ? void 0 : _a.pathname);
|
|
147
|
+
if ((0, generaltranslation_1.isValidLocale)(refererLocale || ''))
|
|
148
|
+
candidates.push(refererLocale || '');
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
// Get locales from accept-language header
|
|
152
|
+
var acceptedLocales = ((_b = headerList
|
|
153
|
+
.get('accept-language')) === null || _b === void 0 ? void 0 : _b.split(',').map(function (item) { var _a; return (_a = item.split(';')) === null || _a === void 0 ? void 0 : _a[0].trim(); })) || [];
|
|
154
|
+
candidates.push.apply(candidates, acceptedLocales);
|
|
155
|
+
// Get default locale
|
|
156
|
+
candidates.push(defaultLocale);
|
|
157
|
+
// determine userLocale
|
|
158
|
+
var userLocale = (0, generaltranslation_1.standardizeLocale)((0, generaltranslation_1.determineLocale)(candidates.filter(generaltranslation_1.isValidLocale), approvedLocales) ||
|
|
159
|
+
defaultLocale);
|
|
160
|
+
return {
|
|
161
|
+
userLocale: userLocale,
|
|
162
|
+
pathnameLocale: pathnameLocale,
|
|
163
|
+
unstandardizedPathnameLocale: unstandardizedPathnameLocale,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/middleware-dir/utils.ts"],"names":[],"mappings":";;AA0BA,sCAGC;AAKD,sCAqBC;AAKD,oDAiBC;AAKD,wDAYC;AAKD,4CAiBC;AAKD,8DAyBC;AAKD,oDAwEC;AA9ND,yDAK4B;AAC5B,wDAIqC;AAErC,gDAI4B;AAM5B;;GAEG;AACH,SAAgB,aAAa,CAAC,QAAgB;IAC5C,IAAM,OAAO,GAAG,QAAQ,CAAC,KAAK,CAAC,qBAAqB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IAAM,eAAe,GACnB,CAAA,MAAA,UAAU;SACP,GAAG,CAAC,iBAAiB,CAAC,0CACrB,KAAK,CAAC,GAAG,EACV,GAAG,CAAC,UAAC,IAAI,YAAK,OAAA,MAAA,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,0CAAG,CAAC,EAAE,IAAI,EAAE,CAAA,EAAA,CAAC,KAAI,EAAE,CAAC;IACtD,UAAU,CAAC,IAAI,OAAf,UAAU,EAAS,eAAe,EAAE;IAEpC,qBAAqB;IACrB,UAAU,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;IAE/B,uBAAuB;IACvB,IAAM,UAAU,GAAG,IAAA,sCAAiB,EAClC,IAAA,oCAAe,EAAC,UAAU,CAAC,MAAM,CAAC,kCAAa,CAAC,EAAE,eAAe,CAAC;QAChE,aAAa,CAChB,CAAC;IAEF,OAAO;QACL,UAAU,YAAA;QACV,cAAc,gBAAA;QACd,4BAA4B,8BAAA;KAC7B,CAAC;AACJ,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ClientProviderWrapper.d.ts","sourceRoot":"","sources":["../../src/provider/ClientProviderWrapper.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"ClientProviderWrapper.d.ts","sourceRoot":"","sources":["../../src/provider/ClientProviderWrapper.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AASxD,MAAM,CAAC,OAAO,UAAU,cAAc,CACpC,KAAK,EAAE,IAAI,CAAC,mBAAmB,EAAE,gBAAgB,CAAC,2CAkCnD"}
|
|
@@ -16,13 +16,34 @@ exports.default = ClientProvider;
|
|
|
16
16
|
var jsx_runtime_1 = require("react/jsx-runtime");
|
|
17
17
|
var client_1 = require("gt-react/client");
|
|
18
18
|
var navigation_1 = require("next/navigation");
|
|
19
|
+
var react_1 = require("react");
|
|
20
|
+
var constants_1 = require("../utils/constants");
|
|
19
21
|
function ClientProvider(props) {
|
|
20
22
|
// locale change on client, trigger page reload
|
|
21
23
|
var router = (0, navigation_1.useRouter)();
|
|
22
24
|
var onLocaleChange = function () {
|
|
23
|
-
document.cookie = "
|
|
25
|
+
document.cookie = "".concat(constants_1.middlewareLocaleResetFlagName, "=true;path=/");
|
|
24
26
|
router.refresh();
|
|
25
27
|
};
|
|
28
|
+
// Trigger page reload when locale changes
|
|
29
|
+
// When nav to same route but in diff locale, client components were cached and not re-rendered
|
|
30
|
+
var pathname = (0, navigation_1.usePathname)();
|
|
31
|
+
(0, react_1.useEffect)(function () {
|
|
32
|
+
var _a, _b;
|
|
33
|
+
var newLocale = (_a = document.cookie
|
|
34
|
+
.split('; ')
|
|
35
|
+
.find(function (row) { return row.startsWith("".concat(constants_1.middlewareLocaleName, "=")); })) === null || _a === void 0 ? void 0 : _a.split('=')[1];
|
|
36
|
+
if (newLocale && newLocale !== props.locale) {
|
|
37
|
+
var rewriteFlag = ((_b = document
|
|
38
|
+
.querySelector("meta[name=\"".concat(constants_1.middlewareLocaleRewriteFlagName, "\"]"))) === null || _b === void 0 ? void 0 : _b.getAttribute('content')) === 'true';
|
|
39
|
+
if (!rewriteFlag) {
|
|
40
|
+
// reload server
|
|
41
|
+
router.refresh();
|
|
42
|
+
// reload client
|
|
43
|
+
window.location.reload();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}, [pathname]); // Re-run when pathname changes
|
|
26
47
|
return (0, jsx_runtime_1.jsx)(client_1.ClientProvider, __assign({ onLocaleChange: onLocaleChange }, props));
|
|
27
48
|
}
|
|
28
49
|
//# sourceMappingURL=ClientProviderWrapper.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ClientProviderWrapper.js","sourceRoot":"","sources":["../../src/provider/ClientProviderWrapper.tsx"],"names":[],"mappings":";AAAA,YAAY,CAAC;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"Cl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AC,uBAAe,aAAC,cAAc,EAAE,cAAc,IAAM,KAAK,EAAI,CAAC;AACxE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getLocale.d.ts","sourceRoot":"","sources":["../../src/request/getLocale.ts"],"names":[],"mappings":"AAKA;;;;;;;;GAQG;AACH,wBAA8B,SAAS,IAAI,OAAO,CAAC,MAAM,CAAC,
|
|
1
|
+
{"version":3,"file":"getLocale.d.ts","sourceRoot":"","sources":["../../src/request/getLocale.ts"],"names":[],"mappings":"AAKA;;;;;;;;GAQG;AACH,wBAA8B,SAAS,IAAI,OAAO,CAAC,MAAM,CAAC,CAqBzD"}
|
|
@@ -76,10 +76,13 @@ function getLocale() {
|
|
|
76
76
|
_a = _c.sent();
|
|
77
77
|
I18NConfig_1 = (0, getI18NConfig_1.default)();
|
|
78
78
|
getLocaleFunction = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
79
|
+
var res;
|
|
79
80
|
return __generator(this, function (_a) {
|
|
80
81
|
switch (_a.label) {
|
|
81
82
|
case 0: return [4 /*yield*/, (0, getNextLocale_1.getNextLocale)(I18NConfig_1.getDefaultLocale(), I18NConfig_1.getLocales())];
|
|
82
|
-
case 1:
|
|
83
|
+
case 1:
|
|
84
|
+
res = _a.sent();
|
|
85
|
+
return [2 /*return*/, res];
|
|
83
86
|
}
|
|
84
87
|
});
|
|
85
88
|
}); };
|