|
21 | 21 |
|
22 | 22 | // eslint-disable-next-line no-control-regex
|
23 | 23 | const fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
|
24 |
| - const serializeCookie = (name, val, options) => { |
25 |
| - const opt = options || {}; |
26 |
| - opt.path = opt.path || '/'; |
| 24 | + const serializeCookie = function (name, val) { |
| 25 | + let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { |
| 26 | + path: '/' |
| 27 | + }; |
| 28 | + const opt = options; |
27 | 29 | const value = encodeURIComponent(val);
|
28 | 30 | let str = `${name}=${value}`;
|
29 | 31 | if (opt.maxAge > 0) {
|
|
179 | 181 | if (lookupLocalStorage && localStorageAvailable()) {
|
180 | 182 | return window.localStorage.getItem(lookupLocalStorage) || undefined; // Undefined ensures type consistency with the previous version of this function
|
181 | 183 | }
|
182 |
| - |
183 | 184 | return undefined;
|
184 | 185 | },
|
185 | 186 | // Deconstruct the options object and extract the lookupLocalStorage property
|
|
305 | 306 | }
|
306 | 307 | };
|
307 | 308 |
|
308 |
| - function getDefaults() { |
309 |
| - return { |
310 |
| - order: ['querystring', 'cookie', 'localStorage', 'sessionStorage', 'navigator', 'htmlTag'], |
311 |
| - lookupQuerystring: 'lng', |
312 |
| - lookupCookie: 'i18next', |
313 |
| - lookupLocalStorage: 'i18nextLng', |
314 |
| - lookupSessionStorage: 'i18nextLng', |
315 |
| - // cache user language |
316 |
| - caches: ['localStorage'], |
317 |
| - excludeCacheFor: ['cimode'], |
318 |
| - // cookieMinutes: 10, |
319 |
| - // cookieDomain: 'myDomain' |
| 309 | + let canCookies = false; |
| 310 | + try { |
| 311 | + // eslint-disable-next-line no-unused-expressions |
| 312 | + document.cookie; |
| 313 | + canCookies = true; |
| 314 | + // eslint-disable-next-line no-empty |
| 315 | + } catch (e) {} |
| 316 | + const order = ['querystring', 'cookie', 'localStorage', 'sessionStorage', 'navigator', 'htmlTag']; |
| 317 | + if (!canCookies) order.splice(1, 1); |
| 318 | + const getDefaults = () => ({ |
| 319 | + order, |
| 320 | + lookupQuerystring: 'lng', |
| 321 | + lookupCookie: 'i18next', |
| 322 | + lookupLocalStorage: 'i18nextLng', |
| 323 | + lookupSessionStorage: 'i18nextLng', |
| 324 | + // cache user language |
| 325 | + caches: ['localStorage'], |
| 326 | + excludeCacheFor: ['cimode'], |
| 327 | + // cookieMinutes: 10, |
| 328 | + // cookieDomain: 'myDomain' |
320 | 329 |
|
321 |
| - convertDetectedLanguage: l => l |
322 |
| - }; |
323 |
| - } |
| 330 | + convertDetectedLanguage: l => l |
| 331 | + }); |
324 | 332 | class Browser {
|
325 | 333 | constructor(services) {
|
326 | 334 | let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
327 | 335 | this.type = 'languageDetector';
|
328 | 336 | this.detectors = {};
|
329 | 337 | this.init(services, options);
|
330 | 338 | }
|
331 |
| - init(services) { |
| 339 | + init() { |
| 340 | + let services = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { |
| 341 | + languageUtils: {} |
| 342 | + }; |
332 | 343 | let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
333 | 344 | let i18nOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
334 |
| - this.services = services || { |
335 |
| - languageUtils: {} |
336 |
| - }; // this way the language detector can be used without i18next |
| 345 | + this.services = services; |
337 | 346 | this.options = defaults(options, this.options || {}, getDefaults());
|
338 | 347 | if (typeof this.options.convertDetectedLanguage === 'string' && this.options.convertDetectedLanguage.indexOf('15897') > -1) {
|
339 | 348 | this.options.convertDetectedLanguage = l => l.replace('-', '_');
|
|
355 | 364 | this.detectors[detector.name] = detector;
|
356 | 365 | return this;
|
357 | 366 | }
|
358 |
| - detect(detectionOrder) { |
359 |
| - if (!detectionOrder) detectionOrder = this.options.order; |
| 367 | + detect() { |
| 368 | + let detectionOrder = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.options.order; |
360 | 369 | let detected = [];
|
361 | 370 | detectionOrder.forEach(detectorName => {
|
362 | 371 | if (this.detectors[detectorName]) {
|
|
369 | 378 | if (this.services.languageUtils.getBestMatchFromCodes) return detected; // new i18next v19.5.0
|
370 | 379 | return detected.length > 0 ? detected[0] : null; // a little backward compatibility
|
371 | 380 | }
|
372 |
| - |
373 |
| - cacheUserLanguage(lng, caches) { |
374 |
| - if (!caches) caches = this.options.caches; |
| 381 | + cacheUserLanguage(lng) { |
| 382 | + let caches = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.options.caches; |
375 | 383 | if (!caches) return;
|
376 | 384 | if (this.options.excludeCacheFor && this.options.excludeCacheFor.indexOf(lng) > -1) return;
|
377 | 385 | caches.forEach(cacheName => {
|
|
0 commit comments