i18next-http-middleware 2.1.1 → 3.0.2
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/CHANGELOG.md +13 -0
- package/README.md +2 -11
- package/cjs/LanguageDetector.js +19 -61
- package/cjs/httpFunctions.js +1 -2
- package/cjs/index.js +14 -22
- package/cjs/languageLookups/cookie.js +2 -3
- package/cjs/languageLookups/header.js +13 -3
- package/cjs/languageLookups/path.js +2 -3
- package/cjs/languageLookups/querystring.js +2 -2
- package/cjs/languageLookups/session.js +2 -2
- package/cjs/utils.js +1 -2
- package/esm/LanguageDetector.js +119 -0
- package/esm/httpFunctions.js +120 -0
- package/esm/index.js +229 -0
- package/esm/languageLookups/cookie.js +112 -0
- package/esm/languageLookups/header.js +59 -0
- package/esm/languageLookups/path.js +29 -0
- package/esm/languageLookups/querystring.js +35 -0
- package/esm/languageLookups/session.js +21 -0
- package/esm/utils.js +55 -0
- package/lib/LanguageDetector.js +6 -42
- package/lib/languageLookups/header.js +8 -1
- package/package.json +17 -12
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
+
## [v3.0.2](https://github.com/i18next/i18next-http-middleware/compare/v3.0.1...v3.0.2)
|
|
2
|
+
- transpile also esm
|
|
3
|
+
|
|
4
|
+
## [v3.0.1](https://github.com/i18next/i18next-http-middleware/compare/v3.0.0...v3.0.1)
|
|
5
|
+
- introduce lookupHeaderRegex option
|
|
6
|
+
|
|
7
|
+
## [v3.0.0](https://github.com/i18next/i18next-http-middleware/compare/v2.1.2...v3.0.0)
|
|
8
|
+
- **BREAKING** needs i18next >= 19.5.0
|
|
9
|
+
- let i18next figure out which detected lng is best match
|
|
10
|
+
|
|
11
|
+
## [v2.1.2](https://github.com/i18next/i18next-http-middleware/compare/v2.1.1...v2.1.2)
|
|
12
|
+
- fix: get whitelist from correct property
|
|
13
|
+
|
|
1
14
|
## [v2.1.1](https://github.com/i18next/i18next-http-middleware/compare/v2.1.0...v2.1.1)
|
|
2
15
|
- extend httpFunctions getQuery to handle some edge cases
|
|
3
16
|
|
package/README.md
CHANGED
|
@@ -288,6 +288,7 @@ As with all modules you can either pass the constructor function (class) to the
|
|
|
288
288
|
lookupQuerystring: 'lng',
|
|
289
289
|
lookupCookie: 'i18next',
|
|
290
290
|
lookupHeader: 'accept-language',
|
|
291
|
+
lookupHeaderRegex: /(([a-z]{2})-?([A-Z]{2})?)\s*;?\s*(q=([0-9.]+))?/gi,
|
|
291
292
|
lookupSession: 'lng',
|
|
292
293
|
lookupPath: 'lng',
|
|
293
294
|
lookupFromPathIndex: 0,
|
|
@@ -300,17 +301,7 @@ As with all modules you can either pass the constructor function (class) to the
|
|
|
300
301
|
cookieDomain: 'myDomain',
|
|
301
302
|
cookiePath: '/my/path',
|
|
302
303
|
cookieSecure: true, // if need secure cookie
|
|
303
|
-
cookieSameSite: 'strict'
|
|
304
|
-
|
|
305
|
-
// only detect languages that are in the whitelist
|
|
306
|
-
checkWhitelist: true,
|
|
307
|
-
|
|
308
|
-
// fallback to a similar whitelist language
|
|
309
|
-
// Example 1: Browser language is 'es'
|
|
310
|
-
// if 'es' is not found in whitelist, first fallback to any whitelist language that starts with 'es-', then fallback to fallbackLng ('es' -> 'es-*' -> fallbackLng)
|
|
311
|
-
// Example 2: Browser language is 'es-MX'
|
|
312
|
-
// if 'es-MX' is not found in whitelist, first fallback to 'es', then fallback to 'es-*', then fallback to fallbackLng ('es-MX' -> 'es' -> 'es-*' -> fallbackLng)
|
|
313
|
-
checkForSimilarInWhitelist: false
|
|
304
|
+
cookieSameSite: 'strict' // 'strict', 'lax' or 'none'
|
|
314
305
|
}
|
|
315
306
|
```
|
|
316
307
|
|
package/cjs/LanguageDetector.js
CHANGED
|
@@ -5,7 +5,7 @@ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "functi
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", {
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
|
-
exports
|
|
8
|
+
exports.default = void 0;
|
|
9
9
|
|
|
10
10
|
var utils = _interopRequireWildcard(require("./utils.js"));
|
|
11
11
|
|
|
@@ -21,11 +21,11 @@ var _session = _interopRequireDefault(require("./languageLookups/session.js"));
|
|
|
21
21
|
|
|
22
22
|
var _httpFunctions = require("./httpFunctions.js");
|
|
23
23
|
|
|
24
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : {
|
|
24
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
25
25
|
|
|
26
26
|
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
|
|
27
27
|
|
|
28
|
-
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return {
|
|
28
|
+
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
29
29
|
|
|
30
30
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
31
31
|
|
|
@@ -35,26 +35,17 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
|
|
|
35
35
|
|
|
36
36
|
function getDefaults() {
|
|
37
37
|
return (0, _httpFunctions.extendOptionsWithDefaults)({
|
|
38
|
-
order: [
|
|
39
|
-
/* 'path', 'session' */
|
|
40
|
-
'querystring', 'cookie', 'header'],
|
|
38
|
+
order: ['querystring', 'cookie', 'header'],
|
|
41
39
|
lookupQuerystring: 'lng',
|
|
42
40
|
lookupCookie: 'i18next',
|
|
43
41
|
lookupSession: 'lng',
|
|
44
42
|
lookupFromPathIndex: 0,
|
|
45
|
-
// cache user language
|
|
46
43
|
caches: false,
|
|
47
|
-
// ['cookie']
|
|
48
|
-
// cookieExpirationDate: new Date(),
|
|
49
|
-
// cookieDomain: 'myDomain'
|
|
50
|
-
// cookiePath: '/my/path'
|
|
51
|
-
checkWhitelist: true,
|
|
52
|
-
checkForSimilarInWhitelist: false,
|
|
53
44
|
cookieSameSite: 'strict'
|
|
54
45
|
});
|
|
55
46
|
}
|
|
56
47
|
|
|
57
|
-
var LanguageDetector =
|
|
48
|
+
var LanguageDetector = function () {
|
|
58
49
|
function LanguageDetector(services) {
|
|
59
50
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
60
51
|
var allOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
@@ -73,14 +64,12 @@ var LanguageDetector = /*#__PURE__*/function () {
|
|
|
73
64
|
var allOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
74
65
|
this.services = services;
|
|
75
66
|
this.options = utils.defaults(options, this.options || {}, getDefaults());
|
|
76
|
-
this.allOptions = allOptions;
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
this.addDetector(
|
|
80
|
-
this.addDetector(
|
|
81
|
-
this.addDetector(
|
|
82
|
-
this.addDetector(_header["default"]);
|
|
83
|
-
this.addDetector(_session["default"]);
|
|
67
|
+
this.allOptions = allOptions;
|
|
68
|
+
this.addDetector(_cookie.default);
|
|
69
|
+
this.addDetector(_querystring.default);
|
|
70
|
+
this.addDetector(_path.default);
|
|
71
|
+
this.addDetector(_header.default);
|
|
72
|
+
this.addDetector(_session.default);
|
|
84
73
|
}
|
|
85
74
|
}, {
|
|
86
75
|
key: "addDetector",
|
|
@@ -102,20 +91,13 @@ var LanguageDetector = /*#__PURE__*/function () {
|
|
|
102
91
|
|
|
103
92
|
if (!detections) return;
|
|
104
93
|
if (!Array.isArray(detections)) detections = [detections];
|
|
105
|
-
detections.forEach(function (lng) {
|
|
106
|
-
if (found || typeof lng !== 'string') return;
|
|
107
94
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
if (
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
if (!found && _this.options.checkForSimilarInWhitelist) {
|
|
116
|
-
found = _this.getSimilarInWhitelist(cleanedLng);
|
|
117
|
-
}
|
|
118
|
-
});
|
|
95
|
+
if (_this.services.languageUtils.getBestMatchFromCodes) {
|
|
96
|
+
found = _this.services.languageUtils.getBestMatchFromCodes(detections);
|
|
97
|
+
if (found) req.i18nextLookupName = detectorName;
|
|
98
|
+
} else {
|
|
99
|
+
found = detections.length > 0 ? detections[0] : null;
|
|
100
|
+
}
|
|
119
101
|
});
|
|
120
102
|
|
|
121
103
|
if (!found) {
|
|
@@ -126,7 +108,7 @@ var LanguageDetector = /*#__PURE__*/function () {
|
|
|
126
108
|
if (Object.prototype.toString.apply(fallbacks) === '[object Array]') {
|
|
127
109
|
found = fallbacks[0];
|
|
128
110
|
} else {
|
|
129
|
-
found = fallbacks[0] || fallbacks
|
|
111
|
+
found = fallbacks[0] || fallbacks.default && fallbacks.default[0];
|
|
130
112
|
}
|
|
131
113
|
}
|
|
132
114
|
|
|
@@ -149,30 +131,6 @@ var LanguageDetector = /*#__PURE__*/function () {
|
|
|
149
131
|
}
|
|
150
132
|
});
|
|
151
133
|
}
|
|
152
|
-
}, {
|
|
153
|
-
key: "getSimilarInWhitelist",
|
|
154
|
-
value: function getSimilarInWhitelist(cleanedLng) {
|
|
155
|
-
var _this3 = this;
|
|
156
|
-
|
|
157
|
-
if (!this.i18nOptions.whitelist) return;
|
|
158
|
-
|
|
159
|
-
if (cleanedLng.includes('-')) {
|
|
160
|
-
// i.e. es-MX should check if es is in whitelist
|
|
161
|
-
var prefix = cleanedLng.split('-')[0];
|
|
162
|
-
var cleanedPrefix = this.services.languageUtils.formatLanguageCode(prefix);
|
|
163
|
-
if (this.services.languageUtils.isWhitelisted(cleanedPrefix)) return cleanedPrefix; // if reached here, nothing found. continue to search for similar using only prefix
|
|
164
|
-
|
|
165
|
-
cleanedLng = cleanedPrefix;
|
|
166
|
-
} // i.e. 'pt' should return 'pt-BR'. If multiple in whitelist with 'pt-', then use first one in whitelist
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
var similar = this.i18nOptions.whitelist.find(function (whitelistLng) {
|
|
170
|
-
var cleanedWhitelistLng = _this3.services.languageUtils.formatLanguageCode(whitelistLng);
|
|
171
|
-
|
|
172
|
-
if (cleanedWhitelistLng.startsWith(cleanedLng)) return cleanedWhitelistLng;
|
|
173
|
-
});
|
|
174
|
-
if (similar) return similar;
|
|
175
|
-
}
|
|
176
134
|
}]);
|
|
177
135
|
|
|
178
136
|
return LanguageDetector;
|
|
@@ -180,5 +138,5 @@ var LanguageDetector = /*#__PURE__*/function () {
|
|
|
180
138
|
|
|
181
139
|
LanguageDetector.type = 'languageDetector';
|
|
182
140
|
var _default = LanguageDetector;
|
|
183
|
-
exports
|
|
141
|
+
exports.default = _default;
|
|
184
142
|
module.exports = exports.default;
|
package/cjs/httpFunctions.js
CHANGED
|
@@ -129,8 +129,7 @@ var setContentType = function setContentType(res, type) {
|
|
|
129
129
|
exports.setContentType = setContentType;
|
|
130
130
|
|
|
131
131
|
var setStatus = function setStatus(res, code) {
|
|
132
|
-
if (typeof res.status === 'function') return res.status(code);
|
|
133
|
-
|
|
132
|
+
if (typeof res.status === 'function') return res.status(code);
|
|
134
133
|
if (res.status) return res.status = code;
|
|
135
134
|
console.log('no possibility found to set status');
|
|
136
135
|
};
|
package/cjs/index.js
CHANGED
|
@@ -10,7 +10,7 @@ exports.plugin = plugin;
|
|
|
10
10
|
exports.getResourcesHandler = getResourcesHandler;
|
|
11
11
|
exports.missingKeyHandler = missingKeyHandler;
|
|
12
12
|
exports.addRoute = addRoute;
|
|
13
|
-
exports
|
|
13
|
+
exports.default = exports.LanguageDetector = void 0;
|
|
14
14
|
|
|
15
15
|
var utils = _interopRequireWildcard(require("./utils.js"));
|
|
16
16
|
|
|
@@ -18,13 +18,13 @@ var _LanguageDetector = _interopRequireDefault(require("./LanguageDetector.js"))
|
|
|
18
18
|
|
|
19
19
|
var _httpFunctions = require("./httpFunctions.js");
|
|
20
20
|
|
|
21
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : {
|
|
21
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
22
22
|
|
|
23
23
|
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
|
|
24
24
|
|
|
25
|
-
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return {
|
|
25
|
+
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
26
26
|
|
|
27
|
-
var LanguageDetector = _LanguageDetector
|
|
27
|
+
var LanguageDetector = _LanguageDetector.default;
|
|
28
28
|
exports.LanguageDetector = LanguageDetector;
|
|
29
29
|
|
|
30
30
|
var checkForCombinedReqRes = function checkForCombinedReqRes(req, res, next) {
|
|
@@ -72,7 +72,6 @@ function handle(i18next) {
|
|
|
72
72
|
initImmediate: false
|
|
73
73
|
});
|
|
74
74
|
i18n.on('languageChanged', function (lng) {
|
|
75
|
-
// Keep language in sync
|
|
76
75
|
req.language = req.locale = req.lng = lng;
|
|
77
76
|
|
|
78
77
|
if (res.locals) {
|
|
@@ -91,16 +90,14 @@ function handle(i18next) {
|
|
|
91
90
|
}
|
|
92
91
|
});
|
|
93
92
|
var lng = req.lng;
|
|
94
|
-
if (!lng && i18next.services.languageDetector) lng = i18next.services.languageDetector.detect(req, res);
|
|
95
|
-
|
|
93
|
+
if (!lng && i18next.services.languageDetector) lng = i18next.services.languageDetector.detect(req, res);
|
|
96
94
|
req.language = req.locale = req.lng = lng;
|
|
97
95
|
|
|
98
96
|
if (lng && options.getHeader(res, 'Content-Language') !== lng) {
|
|
99
97
|
options.setHeader(res, 'Content-Language', lng);
|
|
100
98
|
}
|
|
101
99
|
|
|
102
|
-
req.languages = i18next.services.languageUtils.toResolveHierarchy(lng);
|
|
103
|
-
|
|
100
|
+
req.languages = i18next.services.languageUtils.toResolveHierarchy(lng);
|
|
104
101
|
i18n.changeLanguage(lng || i18next.options.fallbackLng[0]);
|
|
105
102
|
|
|
106
103
|
if (req.i18nextLookupName === 'path' && options.removeLngFromUrl) {
|
|
@@ -108,10 +105,9 @@ function handle(i18next) {
|
|
|
108
105
|
}
|
|
109
106
|
|
|
110
107
|
var t = i18n.t.bind(i18n);
|
|
111
|
-
var exists = i18n.exists.bind(i18n);
|
|
112
|
-
|
|
108
|
+
var exists = i18n.exists.bind(i18n);
|
|
113
109
|
req.i18n = i18n;
|
|
114
|
-
req.t = t;
|
|
110
|
+
req.t = t;
|
|
115
111
|
|
|
116
112
|
if (res.locals) {
|
|
117
113
|
res.locals.t = t;
|
|
@@ -121,8 +117,7 @@ function handle(i18next) {
|
|
|
121
117
|
res.locals.languageDir = i18next.dir(lng);
|
|
122
118
|
}
|
|
123
119
|
|
|
124
|
-
if (i18next.services.languageDetector) i18next.services.languageDetector.cacheUserLanguage(req, res, lng);
|
|
125
|
-
|
|
120
|
+
if (i18next.services.languageDetector) i18next.services.languageDetector.cacheUserLanguage(req, res, lng);
|
|
126
121
|
if (!req.lng) return next();
|
|
127
122
|
i18next.loadLanguages(req.lng, function () {
|
|
128
123
|
return next();
|
|
@@ -138,7 +133,7 @@ function plugin(instance, options, next) {
|
|
|
138
133
|
return next();
|
|
139
134
|
}
|
|
140
135
|
|
|
141
|
-
plugin[Symbol
|
|
136
|
+
plugin[Symbol.for('skip-override')] = true;
|
|
142
137
|
|
|
143
138
|
function getResourcesHandler(i18next) {
|
|
144
139
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
@@ -166,8 +161,7 @@ function getResourcesHandler(i18next) {
|
|
|
166
161
|
}
|
|
167
162
|
|
|
168
163
|
var languages = options.getQuery(req)[options.lngParam || 'lng'] ? options.getQuery(req)[options.lngParam || 'lng'].split(' ') : [];
|
|
169
|
-
var namespaces = options.getQuery(req)[options.nsParam || 'ns'] ? options.getQuery(req)[options.nsParam || 'ns'].split(' ') : [];
|
|
170
|
-
|
|
164
|
+
var namespaces = options.getQuery(req)[options.nsParam || 'ns'] ? options.getQuery(req)[options.nsParam || 'ns'].split(' ') : [];
|
|
171
165
|
namespaces.forEach(function (ns) {
|
|
172
166
|
if (i18next.options.ns && i18next.options.ns.indexOf(ns) < 0) i18next.options.ns.push(ns);
|
|
173
167
|
});
|
|
@@ -228,8 +222,7 @@ function addRoute(i18next, route, lngs, app, verb, fc) {
|
|
|
228
222
|
if (typeof verb === 'function') {
|
|
229
223
|
fc = verb;
|
|
230
224
|
verb = 'get';
|
|
231
|
-
}
|
|
232
|
-
|
|
225
|
+
}
|
|
233
226
|
|
|
234
227
|
var callbacks = [fc].concat(Array.prototype.slice.call(arguments, 6));
|
|
235
228
|
|
|
@@ -238,8 +231,7 @@ function addRoute(i18next, route, lngs, app, verb, fc) {
|
|
|
238
231
|
var locRoute = [];
|
|
239
232
|
|
|
240
233
|
for (var y = 0, ly = parts.length; y < ly; y++) {
|
|
241
|
-
var part = parts[y];
|
|
242
|
-
// this is replaced with the value of the language
|
|
234
|
+
var part = parts[y];
|
|
243
235
|
|
|
244
236
|
if (part === ':lng') {
|
|
245
237
|
locRoute.push(lngs[i]);
|
|
@@ -265,4 +257,4 @@ var _default = {
|
|
|
265
257
|
addRoute: addRoute,
|
|
266
258
|
LanguageDetector: LanguageDetector
|
|
267
259
|
};
|
|
268
|
-
exports
|
|
260
|
+
exports.default = _default;
|
|
@@ -3,8 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
7
|
-
// eslint-disable-next-line no-control-regex
|
|
6
|
+
exports.default = void 0;
|
|
8
7
|
var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
|
|
9
8
|
|
|
10
9
|
var serializeCookie = function serializeCookie(name, val, options) {
|
|
@@ -117,5 +116,5 @@ var _default = {
|
|
|
117
116
|
}
|
|
118
117
|
}
|
|
119
118
|
};
|
|
120
|
-
exports
|
|
119
|
+
exports.default = _default;
|
|
121
120
|
module.exports = exports.default;
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var specialCases = ['hans', 'hant', 'latn', 'cyrl', 'cans', 'mong', 'arab'];
|
|
7
8
|
var _default = {
|
|
8
9
|
name: 'header',
|
|
9
10
|
lookup: function lookup(req, res, options) {
|
|
@@ -16,10 +17,19 @@ var _default = {
|
|
|
16
17
|
var acceptLanguage = options.lookupHeader ? headers[options.lookupHeader] : headers['accept-language'];
|
|
17
18
|
|
|
18
19
|
if (acceptLanguage) {
|
|
20
|
+
var lookupRegex = /(([a-z]{2})-?([A-Z]{2})?)\s*;?\s*(q=([0-9.]+))?/gi;
|
|
21
|
+
|
|
22
|
+
if (acceptLanguage.indexOf('-') > 0) {
|
|
23
|
+
var foundSpecialCase = specialCases.find(function (s) {
|
|
24
|
+
return acceptLanguage.toLowerCase().indexOf("-".concat(s)) > 0;
|
|
25
|
+
});
|
|
26
|
+
if (foundSpecialCase) lookupRegex = /(([a-z]{2})-?([A-Z]{4})?)\s*;?\s*(q=([0-9.]+))?/gi;
|
|
27
|
+
}
|
|
28
|
+
|
|
19
29
|
var lngs = [];
|
|
20
30
|
var i;
|
|
21
31
|
var m;
|
|
22
|
-
var rgx =
|
|
32
|
+
var rgx = options.lookupHeaderRegex || lookupRegex;
|
|
23
33
|
|
|
24
34
|
do {
|
|
25
35
|
m = rgx.exec(acceptLanguage);
|
|
@@ -53,5 +63,5 @@ var _default = {
|
|
|
53
63
|
return found;
|
|
54
64
|
}
|
|
55
65
|
};
|
|
56
|
-
exports
|
|
66
|
+
exports.default = _default;
|
|
57
67
|
module.exports = exports.default;
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
6
|
+
exports.default = void 0;
|
|
7
7
|
var _default = {
|
|
8
8
|
name: 'path',
|
|
9
9
|
lookup: function lookup(req, res, options) {
|
|
@@ -22,7 +22,6 @@ var _default = {
|
|
|
22
22
|
var parts = path.split('/');
|
|
23
23
|
|
|
24
24
|
if (parts[0] === '') {
|
|
25
|
-
// Handle paths that start with a slash, i.e., '/foo' -> ['', 'foo']
|
|
26
25
|
parts.shift();
|
|
27
26
|
}
|
|
28
27
|
|
|
@@ -34,5 +33,5 @@ var _default = {
|
|
|
34
33
|
return found;
|
|
35
34
|
}
|
|
36
35
|
};
|
|
37
|
-
exports
|
|
36
|
+
exports.default = _default;
|
|
38
37
|
module.exports = exports.default;
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
6
|
+
exports.default = void 0;
|
|
7
7
|
var _default = {
|
|
8
8
|
name: 'querystring',
|
|
9
9
|
lookup: function lookup(req, res, options) {
|
|
@@ -39,5 +39,5 @@ var _default = {
|
|
|
39
39
|
return found;
|
|
40
40
|
}
|
|
41
41
|
};
|
|
42
|
-
exports
|
|
42
|
+
exports.default = _default;
|
|
43
43
|
module.exports = exports.default;
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
6
|
+
exports.default = void 0;
|
|
7
7
|
|
|
8
8
|
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
|
9
9
|
|
|
@@ -26,5 +26,5 @@ var _default = {
|
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
};
|
|
29
|
-
exports
|
|
29
|
+
exports.default = _default;
|
|
30
30
|
module.exports = exports.default;
|
package/cjs/utils.js
CHANGED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
2
|
+
|
|
3
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
4
|
+
|
|
5
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
6
|
+
|
|
7
|
+
import * as utils from './utils.js';
|
|
8
|
+
import cookieLookup from './languageLookups/cookie.js';
|
|
9
|
+
import querystringLookup from './languageLookups/querystring.js';
|
|
10
|
+
import pathLookup from './languageLookups/path.js';
|
|
11
|
+
import headerLookup from './languageLookups/header.js';
|
|
12
|
+
import sessionLookup from './languageLookups/session.js';
|
|
13
|
+
import { extendOptionsWithDefaults } from './httpFunctions.js';
|
|
14
|
+
|
|
15
|
+
function getDefaults() {
|
|
16
|
+
return extendOptionsWithDefaults({
|
|
17
|
+
order: ['querystring', 'cookie', 'header'],
|
|
18
|
+
lookupQuerystring: 'lng',
|
|
19
|
+
lookupCookie: 'i18next',
|
|
20
|
+
lookupSession: 'lng',
|
|
21
|
+
lookupFromPathIndex: 0,
|
|
22
|
+
caches: false,
|
|
23
|
+
cookieSameSite: 'strict'
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
var LanguageDetector = function () {
|
|
28
|
+
function LanguageDetector(services) {
|
|
29
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
30
|
+
var allOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
31
|
+
|
|
32
|
+
_classCallCheck(this, LanguageDetector);
|
|
33
|
+
|
|
34
|
+
this.type = 'languageDetector';
|
|
35
|
+
this.detectors = {};
|
|
36
|
+
this.init(services, options, allOptions);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
_createClass(LanguageDetector, [{
|
|
40
|
+
key: "init",
|
|
41
|
+
value: function init(services) {
|
|
42
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
43
|
+
var allOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
44
|
+
this.services = services;
|
|
45
|
+
this.options = utils.defaults(options, this.options || {}, getDefaults());
|
|
46
|
+
this.allOptions = allOptions;
|
|
47
|
+
this.addDetector(cookieLookup);
|
|
48
|
+
this.addDetector(querystringLookup);
|
|
49
|
+
this.addDetector(pathLookup);
|
|
50
|
+
this.addDetector(headerLookup);
|
|
51
|
+
this.addDetector(sessionLookup);
|
|
52
|
+
}
|
|
53
|
+
}, {
|
|
54
|
+
key: "addDetector",
|
|
55
|
+
value: function addDetector(detector) {
|
|
56
|
+
this.detectors[detector.name] = detector;
|
|
57
|
+
}
|
|
58
|
+
}, {
|
|
59
|
+
key: "detect",
|
|
60
|
+
value: function detect(req, res, detectionOrder) {
|
|
61
|
+
var _this = this;
|
|
62
|
+
|
|
63
|
+
if (arguments.length < 2) return;
|
|
64
|
+
if (!detectionOrder) detectionOrder = this.options.order;
|
|
65
|
+
var found;
|
|
66
|
+
detectionOrder.forEach(function (detectorName) {
|
|
67
|
+
if (found || !_this.detectors[detectorName]) return;
|
|
68
|
+
|
|
69
|
+
var detections = _this.detectors[detectorName].lookup(req, res, _this.options);
|
|
70
|
+
|
|
71
|
+
if (!detections) return;
|
|
72
|
+
if (!Array.isArray(detections)) detections = [detections];
|
|
73
|
+
|
|
74
|
+
if (_this.services.languageUtils.getBestMatchFromCodes) {
|
|
75
|
+
found = _this.services.languageUtils.getBestMatchFromCodes(detections);
|
|
76
|
+
if (found) req.i18nextLookupName = detectorName;
|
|
77
|
+
} else {
|
|
78
|
+
found = detections.length > 0 ? detections[0] : null;
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
if (!found) {
|
|
83
|
+
var fallbacks = this.allOptions.fallbackLng;
|
|
84
|
+
if (typeof fallbacks === 'string') fallbacks = [fallbacks];
|
|
85
|
+
if (!fallbacks) fallbacks = [];
|
|
86
|
+
|
|
87
|
+
if (Object.prototype.toString.apply(fallbacks) === '[object Array]') {
|
|
88
|
+
found = fallbacks[0];
|
|
89
|
+
} else {
|
|
90
|
+
found = fallbacks[0] || fallbacks.default && fallbacks.default[0];
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
;
|
|
95
|
+
return found;
|
|
96
|
+
}
|
|
97
|
+
}, {
|
|
98
|
+
key: "cacheUserLanguage",
|
|
99
|
+
value: function cacheUserLanguage(req, res, lng, caches) {
|
|
100
|
+
var _this2 = this;
|
|
101
|
+
|
|
102
|
+
if (arguments.length < 3) return;
|
|
103
|
+
if (!caches) caches = this.options.caches;
|
|
104
|
+
if (!caches) return;
|
|
105
|
+
caches.forEach(function (cacheName) {
|
|
106
|
+
if (_this2.detectors[cacheName] && _this2.detectors[cacheName].cacheUserLanguage && res.cachedUserLanguage !== lng) {
|
|
107
|
+
_this2.detectors[cacheName].cacheUserLanguage(req, res, lng, _this2.options);
|
|
108
|
+
|
|
109
|
+
res.cachedUserLanguage = lng;
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}]);
|
|
114
|
+
|
|
115
|
+
return LanguageDetector;
|
|
116
|
+
}();
|
|
117
|
+
|
|
118
|
+
LanguageDetector.type = 'languageDetector';
|
|
119
|
+
export default LanguageDetector;
|