i18next-http-middleware 3.2.0 → 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 +12 -59
- 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 +12 -45
- 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 +1 -1
- package/lib/languageLookups/header.js +2 -2
- package/package.json +18 -18
|
@@ -3,36 +3,29 @@ export default {
|
|
|
3
3
|
name: 'header',
|
|
4
4
|
lookup: function lookup(req, res, options) {
|
|
5
5
|
var found;
|
|
6
|
-
|
|
7
6
|
if (typeof req !== 'undefined') {
|
|
8
7
|
var headers = options.getHeaders(req);
|
|
9
8
|
if (!headers) return found;
|
|
10
9
|
var locales = [];
|
|
11
10
|
var acceptLanguage = options.lookupHeader ? headers[options.lookupHeader] : headers['accept-language'];
|
|
12
|
-
|
|
13
11
|
if (acceptLanguage) {
|
|
14
|
-
var lookupRegex = /(([a-z]{2})-?([A-Z]{2})?)\s*;?\s*(q=([0-9.]+))?/gi;
|
|
15
|
-
|
|
12
|
+
var lookupRegex = /(([a-z]{2,3})-?([A-Z]{2})?)\s*;?\s*(q=([0-9.]+))?/gi;
|
|
16
13
|
if (acceptLanguage.indexOf('-') > 0) {
|
|
17
14
|
var foundSpecialCase = specialCases.find(function (s) {
|
|
18
15
|
return acceptLanguage.toLowerCase().indexOf("-".concat(s)) > 0;
|
|
19
16
|
});
|
|
20
|
-
if (foundSpecialCase) lookupRegex = /(([a-z]{2})-?([A-Z]{4})?)\s*;?\s*(q=([0-9.]+))?/gi;
|
|
17
|
+
if (foundSpecialCase) lookupRegex = /(([a-z]{2,3})-?([A-Z]{4})?)\s*;?\s*(q=([0-9.]+))?/gi;
|
|
21
18
|
}
|
|
22
|
-
|
|
23
19
|
var lngs = [];
|
|
24
20
|
var i;
|
|
25
21
|
var m;
|
|
26
22
|
var rgx = options.lookupHeaderRegex || lookupRegex;
|
|
27
|
-
|
|
28
23
|
do {
|
|
29
24
|
m = rgx.exec(acceptLanguage);
|
|
30
|
-
|
|
31
25
|
if (m) {
|
|
32
26
|
var lng = m[1];
|
|
33
27
|
var weight = m[5] || '1';
|
|
34
28
|
var q = Number(weight);
|
|
35
|
-
|
|
36
29
|
if (lng && !isNaN(q)) {
|
|
37
30
|
lngs.push({
|
|
38
31
|
lng: lng,
|
|
@@ -41,19 +34,15 @@ export default {
|
|
|
41
34
|
}
|
|
42
35
|
}
|
|
43
36
|
} while (m);
|
|
44
|
-
|
|
45
37
|
lngs.sort(function (a, b) {
|
|
46
38
|
return b.q - a.q;
|
|
47
39
|
});
|
|
48
|
-
|
|
49
40
|
for (i = 0; i < lngs.length; i++) {
|
|
50
41
|
locales.push(lngs[i].lng);
|
|
51
42
|
}
|
|
52
|
-
|
|
53
43
|
if (locales.length) found = locales;
|
|
54
44
|
}
|
|
55
45
|
}
|
|
56
|
-
|
|
57
46
|
return found;
|
|
58
47
|
}
|
|
59
48
|
};
|
|
@@ -2,28 +2,22 @@ export default {
|
|
|
2
2
|
name: 'path',
|
|
3
3
|
lookup: function lookup(req, res, options) {
|
|
4
4
|
var found;
|
|
5
|
-
|
|
6
5
|
if (req === undefined) {
|
|
7
6
|
return found;
|
|
8
7
|
}
|
|
9
|
-
|
|
10
8
|
if (options.lookupPath !== undefined && req.params) {
|
|
11
9
|
found = options.getParams(req)[options.lookupPath];
|
|
12
10
|
}
|
|
13
|
-
|
|
14
11
|
if (!found && typeof options.lookupFromPathIndex === 'number' && options.getOriginalUrl(req)) {
|
|
15
12
|
var path = options.getOriginalUrl(req).split('?')[0];
|
|
16
13
|
var parts = path.split('/');
|
|
17
|
-
|
|
18
14
|
if (parts[0] === '') {
|
|
19
15
|
parts.shift();
|
|
20
16
|
}
|
|
21
|
-
|
|
22
17
|
if (parts.length > options.lookupFromPathIndex) {
|
|
23
18
|
found = parts[options.lookupFromPathIndex];
|
|
24
19
|
}
|
|
25
20
|
}
|
|
26
|
-
|
|
27
21
|
return found;
|
|
28
22
|
}
|
|
29
23
|
};
|
|
@@ -2,24 +2,19 @@ export default {
|
|
|
2
2
|
name: 'querystring',
|
|
3
3
|
lookup: function lookup(req, res, options) {
|
|
4
4
|
var found;
|
|
5
|
-
|
|
6
5
|
if (options.lookupQuerystring !== undefined && typeof req !== 'undefined') {
|
|
7
6
|
if (options.getQuery(req)) {
|
|
8
7
|
found = options.getQuery(req)[options.lookupQuerystring];
|
|
9
8
|
}
|
|
10
|
-
|
|
11
9
|
if (!found && options.getUrl(req) && options.getUrl(req).indexOf('?')) {
|
|
12
10
|
var lastPartOfUri = options.getUrl(req).substring(options.getUrl(req).indexOf('?'));
|
|
13
|
-
|
|
14
11
|
if (typeof URLSearchParams !== 'undefined') {
|
|
15
12
|
var urlParams = new URLSearchParams(lastPartOfUri);
|
|
16
13
|
found = urlParams.get(options.lookupQuerystring);
|
|
17
14
|
} else {
|
|
18
15
|
var indexOfQsStart = lastPartOfUri.indexOf("".concat(options.lookupQuerystring, "="));
|
|
19
|
-
|
|
20
16
|
if (indexOfQsStart > -1) {
|
|
21
17
|
var restOfUri = lastPartOfUri.substring(options.lookupQuerystring.length + 2);
|
|
22
|
-
|
|
23
18
|
if (restOfUri.indexOf('&') < 0) {
|
|
24
19
|
found = restOfUri;
|
|
25
20
|
} else {
|
|
@@ -29,7 +24,6 @@ export default {
|
|
|
29
24
|
}
|
|
30
25
|
}
|
|
31
26
|
}
|
|
32
|
-
|
|
33
27
|
return found;
|
|
34
28
|
}
|
|
35
29
|
};
|
|
@@ -1,19 +1,15 @@
|
|
|
1
1
|
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); }
|
|
2
|
-
|
|
3
2
|
export default {
|
|
4
3
|
name: 'session',
|
|
5
4
|
lookup: function lookup(req, res, options) {
|
|
6
5
|
var found;
|
|
7
|
-
|
|
8
6
|
if (options.lookupSession !== undefined && _typeof(req) && options.getSession(req)) {
|
|
9
7
|
found = options.getSession(req)[options.lookupSession];
|
|
10
8
|
}
|
|
11
|
-
|
|
12
9
|
return found;
|
|
13
10
|
},
|
|
14
11
|
cacheUserLanguage: function cacheUserLanguage(req, res, lng) {
|
|
15
12
|
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
16
|
-
|
|
17
13
|
if (options.lookupSession && req && options.getSession(req)) {
|
|
18
14
|
options.getSession(req)[options.lookupSession] = lng;
|
|
19
15
|
}
|
package/esm/utils.js
CHANGED
|
@@ -2,15 +2,12 @@ export function setPath(object, path, newValue) {
|
|
|
2
2
|
var stack;
|
|
3
3
|
if (typeof path !== 'string') stack = [].concat(path);
|
|
4
4
|
if (typeof path === 'string') stack = path.split('.');
|
|
5
|
-
|
|
6
5
|
while (stack.length > 1) {
|
|
7
6
|
var _key = stack.shift();
|
|
8
|
-
|
|
9
7
|
if (_key.indexOf('###') > -1) _key = _key.replace(/###/g, '.');
|
|
10
8
|
if (!object[_key]) object[_key] = {};
|
|
11
9
|
object = object[_key];
|
|
12
10
|
}
|
|
13
|
-
|
|
14
11
|
var key = stack.shift();
|
|
15
12
|
if (key.indexOf('###') > -1) key = key.replace(/###/g, '.');
|
|
16
13
|
object[key] = newValue;
|
|
@@ -41,7 +38,6 @@ export function extend(obj) {
|
|
|
41
38
|
export function removeLngFromUrl(url, lookupFromPathIndex) {
|
|
42
39
|
var first = '';
|
|
43
40
|
var pos = lookupFromPathIndex;
|
|
44
|
-
|
|
45
41
|
if (url[0] === '/') {
|
|
46
42
|
pos++;
|
|
47
43
|
first = '/';
|
package/lib/index.js
CHANGED
|
@@ -127,7 +127,7 @@ export const hapiPlugin = {
|
|
|
127
127
|
const middleware = handle(options.i18next, {
|
|
128
128
|
...options
|
|
129
129
|
})
|
|
130
|
-
server.ext('
|
|
130
|
+
server.ext('onPreAuth', (request, h) => {
|
|
131
131
|
middleware(
|
|
132
132
|
request,
|
|
133
133
|
request.raw.res || h.response() || request.Response,
|
|
@@ -14,10 +14,10 @@ export default {
|
|
|
14
14
|
const acceptLanguage = options.lookupHeader ? headers[options.lookupHeader] : headers['accept-language']
|
|
15
15
|
|
|
16
16
|
if (acceptLanguage) {
|
|
17
|
-
let lookupRegex = /(([a-z]{2})-?([A-Z]{2})?)\s*;?\s*(q=([0-9.]+))?/gi
|
|
17
|
+
let lookupRegex = /(([a-z]{2,3})-?([A-Z]{2})?)\s*;?\s*(q=([0-9.]+))?/gi
|
|
18
18
|
if (acceptLanguage.indexOf('-') > 0) {
|
|
19
19
|
const foundSpecialCase = specialCases.find((s) => acceptLanguage.toLowerCase().indexOf(`-${s}`) > 0)
|
|
20
|
-
if (foundSpecialCase) lookupRegex = /(([a-z]{2})-?([A-Z]{4})?)\s*;?\s*(q=([0-9.]+))?/gi
|
|
20
|
+
if (foundSpecialCase) lookupRegex = /(([a-z]{2,3})-?([A-Z]{4})?)\s*;?\s*(q=([0-9.]+))?/gi
|
|
21
21
|
}
|
|
22
22
|
const lngs = []; let i; let m
|
|
23
23
|
const rgx = options.lookupHeaderRegex || lookupRegex
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "i18next-http-middleware",
|
|
3
|
-
"version": "3.2.
|
|
3
|
+
"version": "3.2.2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./cjs/index.js",
|
|
@@ -21,27 +21,27 @@
|
|
|
21
21
|
"module": "./esm/index.js",
|
|
22
22
|
"dependencies": {},
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@babel/cli": "7.
|
|
25
|
-
"@babel/core": "7.
|
|
26
|
-
"@babel/preset-env": "7.
|
|
27
|
-
"@hapi/hapi": "^20.2.
|
|
28
|
-
"@types/express-serve-static-core": "^4.17.
|
|
24
|
+
"@babel/cli": "7.19.3",
|
|
25
|
+
"@babel/core": "7.19.6",
|
|
26
|
+
"@babel/preset-env": "7.19.4",
|
|
27
|
+
"@hapi/hapi": "^20.2.2",
|
|
28
|
+
"@types/express-serve-static-core": "^4.17.31",
|
|
29
29
|
"babel-plugin-add-module-exports": "1.0.4",
|
|
30
|
-
"eslint": "
|
|
31
|
-
"eslint-config-standard": "
|
|
32
|
-
"eslint-plugin-import": "2.
|
|
33
|
-
"eslint-plugin-
|
|
34
|
-
"eslint-plugin-promise": "
|
|
30
|
+
"eslint": "8.26.0",
|
|
31
|
+
"eslint-config-standard": "17.0.0",
|
|
32
|
+
"eslint-plugin-import": "2.26.0",
|
|
33
|
+
"eslint-plugin-n": "15.3.0",
|
|
34
|
+
"eslint-plugin-promise": "6.1.1",
|
|
35
35
|
"eslint-plugin-require-path-exists": "1.1.9",
|
|
36
36
|
"eslint-plugin-standard": "5.0.0",
|
|
37
37
|
"expect.js": "0.3.1",
|
|
38
|
-
"express": "4.
|
|
39
|
-
"fastify": "
|
|
40
|
-
"i18next": "
|
|
41
|
-
"mocha": "
|
|
42
|
-
"supertest": "6.1
|
|
43
|
-
"tsd": "0.
|
|
44
|
-
"uglify-js": "3.
|
|
38
|
+
"express": "4.18.2",
|
|
39
|
+
"fastify": "4.9.2",
|
|
40
|
+
"i18next": "22.0.2",
|
|
41
|
+
"mocha": "10.1.0",
|
|
42
|
+
"supertest": "6.3.1",
|
|
43
|
+
"tsd": "0.24.1",
|
|
44
|
+
"uglify-js": "3.17.4"
|
|
45
45
|
},
|
|
46
46
|
"description": "i18next-http-middleware is a middleware to be used with Node.js web frameworks like express or Fastify and also for Deno.",
|
|
47
47
|
"keywords": [
|