i18next-http-middleware 3.2.2 → 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/index.js +6 -0
- package/esm/index.js +6 -0
- package/lib/index.js +13 -2
- package/package.json +1 -1
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/index.js
CHANGED
|
@@ -160,9 +160,15 @@ function getResourcesHandler(i18next) {
|
|
|
160
160
|
options.setHeader(res, 'Pragma', 'no-cache');
|
|
161
161
|
options.setHeader(res, 'Cache-Control', 'no-cache');
|
|
162
162
|
}
|
|
163
|
+
|
|
163
164
|
var languages = options.getQuery(req)[options.lngParam || 'lng'] ? options.getQuery(req)[options.lngParam || 'lng'].split(' ') : [];
|
|
164
165
|
var namespaces = options.getQuery(req)[options.nsParam || 'ns'] ? options.getQuery(req)[options.nsParam || 'ns'].split(' ') : [];
|
|
165
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
|
+
|
|
166
172
|
namespaces.forEach(function (ns) {
|
|
167
173
|
if (i18next.options.ns && i18next.options.ns.indexOf(ns) < 0) {
|
|
168
174
|
i18next.options.ns.push(ns);
|
package/esm/index.js
CHANGED
|
@@ -141,9 +141,15 @@ export function getResourcesHandler(i18next) {
|
|
|
141
141
|
options.setHeader(res, 'Pragma', 'no-cache');
|
|
142
142
|
options.setHeader(res, 'Cache-Control', 'no-cache');
|
|
143
143
|
}
|
|
144
|
+
|
|
144
145
|
var languages = options.getQuery(req)[options.lngParam || 'lng'] ? options.getQuery(req)[options.lngParam || 'lng'].split(' ') : [];
|
|
145
146
|
var namespaces = options.getQuery(req)[options.nsParam || 'ns'] ? options.getQuery(req)[options.nsParam || 'ns'].split(' ') : [];
|
|
146
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
|
+
|
|
147
153
|
namespaces.forEach(function (ns) {
|
|
148
154
|
if (i18next.options.ns && i18next.options.ns.indexOf(ns) < 0) {
|
|
149
155
|
i18next.options.ns.push(ns);
|
package/lib/index.js
CHANGED
|
@@ -176,13 +176,24 @@ export function getResourcesHandler (i18next, options = {}) {
|
|
|
176
176
|
options.setHeader(res, 'Cache-Control', 'no-cache')
|
|
177
177
|
}
|
|
178
178
|
|
|
179
|
-
|
|
179
|
+
// first check query
|
|
180
|
+
let languages = options.getQuery(req)[options.lngParam || 'lng']
|
|
180
181
|
? options.getQuery(req)[options.lngParam || 'lng'].split(' ')
|
|
181
182
|
: []
|
|
182
|
-
|
|
183
|
+
let namespaces = options.getQuery(req)[options.nsParam || 'ns']
|
|
183
184
|
? options.getQuery(req)[options.nsParam || 'ns'].split(' ')
|
|
184
185
|
: []
|
|
185
186
|
|
|
187
|
+
// then check route params
|
|
188
|
+
if (languages.length === 0 && namespaces.length === 0) {
|
|
189
|
+
languages = options.getParams(req)[options.lngParam || 'lng']
|
|
190
|
+
? options.getParams(req)[options.lngParam || 'lng'].split(' ')
|
|
191
|
+
: []
|
|
192
|
+
namespaces = options.getParams(req)[options.nsParam || 'ns']
|
|
193
|
+
? options.getParams(req)[options.nsParam || 'ns'].split(' ')
|
|
194
|
+
: []
|
|
195
|
+
}
|
|
196
|
+
|
|
186
197
|
// extend ns
|
|
187
198
|
namespaces.forEach(ns => {
|
|
188
199
|
if (i18next.options.ns && i18next.options.ns.indexOf(ns) < 0) {
|