Skip to content

Commit 0ee5832

Browse files
committed
typescript fix for i18next v22
1 parent 8d199fc commit 0ee5832

5 files changed

Lines changed: 1182 additions & 364 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 7.0.0
2+
3+
- typescript fix for i18next v22
4+
15
### 6.1.8
26

37
- fix export order for typescript [267](https://github.com/i18next/i18next-browser-languageDetector/issues/267)

i18nextBrowserLanguageDetector.js

Lines changed: 10 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
Object.defineProperty(target, descriptor.key, descriptor);
2020
}
2121
}
22-
2322
function _createClass(Constructor, protoProps, staticProps) {
2423
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
2524
if (staticProps) _defineProperties(Constructor, staticProps);
@@ -45,103 +44,80 @@
4544

4645
// eslint-disable-next-line no-control-regex
4746
var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
48-
4947
var serializeCookie = function serializeCookie(name, val, options) {
5048
var opt = options || {};
5149
opt.path = opt.path || '/';
5250
var value = encodeURIComponent(val);
5351
var str = "".concat(name, "=").concat(value);
54-
5552
if (opt.maxAge > 0) {
5653
var maxAge = opt.maxAge - 0;
5754
if (Number.isNaN(maxAge)) throw new Error('maxAge should be a Number');
5855
str += "; Max-Age=".concat(Math.floor(maxAge));
5956
}
60-
6157
if (opt.domain) {
6258
if (!fieldContentRegExp.test(opt.domain)) {
6359
throw new TypeError('option domain is invalid');
6460
}
65-
6661
str += "; Domain=".concat(opt.domain);
6762
}
68-
6963
if (opt.path) {
7064
if (!fieldContentRegExp.test(opt.path)) {
7165
throw new TypeError('option path is invalid');
7266
}
73-
7467
str += "; Path=".concat(opt.path);
7568
}
76-
7769
if (opt.expires) {
7870
if (typeof opt.expires.toUTCString !== 'function') {
7971
throw new TypeError('option expires is invalid');
8072
}
81-
8273
str += "; Expires=".concat(opt.expires.toUTCString());
8374
}
84-
8575
if (opt.httpOnly) str += '; HttpOnly';
8676
if (opt.secure) str += '; Secure';
87-
8877
if (opt.sameSite) {
8978
var sameSite = typeof opt.sameSite === 'string' ? opt.sameSite.toLowerCase() : opt.sameSite;
90-
9179
switch (sameSite) {
9280
case true:
9381
str += '; SameSite=Strict';
9482
break;
95-
9683
case 'lax':
9784
str += '; SameSite=Lax';
9885
break;
99-
10086
case 'strict':
10187
str += '; SameSite=Strict';
10288
break;
103-
10489
case 'none':
10590
str += '; SameSite=None';
10691
break;
107-
10892
default:
10993
throw new TypeError('option sameSite is invalid');
11094
}
11195
}
112-
11396
return str;
11497
};
115-
11698
var cookie = {
11799
create: function create(name, value, minutes, domain) {
118100
var cookieOptions = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {
119101
path: '/',
120102
sameSite: 'strict'
121103
};
122-
123104
if (minutes) {
124105
cookieOptions.expires = new Date();
125106
cookieOptions.expires.setTime(cookieOptions.expires.getTime() + minutes * 60 * 1000);
126107
}
127-
128108
if (domain) cookieOptions.domain = domain;
129109
document.cookie = serializeCookie(name, encodeURIComponent(value), cookieOptions);
130110
},
131111
read: function read(name) {
132112
var nameEQ = "".concat(name, "=");
133113
var ca = document.cookie.split(';');
134-
135114
for (var i = 0; i < ca.length; i++) {
136115
var c = ca[i];
137-
138116
while (c.charAt(0) === ' ') {
139117
c = c.substring(1, c.length);
140118
}
141-
142119
if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
143120
}
144-
145121
return null;
146122
},
147123
remove: function remove(name) {
@@ -152,12 +128,10 @@
152128
name: 'cookie',
153129
lookup: function lookup(options) {
154130
var found;
155-
156131
if (options.lookupCookie && typeof document !== 'undefined') {
157132
var c = cookie.read(options.lookupCookie);
158133
if (c) found = c;
159134
}
160-
161135
return found;
162136
},
163137
cacheUserLanguage: function cacheUserLanguage(lng, options) {
@@ -171,39 +145,30 @@
171145
name: 'querystring',
172146
lookup: function lookup(options) {
173147
var found;
174-
175148
if (typeof window !== 'undefined') {
176149
var search = window.location.search;
177-
178150
if (!window.location.search && window.location.hash && window.location.hash.indexOf('?') > -1) {
179151
search = window.location.hash.substring(window.location.hash.indexOf('?'));
180152
}
181-
182153
var query = search.substring(1);
183154
var params = query.split('&');
184-
185155
for (var i = 0; i < params.length; i++) {
186156
var pos = params[i].indexOf('=');
187-
188157
if (pos > 0) {
189158
var key = params[i].substring(0, pos);
190-
191159
if (key === options.lookupQuerystring) {
192160
found = params[i].substring(pos + 1);
193161
}
194162
}
195163
}
196164
}
197-
198165
return found;
199166
}
200167
};
201168

202169
var hasLocalStorageSupport = null;
203-
204170
var localStorageAvailable = function localStorageAvailable() {
205171
if (hasLocalStorageSupport !== null) return hasLocalStorageSupport;
206-
207172
try {
208173
hasLocalStorageSupport = window !== 'undefined' && window.localStorage !== null;
209174
var testKey = 'i18next.translate.boo';
@@ -212,20 +177,16 @@
212177
} catch (e) {
213178
hasLocalStorageSupport = false;
214179
}
215-
216180
return hasLocalStorageSupport;
217181
};
218-
219182
var localStorage = {
220183
name: 'localStorage',
221184
lookup: function lookup(options) {
222185
var found;
223-
224186
if (options.lookupLocalStorage && localStorageAvailable()) {
225187
var lng = window.localStorage.getItem(options.lookupLocalStorage);
226188
if (lng) found = lng;
227189
}
228-
229190
return found;
230191
},
231192
cacheUserLanguage: function cacheUserLanguage(lng, options) {
@@ -236,10 +197,8 @@
236197
};
237198

238199
var hasSessionStorageSupport = null;
239-
240200
var sessionStorageAvailable = function sessionStorageAvailable() {
241201
if (hasSessionStorageSupport !== null) return hasSessionStorageSupport;
242-
243202
try {
244203
hasSessionStorageSupport = window !== 'undefined' && window.sessionStorage !== null;
245204
var testKey = 'i18next.translate.boo';
@@ -248,20 +207,16 @@
248207
} catch (e) {
249208
hasSessionStorageSupport = false;
250209
}
251-
252210
return hasSessionStorageSupport;
253211
};
254-
255212
var sessionStorage = {
256213
name: 'sessionStorage',
257214
lookup: function lookup(options) {
258215
var found;
259-
260216
if (options.lookupSessionStorage && sessionStorageAvailable()) {
261217
var lng = window.sessionStorage.getItem(options.lookupSessionStorage);
262218
if (lng) found = lng;
263219
}
264-
265220
return found;
266221
},
267222
cacheUserLanguage: function cacheUserLanguage(lng, options) {
@@ -275,24 +230,20 @@
275230
name: 'navigator',
276231
lookup: function lookup(options) {
277232
var found = [];
278-
279233
if (typeof navigator !== 'undefined') {
280234
if (navigator.languages) {
281235
// chrome only; not an array, so can't use .push.apply instead of iterating
282236
for (var i = 0; i < navigator.languages.length; i++) {
283237
found.push(navigator.languages[i]);
284238
}
285239
}
286-
287240
if (navigator.userLanguage) {
288241
found.push(navigator.userLanguage);
289242
}
290-
291243
if (navigator.language) {
292244
found.push(navigator.language);
293245
}
294246
}
295-
296247
return found.length > 0 ? found : undefined;
297248
}
298249
};
@@ -302,11 +253,9 @@
302253
lookup: function lookup(options) {
303254
var found;
304255
var htmlTag = options.htmlTag || (typeof document !== 'undefined' ? document.documentElement : null);
305-
306256
if (htmlTag && typeof htmlTag.getAttribute === 'function') {
307257
found = htmlTag.getAttribute('lang');
308258
}
309-
310259
return found;
311260
}
312261
};
@@ -315,23 +264,19 @@
315264
name: 'path',
316265
lookup: function lookup(options) {
317266
var found;
318-
319267
if (typeof window !== 'undefined') {
320268
var language = window.location.pathname.match(/\/([a-zA-Z-]*)/g);
321-
322269
if (language instanceof Array) {
323270
if (typeof options.lookupFromPathIndex === 'number') {
324271
if (typeof language[options.lookupFromPathIndex] !== 'string') {
325272
return undefined;
326273
}
327-
328274
found = language[options.lookupFromPathIndex].replace('/', '');
329275
} else {
330276
found = language[0].replace('/', '');
331277
}
332278
}
333279
}
334-
335280
return found;
336281
}
337282
};
@@ -340,14 +285,15 @@
340285
name: 'subdomain',
341286
lookup: function lookup(options) {
342287
// If given get the subdomain index else 1
343-
var lookupFromSubdomainIndex = typeof options.lookupFromSubdomainIndex === 'number' ? options.lookupFromSubdomainIndex + 1 : 1; // get all matches if window.location. is existing
288+
var lookupFromSubdomainIndex = typeof options.lookupFromSubdomainIndex === 'number' ? options.lookupFromSubdomainIndex + 1 : 1;
289+
// get all matches if window.location. is existing
344290
// first item of match is the match itself and the second is the first group macht which sould be the first subdomain match
345291
// is the hostname no public domain get the or option of localhost
292+
var language = typeof window !== 'undefined' && window.location && window.location.hostname && window.location.hostname.match(/^(\w{2,5})\.(([a-z0-9-]{1,63}\.[a-z]{2,6})|localhost)/i);
346293

347-
var language = typeof window !== 'undefined' && window.location && window.location.hostname && window.location.hostname.match(/^(\w{2,5})\.(([a-z0-9-]{1,63}\.[a-z]{2,6})|localhost)/i); // if there is no match (null) return undefined
348-
349-
if (!language) return undefined; // return the given group match
350-
294+
// if there is no match (null) return undefined
295+
if (!language) return undefined;
296+
// return the given group match
351297
return language[lookupFromSubdomainIndex];
352298
}
353299
};
@@ -361,31 +307,28 @@
361307
lookupSessionStorage: 'i18nextLng',
362308
// cache user language
363309
caches: ['localStorage'],
364-
excludeCacheFor: ['cimode'] // cookieMinutes: 10,
310+
excludeCacheFor: ['cimode']
311+
// cookieMinutes: 10,
365312
// cookieDomain: 'myDomain'
366-
367313
};
368314
}
369-
370315
var Browser = /*#__PURE__*/function () {
371316
function Browser(services) {
372317
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
373-
374318
_classCallCheck(this, Browser);
375-
376319
this.type = 'languageDetector';
377320
this.detectors = {};
378321
this.init(services, options);
379322
}
380-
381323
_createClass(Browser, [{
382324
key: "init",
383325
value: function init(services) {
384326
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
385327
var i18nOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
386328
this.services = services;
387-
this.options = defaults(options, this.options || {}, getDefaults()); // backwards compatibility
329+
this.options = defaults(options, this.options || {}, getDefaults());
388330

331+
// backwards compatibility
389332
if (this.options.lookupFromUrlIndex) this.options.lookupFromPathIndex = this.options.lookupFromUrlIndex;
390333
this.i18nOptions = i18nOptions;
391334
this.addDetector(cookie$1);
@@ -406,26 +349,22 @@
406349
key: "detect",
407350
value: function detect(detectionOrder) {
408351
var _this = this;
409-
410352
if (!detectionOrder) detectionOrder = this.options.order;
411353
var detected = [];
412354
detectionOrder.forEach(function (detectorName) {
413355
if (_this.detectors[detectorName]) {
414356
var lookup = _this.detectors[detectorName].lookup(_this.options);
415-
416357
if (lookup && typeof lookup === 'string') lookup = [lookup];
417358
if (lookup) detected = detected.concat(lookup);
418359
}
419360
});
420361
if (this.services.languageUtils.getBestMatchFromCodes) return detected; // new i18next v19.5.0
421-
422362
return detected.length > 0 ? detected[0] : null; // a little backward compatibility
423363
}
424364
}, {
425365
key: "cacheUserLanguage",
426366
value: function cacheUserLanguage(lng, caches) {
427367
var _this2 = this;
428-
429368
if (!caches) caches = this.options.caches;
430369
if (!caches) return;
431370
if (this.options.excludeCacheFor && this.options.excludeCacheFor.indexOf(lng) > -1) return;
@@ -434,10 +373,8 @@
434373
});
435374
}
436375
}]);
437-
438376
return Browser;
439377
}();
440-
441378
Browser.type = 'languageDetector';
442379

443380
return Browser;

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export default class I18nextBrowserLanguageDetector implements i18next.LanguageD
9090
}
9191

9292
declare module 'i18next' {
93-
interface PluginOptions {
93+
interface CustomPluginOptions {
9494
detection?: DetectorOptions;
9595
}
9696
}

0 commit comments

Comments
 (0)