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
package/README.md
CHANGED
|
@@ -208,6 +208,7 @@ await app.listen({ port })
|
|
|
208
208
|
```js
|
|
209
209
|
// missing keys make sure the body is parsed (i.e. with [body-parser](https://github.com/expressjs/body-parser#bodyparserjsonoptions))
|
|
210
210
|
app.post('/locales/add/:lng/:ns', middleware.missingKeyHandler(i18next))
|
|
211
|
+
// addPath for client: http://localhost:8080/locales/add/{{lng}}/{{ns}}
|
|
211
212
|
|
|
212
213
|
// multiload backend route
|
|
213
214
|
app.get('/locales/resources.json', middleware.getResourcesHandler(i18next))
|
|
@@ -215,6 +216,16 @@ app.get('/locales/resources.json', middleware.getResourcesHandler(i18next))
|
|
|
215
216
|
// GET /locales/resources.json
|
|
216
217
|
// GET /locales/resources.json?lng=en
|
|
217
218
|
// GET /locales/resources.json?lng=en&ns=translation
|
|
219
|
+
|
|
220
|
+
// serve translations:
|
|
221
|
+
app.use('/locales', express.static('locales'))
|
|
222
|
+
// GET /locales/en/translation.json
|
|
223
|
+
// loadPath for client: http://localhost:8080/locales/{{lng}}/{{ns}}.json
|
|
224
|
+
|
|
225
|
+
// or instead of static
|
|
226
|
+
app.get('/locales/:lng/:ns', middleware.getResourcesHandler(i18next))
|
|
227
|
+
// GET /locales/en/translation
|
|
228
|
+
// loadPath for client: http://localhost:8080/locales/{{lng}}/{{ns}}
|
|
218
229
|
```
|
|
219
230
|
|
|
220
231
|
## add localized routes
|
|
@@ -419,6 +430,8 @@ i18next.use(lngDetector).init({
|
|
|
419
430
|
})
|
|
420
431
|
```
|
|
421
432
|
|
|
433
|
+
Don't forget: You have to add the name of your detector (`myDetectorsName` in this case) to the `order` array in your `options` object. Without that, your detector won't be used. See the [Detector Options section for more](#detector-options).
|
|
434
|
+
|
|
422
435
|
---
|
|
423
436
|
|
|
424
437
|
<h3 align='center'>Gold Sponsors</h3>
|
package/cjs/LanguageDetector.js
CHANGED
|
@@ -1,38 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
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); }
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.default = void 0;
|
|
9
|
-
|
|
10
8
|
var utils = _interopRequireWildcard(require("./utils.js"));
|
|
11
|
-
|
|
12
9
|
var _cookie = _interopRequireDefault(require("./languageLookups/cookie.js"));
|
|
13
|
-
|
|
14
10
|
var _querystring = _interopRequireDefault(require("./languageLookups/querystring.js"));
|
|
15
|
-
|
|
16
11
|
var _path = _interopRequireDefault(require("./languageLookups/path.js"));
|
|
17
|
-
|
|
18
12
|
var _header = _interopRequireDefault(require("./languageLookups/header.js"));
|
|
19
|
-
|
|
20
13
|
var _session = _interopRequireDefault(require("./languageLookups/session.js"));
|
|
21
|
-
|
|
22
14
|
var _httpFunctions = require("./httpFunctions.js");
|
|
23
|
-
|
|
24
15
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
25
|
-
|
|
26
16
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
27
|
-
|
|
28
17
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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
|
-
|
|
30
18
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
31
|
-
|
|
32
19
|
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); } }
|
|
33
|
-
|
|
34
20
|
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
35
|
-
|
|
36
21
|
function getDefaults() {
|
|
37
22
|
return (0, _httpFunctions.extendOptionsWithDefaults)({
|
|
38
23
|
order: ['querystring', 'cookie', 'header'],
|
|
@@ -45,19 +30,15 @@ function getDefaults() {
|
|
|
45
30
|
ignoreCase: true
|
|
46
31
|
});
|
|
47
32
|
}
|
|
48
|
-
|
|
49
33
|
var LanguageDetector = function () {
|
|
50
34
|
function LanguageDetector(services) {
|
|
51
35
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
52
36
|
var allOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
53
|
-
|
|
54
37
|
_classCallCheck(this, LanguageDetector);
|
|
55
|
-
|
|
56
38
|
this.type = 'languageDetector';
|
|
57
39
|
this.detectors = {};
|
|
58
40
|
this.init(services, options, allOptions);
|
|
59
41
|
}
|
|
60
|
-
|
|
61
42
|
_createClass(LanguageDetector, [{
|
|
62
43
|
key: "init",
|
|
63
44
|
value: function init(services) {
|
|
@@ -81,24 +62,19 @@ var LanguageDetector = function () {
|
|
|
81
62
|
key: "detect",
|
|
82
63
|
value: function detect(req, res, detectionOrder) {
|
|
83
64
|
var _this = this;
|
|
84
|
-
|
|
85
65
|
if (arguments.length < 2) return;
|
|
86
66
|
if (!detectionOrder) detectionOrder = this.options.order;
|
|
87
67
|
var found;
|
|
88
68
|
detectionOrder.forEach(function (detectorName) {
|
|
89
69
|
if (found || !_this.detectors[detectorName]) return;
|
|
90
|
-
|
|
91
70
|
var detections = _this.detectors[detectorName].lookup(req, res, _this.options);
|
|
92
|
-
|
|
93
71
|
if (!detections) return;
|
|
94
72
|
if (!Array.isArray(detections)) detections = [detections];
|
|
95
73
|
detections = detections.filter(function (d) {
|
|
96
74
|
return d !== undefined && d !== null;
|
|
97
75
|
});
|
|
98
|
-
|
|
99
76
|
if (_this.services.languageUtils.getBestMatchFromCodes) {
|
|
100
77
|
found = _this.services.languageUtils.getBestMatchFromCodes(detections);
|
|
101
|
-
|
|
102
78
|
if (found) {
|
|
103
79
|
if (_this.options.ignoreCase) {
|
|
104
80
|
if (detections.map(function (d) {
|
|
@@ -108,7 +84,6 @@ var LanguageDetector = function () {
|
|
|
108
84
|
if (detections.indexOf(found) < 0) found = undefined;
|
|
109
85
|
}
|
|
110
86
|
}
|
|
111
|
-
|
|
112
87
|
if (found) req.i18nextLookupName = detectorName;
|
|
113
88
|
} else {
|
|
114
89
|
found = detections.length > 0 ? detections[0] : null;
|
|
@@ -120,14 +95,12 @@ var LanguageDetector = function () {
|
|
|
120
95
|
if (typeof fallbacks === 'function') fallbacks = fallbacks();
|
|
121
96
|
if (typeof fallbacks === 'string') fallbacks = [fallbacks];
|
|
122
97
|
if (!fallbacks) fallbacks = [];
|
|
123
|
-
|
|
124
98
|
if (Object.prototype.toString.apply(fallbacks) === '[object Array]') {
|
|
125
99
|
found = fallbacks[0];
|
|
126
100
|
} else {
|
|
127
101
|
found = fallbacks[0] || fallbacks.default && fallbacks.default[0];
|
|
128
102
|
}
|
|
129
103
|
}
|
|
130
|
-
|
|
131
104
|
;
|
|
132
105
|
return found;
|
|
133
106
|
}
|
|
@@ -135,23 +108,19 @@ var LanguageDetector = function () {
|
|
|
135
108
|
key: "cacheUserLanguage",
|
|
136
109
|
value: function cacheUserLanguage(req, res, lng, caches) {
|
|
137
110
|
var _this2 = this;
|
|
138
|
-
|
|
139
111
|
if (arguments.length < 3) return;
|
|
140
112
|
if (!caches) caches = this.options.caches;
|
|
141
113
|
if (!caches) return;
|
|
142
114
|
caches.forEach(function (cacheName) {
|
|
143
115
|
if (_this2.detectors[cacheName] && _this2.detectors[cacheName].cacheUserLanguage && res.cachedUserLanguage !== lng) {
|
|
144
116
|
_this2.detectors[cacheName].cacheUserLanguage(req, res, lng, _this2.options);
|
|
145
|
-
|
|
146
117
|
res.cachedUserLanguage = lng;
|
|
147
118
|
}
|
|
148
119
|
});
|
|
149
120
|
}
|
|
150
121
|
}]);
|
|
151
|
-
|
|
152
122
|
return LanguageDetector;
|
|
153
123
|
}();
|
|
154
|
-
|
|
155
124
|
LanguageDetector.type = 'languageDetector';
|
|
156
125
|
var _default = LanguageDetector;
|
|
157
126
|
exports.default = _default;
|
package/cjs/httpFunctions.js
CHANGED
|
@@ -4,49 +4,38 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.setUrl = exports.setStatus = exports.setHeader = exports.setContentType = exports.send = exports.getUrl = exports.getSession = exports.getQuery = exports.getPath = exports.getParams = exports.getOriginalUrl = exports.getHeaders = exports.getHeader = exports.getCookies = exports.getBody = exports.extendOptionsWithDefaults = void 0;
|
|
7
|
-
|
|
8
7
|
var getPath = function getPath(req) {
|
|
9
8
|
if (req.path) return req.path;
|
|
10
9
|
if (req.raw && req.raw.path) return req.raw.path;
|
|
11
10
|
if (req.url) return req.url;
|
|
12
11
|
console.log('no possibility found to get path');
|
|
13
12
|
};
|
|
14
|
-
|
|
15
13
|
exports.getPath = getPath;
|
|
16
|
-
|
|
17
14
|
var getUrl = function getUrl(req) {
|
|
18
15
|
if (req.url && req.url.href) return req.url.href;
|
|
19
16
|
if (req.url) return req.url;
|
|
20
17
|
if (req.raw && req.raw.url) return req.raw.url;
|
|
21
18
|
console.log('no possibility found to get url');
|
|
22
19
|
};
|
|
23
|
-
|
|
24
20
|
exports.getUrl = getUrl;
|
|
25
|
-
|
|
26
21
|
var setUrl = function setUrl(req, url) {
|
|
27
22
|
if (req.url) {
|
|
28
23
|
req.url = url;
|
|
29
24
|
return;
|
|
30
25
|
}
|
|
31
|
-
|
|
32
26
|
console.log('no possibility found to get url');
|
|
33
27
|
};
|
|
34
|
-
|
|
35
28
|
exports.setUrl = setUrl;
|
|
36
|
-
|
|
37
29
|
var getOriginalUrl = function getOriginalUrl(req) {
|
|
38
30
|
if (req.originalUrl) return req.originalUrl;
|
|
39
31
|
if (req.raw && req.raw.originalUrl) return req.raw.originalUrl;
|
|
40
32
|
return getUrl(req);
|
|
41
33
|
};
|
|
42
|
-
|
|
43
34
|
exports.getOriginalUrl = getOriginalUrl;
|
|
44
|
-
|
|
45
35
|
var getQuery = function getQuery(req) {
|
|
46
36
|
if (req.query && typeof req.query.entries === 'function' && typeof Object.fromEntries === 'function' && typeof req.query[Symbol.iterator] === 'function') {
|
|
47
37
|
return Object.fromEntries(req.query);
|
|
48
38
|
}
|
|
49
|
-
|
|
50
39
|
if (req.query) return req.query;
|
|
51
40
|
if (req.searchParams) return req.searchParams;
|
|
52
41
|
if (req.raw && req.raw.query) return req.raw.query;
|
|
@@ -56,9 +45,7 @@ var getQuery = function getQuery(req) {
|
|
|
56
45
|
console.log('no possibility found to get query');
|
|
57
46
|
return {};
|
|
58
47
|
};
|
|
59
|
-
|
|
60
48
|
exports.getQuery = getQuery;
|
|
61
|
-
|
|
62
49
|
var getParams = function getParams(req) {
|
|
63
50
|
if (req.params) return req.params;
|
|
64
51
|
if (req.raw && req.raw.params) return req.raw.params;
|
|
@@ -66,19 +53,14 @@ var getParams = function getParams(req) {
|
|
|
66
53
|
console.log('no possibility found to get params');
|
|
67
54
|
return {};
|
|
68
55
|
};
|
|
69
|
-
|
|
70
56
|
exports.getParams = getParams;
|
|
71
|
-
|
|
72
57
|
var getHeaders = function getHeaders(req) {
|
|
73
58
|
if (req.headers) return req.headers;
|
|
74
59
|
console.log('no possibility found to get headers');
|
|
75
60
|
};
|
|
76
|
-
|
|
77
61
|
exports.getHeaders = getHeaders;
|
|
78
|
-
|
|
79
62
|
var getCookies = function getCookies(req) {
|
|
80
63
|
if (req.cookies) return req.cookies;
|
|
81
|
-
|
|
82
64
|
if (getHeaders(req)) {
|
|
83
65
|
var list = {};
|
|
84
66
|
var rc = getHeaders(req).cookie;
|
|
@@ -88,17 +70,13 @@ var getCookies = function getCookies(req) {
|
|
|
88
70
|
});
|
|
89
71
|
return list;
|
|
90
72
|
}
|
|
91
|
-
|
|
92
73
|
console.log('no possibility found to get cookies');
|
|
93
74
|
};
|
|
94
|
-
|
|
95
75
|
exports.getCookies = getCookies;
|
|
96
|
-
|
|
97
76
|
var getBody = function getBody(req) {
|
|
98
77
|
if (req.ctx && typeof req.ctx.body === 'function') {
|
|
99
78
|
return req.ctx.body.bind(req.ctx);
|
|
100
79
|
}
|
|
101
|
-
|
|
102
80
|
if (req.ctx && req.ctx.body) return req.ctx.body;
|
|
103
81
|
if (req.json) return req.json;
|
|
104
82
|
if (req.body) return req.body;
|
|
@@ -106,9 +84,7 @@ var getBody = function getBody(req) {
|
|
|
106
84
|
console.log('no possibility found to get body');
|
|
107
85
|
return {};
|
|
108
86
|
};
|
|
109
|
-
|
|
110
87
|
exports.getBody = getBody;
|
|
111
|
-
|
|
112
88
|
var getHeader = function getHeader(res, name) {
|
|
113
89
|
if (res.getHeader) return res.getHeader(name);
|
|
114
90
|
if (res.headers) return res.headers[name];
|
|
@@ -116,58 +92,43 @@ var getHeader = function getHeader(res, name) {
|
|
|
116
92
|
console.log('no possibility found to get header');
|
|
117
93
|
return undefined;
|
|
118
94
|
};
|
|
119
|
-
|
|
120
95
|
exports.getHeader = getHeader;
|
|
121
|
-
|
|
122
96
|
var setHeader = function setHeader(res, name, value) {
|
|
123
97
|
if (res._headerSent || res.headersSent) return;
|
|
124
98
|
if (typeof res.setHeader === 'function') return res.setHeader(name, value);
|
|
125
99
|
if (typeof res.header === 'function') return res.header(name, value);
|
|
126
|
-
|
|
127
100
|
if (res.responseHeaders && typeof res.responseHeaders.set === 'function') {
|
|
128
101
|
return res.responseHeaders.set(name, value);
|
|
129
102
|
}
|
|
130
|
-
|
|
131
103
|
if (res.headers && typeof res.headers.set === 'function') {
|
|
132
104
|
return res.headers.set(name, value);
|
|
133
105
|
}
|
|
134
|
-
|
|
135
106
|
console.log('no possibility found to set header');
|
|
136
107
|
};
|
|
137
|
-
|
|
138
108
|
exports.setHeader = setHeader;
|
|
139
|
-
|
|
140
109
|
var setContentType = function setContentType(res, type) {
|
|
141
110
|
if (typeof res.contentType === 'function') return res.contentType(type);
|
|
142
111
|
if (typeof res.type === 'function') return res.type(type);
|
|
143
112
|
setHeader(res, 'Content-Type', type);
|
|
144
113
|
};
|
|
145
|
-
|
|
146
114
|
exports.setContentType = setContentType;
|
|
147
|
-
|
|
148
115
|
var setStatus = function setStatus(res, code) {
|
|
149
116
|
if (typeof res.status === 'function') return res.status(code);
|
|
150
117
|
if (res.status) return res.status = code;
|
|
151
118
|
console.log('no possibility found to set status');
|
|
152
119
|
};
|
|
153
|
-
|
|
154
120
|
exports.setStatus = setStatus;
|
|
155
|
-
|
|
156
121
|
var send = function send(res, body) {
|
|
157
122
|
if (typeof res.send === 'function') return res.send(body);
|
|
158
123
|
return body;
|
|
159
124
|
};
|
|
160
|
-
|
|
161
125
|
exports.send = send;
|
|
162
|
-
|
|
163
126
|
var getSession = function getSession(req) {
|
|
164
127
|
if (req.session) return req.session;
|
|
165
128
|
if (req.raw && req.raw.session) return req.raw.session;
|
|
166
129
|
console.log('no possibility found to get session');
|
|
167
130
|
};
|
|
168
|
-
|
|
169
131
|
exports.getSession = getSession;
|
|
170
|
-
|
|
171
132
|
var extendOptionsWithDefaults = function extendOptionsWithDefaults() {
|
|
172
133
|
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
173
134
|
options.getPath = options.getPath || getPath;
|
|
@@ -187,5 +148,4 @@ var extendOptionsWithDefaults = function extendOptionsWithDefaults() {
|
|
|
187
148
|
options.send = options.send || send;
|
|
188
149
|
return options;
|
|
189
150
|
};
|
|
190
|
-
|
|
191
151
|
exports.extendOptionsWithDefaults = extendOptionsWithDefaults;
|
package/cjs/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
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); }
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
@@ -13,28 +12,17 @@ exports.handle = handle;
|
|
|
13
12
|
exports.hapiPlugin = void 0;
|
|
14
13
|
exports.missingKeyHandler = missingKeyHandler;
|
|
15
14
|
exports.plugin = plugin;
|
|
16
|
-
|
|
17
15
|
var utils = _interopRequireWildcard(require("./utils.js"));
|
|
18
|
-
|
|
19
16
|
var _LanguageDetector = _interopRequireDefault(require("./LanguageDetector.js"));
|
|
20
|
-
|
|
21
17
|
var _httpFunctions = require("./httpFunctions.js");
|
|
22
|
-
|
|
23
18
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
24
|
-
|
|
25
19
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
26
|
-
|
|
27
20
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; }
|
|
28
|
-
|
|
29
21
|
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; }
|
|
30
|
-
|
|
31
22
|
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; }
|
|
32
|
-
|
|
33
23
|
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; }
|
|
34
|
-
|
|
35
24
|
var LanguageDetector = _LanguageDetector.default;
|
|
36
25
|
exports.LanguageDetector = LanguageDetector;
|
|
37
|
-
|
|
38
26
|
var checkForCombinedReqRes = function checkForCombinedReqRes(req, res, next) {
|
|
39
27
|
if (!res) {
|
|
40
28
|
if (req.request && req.response) {
|
|
@@ -47,82 +35,70 @@ var checkForCombinedReqRes = function checkForCombinedReqRes(req, res, next) {
|
|
|
47
35
|
if (!next) next = function next() {};
|
|
48
36
|
}
|
|
49
37
|
}
|
|
50
|
-
|
|
51
38
|
return {
|
|
52
39
|
req: req,
|
|
53
40
|
res: res,
|
|
54
41
|
next: next
|
|
55
42
|
};
|
|
56
43
|
};
|
|
57
|
-
|
|
58
44
|
function handle(i18next) {
|
|
59
45
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
60
46
|
(0, _httpFunctions.extendOptionsWithDefaults)(options);
|
|
61
47
|
return function i18nextMiddleware(rq, rs, n) {
|
|
62
48
|
var _checkForCombinedReqR = checkForCombinedReqRes(rq, rs, n),
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
49
|
+
req = _checkForCombinedReqR.req,
|
|
50
|
+
res = _checkForCombinedReqR.res,
|
|
51
|
+
next = _checkForCombinedReqR.next;
|
|
67
52
|
if (typeof options.ignoreRoutes === 'function') {
|
|
68
53
|
if (options.ignoreRoutes(req, res, options, i18next)) {
|
|
69
54
|
return next();
|
|
70
55
|
}
|
|
71
56
|
} else {
|
|
72
57
|
var ignores = options.ignoreRoutes instanceof Array && options.ignoreRoutes || [];
|
|
73
|
-
|
|
74
58
|
for (var i = 0; i < ignores.length; i++) {
|
|
75
59
|
if (options.getPath(req).indexOf(ignores[i]) > -1) return next();
|
|
76
60
|
}
|
|
77
61
|
}
|
|
78
|
-
|
|
79
62
|
var i18n = i18next.cloneInstance({
|
|
80
63
|
initImmediate: false
|
|
81
64
|
});
|
|
82
65
|
i18n.on('languageChanged', function (lng) {
|
|
83
66
|
req.language = req.locale = req.lng = lng;
|
|
84
67
|
if (options.attachLocals) res.locals = res.locals || {};
|
|
85
|
-
|
|
86
68
|
if (res.locals) {
|
|
87
69
|
res.locals.language = lng;
|
|
88
70
|
res.locals.languageDir = i18next.dir(lng);
|
|
89
71
|
}
|
|
90
|
-
|
|
91
72
|
if (lng && options.getHeader(res, 'Content-Language') !== lng) {
|
|
92
73
|
options.setHeader(res, 'Content-Language', lng);
|
|
93
74
|
}
|
|
94
|
-
|
|
95
75
|
req.languages = i18next.services.languageUtils.toResolveHierarchy(lng);
|
|
96
|
-
|
|
97
76
|
if (i18next.services.languageDetector) {
|
|
98
77
|
i18next.services.languageDetector.cacheUserLanguage(req, res, lng);
|
|
99
78
|
}
|
|
100
79
|
});
|
|
101
80
|
var lng = req.lng;
|
|
102
|
-
|
|
103
81
|
if (!lng && i18next.services.languageDetector) {
|
|
104
82
|
lng = i18next.services.languageDetector.detect(req, res);
|
|
105
83
|
}
|
|
106
84
|
|
|
107
85
|
req.language = req.locale = req.lng = lng;
|
|
108
|
-
|
|
109
86
|
if (lng && options.getHeader(res, 'Content-Language') !== lng) {
|
|
110
87
|
options.setHeader(res, 'Content-Language', lng);
|
|
111
88
|
}
|
|
112
|
-
|
|
113
89
|
req.languages = i18next.services.languageUtils.toResolveHierarchy(lng);
|
|
114
|
-
i18n.changeLanguage(lng || i18next.options.fallbackLng[0]);
|
|
115
90
|
|
|
91
|
+
i18n.changeLanguage(lng || i18next.options.fallbackLng[0]);
|
|
116
92
|
if (req.i18nextLookupName === 'path' && options.removeLngFromUrl) {
|
|
117
93
|
options.setUrl(req, utils.removeLngFromUrl(options.getUrl(req), i18next.services.languageDetector.options.lookupFromPathIndex));
|
|
118
94
|
}
|
|
119
|
-
|
|
120
95
|
var t = i18n.t.bind(i18n);
|
|
121
96
|
var exists = i18n.exists.bind(i18n);
|
|
97
|
+
|
|
122
98
|
req.i18n = i18n;
|
|
123
99
|
req.t = t;
|
|
124
|
-
if (options.attachLocals) res.locals = res.locals || {};
|
|
125
100
|
|
|
101
|
+
if (options.attachLocals) res.locals = res.locals || {};
|
|
126
102
|
if (res.locals) {
|
|
127
103
|
res.locals.t = t;
|
|
128
104
|
res.locals.exists = exists;
|
|
@@ -130,18 +106,15 @@ function handle(i18next) {
|
|
|
130
106
|
res.locals.language = lng;
|
|
131
107
|
res.locals.languageDir = i18next.dir(lng);
|
|
132
108
|
}
|
|
133
|
-
|
|
134
109
|
if (i18next.services.languageDetector) {
|
|
135
110
|
i18next.services.languageDetector.cacheUserLanguage(req, res, lng);
|
|
136
111
|
}
|
|
137
|
-
|
|
138
112
|
if (!req.lng) return next();
|
|
139
113
|
i18next.loadLanguages(req.lng, function () {
|
|
140
114
|
return next();
|
|
141
115
|
});
|
|
142
116
|
};
|
|
143
117
|
}
|
|
144
|
-
|
|
145
118
|
function plugin(instance, options, next) {
|
|
146
119
|
options.attachLocals = true;
|
|
147
120
|
var middleware = handle(options.i18next, options);
|
|
@@ -150,7 +123,6 @@ function plugin(instance, options, next) {
|
|
|
150
123
|
});
|
|
151
124
|
return next();
|
|
152
125
|
}
|
|
153
|
-
|
|
154
126
|
var hapiPlugin = {
|
|
155
127
|
name: 'i18next-http-middleware',
|
|
156
128
|
version: '1',
|
|
@@ -167,24 +139,20 @@ var hapiPlugin = {
|
|
|
167
139
|
};
|
|
168
140
|
exports.hapiPlugin = hapiPlugin;
|
|
169
141
|
plugin[Symbol.for('skip-override')] = true;
|
|
170
|
-
|
|
171
142
|
function getResourcesHandler(i18next) {
|
|
172
143
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
173
144
|
(0, _httpFunctions.extendOptionsWithDefaults)(options);
|
|
174
145
|
var maxAge = options.maxAge || 60 * 60 * 24 * 30;
|
|
175
146
|
return function (rq, rs) {
|
|
176
147
|
var _checkForCombinedReqR2 = checkForCombinedReqRes(rq, rs),
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
148
|
+
req = _checkForCombinedReqR2.req,
|
|
149
|
+
res = _checkForCombinedReqR2.res;
|
|
180
150
|
if (!i18next.services.backendConnector) {
|
|
181
151
|
options.setStatus(res, 404);
|
|
182
152
|
return options.send(res, 'i18next-express-middleware:: no backend configured');
|
|
183
153
|
}
|
|
184
|
-
|
|
185
154
|
var resources = {};
|
|
186
155
|
options.setContentType(res, 'application/json');
|
|
187
|
-
|
|
188
156
|
if (options.cache !== undefined ? options.cache : typeof process !== 'undefined' && process.env && process.env.NODE_ENV === 'production') {
|
|
189
157
|
options.setHeader(res, 'Cache-Control', 'public, max-age=' + maxAge);
|
|
190
158
|
options.setHeader(res, 'Expires', new Date(new Date().getTime() + maxAge * 1000).toUTCString());
|
|
@@ -195,6 +163,12 @@ function getResourcesHandler(i18next) {
|
|
|
195
163
|
|
|
196
164
|
var languages = options.getQuery(req)[options.lngParam || 'lng'] ? options.getQuery(req)[options.lngParam || 'lng'].split(' ') : [];
|
|
197
165
|
var namespaces = options.getQuery(req)[options.nsParam || 'ns'] ? options.getQuery(req)[options.nsParam || 'ns'].split(' ') : [];
|
|
166
|
+
|
|
167
|
+
if (languages.length === 0 && namespaces.length === 0) {
|
|
168
|
+
languages = options.getParams(req)[options.lngParam || 'lng'] ? options.getParams(req)[options.lngParam || 'lng'].split(' ') : [];
|
|
169
|
+
namespaces = options.getParams(req)[options.nsParam || 'ns'] ? options.getParams(req)[options.nsParam || 'ns'].split(' ') : [];
|
|
170
|
+
}
|
|
171
|
+
|
|
198
172
|
namespaces.forEach(function (ns) {
|
|
199
173
|
if (i18next.options.ns && i18next.options.ns.indexOf(ns) < 0) {
|
|
200
174
|
i18next.options.ns.push(ns);
|
|
@@ -210,49 +184,39 @@ function getResourcesHandler(i18next) {
|
|
|
210
184
|
return options.send(res, resources);
|
|
211
185
|
};
|
|
212
186
|
}
|
|
213
|
-
|
|
214
187
|
function missingKeyHandler(i18next) {
|
|
215
188
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
216
189
|
(0, _httpFunctions.extendOptionsWithDefaults)(options);
|
|
217
190
|
return function (rq, rs) {
|
|
218
191
|
var _checkForCombinedReqR3 = checkForCombinedReqRes(rq, rs),
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
192
|
+
req = _checkForCombinedReqR3.req,
|
|
193
|
+
res = _checkForCombinedReqR3.res;
|
|
222
194
|
var lng = options.getParams(req)[options.lngParam || 'lng'];
|
|
223
195
|
var ns = options.getParams(req)[options.nsParam || 'ns'];
|
|
224
|
-
|
|
225
196
|
if (!i18next.services.backendConnector) {
|
|
226
197
|
options.setStatus(res, 404);
|
|
227
198
|
return options.send(res, 'i18next-express-middleware:: no backend configured');
|
|
228
199
|
}
|
|
229
|
-
|
|
230
200
|
var body = options.getBody(req);
|
|
231
|
-
|
|
232
201
|
if (typeof body === 'function') {
|
|
233
202
|
var promise = body();
|
|
234
|
-
|
|
235
203
|
if (promise && typeof promise.then === 'function') {
|
|
236
204
|
return new Promise(function (resolve) {
|
|
237
205
|
promise.then(function (b) {
|
|
238
206
|
for (var m in b) {
|
|
239
207
|
i18next.services.backendConnector.saveMissing([lng], ns, m, b[m]);
|
|
240
208
|
}
|
|
241
|
-
|
|
242
209
|
resolve(options.send(res, 'ok'));
|
|
243
210
|
});
|
|
244
211
|
});
|
|
245
212
|
}
|
|
246
213
|
}
|
|
247
|
-
|
|
248
214
|
for (var m in body) {
|
|
249
215
|
i18next.services.backendConnector.saveMissing([lng], ns, m, body[m]);
|
|
250
216
|
}
|
|
251
|
-
|
|
252
217
|
return options.send(res, 'ok');
|
|
253
218
|
};
|
|
254
219
|
}
|
|
255
|
-
|
|
256
220
|
function addRoute(i18next, route, lngs, app, verb, fc) {
|
|
257
221
|
if (typeof verb === 'function') {
|
|
258
222
|
fc = verb;
|
|
@@ -260,14 +224,11 @@ function addRoute(i18next, route, lngs, app, verb, fc) {
|
|
|
260
224
|
}
|
|
261
225
|
|
|
262
226
|
var callbacks = [fc].concat(Array.prototype.slice.call(arguments, 6));
|
|
263
|
-
|
|
264
227
|
for (var i = 0, li = lngs.length; i < li; i++) {
|
|
265
228
|
var parts = String(route).split('/');
|
|
266
229
|
var locRoute = [];
|
|
267
|
-
|
|
268
230
|
for (var y = 0, ly = parts.length; y < ly; y++) {
|
|
269
231
|
var part = parts[y];
|
|
270
|
-
|
|
271
232
|
if (part === ':lng') {
|
|
272
233
|
locRoute.push(lngs[i]);
|
|
273
234
|
} else if (part.indexOf(':') === 0 || part === '') {
|
|
@@ -278,12 +239,10 @@ function addRoute(i18next, route, lngs, app, verb, fc) {
|
|
|
278
239
|
}));
|
|
279
240
|
}
|
|
280
241
|
}
|
|
281
|
-
|
|
282
242
|
var routes = [locRoute.join('/')];
|
|
283
243
|
app[verb || 'get'].apply(app, routes.concat(callbacks));
|
|
284
244
|
}
|
|
285
245
|
}
|
|
286
|
-
|
|
287
246
|
var _default = {
|
|
288
247
|
plugin: plugin,
|
|
289
248
|
hapiPlugin: hapiPlugin,
|