i18next-http-middleware 3.7.4 → 3.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/cjs/LanguageDetector.js +14 -7
- package/cjs/index.js +1 -1
- package/cjs/languageLookups/cookie.js +2 -1
- package/esm/LanguageDetector.js +14 -7
- package/esm/index.js +1 -1
- package/esm/languageLookups/cookie.js +2 -1
- package/lib/LanguageDetector.js +13 -6
- package/lib/index.js +1 -0
- package/lib/languageLookups/cookie.js +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -403,6 +403,7 @@ As with all modules you can either pass the constructor function (class) to the
|
|
|
403
403
|
cookiePath: '/my/path',
|
|
404
404
|
cookieSecure: true, // if need secure cookie
|
|
405
405
|
cookieSameSite: 'strict', // 'strict', 'lax' or 'none'
|
|
406
|
+
cookieHttpOnly: true // prevent client-side script access to the cookie
|
|
406
407
|
|
|
407
408
|
// optional conversion function used to modify the detected language code
|
|
408
409
|
convertDetectedLanguage: 'Iso15897',
|
package/cjs/LanguageDetector.js
CHANGED
|
@@ -35,6 +35,13 @@ function getDefaults() {
|
|
|
35
35
|
}
|
|
36
36
|
});
|
|
37
37
|
}
|
|
38
|
+
function getFallbackLngs(fallbackLng) {
|
|
39
|
+
var fallbacks = fallbackLng;
|
|
40
|
+
if (typeof fallbacks === 'function') fallbacks = fallbacks();
|
|
41
|
+
if (typeof fallbacks === 'string') fallbacks = [fallbacks];
|
|
42
|
+
if (!fallbacks) fallbacks = [];
|
|
43
|
+
return fallbacks;
|
|
44
|
+
}
|
|
38
45
|
var LanguageDetector = function () {
|
|
39
46
|
function LanguageDetector(services) {
|
|
40
47
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
@@ -62,6 +69,7 @@ var LanguageDetector = function () {
|
|
|
62
69
|
this.addDetector(_path.default);
|
|
63
70
|
this.addDetector(_header.default);
|
|
64
71
|
this.addDetector(_session.default);
|
|
72
|
+
this.fallbacks = getFallbackLngs(this.allOptions.fallbackLng);
|
|
65
73
|
}
|
|
66
74
|
}, {
|
|
67
75
|
key: "addDetector",
|
|
@@ -89,11 +97,13 @@ var LanguageDetector = function () {
|
|
|
89
97
|
found = _this.services.languageUtils.getBestMatchFromCodes(detections);
|
|
90
98
|
if (found) {
|
|
91
99
|
if (_this.options.ignoreCase) {
|
|
92
|
-
if (detections.map(function (d) {
|
|
100
|
+
if (detections.length === 0 || detections.map(function (d) {
|
|
101
|
+
return d.toLowerCase();
|
|
102
|
+
}).indexOf(found.toLowerCase()) < 0 && _this.fallbacks.map(function (d) {
|
|
93
103
|
return d.toLowerCase();
|
|
94
|
-
}).indexOf(found.toLowerCase())
|
|
104
|
+
}).indexOf(found.toLowerCase()) > -1) found = undefined;
|
|
95
105
|
} else {
|
|
96
|
-
if (detections.indexOf(found) < 0) found = undefined;
|
|
106
|
+
if (detections.length === 0 || detections.indexOf(found) < 0 && _this.fallbacks.indexOf(found) > -1) found = undefined;
|
|
97
107
|
}
|
|
98
108
|
}
|
|
99
109
|
if (found) req.i18nextLookupName = detectorName;
|
|
@@ -102,10 +112,7 @@ var LanguageDetector = function () {
|
|
|
102
112
|
}
|
|
103
113
|
});
|
|
104
114
|
if (!found) {
|
|
105
|
-
var fallbacks = this.
|
|
106
|
-
if (typeof fallbacks === 'function') fallbacks = fallbacks();
|
|
107
|
-
if (typeof fallbacks === 'string') fallbacks = [fallbacks];
|
|
108
|
-
if (!fallbacks) fallbacks = [];
|
|
115
|
+
var fallbacks = this.fallbacks;
|
|
109
116
|
if (Object.prototype.toString.apply(fallbacks) === '[object Array]') {
|
|
110
117
|
found = fallbacks[0];
|
|
111
118
|
} else {
|
package/cjs/index.js
CHANGED
|
@@ -190,7 +190,7 @@ function getResourcesHandler(i18next) {
|
|
|
190
190
|
}
|
|
191
191
|
var resources = {};
|
|
192
192
|
options.setContentType(res, 'application/json');
|
|
193
|
-
if (options.cache !== undefined ? options.cache : typeof process !== 'undefined' && process.env && process.env.NODE_ENV === 'production') {
|
|
193
|
+
if (options.cache !== undefined ? options.cache : typeof process !== 'undefined' && typeof process.env !== 'undefined' && process.env && process.env.NODE_ENV === 'production') {
|
|
194
194
|
options.setHeader(res, 'Cache-Control', 'public, max-age=' + maxAge);
|
|
195
195
|
options.setHeader(res, 'Expires', new Date(new Date().getTime() + maxAge * 1000).toUTCString());
|
|
196
196
|
} else {
|
|
@@ -69,6 +69,7 @@ var _default = exports.default = {
|
|
|
69
69
|
cacheUserLanguage: function cacheUserLanguage(req, res, lng) {
|
|
70
70
|
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
71
71
|
if (options.lookupCookie && req !== 'undefined') {
|
|
72
|
+
var _options$cookieHttpOn;
|
|
72
73
|
var expirationDate = options.cookieExpirationDate;
|
|
73
74
|
if (!expirationDate) {
|
|
74
75
|
expirationDate = new Date();
|
|
@@ -78,7 +79,7 @@ var _default = exports.default = {
|
|
|
78
79
|
expires: expirationDate,
|
|
79
80
|
domain: options.cookieDomain,
|
|
80
81
|
path: options.cookiePath,
|
|
81
|
-
httpOnly: false,
|
|
82
|
+
httpOnly: (_options$cookieHttpOn = options.cookieHttpOnly) !== null && _options$cookieHttpOn !== void 0 ? _options$cookieHttpOn : false,
|
|
82
83
|
overwrite: true,
|
|
83
84
|
sameSite: options.cookieSameSite
|
|
84
85
|
};
|
package/esm/LanguageDetector.js
CHANGED
|
@@ -26,6 +26,13 @@ function getDefaults() {
|
|
|
26
26
|
}
|
|
27
27
|
});
|
|
28
28
|
}
|
|
29
|
+
function getFallbackLngs(fallbackLng) {
|
|
30
|
+
var fallbacks = fallbackLng;
|
|
31
|
+
if (typeof fallbacks === 'function') fallbacks = fallbacks();
|
|
32
|
+
if (typeof fallbacks === 'string') fallbacks = [fallbacks];
|
|
33
|
+
if (!fallbacks) fallbacks = [];
|
|
34
|
+
return fallbacks;
|
|
35
|
+
}
|
|
29
36
|
var LanguageDetector = function () {
|
|
30
37
|
function LanguageDetector(services) {
|
|
31
38
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
@@ -53,6 +60,7 @@ var LanguageDetector = function () {
|
|
|
53
60
|
this.addDetector(pathLookup);
|
|
54
61
|
this.addDetector(headerLookup);
|
|
55
62
|
this.addDetector(sessionLookup);
|
|
63
|
+
this.fallbacks = getFallbackLngs(this.allOptions.fallbackLng);
|
|
56
64
|
}
|
|
57
65
|
}, {
|
|
58
66
|
key: "addDetector",
|
|
@@ -80,11 +88,13 @@ var LanguageDetector = function () {
|
|
|
80
88
|
found = _this.services.languageUtils.getBestMatchFromCodes(detections);
|
|
81
89
|
if (found) {
|
|
82
90
|
if (_this.options.ignoreCase) {
|
|
83
|
-
if (detections.map(function (d) {
|
|
91
|
+
if (detections.length === 0 || detections.map(function (d) {
|
|
92
|
+
return d.toLowerCase();
|
|
93
|
+
}).indexOf(found.toLowerCase()) < 0 && _this.fallbacks.map(function (d) {
|
|
84
94
|
return d.toLowerCase();
|
|
85
|
-
}).indexOf(found.toLowerCase())
|
|
95
|
+
}).indexOf(found.toLowerCase()) > -1) found = undefined;
|
|
86
96
|
} else {
|
|
87
|
-
if (detections.indexOf(found) < 0) found = undefined;
|
|
97
|
+
if (detections.length === 0 || detections.indexOf(found) < 0 && _this.fallbacks.indexOf(found) > -1) found = undefined;
|
|
88
98
|
}
|
|
89
99
|
}
|
|
90
100
|
if (found) req.i18nextLookupName = detectorName;
|
|
@@ -93,10 +103,7 @@ var LanguageDetector = function () {
|
|
|
93
103
|
}
|
|
94
104
|
});
|
|
95
105
|
if (!found) {
|
|
96
|
-
var fallbacks = this.
|
|
97
|
-
if (typeof fallbacks === 'function') fallbacks = fallbacks();
|
|
98
|
-
if (typeof fallbacks === 'string') fallbacks = [fallbacks];
|
|
99
|
-
if (!fallbacks) fallbacks = [];
|
|
106
|
+
var fallbacks = this.fallbacks;
|
|
100
107
|
if (Object.prototype.toString.apply(fallbacks) === '[object Array]') {
|
|
101
108
|
found = fallbacks[0];
|
|
102
109
|
} else {
|
package/esm/index.js
CHANGED
|
@@ -173,7 +173,7 @@ export function getResourcesHandler(i18next) {
|
|
|
173
173
|
}
|
|
174
174
|
var resources = {};
|
|
175
175
|
options.setContentType(res, 'application/json');
|
|
176
|
-
if (options.cache !== undefined ? options.cache : typeof process !== 'undefined' && process.env && process.env.NODE_ENV === 'production') {
|
|
176
|
+
if (options.cache !== undefined ? options.cache : typeof process !== 'undefined' && typeof process.env !== 'undefined' && process.env && process.env.NODE_ENV === 'production') {
|
|
177
177
|
options.setHeader(res, 'Cache-Control', 'public, max-age=' + maxAge);
|
|
178
178
|
options.setHeader(res, 'Expires', new Date(new Date().getTime() + maxAge * 1000).toUTCString());
|
|
179
179
|
} else {
|
|
@@ -63,6 +63,7 @@ export default {
|
|
|
63
63
|
cacheUserLanguage: function cacheUserLanguage(req, res, lng) {
|
|
64
64
|
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
65
65
|
if (options.lookupCookie && req !== 'undefined') {
|
|
66
|
+
var _options$cookieHttpOn;
|
|
66
67
|
var expirationDate = options.cookieExpirationDate;
|
|
67
68
|
if (!expirationDate) {
|
|
68
69
|
expirationDate = new Date();
|
|
@@ -72,7 +73,7 @@ export default {
|
|
|
72
73
|
expires: expirationDate,
|
|
73
74
|
domain: options.cookieDomain,
|
|
74
75
|
path: options.cookiePath,
|
|
75
|
-
httpOnly: false,
|
|
76
|
+
httpOnly: (_options$cookieHttpOn = options.cookieHttpOnly) !== null && _options$cookieHttpOn !== void 0 ? _options$cookieHttpOn : false,
|
|
76
77
|
overwrite: true,
|
|
77
78
|
sameSite: options.cookieSameSite
|
|
78
79
|
};
|
package/lib/LanguageDetector.js
CHANGED
|
@@ -26,6 +26,14 @@ function getDefaults () {
|
|
|
26
26
|
})
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
function getFallbackLngs (fallbackLng) {
|
|
30
|
+
let fallbacks = fallbackLng
|
|
31
|
+
if (typeof fallbacks === 'function') fallbacks = fallbacks()
|
|
32
|
+
if (typeof fallbacks === 'string') fallbacks = [fallbacks]
|
|
33
|
+
if (!fallbacks) fallbacks = []
|
|
34
|
+
return fallbacks
|
|
35
|
+
}
|
|
36
|
+
|
|
29
37
|
class LanguageDetector {
|
|
30
38
|
constructor (services, options = {}, allOptions = {}) {
|
|
31
39
|
this.type = 'languageDetector'
|
|
@@ -48,6 +56,8 @@ class LanguageDetector {
|
|
|
48
56
|
this.addDetector(pathLookup)
|
|
49
57
|
this.addDetector(headerLookup)
|
|
50
58
|
this.addDetector(sessionLookup)
|
|
59
|
+
|
|
60
|
+
this.fallbacks = getFallbackLngs(this.allOptions.fallbackLng)
|
|
51
61
|
}
|
|
52
62
|
|
|
53
63
|
addDetector (detector) {
|
|
@@ -74,9 +84,9 @@ class LanguageDetector {
|
|
|
74
84
|
found = this.services.languageUtils.getBestMatchFromCodes(detections)
|
|
75
85
|
if (found) {
|
|
76
86
|
if (this.options.ignoreCase) {
|
|
77
|
-
if (detections.map(d => d.toLowerCase()).indexOf(found.toLowerCase()) < 0) found = undefined
|
|
87
|
+
if (detections.length === 0 || (detections.map(d => d.toLowerCase()).indexOf(found.toLowerCase()) < 0 && this.fallbacks.map(d => d.toLowerCase()).indexOf(found.toLowerCase()) > -1)) found = undefined
|
|
78
88
|
} else {
|
|
79
|
-
if (detections.indexOf(found) < 0) found = undefined
|
|
89
|
+
if (detections.length === 0 || (detections.indexOf(found) < 0 && this.fallbacks.indexOf(found) > -1)) found = undefined
|
|
80
90
|
}
|
|
81
91
|
}
|
|
82
92
|
if (found) req.i18nextLookupName = detectorName
|
|
@@ -86,10 +96,7 @@ class LanguageDetector {
|
|
|
86
96
|
})
|
|
87
97
|
|
|
88
98
|
if (!found) {
|
|
89
|
-
|
|
90
|
-
if (typeof fallbacks === 'function') fallbacks = fallbacks()
|
|
91
|
-
if (typeof fallbacks === 'string') fallbacks = [fallbacks]
|
|
92
|
-
if (!fallbacks) fallbacks = []
|
|
99
|
+
const fallbacks = this.fallbacks
|
|
93
100
|
if (Object.prototype.toString.apply(fallbacks) === '[object Array]') {
|
|
94
101
|
found = fallbacks[0]
|
|
95
102
|
} else {
|
package/lib/index.js
CHANGED
|
@@ -180,6 +180,7 @@ export function getResourcesHandler (i18next, options = {}) {
|
|
|
180
180
|
options.cache !== undefined
|
|
181
181
|
? options.cache
|
|
182
182
|
: typeof process !== 'undefined' &&
|
|
183
|
+
typeof process.env !== 'undefined' &&
|
|
183
184
|
process.env &&
|
|
184
185
|
process.env.NODE_ENV === 'production'
|
|
185
186
|
) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "i18next-http-middleware",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.8.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./cjs/index.js",
|
|
@@ -45,8 +45,8 @@
|
|
|
45
45
|
"eslint-plugin-standard": "5.0.0",
|
|
46
46
|
"expect.js": "0.3.1",
|
|
47
47
|
"express": "4.21.1",
|
|
48
|
-
"fastify": "5.
|
|
49
|
-
"i18next": "
|
|
48
|
+
"fastify": "5.3.2",
|
|
49
|
+
"i18next": "25.5.2",
|
|
50
50
|
"mocha": "10.8.2",
|
|
51
51
|
"supertest": "7.0.0",
|
|
52
52
|
"tsd": "0.31.2",
|
|
@@ -75,7 +75,7 @@
|
|
|
75
75
|
"compile": "npm run compile:esm && npm run compile:cjs",
|
|
76
76
|
"build": "npm run compile",
|
|
77
77
|
"test": "npm run lint && npm run build && mocha test -R spec --exit --experimental-modules && npm run test:types",
|
|
78
|
-
"test:deno": "deno test --allow-net test/deno/*.js",
|
|
78
|
+
"test:deno": "deno test --allow-net --allow-env test/deno/*.js",
|
|
79
79
|
"test:types": "tsd",
|
|
80
80
|
"preversion": "npm run test && npm run build && git push",
|
|
81
81
|
"postversion": "git push && git push --tags"
|