i18next-http-middleware 3.8.0 → 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.
@@ -35,6 +35,16 @@ function getDefaults() {
35
35
  }
36
36
  });
37
37
  }
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 || [];
47
+ }
38
48
  var LanguageDetector = function () {
39
49
  function LanguageDetector(services) {
40
50
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
@@ -88,12 +98,15 @@ var LanguageDetector = function () {
88
98
  if (_this.services.languageUtils.getBestMatchFromCodes) {
89
99
  found = _this.services.languageUtils.getBestMatchFromCodes(detections);
90
100
  if (found) {
101
+ var fallbacks = getFallbackLngs(_this.allOptions.fallbackLng, found);
91
102
  if (_this.options.ignoreCase) {
92
- if (detections.map(function (d) {
103
+ if (detections.length === 0 || detections.map(function (d) {
104
+ return d.toLowerCase();
105
+ }).indexOf(found.toLowerCase()) < 0 && fallbacks.map(function (d) {
93
106
  return d.toLowerCase();
94
- }).indexOf(found.toLowerCase()) < 0) found = undefined;
107
+ }).indexOf(found.toLowerCase()) > -1) found = undefined;
95
108
  } else {
96
- if (detections.indexOf(found) < 0) found = undefined;
109
+ if (detections.length === 0 || detections.indexOf(found) < 0 && fallbacks.indexOf(found) > -1) found = undefined;
97
110
  }
98
111
  }
99
112
  if (found) req.i18nextLookupName = detectorName;
@@ -102,11 +115,8 @@ var LanguageDetector = function () {
102
115
  }
103
116
  });
104
117
  if (!found) {
105
- var fallbacks = this.allOptions.fallbackLng;
106
- if (typeof fallbacks === 'function') fallbacks = fallbacks();
107
- if (typeof fallbacks === 'string') fallbacks = [fallbacks];
108
- if (!fallbacks) fallbacks = [];
109
- if (Object.prototype.toString.apply(fallbacks) === '[object Array]') {
118
+ var fallbacks = getFallbackLngs(this.allOptions.fallbackLng);
119
+ if (Array.isArray(fallbacks)) {
110
120
  found = fallbacks[0];
111
121
  } else {
112
122
  found = fallbacks[0] || fallbacks.default && fallbacks.default[0];
@@ -26,6 +26,16 @@ function getDefaults() {
26
26
  }
27
27
  });
28
28
  }
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 || [];
38
+ }
29
39
  var LanguageDetector = function () {
30
40
  function LanguageDetector(services) {
31
41
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
@@ -79,12 +89,15 @@ var LanguageDetector = function () {
79
89
  if (_this.services.languageUtils.getBestMatchFromCodes) {
80
90
  found = _this.services.languageUtils.getBestMatchFromCodes(detections);
81
91
  if (found) {
92
+ var fallbacks = getFallbackLngs(_this.allOptions.fallbackLng, found);
82
93
  if (_this.options.ignoreCase) {
83
- if (detections.map(function (d) {
94
+ if (detections.length === 0 || detections.map(function (d) {
95
+ return d.toLowerCase();
96
+ }).indexOf(found.toLowerCase()) < 0 && fallbacks.map(function (d) {
84
97
  return d.toLowerCase();
85
- }).indexOf(found.toLowerCase()) < 0) found = undefined;
98
+ }).indexOf(found.toLowerCase()) > -1) found = undefined;
86
99
  } else {
87
- if (detections.indexOf(found) < 0) found = undefined;
100
+ if (detections.length === 0 || detections.indexOf(found) < 0 && fallbacks.indexOf(found) > -1) found = undefined;
88
101
  }
89
102
  }
90
103
  if (found) req.i18nextLookupName = detectorName;
@@ -93,11 +106,8 @@ var LanguageDetector = function () {
93
106
  }
94
107
  });
95
108
  if (!found) {
96
- var fallbacks = this.allOptions.fallbackLng;
97
- if (typeof fallbacks === 'function') fallbacks = fallbacks();
98
- if (typeof fallbacks === 'string') fallbacks = [fallbacks];
99
- if (!fallbacks) fallbacks = [];
100
- if (Object.prototype.toString.apply(fallbacks) === '[object Array]') {
109
+ var fallbacks = getFallbackLngs(this.allOptions.fallbackLng);
110
+ if (Array.isArray(fallbacks)) {
101
111
  found = fallbacks[0];
102
112
  } else {
103
113
  found = fallbacks[0] || fallbacks.default && fallbacks.default[0];
@@ -26,6 +26,21 @@ function getDefaults () {
26
26
  })
27
27
  }
28
28
 
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 || []
42
+ }
43
+
29
44
  class LanguageDetector {
30
45
  constructor (services, options = {}, allOptions = {}) {
31
46
  this.type = 'languageDetector'
@@ -73,10 +88,11 @@ class LanguageDetector {
73
88
  if (this.services.languageUtils.getBestMatchFromCodes) { // new i18next v19.5.0
74
89
  found = this.services.languageUtils.getBestMatchFromCodes(detections)
75
90
  if (found) {
91
+ const fallbacks = getFallbackLngs(this.allOptions.fallbackLng, found)
76
92
  if (this.options.ignoreCase) {
77
- if (detections.map(d => d.toLowerCase()).indexOf(found.toLowerCase()) < 0) found = undefined
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
78
94
  } else {
79
- if (detections.indexOf(found) < 0) found = undefined
95
+ if (detections.length === 0 || (detections.indexOf(found) < 0 && fallbacks.indexOf(found) > -1)) found = undefined
80
96
  }
81
97
  }
82
98
  if (found) req.i18nextLookupName = detectorName
@@ -86,11 +102,8 @@ class LanguageDetector {
86
102
  })
87
103
 
88
104
  if (!found) {
89
- let fallbacks = this.allOptions.fallbackLng
90
- if (typeof fallbacks === 'function') fallbacks = fallbacks()
91
- if (typeof fallbacks === 'string') fallbacks = [fallbacks]
92
- if (!fallbacks) fallbacks = []
93
- if (Object.prototype.toString.apply(fallbacks) === '[object Array]') {
105
+ const fallbacks = getFallbackLngs(this.allOptions.fallbackLng)
106
+ if (Array.isArray(fallbacks)) {
94
107
  found = fallbacks[0]
95
108
  } else {
96
109
  found = fallbacks[0] || (fallbacks.default && fallbacks.default[0])
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "i18next-http-middleware",
3
- "version": "3.8.0",
3
+ "version": "3.8.2",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./cjs/index.js",
@@ -46,7 +46,7 @@
46
46
  "expect.js": "0.3.1",
47
47
  "express": "4.21.1",
48
48
  "fastify": "5.3.2",
49
- "i18next": "24.0.0",
49
+ "i18next": "25.5.2",
50
50
  "mocha": "10.8.2",
51
51
  "supertest": "7.0.0",
52
52
  "tsd": "0.31.2",