i18next-http-middleware 3.8.1 → 3.8.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 +14 -11
- package/esm/LanguageDetector.js +14 -11
- package/lib/LanguageDetector.js +18 -12
- package/package.json +1 -1
package/cjs/LanguageDetector.js
CHANGED
|
@@ -35,12 +35,15 @@ function getDefaults() {
|
|
|
35
35
|
}
|
|
36
36
|
});
|
|
37
37
|
}
|
|
38
|
-
function getFallbackLngs(fallbackLng) {
|
|
39
|
-
|
|
40
|
-
if (typeof
|
|
41
|
-
if (typeof
|
|
42
|
-
if (
|
|
43
|
-
return
|
|
38
|
+
function getFallbackLngs(fallbackLng, code) {
|
|
39
|
+
if (!fallbackLng) return [];
|
|
40
|
+
if (typeof fallbackLng === 'function') fallbackLng = fallbackLng(code);
|
|
41
|
+
if (typeof fallbackLng === 'string') return [fallbackLng];
|
|
42
|
+
if (Array.isArray(fallbackLng)) return fallbackLng;
|
|
43
|
+
if (!code) return fallbackLng.default || [];
|
|
44
|
+
var found = fallbackLng[code];
|
|
45
|
+
if (!found) found = fallbackLng.default;
|
|
46
|
+
return found || [];
|
|
44
47
|
}
|
|
45
48
|
var LanguageDetector = function () {
|
|
46
49
|
function LanguageDetector(services) {
|
|
@@ -69,7 +72,6 @@ var LanguageDetector = function () {
|
|
|
69
72
|
this.addDetector(_path.default);
|
|
70
73
|
this.addDetector(_header.default);
|
|
71
74
|
this.addDetector(_session.default);
|
|
72
|
-
this.fallbacks = getFallbackLngs(this.allOptions.fallbackLng);
|
|
73
75
|
}
|
|
74
76
|
}, {
|
|
75
77
|
key: "addDetector",
|
|
@@ -96,14 +98,15 @@ var LanguageDetector = function () {
|
|
|
96
98
|
if (_this.services.languageUtils.getBestMatchFromCodes) {
|
|
97
99
|
found = _this.services.languageUtils.getBestMatchFromCodes(detections);
|
|
98
100
|
if (found) {
|
|
101
|
+
var fallbacks = getFallbackLngs(_this.allOptions.fallbackLng, found);
|
|
99
102
|
if (_this.options.ignoreCase) {
|
|
100
103
|
if (detections.length === 0 || detections.map(function (d) {
|
|
101
104
|
return d.toLowerCase();
|
|
102
|
-
}).indexOf(found.toLowerCase()) < 0 &&
|
|
105
|
+
}).indexOf(found.toLowerCase()) < 0 && fallbacks.map(function (d) {
|
|
103
106
|
return d.toLowerCase();
|
|
104
107
|
}).indexOf(found.toLowerCase()) > -1) found = undefined;
|
|
105
108
|
} else {
|
|
106
|
-
if (detections.length === 0 || detections.indexOf(found) < 0 &&
|
|
109
|
+
if (detections.length === 0 || detections.indexOf(found) < 0 && fallbacks.indexOf(found) > -1) found = undefined;
|
|
107
110
|
}
|
|
108
111
|
}
|
|
109
112
|
if (found) req.i18nextLookupName = detectorName;
|
|
@@ -112,8 +115,8 @@ var LanguageDetector = function () {
|
|
|
112
115
|
}
|
|
113
116
|
});
|
|
114
117
|
if (!found) {
|
|
115
|
-
var fallbacks = this.
|
|
116
|
-
if (
|
|
118
|
+
var fallbacks = getFallbackLngs(this.allOptions.fallbackLng);
|
|
119
|
+
if (Array.isArray(fallbacks)) {
|
|
117
120
|
found = fallbacks[0];
|
|
118
121
|
} else {
|
|
119
122
|
found = fallbacks[0] || fallbacks.default && fallbacks.default[0];
|
package/esm/LanguageDetector.js
CHANGED
|
@@ -26,12 +26,15 @@ function getDefaults() {
|
|
|
26
26
|
}
|
|
27
27
|
});
|
|
28
28
|
}
|
|
29
|
-
function getFallbackLngs(fallbackLng) {
|
|
30
|
-
|
|
31
|
-
if (typeof
|
|
32
|
-
if (typeof
|
|
33
|
-
if (
|
|
34
|
-
return
|
|
29
|
+
function getFallbackLngs(fallbackLng, code) {
|
|
30
|
+
if (!fallbackLng) return [];
|
|
31
|
+
if (typeof fallbackLng === 'function') fallbackLng = fallbackLng(code);
|
|
32
|
+
if (typeof fallbackLng === 'string') return [fallbackLng];
|
|
33
|
+
if (Array.isArray(fallbackLng)) return fallbackLng;
|
|
34
|
+
if (!code) return fallbackLng.default || [];
|
|
35
|
+
var found = fallbackLng[code];
|
|
36
|
+
if (!found) found = fallbackLng.default;
|
|
37
|
+
return found || [];
|
|
35
38
|
}
|
|
36
39
|
var LanguageDetector = function () {
|
|
37
40
|
function LanguageDetector(services) {
|
|
@@ -60,7 +63,6 @@ var LanguageDetector = function () {
|
|
|
60
63
|
this.addDetector(pathLookup);
|
|
61
64
|
this.addDetector(headerLookup);
|
|
62
65
|
this.addDetector(sessionLookup);
|
|
63
|
-
this.fallbacks = getFallbackLngs(this.allOptions.fallbackLng);
|
|
64
66
|
}
|
|
65
67
|
}, {
|
|
66
68
|
key: "addDetector",
|
|
@@ -87,14 +89,15 @@ var LanguageDetector = function () {
|
|
|
87
89
|
if (_this.services.languageUtils.getBestMatchFromCodes) {
|
|
88
90
|
found = _this.services.languageUtils.getBestMatchFromCodes(detections);
|
|
89
91
|
if (found) {
|
|
92
|
+
var fallbacks = getFallbackLngs(_this.allOptions.fallbackLng, found);
|
|
90
93
|
if (_this.options.ignoreCase) {
|
|
91
94
|
if (detections.length === 0 || detections.map(function (d) {
|
|
92
95
|
return d.toLowerCase();
|
|
93
|
-
}).indexOf(found.toLowerCase()) < 0 &&
|
|
96
|
+
}).indexOf(found.toLowerCase()) < 0 && fallbacks.map(function (d) {
|
|
94
97
|
return d.toLowerCase();
|
|
95
98
|
}).indexOf(found.toLowerCase()) > -1) found = undefined;
|
|
96
99
|
} else {
|
|
97
|
-
if (detections.length === 0 || detections.indexOf(found) < 0 &&
|
|
100
|
+
if (detections.length === 0 || detections.indexOf(found) < 0 && fallbacks.indexOf(found) > -1) found = undefined;
|
|
98
101
|
}
|
|
99
102
|
}
|
|
100
103
|
if (found) req.i18nextLookupName = detectorName;
|
|
@@ -103,8 +106,8 @@ var LanguageDetector = function () {
|
|
|
103
106
|
}
|
|
104
107
|
});
|
|
105
108
|
if (!found) {
|
|
106
|
-
var fallbacks = this.
|
|
107
|
-
if (
|
|
109
|
+
var fallbacks = getFallbackLngs(this.allOptions.fallbackLng);
|
|
110
|
+
if (Array.isArray(fallbacks)) {
|
|
108
111
|
found = fallbacks[0];
|
|
109
112
|
} else {
|
|
110
113
|
found = fallbacks[0] || fallbacks.default && fallbacks.default[0];
|
package/lib/LanguageDetector.js
CHANGED
|
@@ -26,12 +26,19 @@ function getDefaults () {
|
|
|
26
26
|
})
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
function getFallbackLngs (fallbackLng) {
|
|
30
|
-
|
|
31
|
-
if (typeof
|
|
32
|
-
if (typeof
|
|
33
|
-
if (
|
|
34
|
-
|
|
29
|
+
function getFallbackLngs (fallbackLng, code) {
|
|
30
|
+
if (!fallbackLng) return []
|
|
31
|
+
if (typeof fallbackLng === 'function') fallbackLng = fallbackLng(code)
|
|
32
|
+
if (typeof fallbackLng === 'string') return [fallbackLng]
|
|
33
|
+
if (Array.isArray(fallbackLng)) return fallbackLng
|
|
34
|
+
|
|
35
|
+
if (!code) return fallbackLng.default || []
|
|
36
|
+
|
|
37
|
+
// assume we have an object defining fallbacks
|
|
38
|
+
let found = fallbackLng[code]
|
|
39
|
+
if (!found) found = fallbackLng.default
|
|
40
|
+
|
|
41
|
+
return found || []
|
|
35
42
|
}
|
|
36
43
|
|
|
37
44
|
class LanguageDetector {
|
|
@@ -56,8 +63,6 @@ class LanguageDetector {
|
|
|
56
63
|
this.addDetector(pathLookup)
|
|
57
64
|
this.addDetector(headerLookup)
|
|
58
65
|
this.addDetector(sessionLookup)
|
|
59
|
-
|
|
60
|
-
this.fallbacks = getFallbackLngs(this.allOptions.fallbackLng)
|
|
61
66
|
}
|
|
62
67
|
|
|
63
68
|
addDetector (detector) {
|
|
@@ -83,10 +88,11 @@ class LanguageDetector {
|
|
|
83
88
|
if (this.services.languageUtils.getBestMatchFromCodes) { // new i18next v19.5.0
|
|
84
89
|
found = this.services.languageUtils.getBestMatchFromCodes(detections)
|
|
85
90
|
if (found) {
|
|
91
|
+
const fallbacks = getFallbackLngs(this.allOptions.fallbackLng, found)
|
|
86
92
|
if (this.options.ignoreCase) {
|
|
87
|
-
if (detections.length === 0 || (detections.map(d => d.toLowerCase()).indexOf(found.toLowerCase()) < 0 &&
|
|
93
|
+
if (detections.length === 0 || (detections.map(d => d.toLowerCase()).indexOf(found.toLowerCase()) < 0 && fallbacks.map(d => d.toLowerCase()).indexOf(found.toLowerCase()) > -1)) found = undefined
|
|
88
94
|
} else {
|
|
89
|
-
if (detections.length === 0 || (detections.indexOf(found) < 0 &&
|
|
95
|
+
if (detections.length === 0 || (detections.indexOf(found) < 0 && fallbacks.indexOf(found) > -1)) found = undefined
|
|
90
96
|
}
|
|
91
97
|
}
|
|
92
98
|
if (found) req.i18nextLookupName = detectorName
|
|
@@ -96,8 +102,8 @@ class LanguageDetector {
|
|
|
96
102
|
})
|
|
97
103
|
|
|
98
104
|
if (!found) {
|
|
99
|
-
const fallbacks = this.
|
|
100
|
-
if (
|
|
105
|
+
const fallbacks = getFallbackLngs(this.allOptions.fallbackLng)
|
|
106
|
+
if (Array.isArray(fallbacks)) {
|
|
101
107
|
found = fallbacks[0]
|
|
102
108
|
} else {
|
|
103
109
|
found = fallbacks[0] || (fallbacks.default && fallbacks.default[0])
|