i18next-http-middleware 3.8.0 → 3.8.1

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,13 @@ function getDefaults() {
35
35
  }
36
36
  });
37
37
  }
38
+ function getFallbackLngs(fallbackLng) {
39
+ var fallbacks = fallbackLng;
40
+ if (typeof fallbacks === 'function') fallbacks = fallbacks();
41
+ if (typeof fallbacks === 'string') fallbacks = [fallbacks];
42
+ if (!fallbacks) fallbacks = [];
43
+ return fallbacks;
44
+ }
38
45
  var LanguageDetector = function () {
39
46
  function LanguageDetector(services) {
40
47
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
@@ -62,6 +69,7 @@ var LanguageDetector = function () {
62
69
  this.addDetector(_path.default);
63
70
  this.addDetector(_header.default);
64
71
  this.addDetector(_session.default);
72
+ this.fallbacks = getFallbackLngs(this.allOptions.fallbackLng);
65
73
  }
66
74
  }, {
67
75
  key: "addDetector",
@@ -89,11 +97,13 @@ var LanguageDetector = function () {
89
97
  found = _this.services.languageUtils.getBestMatchFromCodes(detections);
90
98
  if (found) {
91
99
  if (_this.options.ignoreCase) {
92
- if (detections.map(function (d) {
100
+ if (detections.length === 0 || detections.map(function (d) {
101
+ return d.toLowerCase();
102
+ }).indexOf(found.toLowerCase()) < 0 && _this.fallbacks.map(function (d) {
93
103
  return d.toLowerCase();
94
- }).indexOf(found.toLowerCase()) < 0) found = undefined;
104
+ }).indexOf(found.toLowerCase()) > -1) found = undefined;
95
105
  } else {
96
- if (detections.indexOf(found) < 0) found = undefined;
106
+ if (detections.length === 0 || detections.indexOf(found) < 0 && _this.fallbacks.indexOf(found) > -1) found = undefined;
97
107
  }
98
108
  }
99
109
  if (found) req.i18nextLookupName = detectorName;
@@ -102,10 +112,7 @@ var LanguageDetector = function () {
102
112
  }
103
113
  });
104
114
  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 = [];
115
+ var fallbacks = this.fallbacks;
109
116
  if (Object.prototype.toString.apply(fallbacks) === '[object Array]') {
110
117
  found = fallbacks[0];
111
118
  } else {
@@ -26,6 +26,13 @@ function getDefaults() {
26
26
  }
27
27
  });
28
28
  }
29
+ function getFallbackLngs(fallbackLng) {
30
+ var fallbacks = fallbackLng;
31
+ if (typeof fallbacks === 'function') fallbacks = fallbacks();
32
+ if (typeof fallbacks === 'string') fallbacks = [fallbacks];
33
+ if (!fallbacks) fallbacks = [];
34
+ return fallbacks;
35
+ }
29
36
  var LanguageDetector = function () {
30
37
  function LanguageDetector(services) {
31
38
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
@@ -53,6 +60,7 @@ var LanguageDetector = function () {
53
60
  this.addDetector(pathLookup);
54
61
  this.addDetector(headerLookup);
55
62
  this.addDetector(sessionLookup);
63
+ this.fallbacks = getFallbackLngs(this.allOptions.fallbackLng);
56
64
  }
57
65
  }, {
58
66
  key: "addDetector",
@@ -80,11 +88,13 @@ var LanguageDetector = function () {
80
88
  found = _this.services.languageUtils.getBestMatchFromCodes(detections);
81
89
  if (found) {
82
90
  if (_this.options.ignoreCase) {
83
- if (detections.map(function (d) {
91
+ if (detections.length === 0 || detections.map(function (d) {
92
+ return d.toLowerCase();
93
+ }).indexOf(found.toLowerCase()) < 0 && _this.fallbacks.map(function (d) {
84
94
  return d.toLowerCase();
85
- }).indexOf(found.toLowerCase()) < 0) found = undefined;
95
+ }).indexOf(found.toLowerCase()) > -1) found = undefined;
86
96
  } else {
87
- if (detections.indexOf(found) < 0) found = undefined;
97
+ if (detections.length === 0 || detections.indexOf(found) < 0 && _this.fallbacks.indexOf(found) > -1) found = undefined;
88
98
  }
89
99
  }
90
100
  if (found) req.i18nextLookupName = detectorName;
@@ -93,10 +103,7 @@ var LanguageDetector = function () {
93
103
  }
94
104
  });
95
105
  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 = [];
106
+ var fallbacks = this.fallbacks;
100
107
  if (Object.prototype.toString.apply(fallbacks) === '[object Array]') {
101
108
  found = fallbacks[0];
102
109
  } else {
@@ -26,6 +26,14 @@ function getDefaults () {
26
26
  })
27
27
  }
28
28
 
29
+ function getFallbackLngs (fallbackLng) {
30
+ let fallbacks = fallbackLng
31
+ if (typeof fallbacks === 'function') fallbacks = fallbacks()
32
+ if (typeof fallbacks === 'string') fallbacks = [fallbacks]
33
+ if (!fallbacks) fallbacks = []
34
+ return fallbacks
35
+ }
36
+
29
37
  class LanguageDetector {
30
38
  constructor (services, options = {}, allOptions = {}) {
31
39
  this.type = 'languageDetector'
@@ -48,6 +56,8 @@ class LanguageDetector {
48
56
  this.addDetector(pathLookup)
49
57
  this.addDetector(headerLookup)
50
58
  this.addDetector(sessionLookup)
59
+
60
+ this.fallbacks = getFallbackLngs(this.allOptions.fallbackLng)
51
61
  }
52
62
 
53
63
  addDetector (detector) {
@@ -74,9 +84,9 @@ class LanguageDetector {
74
84
  found = this.services.languageUtils.getBestMatchFromCodes(detections)
75
85
  if (found) {
76
86
  if (this.options.ignoreCase) {
77
- if (detections.map(d => d.toLowerCase()).indexOf(found.toLowerCase()) < 0) found = undefined
87
+ if (detections.length === 0 || (detections.map(d => d.toLowerCase()).indexOf(found.toLowerCase()) < 0 && this.fallbacks.map(d => d.toLowerCase()).indexOf(found.toLowerCase()) > -1)) found = undefined
78
88
  } else {
79
- if (detections.indexOf(found) < 0) found = undefined
89
+ if (detections.length === 0 || (detections.indexOf(found) < 0 && this.fallbacks.indexOf(found) > -1)) found = undefined
80
90
  }
81
91
  }
82
92
  if (found) req.i18nextLookupName = detectorName
@@ -86,10 +96,7 @@ class LanguageDetector {
86
96
  })
87
97
 
88
98
  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 = []
99
+ const fallbacks = this.fallbacks
93
100
  if (Object.prototype.toString.apply(fallbacks) === '[object Array]') {
94
101
  found = fallbacks[0]
95
102
  } else {
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.1",
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",