i18next-http-middleware 2.1.1 → 3.0.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/CHANGELOG.md +13 -0
- package/README.md +2 -11
- package/cjs/LanguageDetector.js +19 -61
- package/cjs/httpFunctions.js +1 -2
- package/cjs/index.js +14 -22
- package/cjs/languageLookups/cookie.js +2 -3
- package/cjs/languageLookups/header.js +13 -3
- package/cjs/languageLookups/path.js +2 -3
- package/cjs/languageLookups/querystring.js +2 -2
- package/cjs/languageLookups/session.js +2 -2
- package/cjs/utils.js +1 -2
- package/esm/LanguageDetector.js +119 -0
- package/esm/httpFunctions.js +120 -0
- package/esm/index.js +229 -0
- package/esm/languageLookups/cookie.js +112 -0
- package/esm/languageLookups/header.js +59 -0
- package/esm/languageLookups/path.js +29 -0
- package/esm/languageLookups/querystring.js +35 -0
- package/esm/languageLookups/session.js +21 -0
- package/esm/utils.js +55 -0
- package/lib/LanguageDetector.js +6 -42
- package/lib/languageLookups/header.js +8 -1
- package/package.json +17 -12
package/lib/LanguageDetector.js
CHANGED
|
@@ -19,10 +19,6 @@ function getDefaults () {
|
|
|
19
19
|
// cookieExpirationDate: new Date(),
|
|
20
20
|
// cookieDomain: 'myDomain'
|
|
21
21
|
// cookiePath: '/my/path'
|
|
22
|
-
|
|
23
|
-
checkWhitelist: true,
|
|
24
|
-
checkForSimilarInWhitelist: false,
|
|
25
|
-
|
|
26
22
|
cookieSameSite: 'strict'
|
|
27
23
|
})
|
|
28
24
|
}
|
|
@@ -39,8 +35,6 @@ class LanguageDetector {
|
|
|
39
35
|
this.services = services
|
|
40
36
|
this.options = utils.defaults(options, this.options || {}, getDefaults())
|
|
41
37
|
this.allOptions = allOptions
|
|
42
|
-
// if checking for similar, user needs to check whitelist
|
|
43
|
-
if (this.options.checkForSimilarInWhitelist) this.options.checkWhitelist = true
|
|
44
38
|
|
|
45
39
|
this.addDetector(cookieLookup)
|
|
46
40
|
this.addDetector(querystringLookup)
|
|
@@ -65,18 +59,12 @@ class LanguageDetector {
|
|
|
65
59
|
if (!detections) return
|
|
66
60
|
if (!Array.isArray(detections)) detections = [detections]
|
|
67
61
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (!found && this.options.checkForSimilarInWhitelist) {
|
|
77
|
-
found = this.getSimilarInWhitelist(cleanedLng)
|
|
78
|
-
}
|
|
79
|
-
})
|
|
62
|
+
if (this.services.languageUtils.getBestMatchFromCodes) { // new i18next v19.5.0
|
|
63
|
+
found = this.services.languageUtils.getBestMatchFromCodes(detections)
|
|
64
|
+
if (found) req.i18nextLookupName = detectorName
|
|
65
|
+
} else {
|
|
66
|
+
found = detections.length > 0 ? detections[0] : null // a little backward compatibility
|
|
67
|
+
}
|
|
80
68
|
})
|
|
81
69
|
|
|
82
70
|
if (!found) {
|
|
@@ -105,30 +93,6 @@ class LanguageDetector {
|
|
|
105
93
|
}
|
|
106
94
|
})
|
|
107
95
|
}
|
|
108
|
-
|
|
109
|
-
getSimilarInWhitelist (cleanedLng) {
|
|
110
|
-
if (!this.i18nOptions.whitelist) return
|
|
111
|
-
|
|
112
|
-
if (cleanedLng.includes('-')) {
|
|
113
|
-
// i.e. es-MX should check if es is in whitelist
|
|
114
|
-
const prefix = cleanedLng.split('-')[0]
|
|
115
|
-
|
|
116
|
-
const cleanedPrefix = this.services.languageUtils.formatLanguageCode(prefix)
|
|
117
|
-
|
|
118
|
-
if (this.services.languageUtils.isWhitelisted(cleanedPrefix)) return cleanedPrefix
|
|
119
|
-
|
|
120
|
-
// if reached here, nothing found. continue to search for similar using only prefix
|
|
121
|
-
cleanedLng = cleanedPrefix
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// i.e. 'pt' should return 'pt-BR'. If multiple in whitelist with 'pt-', then use first one in whitelist
|
|
125
|
-
const similar = this.i18nOptions.whitelist.find((whitelistLng) => {
|
|
126
|
-
const cleanedWhitelistLng = this.services.languageUtils.formatLanguageCode(whitelistLng)
|
|
127
|
-
if (cleanedWhitelistLng.startsWith(cleanedLng)) return cleanedWhitelistLng
|
|
128
|
-
})
|
|
129
|
-
|
|
130
|
-
if (similar) return similar
|
|
131
|
-
}
|
|
132
96
|
}
|
|
133
97
|
|
|
134
98
|
LanguageDetector.type = 'languageDetector'
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
const specialCases = ['hans', 'hant', 'latn', 'cyrl', 'cans', 'mong', 'arab']
|
|
2
|
+
|
|
1
3
|
export default {
|
|
2
4
|
name: 'header',
|
|
3
5
|
|
|
@@ -12,8 +14,13 @@ export default {
|
|
|
12
14
|
const acceptLanguage = options.lookupHeader ? headers[options.lookupHeader] : headers['accept-language']
|
|
13
15
|
|
|
14
16
|
if (acceptLanguage) {
|
|
17
|
+
let lookupRegex = /(([a-z]{2})-?([A-Z]{2})?)\s*;?\s*(q=([0-9.]+))?/gi
|
|
18
|
+
if (acceptLanguage.indexOf('-') > 0) {
|
|
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
|
|
21
|
+
}
|
|
15
22
|
const lngs = []; let i; let m
|
|
16
|
-
const rgx =
|
|
23
|
+
const rgx = options.lookupHeaderRegex || lookupRegex
|
|
17
24
|
|
|
18
25
|
do {
|
|
19
26
|
m = rgx.exec(acceptLanguage)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "i18next-http-middleware",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./cjs/index.js",
|
|
@@ -8,22 +8,25 @@
|
|
|
8
8
|
"./package.json": "./package.json",
|
|
9
9
|
".": {
|
|
10
10
|
"require": "./cjs/index.js",
|
|
11
|
-
"default": "./
|
|
11
|
+
"default": "./esm/index.js"
|
|
12
12
|
},
|
|
13
13
|
"./cjs": {
|
|
14
14
|
"default": "./cjs/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./esm": {
|
|
17
|
+
"default": "./esm/index.js"
|
|
15
18
|
}
|
|
16
19
|
},
|
|
17
|
-
"module": "./
|
|
20
|
+
"module": "./esm/index.js",
|
|
18
21
|
"dependencies": {},
|
|
19
22
|
"devDependencies": {
|
|
20
|
-
"@babel/cli": "7.10.
|
|
21
|
-
"@babel/core": "7.10.
|
|
22
|
-
"@babel/preset-env": "7.10.
|
|
23
|
+
"@babel/cli": "7.10.4",
|
|
24
|
+
"@babel/core": "7.10.4",
|
|
25
|
+
"@babel/preset-env": "7.10.4",
|
|
23
26
|
"babel-plugin-add-module-exports": "1.0.2",
|
|
24
|
-
"eslint": "7.
|
|
27
|
+
"eslint": "7.3.1",
|
|
25
28
|
"eslint-config-standard": "14.1.1",
|
|
26
|
-
"eslint-plugin-import": "2.
|
|
29
|
+
"eslint-plugin-import": "2.22.0",
|
|
27
30
|
"eslint-plugin-node": "11.1.0",
|
|
28
31
|
"eslint-plugin-promise": "4.2.1",
|
|
29
32
|
"eslint-plugin-require-path-exists": "1.1.9",
|
|
@@ -31,10 +34,10 @@
|
|
|
31
34
|
"expect.js": "0.3.1",
|
|
32
35
|
"express": "4.17.1",
|
|
33
36
|
"fastify": "3.0.0-rc.1",
|
|
34
|
-
"i18next": "19.
|
|
35
|
-
"mocha": "
|
|
37
|
+
"i18next": "19.5.2",
|
|
38
|
+
"mocha": "8.0.1",
|
|
36
39
|
"supertest": "4.0.2",
|
|
37
|
-
"uglify-js": "3.
|
|
40
|
+
"uglify-js": "3.10.0"
|
|
38
41
|
},
|
|
39
42
|
"description": "i18next-http-middleware is a middleware to be used with Node.js web frameworks like express or Fastify and also for Deno.",
|
|
40
43
|
"keywords": [
|
|
@@ -59,7 +62,9 @@
|
|
|
59
62
|
],
|
|
60
63
|
"scripts": {
|
|
61
64
|
"lint": "eslint .",
|
|
62
|
-
"compile": "rm -rf
|
|
65
|
+
"compile:esm": "rm -rf esm && mkdir esm && BABEL_ENV=esm babel lib -d esm",
|
|
66
|
+
"compile:cjs": "rm -rf cjs && mkdir cjs && BABEL_ENV=cjs babel lib -d cjs && echo '{\"type\":\"commonjs\"}' > cjs/package.json",
|
|
67
|
+
"compile": "npm run compile:esm && npm run compile:cjs",
|
|
63
68
|
"build": "npm run compile",
|
|
64
69
|
"test": "npm run lint && npm run build && mocha test -R spec --exit --experimental-modules",
|
|
65
70
|
"test:deno": "deno test test/deno/*.js --allow-net",
|