i18next-http-middleware 3.2.1 → 3.3.0
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/README.md +13 -0
- package/cjs/LanguageDetector.js +0 -31
- package/cjs/httpFunctions.js +0 -40
- package/cjs/index.js +16 -57
- 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 +16 -43
- 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/index.js +13 -2
- package/lib/languageLookups/header.js +2 -2
- package/package.json +18 -18
|
@@ -5,97 +5,75 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
|
|
8
|
-
|
|
9
8
|
var serializeCookie = function serializeCookie(name, val, options) {
|
|
10
9
|
var opt = options || {};
|
|
11
10
|
opt.path = opt.path || '/';
|
|
12
11
|
var value = encodeURIComponent(val);
|
|
13
12
|
var str = name + '=' + value;
|
|
14
|
-
|
|
15
13
|
if (opt.maxAge > 0) {
|
|
16
14
|
var maxAge = opt.maxAge - 0;
|
|
17
15
|
if (isNaN(maxAge)) throw new Error('maxAge should be a Number');
|
|
18
16
|
str += '; Max-Age=' + Math.floor(maxAge);
|
|
19
17
|
}
|
|
20
|
-
|
|
21
18
|
if (opt.domain) {
|
|
22
19
|
if (!fieldContentRegExp.test(opt.domain)) {
|
|
23
20
|
throw new TypeError('option domain is invalid');
|
|
24
21
|
}
|
|
25
|
-
|
|
26
22
|
str += '; Domain=' + opt.domain;
|
|
27
23
|
}
|
|
28
|
-
|
|
29
24
|
if (opt.path) {
|
|
30
25
|
if (!fieldContentRegExp.test(opt.path)) {
|
|
31
26
|
throw new TypeError('option path is invalid');
|
|
32
27
|
}
|
|
33
|
-
|
|
34
28
|
str += '; Path=' + opt.path;
|
|
35
29
|
}
|
|
36
|
-
|
|
37
30
|
if (opt.expires) {
|
|
38
31
|
if (typeof opt.expires.toUTCString !== 'function') {
|
|
39
32
|
throw new TypeError('option expires is invalid');
|
|
40
33
|
}
|
|
41
|
-
|
|
42
34
|
str += '; Expires=' + opt.expires.toUTCString();
|
|
43
35
|
}
|
|
44
|
-
|
|
45
36
|
if (opt.httpOnly) str += '; HttpOnly';
|
|
46
37
|
if (opt.secure) str += '; Secure';
|
|
47
|
-
|
|
48
38
|
if (opt.sameSite) {
|
|
49
39
|
var sameSite = typeof opt.sameSite === 'string' ? opt.sameSite.toLowerCase() : opt.sameSite;
|
|
50
|
-
|
|
51
40
|
switch (sameSite) {
|
|
52
41
|
case true:
|
|
53
42
|
str += '; SameSite=Strict';
|
|
54
43
|
break;
|
|
55
|
-
|
|
56
44
|
case 'lax':
|
|
57
45
|
str += '; SameSite=Lax';
|
|
58
46
|
break;
|
|
59
|
-
|
|
60
47
|
case 'strict':
|
|
61
48
|
str += '; SameSite=Strict';
|
|
62
49
|
break;
|
|
63
|
-
|
|
64
50
|
case 'none':
|
|
65
51
|
str += '; SameSite=None';
|
|
66
52
|
break;
|
|
67
|
-
|
|
68
53
|
default:
|
|
69
54
|
throw new TypeError('option sameSite is invalid');
|
|
70
55
|
}
|
|
71
56
|
}
|
|
72
|
-
|
|
73
57
|
return str;
|
|
74
58
|
};
|
|
75
|
-
|
|
76
59
|
var _default = {
|
|
77
60
|
name: 'cookie',
|
|
78
61
|
lookup: function lookup(req, res, options) {
|
|
79
62
|
var found;
|
|
80
|
-
|
|
81
63
|
if (options.lookupCookie && typeof req !== 'undefined') {
|
|
82
64
|
var cookies = options.getCookies(req);
|
|
83
65
|
if (cookies) found = cookies[options.lookupCookie];
|
|
84
66
|
}
|
|
85
|
-
|
|
86
67
|
return found;
|
|
87
68
|
},
|
|
88
69
|
cacheUserLanguage: function cacheUserLanguage(req, res, lng) {
|
|
89
70
|
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
90
|
-
|
|
91
71
|
if (options.lookupCookie && req !== 'undefined') {
|
|
92
72
|
var expirationDate = options.cookieExpirationDate;
|
|
93
|
-
|
|
94
73
|
if (!expirationDate) {
|
|
95
74
|
expirationDate = new Date();
|
|
96
75
|
expirationDate.setFullYear(expirationDate.getFullYear() + 1);
|
|
97
76
|
}
|
|
98
|
-
|
|
99
77
|
var cookieOptions = {
|
|
100
78
|
expires: expirationDate,
|
|
101
79
|
domain: options.cookieDomain,
|
|
@@ -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());
|
|
@@ -165,6 +144,12 @@ export function getResourcesHandler(i18next) {
|
|
|
165
144
|
|
|
166
145
|
var languages = options.getQuery(req)[options.lngParam || 'lng'] ? options.getQuery(req)[options.lngParam || 'lng'].split(' ') : [];
|
|
167
146
|
var namespaces = options.getQuery(req)[options.nsParam || 'ns'] ? options.getQuery(req)[options.nsParam || 'ns'].split(' ') : [];
|
|
147
|
+
|
|
148
|
+
if (languages.length === 0 && namespaces.length === 0) {
|
|
149
|
+
languages = options.getParams(req)[options.lngParam || 'lng'] ? options.getParams(req)[options.lngParam || 'lng'].split(' ') : [];
|
|
150
|
+
namespaces = options.getParams(req)[options.nsParam || 'ns'] ? options.getParams(req)[options.nsParam || 'ns'].split(' ') : [];
|
|
151
|
+
}
|
|
152
|
+
|
|
168
153
|
namespaces.forEach(function (ns) {
|
|
169
154
|
if (i18next.options.ns && i18next.options.ns.indexOf(ns) < 0) {
|
|
170
155
|
i18next.options.ns.push(ns);
|
|
@@ -185,39 +170,31 @@ export function missingKeyHandler(i18next) {
|
|
|
185
170
|
extendOptionsWithDefaults(options);
|
|
186
171
|
return function (rq, rs) {
|
|
187
172
|
var _checkForCombinedReqR3 = checkForCombinedReqRes(rq, rs),
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
173
|
+
req = _checkForCombinedReqR3.req,
|
|
174
|
+
res = _checkForCombinedReqR3.res;
|
|
191
175
|
var lng = options.getParams(req)[options.lngParam || 'lng'];
|
|
192
176
|
var ns = options.getParams(req)[options.nsParam || 'ns'];
|
|
193
|
-
|
|
194
177
|
if (!i18next.services.backendConnector) {
|
|
195
178
|
options.setStatus(res, 404);
|
|
196
179
|
return options.send(res, 'i18next-express-middleware:: no backend configured');
|
|
197
180
|
}
|
|
198
|
-
|
|
199
181
|
var body = options.getBody(req);
|
|
200
|
-
|
|
201
182
|
if (typeof body === 'function') {
|
|
202
183
|
var promise = body();
|
|
203
|
-
|
|
204
184
|
if (promise && typeof promise.then === 'function') {
|
|
205
185
|
return new Promise(function (resolve) {
|
|
206
186
|
promise.then(function (b) {
|
|
207
187
|
for (var m in b) {
|
|
208
188
|
i18next.services.backendConnector.saveMissing([lng], ns, m, b[m]);
|
|
209
189
|
}
|
|
210
|
-
|
|
211
190
|
resolve(options.send(res, 'ok'));
|
|
212
191
|
});
|
|
213
192
|
});
|
|
214
193
|
}
|
|
215
194
|
}
|
|
216
|
-
|
|
217
195
|
for (var m in body) {
|
|
218
196
|
i18next.services.backendConnector.saveMissing([lng], ns, m, body[m]);
|
|
219
197
|
}
|
|
220
|
-
|
|
221
198
|
return options.send(res, 'ok');
|
|
222
199
|
};
|
|
223
200
|
}
|
|
@@ -228,14 +205,11 @@ export function addRoute(i18next, route, lngs, app, verb, fc) {
|
|
|
228
205
|
}
|
|
229
206
|
|
|
230
207
|
var callbacks = [fc].concat(Array.prototype.slice.call(arguments, 6));
|
|
231
|
-
|
|
232
208
|
for (var i = 0, li = lngs.length; i < li; i++) {
|
|
233
209
|
var parts = String(route).split('/');
|
|
234
210
|
var locRoute = [];
|
|
235
|
-
|
|
236
211
|
for (var y = 0, ly = parts.length; y < ly; y++) {
|
|
237
212
|
var part = parts[y];
|
|
238
|
-
|
|
239
213
|
if (part === ':lng') {
|
|
240
214
|
locRoute.push(lngs[i]);
|
|
241
215
|
} else if (part.indexOf(':') === 0 || part === '') {
|
|
@@ -246,7 +220,6 @@ export function addRoute(i18next, route, lngs, app, verb, fc) {
|
|
|
246
220
|
}));
|
|
247
221
|
}
|
|
248
222
|
}
|
|
249
|
-
|
|
250
223
|
var routes = [locRoute.join('/')];
|
|
251
224
|
app[verb || 'get'].apply(app, routes.concat(callbacks));
|
|
252
225
|
}
|