i18next-http-middleware 3.2.1 → 3.2.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/cjs/LanguageDetector.js +0 -31
- package/cjs/httpFunctions.js +0 -40
- package/cjs/index.js +11 -58
- package/cjs/languageLookups/cookie.js +0 -22
- package/cjs/languageLookups/header.js +2 -13
- package/cjs/languageLookups/path.js +0 -6
- package/cjs/languageLookups/querystring.js +0 -6
- package/cjs/languageLookups/session.js +0 -5
- package/cjs/utils.js +0 -9
- package/esm/LanguageDetector.js +0 -20
- package/esm/httpFunctions.js +0 -8
- package/esm/index.js +11 -44
- package/esm/languageLookups/cookie.js +1 -22
- package/esm/languageLookups/header.js +2 -13
- package/esm/languageLookups/path.js +0 -6
- package/esm/languageLookups/querystring.js +0 -6
- package/esm/languageLookups/session.js +0 -4
- package/esm/utils.js +0 -4
- package/lib/languageLookups/header.js +2 -2
- package/package.json +18 -18
|
@@ -9,36 +9,29 @@ var _default = {
|
|
|
9
9
|
name: 'header',
|
|
10
10
|
lookup: function lookup(req, res, options) {
|
|
11
11
|
var found;
|
|
12
|
-
|
|
13
12
|
if (typeof req !== 'undefined') {
|
|
14
13
|
var headers = options.getHeaders(req);
|
|
15
14
|
if (!headers) return found;
|
|
16
15
|
var locales = [];
|
|
17
16
|
var acceptLanguage = options.lookupHeader ? headers[options.lookupHeader] : headers['accept-language'];
|
|
18
|
-
|
|
19
17
|
if (acceptLanguage) {
|
|
20
|
-
var lookupRegex = /(([a-z]{2})-?([A-Z]{2})?)\s*;?\s*(q=([0-9.]+))?/gi;
|
|
21
|
-
|
|
18
|
+
var lookupRegex = /(([a-z]{2,3})-?([A-Z]{2})?)\s*;?\s*(q=([0-9.]+))?/gi;
|
|
22
19
|
if (acceptLanguage.indexOf('-') > 0) {
|
|
23
20
|
var foundSpecialCase = specialCases.find(function (s) {
|
|
24
21
|
return acceptLanguage.toLowerCase().indexOf("-".concat(s)) > 0;
|
|
25
22
|
});
|
|
26
|
-
if (foundSpecialCase) lookupRegex = /(([a-z]{2})-?([A-Z]{4})?)\s*;?\s*(q=([0-9.]+))?/gi;
|
|
23
|
+
if (foundSpecialCase) lookupRegex = /(([a-z]{2,3})-?([A-Z]{4})?)\s*;?\s*(q=([0-9.]+))?/gi;
|
|
27
24
|
}
|
|
28
|
-
|
|
29
25
|
var lngs = [];
|
|
30
26
|
var i;
|
|
31
27
|
var m;
|
|
32
28
|
var rgx = options.lookupHeaderRegex || lookupRegex;
|
|
33
|
-
|
|
34
29
|
do {
|
|
35
30
|
m = rgx.exec(acceptLanguage);
|
|
36
|
-
|
|
37
31
|
if (m) {
|
|
38
32
|
var lng = m[1];
|
|
39
33
|
var weight = m[5] || '1';
|
|
40
34
|
var q = Number(weight);
|
|
41
|
-
|
|
42
35
|
if (lng && !isNaN(q)) {
|
|
43
36
|
lngs.push({
|
|
44
37
|
lng: lng,
|
|
@@ -47,19 +40,15 @@ var _default = {
|
|
|
47
40
|
}
|
|
48
41
|
}
|
|
49
42
|
} while (m);
|
|
50
|
-
|
|
51
43
|
lngs.sort(function (a, b) {
|
|
52
44
|
return b.q - a.q;
|
|
53
45
|
});
|
|
54
|
-
|
|
55
46
|
for (i = 0; i < lngs.length; i++) {
|
|
56
47
|
locales.push(lngs[i].lng);
|
|
57
48
|
}
|
|
58
|
-
|
|
59
49
|
if (locales.length) found = locales;
|
|
60
50
|
}
|
|
61
51
|
}
|
|
62
|
-
|
|
63
52
|
return found;
|
|
64
53
|
}
|
|
65
54
|
};
|
|
@@ -8,28 +8,22 @@ var _default = {
|
|
|
8
8
|
name: 'path',
|
|
9
9
|
lookup: function lookup(req, res, options) {
|
|
10
10
|
var found;
|
|
11
|
-
|
|
12
11
|
if (req === undefined) {
|
|
13
12
|
return found;
|
|
14
13
|
}
|
|
15
|
-
|
|
16
14
|
if (options.lookupPath !== undefined && req.params) {
|
|
17
15
|
found = options.getParams(req)[options.lookupPath];
|
|
18
16
|
}
|
|
19
|
-
|
|
20
17
|
if (!found && typeof options.lookupFromPathIndex === 'number' && options.getOriginalUrl(req)) {
|
|
21
18
|
var path = options.getOriginalUrl(req).split('?')[0];
|
|
22
19
|
var parts = path.split('/');
|
|
23
|
-
|
|
24
20
|
if (parts[0] === '') {
|
|
25
21
|
parts.shift();
|
|
26
22
|
}
|
|
27
|
-
|
|
28
23
|
if (parts.length > options.lookupFromPathIndex) {
|
|
29
24
|
found = parts[options.lookupFromPathIndex];
|
|
30
25
|
}
|
|
31
26
|
}
|
|
32
|
-
|
|
33
27
|
return found;
|
|
34
28
|
}
|
|
35
29
|
};
|
|
@@ -8,24 +8,19 @@ var _default = {
|
|
|
8
8
|
name: 'querystring',
|
|
9
9
|
lookup: function lookup(req, res, options) {
|
|
10
10
|
var found;
|
|
11
|
-
|
|
12
11
|
if (options.lookupQuerystring !== undefined && typeof req !== 'undefined') {
|
|
13
12
|
if (options.getQuery(req)) {
|
|
14
13
|
found = options.getQuery(req)[options.lookupQuerystring];
|
|
15
14
|
}
|
|
16
|
-
|
|
17
15
|
if (!found && options.getUrl(req) && options.getUrl(req).indexOf('?')) {
|
|
18
16
|
var lastPartOfUri = options.getUrl(req).substring(options.getUrl(req).indexOf('?'));
|
|
19
|
-
|
|
20
17
|
if (typeof URLSearchParams !== 'undefined') {
|
|
21
18
|
var urlParams = new URLSearchParams(lastPartOfUri);
|
|
22
19
|
found = urlParams.get(options.lookupQuerystring);
|
|
23
20
|
} else {
|
|
24
21
|
var indexOfQsStart = lastPartOfUri.indexOf("".concat(options.lookupQuerystring, "="));
|
|
25
|
-
|
|
26
22
|
if (indexOfQsStart > -1) {
|
|
27
23
|
var restOfUri = lastPartOfUri.substring(options.lookupQuerystring.length + 2);
|
|
28
|
-
|
|
29
24
|
if (restOfUri.indexOf('&') < 0) {
|
|
30
25
|
found = restOfUri;
|
|
31
26
|
} else {
|
|
@@ -35,7 +30,6 @@ var _default = {
|
|
|
35
30
|
}
|
|
36
31
|
}
|
|
37
32
|
}
|
|
38
|
-
|
|
39
33
|
return found;
|
|
40
34
|
}
|
|
41
35
|
};
|
|
@@ -4,23 +4,18 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
9
|
-
|
|
10
8
|
var _default = {
|
|
11
9
|
name: 'session',
|
|
12
10
|
lookup: function lookup(req, res, options) {
|
|
13
11
|
var found;
|
|
14
|
-
|
|
15
12
|
if (options.lookupSession !== undefined && _typeof(req) && options.getSession(req)) {
|
|
16
13
|
found = options.getSession(req)[options.lookupSession];
|
|
17
14
|
}
|
|
18
|
-
|
|
19
15
|
return found;
|
|
20
16
|
},
|
|
21
17
|
cacheUserLanguage: function cacheUserLanguage(req, res, lng) {
|
|
22
18
|
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
23
|
-
|
|
24
19
|
if (options.lookupSession && req && options.getSession(req)) {
|
|
25
20
|
options.getSession(req)[options.lookupSession] = lng;
|
|
26
21
|
}
|
package/cjs/utils.js
CHANGED
|
@@ -7,29 +7,23 @@ exports.defaults = defaults;
|
|
|
7
7
|
exports.extend = extend;
|
|
8
8
|
exports.removeLngFromUrl = removeLngFromUrl;
|
|
9
9
|
exports.setPath = setPath;
|
|
10
|
-
|
|
11
10
|
function setPath(object, path, newValue) {
|
|
12
11
|
var stack;
|
|
13
12
|
if (typeof path !== 'string') stack = [].concat(path);
|
|
14
13
|
if (typeof path === 'string') stack = path.split('.');
|
|
15
|
-
|
|
16
14
|
while (stack.length > 1) {
|
|
17
15
|
var _key = stack.shift();
|
|
18
|
-
|
|
19
16
|
if (_key.indexOf('###') > -1) _key = _key.replace(/###/g, '.');
|
|
20
17
|
if (!object[_key]) object[_key] = {};
|
|
21
18
|
object = object[_key];
|
|
22
19
|
}
|
|
23
|
-
|
|
24
20
|
var key = stack.shift();
|
|
25
21
|
if (key.indexOf('###') > -1) key = key.replace(/###/g, '.');
|
|
26
22
|
object[key] = newValue;
|
|
27
23
|
}
|
|
28
|
-
|
|
29
24
|
var arr = [];
|
|
30
25
|
var each = arr.forEach;
|
|
31
26
|
var slice = arr.slice;
|
|
32
|
-
|
|
33
27
|
function defaults(obj) {
|
|
34
28
|
each.call(slice.call(arguments, 1), function (source) {
|
|
35
29
|
if (source) {
|
|
@@ -40,7 +34,6 @@ function defaults(obj) {
|
|
|
40
34
|
});
|
|
41
35
|
return obj;
|
|
42
36
|
}
|
|
43
|
-
|
|
44
37
|
function extend(obj) {
|
|
45
38
|
each.call(slice.call(arguments, 1), function (source) {
|
|
46
39
|
if (source) {
|
|
@@ -51,11 +44,9 @@ function extend(obj) {
|
|
|
51
44
|
});
|
|
52
45
|
return obj;
|
|
53
46
|
}
|
|
54
|
-
|
|
55
47
|
function removeLngFromUrl(url, lookupFromPathIndex) {
|
|
56
48
|
var first = '';
|
|
57
49
|
var pos = lookupFromPathIndex;
|
|
58
|
-
|
|
59
50
|
if (url[0] === '/') {
|
|
60
51
|
pos++;
|
|
61
52
|
first = '/';
|
package/esm/LanguageDetector.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
2
|
-
|
|
3
2
|
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
3
|
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
6
|
-
|
|
7
4
|
import * as utils from './utils.js';
|
|
8
5
|
import cookieLookup from './languageLookups/cookie.js';
|
|
9
6
|
import querystringLookup from './languageLookups/querystring.js';
|
|
@@ -11,7 +8,6 @@ import pathLookup from './languageLookups/path.js';
|
|
|
11
8
|
import headerLookup from './languageLookups/header.js';
|
|
12
9
|
import sessionLookup from './languageLookups/session.js';
|
|
13
10
|
import { extendOptionsWithDefaults } from './httpFunctions.js';
|
|
14
|
-
|
|
15
11
|
function getDefaults() {
|
|
16
12
|
return extendOptionsWithDefaults({
|
|
17
13
|
order: ['querystring', 'cookie', 'header'],
|
|
@@ -24,19 +20,15 @@ function getDefaults() {
|
|
|
24
20
|
ignoreCase: true
|
|
25
21
|
});
|
|
26
22
|
}
|
|
27
|
-
|
|
28
23
|
var LanguageDetector = function () {
|
|
29
24
|
function LanguageDetector(services) {
|
|
30
25
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
31
26
|
var allOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
32
|
-
|
|
33
27
|
_classCallCheck(this, LanguageDetector);
|
|
34
|
-
|
|
35
28
|
this.type = 'languageDetector';
|
|
36
29
|
this.detectors = {};
|
|
37
30
|
this.init(services, options, allOptions);
|
|
38
31
|
}
|
|
39
|
-
|
|
40
32
|
_createClass(LanguageDetector, [{
|
|
41
33
|
key: "init",
|
|
42
34
|
value: function init(services) {
|
|
@@ -60,24 +52,19 @@ var LanguageDetector = function () {
|
|
|
60
52
|
key: "detect",
|
|
61
53
|
value: function detect(req, res, detectionOrder) {
|
|
62
54
|
var _this = this;
|
|
63
|
-
|
|
64
55
|
if (arguments.length < 2) return;
|
|
65
56
|
if (!detectionOrder) detectionOrder = this.options.order;
|
|
66
57
|
var found;
|
|
67
58
|
detectionOrder.forEach(function (detectorName) {
|
|
68
59
|
if (found || !_this.detectors[detectorName]) return;
|
|
69
|
-
|
|
70
60
|
var detections = _this.detectors[detectorName].lookup(req, res, _this.options);
|
|
71
|
-
|
|
72
61
|
if (!detections) return;
|
|
73
62
|
if (!Array.isArray(detections)) detections = [detections];
|
|
74
63
|
detections = detections.filter(function (d) {
|
|
75
64
|
return d !== undefined && d !== null;
|
|
76
65
|
});
|
|
77
|
-
|
|
78
66
|
if (_this.services.languageUtils.getBestMatchFromCodes) {
|
|
79
67
|
found = _this.services.languageUtils.getBestMatchFromCodes(detections);
|
|
80
|
-
|
|
81
68
|
if (found) {
|
|
82
69
|
if (_this.options.ignoreCase) {
|
|
83
70
|
if (detections.map(function (d) {
|
|
@@ -87,7 +74,6 @@ var LanguageDetector = function () {
|
|
|
87
74
|
if (detections.indexOf(found) < 0) found = undefined;
|
|
88
75
|
}
|
|
89
76
|
}
|
|
90
|
-
|
|
91
77
|
if (found) req.i18nextLookupName = detectorName;
|
|
92
78
|
} else {
|
|
93
79
|
found = detections.length > 0 ? detections[0] : null;
|
|
@@ -99,14 +85,12 @@ var LanguageDetector = function () {
|
|
|
99
85
|
if (typeof fallbacks === 'function') fallbacks = fallbacks();
|
|
100
86
|
if (typeof fallbacks === 'string') fallbacks = [fallbacks];
|
|
101
87
|
if (!fallbacks) fallbacks = [];
|
|
102
|
-
|
|
103
88
|
if (Object.prototype.toString.apply(fallbacks) === '[object Array]') {
|
|
104
89
|
found = fallbacks[0];
|
|
105
90
|
} else {
|
|
106
91
|
found = fallbacks[0] || fallbacks.default && fallbacks.default[0];
|
|
107
92
|
}
|
|
108
93
|
}
|
|
109
|
-
|
|
110
94
|
;
|
|
111
95
|
return found;
|
|
112
96
|
}
|
|
@@ -114,22 +98,18 @@ var LanguageDetector = function () {
|
|
|
114
98
|
key: "cacheUserLanguage",
|
|
115
99
|
value: function cacheUserLanguage(req, res, lng, caches) {
|
|
116
100
|
var _this2 = this;
|
|
117
|
-
|
|
118
101
|
if (arguments.length < 3) return;
|
|
119
102
|
if (!caches) caches = this.options.caches;
|
|
120
103
|
if (!caches) return;
|
|
121
104
|
caches.forEach(function (cacheName) {
|
|
122
105
|
if (_this2.detectors[cacheName] && _this2.detectors[cacheName].cacheUserLanguage && res.cachedUserLanguage !== lng) {
|
|
123
106
|
_this2.detectors[cacheName].cacheUserLanguage(req, res, lng, _this2.options);
|
|
124
|
-
|
|
125
107
|
res.cachedUserLanguage = lng;
|
|
126
108
|
}
|
|
127
109
|
});
|
|
128
110
|
}
|
|
129
111
|
}]);
|
|
130
|
-
|
|
131
112
|
return LanguageDetector;
|
|
132
113
|
}();
|
|
133
|
-
|
|
134
114
|
LanguageDetector.type = 'languageDetector';
|
|
135
115
|
export default LanguageDetector;
|
package/esm/httpFunctions.js
CHANGED
|
@@ -15,7 +15,6 @@ export var setUrl = function setUrl(req, url) {
|
|
|
15
15
|
req.url = url;
|
|
16
16
|
return;
|
|
17
17
|
}
|
|
18
|
-
|
|
19
18
|
console.log('no possibility found to get url');
|
|
20
19
|
};
|
|
21
20
|
export var getOriginalUrl = function getOriginalUrl(req) {
|
|
@@ -27,7 +26,6 @@ export var getQuery = function getQuery(req) {
|
|
|
27
26
|
if (req.query && typeof req.query.entries === 'function' && typeof Object.fromEntries === 'function' && typeof req.query[Symbol.iterator] === 'function') {
|
|
28
27
|
return Object.fromEntries(req.query);
|
|
29
28
|
}
|
|
30
|
-
|
|
31
29
|
if (req.query) return req.query;
|
|
32
30
|
if (req.searchParams) return req.searchParams;
|
|
33
31
|
if (req.raw && req.raw.query) return req.raw.query;
|
|
@@ -50,7 +48,6 @@ export var getHeaders = function getHeaders(req) {
|
|
|
50
48
|
};
|
|
51
49
|
export var getCookies = function getCookies(req) {
|
|
52
50
|
if (req.cookies) return req.cookies;
|
|
53
|
-
|
|
54
51
|
if (getHeaders(req)) {
|
|
55
52
|
var list = {};
|
|
56
53
|
var rc = getHeaders(req).cookie;
|
|
@@ -60,14 +57,12 @@ export var getCookies = function getCookies(req) {
|
|
|
60
57
|
});
|
|
61
58
|
return list;
|
|
62
59
|
}
|
|
63
|
-
|
|
64
60
|
console.log('no possibility found to get cookies');
|
|
65
61
|
};
|
|
66
62
|
export var getBody = function getBody(req) {
|
|
67
63
|
if (req.ctx && typeof req.ctx.body === 'function') {
|
|
68
64
|
return req.ctx.body.bind(req.ctx);
|
|
69
65
|
}
|
|
70
|
-
|
|
71
66
|
if (req.ctx && req.ctx.body) return req.ctx.body;
|
|
72
67
|
if (req.json) return req.json;
|
|
73
68
|
if (req.body) return req.body;
|
|
@@ -86,15 +81,12 @@ export var setHeader = function setHeader(res, name, value) {
|
|
|
86
81
|
if (res._headerSent || res.headersSent) return;
|
|
87
82
|
if (typeof res.setHeader === 'function') return res.setHeader(name, value);
|
|
88
83
|
if (typeof res.header === 'function') return res.header(name, value);
|
|
89
|
-
|
|
90
84
|
if (res.responseHeaders && typeof res.responseHeaders.set === 'function') {
|
|
91
85
|
return res.responseHeaders.set(name, value);
|
|
92
86
|
}
|
|
93
|
-
|
|
94
87
|
if (res.headers && typeof res.headers.set === 'function') {
|
|
95
88
|
return res.headers.set(name, value);
|
|
96
89
|
}
|
|
97
|
-
|
|
98
90
|
console.log('no possibility found to set header');
|
|
99
91
|
};
|
|
100
92
|
export var setContentType = function setContentType(res, type) {
|
package/esm/index.js
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
2
|
-
|
|
3
2
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
4
|
-
|
|
5
3
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
6
|
-
|
|
7
4
|
import * as utils from './utils.js';
|
|
8
5
|
import LD from './LanguageDetector.js';
|
|
9
6
|
import { extendOptionsWithDefaults } from './httpFunctions.js';
|
|
10
7
|
export var LanguageDetector = LD;
|
|
11
|
-
|
|
12
8
|
var checkForCombinedReqRes = function checkForCombinedReqRes(req, res, next) {
|
|
13
9
|
if (!res) {
|
|
14
10
|
if (req.request && req.response) {
|
|
@@ -21,82 +17,70 @@ var checkForCombinedReqRes = function checkForCombinedReqRes(req, res, next) {
|
|
|
21
17
|
if (!next) next = function next() {};
|
|
22
18
|
}
|
|
23
19
|
}
|
|
24
|
-
|
|
25
20
|
return {
|
|
26
21
|
req: req,
|
|
27
22
|
res: res,
|
|
28
23
|
next: next
|
|
29
24
|
};
|
|
30
25
|
};
|
|
31
|
-
|
|
32
26
|
export function handle(i18next) {
|
|
33
27
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
34
28
|
extendOptionsWithDefaults(options);
|
|
35
29
|
return function i18nextMiddleware(rq, rs, n) {
|
|
36
30
|
var _checkForCombinedReqR = checkForCombinedReqRes(rq, rs, n),
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
31
|
+
req = _checkForCombinedReqR.req,
|
|
32
|
+
res = _checkForCombinedReqR.res,
|
|
33
|
+
next = _checkForCombinedReqR.next;
|
|
41
34
|
if (typeof options.ignoreRoutes === 'function') {
|
|
42
35
|
if (options.ignoreRoutes(req, res, options, i18next)) {
|
|
43
36
|
return next();
|
|
44
37
|
}
|
|
45
38
|
} else {
|
|
46
39
|
var ignores = options.ignoreRoutes instanceof Array && options.ignoreRoutes || [];
|
|
47
|
-
|
|
48
40
|
for (var i = 0; i < ignores.length; i++) {
|
|
49
41
|
if (options.getPath(req).indexOf(ignores[i]) > -1) return next();
|
|
50
42
|
}
|
|
51
43
|
}
|
|
52
|
-
|
|
53
44
|
var i18n = i18next.cloneInstance({
|
|
54
45
|
initImmediate: false
|
|
55
46
|
});
|
|
56
47
|
i18n.on('languageChanged', function (lng) {
|
|
57
48
|
req.language = req.locale = req.lng = lng;
|
|
58
49
|
if (options.attachLocals) res.locals = res.locals || {};
|
|
59
|
-
|
|
60
50
|
if (res.locals) {
|
|
61
51
|
res.locals.language = lng;
|
|
62
52
|
res.locals.languageDir = i18next.dir(lng);
|
|
63
53
|
}
|
|
64
|
-
|
|
65
54
|
if (lng && options.getHeader(res, 'Content-Language') !== lng) {
|
|
66
55
|
options.setHeader(res, 'Content-Language', lng);
|
|
67
56
|
}
|
|
68
|
-
|
|
69
57
|
req.languages = i18next.services.languageUtils.toResolveHierarchy(lng);
|
|
70
|
-
|
|
71
58
|
if (i18next.services.languageDetector) {
|
|
72
59
|
i18next.services.languageDetector.cacheUserLanguage(req, res, lng);
|
|
73
60
|
}
|
|
74
61
|
});
|
|
75
62
|
var lng = req.lng;
|
|
76
|
-
|
|
77
63
|
if (!lng && i18next.services.languageDetector) {
|
|
78
64
|
lng = i18next.services.languageDetector.detect(req, res);
|
|
79
65
|
}
|
|
80
66
|
|
|
81
67
|
req.language = req.locale = req.lng = lng;
|
|
82
|
-
|
|
83
68
|
if (lng && options.getHeader(res, 'Content-Language') !== lng) {
|
|
84
69
|
options.setHeader(res, 'Content-Language', lng);
|
|
85
70
|
}
|
|
86
|
-
|
|
87
71
|
req.languages = i18next.services.languageUtils.toResolveHierarchy(lng);
|
|
88
|
-
i18n.changeLanguage(lng || i18next.options.fallbackLng[0]);
|
|
89
72
|
|
|
73
|
+
i18n.changeLanguage(lng || i18next.options.fallbackLng[0]);
|
|
90
74
|
if (req.i18nextLookupName === 'path' && options.removeLngFromUrl) {
|
|
91
75
|
options.setUrl(req, utils.removeLngFromUrl(options.getUrl(req), i18next.services.languageDetector.options.lookupFromPathIndex));
|
|
92
76
|
}
|
|
93
|
-
|
|
94
77
|
var t = i18n.t.bind(i18n);
|
|
95
78
|
var exists = i18n.exists.bind(i18n);
|
|
79
|
+
|
|
96
80
|
req.i18n = i18n;
|
|
97
81
|
req.t = t;
|
|
98
|
-
if (options.attachLocals) res.locals = res.locals || {};
|
|
99
82
|
|
|
83
|
+
if (options.attachLocals) res.locals = res.locals || {};
|
|
100
84
|
if (res.locals) {
|
|
101
85
|
res.locals.t = t;
|
|
102
86
|
res.locals.exists = exists;
|
|
@@ -104,11 +88,9 @@ export function handle(i18next) {
|
|
|
104
88
|
res.locals.language = lng;
|
|
105
89
|
res.locals.languageDir = i18next.dir(lng);
|
|
106
90
|
}
|
|
107
|
-
|
|
108
91
|
if (i18next.services.languageDetector) {
|
|
109
92
|
i18next.services.languageDetector.cacheUserLanguage(req, res, lng);
|
|
110
93
|
}
|
|
111
|
-
|
|
112
94
|
if (!req.lng) return next();
|
|
113
95
|
i18next.loadLanguages(req.lng, function () {
|
|
114
96
|
return next();
|
|
@@ -144,17 +126,14 @@ export function getResourcesHandler(i18next) {
|
|
|
144
126
|
var maxAge = options.maxAge || 60 * 60 * 24 * 30;
|
|
145
127
|
return function (rq, rs) {
|
|
146
128
|
var _checkForCombinedReqR2 = checkForCombinedReqRes(rq, rs),
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
129
|
+
req = _checkForCombinedReqR2.req,
|
|
130
|
+
res = _checkForCombinedReqR2.res;
|
|
150
131
|
if (!i18next.services.backendConnector) {
|
|
151
132
|
options.setStatus(res, 404);
|
|
152
133
|
return options.send(res, 'i18next-express-middleware:: no backend configured');
|
|
153
134
|
}
|
|
154
|
-
|
|
155
135
|
var resources = {};
|
|
156
136
|
options.setContentType(res, 'application/json');
|
|
157
|
-
|
|
158
137
|
if (options.cache !== undefined ? options.cache : typeof process !== 'undefined' && process.env && process.env.NODE_ENV === 'production') {
|
|
159
138
|
options.setHeader(res, 'Cache-Control', 'public, max-age=' + maxAge);
|
|
160
139
|
options.setHeader(res, 'Expires', new Date(new Date().getTime() + maxAge * 1000).toUTCString());
|
|
@@ -162,9 +141,9 @@ export function getResourcesHandler(i18next) {
|
|
|
162
141
|
options.setHeader(res, 'Pragma', 'no-cache');
|
|
163
142
|
options.setHeader(res, 'Cache-Control', 'no-cache');
|
|
164
143
|
}
|
|
165
|
-
|
|
166
144
|
var languages = options.getQuery(req)[options.lngParam || 'lng'] ? options.getQuery(req)[options.lngParam || 'lng'].split(' ') : [];
|
|
167
145
|
var namespaces = options.getQuery(req)[options.nsParam || 'ns'] ? options.getQuery(req)[options.nsParam || 'ns'].split(' ') : [];
|
|
146
|
+
|
|
168
147
|
namespaces.forEach(function (ns) {
|
|
169
148
|
if (i18next.options.ns && i18next.options.ns.indexOf(ns) < 0) {
|
|
170
149
|
i18next.options.ns.push(ns);
|
|
@@ -185,39 +164,31 @@ export function missingKeyHandler(i18next) {
|
|
|
185
164
|
extendOptionsWithDefaults(options);
|
|
186
165
|
return function (rq, rs) {
|
|
187
166
|
var _checkForCombinedReqR3 = checkForCombinedReqRes(rq, rs),
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
167
|
+
req = _checkForCombinedReqR3.req,
|
|
168
|
+
res = _checkForCombinedReqR3.res;
|
|
191
169
|
var lng = options.getParams(req)[options.lngParam || 'lng'];
|
|
192
170
|
var ns = options.getParams(req)[options.nsParam || 'ns'];
|
|
193
|
-
|
|
194
171
|
if (!i18next.services.backendConnector) {
|
|
195
172
|
options.setStatus(res, 404);
|
|
196
173
|
return options.send(res, 'i18next-express-middleware:: no backend configured');
|
|
197
174
|
}
|
|
198
|
-
|
|
199
175
|
var body = options.getBody(req);
|
|
200
|
-
|
|
201
176
|
if (typeof body === 'function') {
|
|
202
177
|
var promise = body();
|
|
203
|
-
|
|
204
178
|
if (promise && typeof promise.then === 'function') {
|
|
205
179
|
return new Promise(function (resolve) {
|
|
206
180
|
promise.then(function (b) {
|
|
207
181
|
for (var m in b) {
|
|
208
182
|
i18next.services.backendConnector.saveMissing([lng], ns, m, b[m]);
|
|
209
183
|
}
|
|
210
|
-
|
|
211
184
|
resolve(options.send(res, 'ok'));
|
|
212
185
|
});
|
|
213
186
|
});
|
|
214
187
|
}
|
|
215
188
|
}
|
|
216
|
-
|
|
217
189
|
for (var m in body) {
|
|
218
190
|
i18next.services.backendConnector.saveMissing([lng], ns, m, body[m]);
|
|
219
191
|
}
|
|
220
|
-
|
|
221
192
|
return options.send(res, 'ok');
|
|
222
193
|
};
|
|
223
194
|
}
|
|
@@ -228,14 +199,11 @@ export function addRoute(i18next, route, lngs, app, verb, fc) {
|
|
|
228
199
|
}
|
|
229
200
|
|
|
230
201
|
var callbacks = [fc].concat(Array.prototype.slice.call(arguments, 6));
|
|
231
|
-
|
|
232
202
|
for (var i = 0, li = lngs.length; i < li; i++) {
|
|
233
203
|
var parts = String(route).split('/');
|
|
234
204
|
var locRoute = [];
|
|
235
|
-
|
|
236
205
|
for (var y = 0, ly = parts.length; y < ly; y++) {
|
|
237
206
|
var part = parts[y];
|
|
238
|
-
|
|
239
207
|
if (part === ':lng') {
|
|
240
208
|
locRoute.push(lngs[i]);
|
|
241
209
|
} else if (part.indexOf(':') === 0 || part === '') {
|
|
@@ -246,7 +214,6 @@ export function addRoute(i18next, route, lngs, app, verb, fc) {
|
|
|
246
214
|
}));
|
|
247
215
|
}
|
|
248
216
|
}
|
|
249
|
-
|
|
250
217
|
var routes = [locRoute.join('/')];
|
|
251
218
|
app[verb || 'get'].apply(app, routes.concat(callbacks));
|
|
252
219
|
}
|
|
@@ -1,95 +1,74 @@
|
|
|
1
|
-
var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
|
|
2
1
|
|
|
2
|
+
var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
|
|
3
3
|
var serializeCookie = function serializeCookie(name, val, options) {
|
|
4
4
|
var opt = options || {};
|
|
5
5
|
opt.path = opt.path || '/';
|
|
6
6
|
var value = encodeURIComponent(val);
|
|
7
7
|
var str = name + '=' + value;
|
|
8
|
-
|
|
9
8
|
if (opt.maxAge > 0) {
|
|
10
9
|
var maxAge = opt.maxAge - 0;
|
|
11
10
|
if (isNaN(maxAge)) throw new Error('maxAge should be a Number');
|
|
12
11
|
str += '; Max-Age=' + Math.floor(maxAge);
|
|
13
12
|
}
|
|
14
|
-
|
|
15
13
|
if (opt.domain) {
|
|
16
14
|
if (!fieldContentRegExp.test(opt.domain)) {
|
|
17
15
|
throw new TypeError('option domain is invalid');
|
|
18
16
|
}
|
|
19
|
-
|
|
20
17
|
str += '; Domain=' + opt.domain;
|
|
21
18
|
}
|
|
22
|
-
|
|
23
19
|
if (opt.path) {
|
|
24
20
|
if (!fieldContentRegExp.test(opt.path)) {
|
|
25
21
|
throw new TypeError('option path is invalid');
|
|
26
22
|
}
|
|
27
|
-
|
|
28
23
|
str += '; Path=' + opt.path;
|
|
29
24
|
}
|
|
30
|
-
|
|
31
25
|
if (opt.expires) {
|
|
32
26
|
if (typeof opt.expires.toUTCString !== 'function') {
|
|
33
27
|
throw new TypeError('option expires is invalid');
|
|
34
28
|
}
|
|
35
|
-
|
|
36
29
|
str += '; Expires=' + opt.expires.toUTCString();
|
|
37
30
|
}
|
|
38
|
-
|
|
39
31
|
if (opt.httpOnly) str += '; HttpOnly';
|
|
40
32
|
if (opt.secure) str += '; Secure';
|
|
41
|
-
|
|
42
33
|
if (opt.sameSite) {
|
|
43
34
|
var sameSite = typeof opt.sameSite === 'string' ? opt.sameSite.toLowerCase() : opt.sameSite;
|
|
44
|
-
|
|
45
35
|
switch (sameSite) {
|
|
46
36
|
case true:
|
|
47
37
|
str += '; SameSite=Strict';
|
|
48
38
|
break;
|
|
49
|
-
|
|
50
39
|
case 'lax':
|
|
51
40
|
str += '; SameSite=Lax';
|
|
52
41
|
break;
|
|
53
|
-
|
|
54
42
|
case 'strict':
|
|
55
43
|
str += '; SameSite=Strict';
|
|
56
44
|
break;
|
|
57
|
-
|
|
58
45
|
case 'none':
|
|
59
46
|
str += '; SameSite=None';
|
|
60
47
|
break;
|
|
61
|
-
|
|
62
48
|
default:
|
|
63
49
|
throw new TypeError('option sameSite is invalid');
|
|
64
50
|
}
|
|
65
51
|
}
|
|
66
|
-
|
|
67
52
|
return str;
|
|
68
53
|
};
|
|
69
|
-
|
|
70
54
|
export default {
|
|
71
55
|
name: 'cookie',
|
|
72
56
|
lookup: function lookup(req, res, options) {
|
|
73
57
|
var found;
|
|
74
|
-
|
|
75
58
|
if (options.lookupCookie && typeof req !== 'undefined') {
|
|
76
59
|
var cookies = options.getCookies(req);
|
|
77
60
|
if (cookies) found = cookies[options.lookupCookie];
|
|
78
61
|
}
|
|
79
|
-
|
|
80
62
|
return found;
|
|
81
63
|
},
|
|
82
64
|
cacheUserLanguage: function cacheUserLanguage(req, res, lng) {
|
|
83
65
|
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
84
|
-
|
|
85
66
|
if (options.lookupCookie && req !== 'undefined') {
|
|
86
67
|
var expirationDate = options.cookieExpirationDate;
|
|
87
|
-
|
|
88
68
|
if (!expirationDate) {
|
|
89
69
|
expirationDate = new Date();
|
|
90
70
|
expirationDate.setFullYear(expirationDate.getFullYear() + 1);
|
|
91
71
|
}
|
|
92
|
-
|
|
93
72
|
var cookieOptions = {
|
|
94
73
|
expires: expirationDate,
|
|
95
74
|
domain: options.cookieDomain,
|