update to state of the art
This commit is contained in:
212
node_modules/i18next-browser-languagedetector/dist/esm/i18nextBrowserLanguageDetector.js
generated
vendored
212
node_modules/i18next-browser-languagedetector/dist/esm/i18nextBrowserLanguageDetector.js
generated
vendored
@@ -15,26 +15,90 @@ function defaults(obj) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-control-regex
|
||||
var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
|
||||
|
||||
var serializeCookie = function serializeCookie(name, val, options) {
|
||||
var opt = options || {};
|
||||
opt.path = opt.path || '/';
|
||||
var value = encodeURIComponent(val);
|
||||
var str = name + '=' + value;
|
||||
|
||||
if (opt.maxAge > 0) {
|
||||
var maxAge = opt.maxAge - 0;
|
||||
if (isNaN(maxAge)) throw new Error('maxAge should be a Number');
|
||||
str += '; Max-Age=' + Math.floor(maxAge);
|
||||
}
|
||||
|
||||
if (opt.domain) {
|
||||
if (!fieldContentRegExp.test(opt.domain)) {
|
||||
throw new TypeError('option domain is invalid');
|
||||
}
|
||||
|
||||
str += '; Domain=' + opt.domain;
|
||||
}
|
||||
|
||||
if (opt.path) {
|
||||
if (!fieldContentRegExp.test(opt.path)) {
|
||||
throw new TypeError('option path is invalid');
|
||||
}
|
||||
|
||||
str += '; Path=' + opt.path;
|
||||
}
|
||||
|
||||
if (opt.expires) {
|
||||
if (typeof opt.expires.toUTCString !== 'function') {
|
||||
throw new TypeError('option expires is invalid');
|
||||
}
|
||||
|
||||
str += '; Expires=' + opt.expires.toUTCString();
|
||||
}
|
||||
|
||||
if (opt.httpOnly) str += '; HttpOnly';
|
||||
if (opt.secure) str += '; Secure';
|
||||
|
||||
if (opt.sameSite) {
|
||||
var sameSite = typeof opt.sameSite === 'string' ? opt.sameSite.toLowerCase() : opt.sameSite;
|
||||
|
||||
switch (sameSite) {
|
||||
case true:
|
||||
str += '; SameSite=Strict';
|
||||
break;
|
||||
|
||||
case 'lax':
|
||||
str += '; SameSite=Lax';
|
||||
break;
|
||||
|
||||
case 'strict':
|
||||
str += '; SameSite=Strict';
|
||||
break;
|
||||
|
||||
case 'none':
|
||||
str += '; SameSite=None';
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new TypeError('option sameSite is invalid');
|
||||
}
|
||||
}
|
||||
|
||||
return str;
|
||||
};
|
||||
|
||||
var cookie = {
|
||||
create: function create(name, value, minutes, domain) {
|
||||
var cookieOptions = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {
|
||||
path: '/'
|
||||
path: '/',
|
||||
sameSite: 'strict'
|
||||
};
|
||||
var expires;
|
||||
|
||||
if (minutes) {
|
||||
var date = new Date();
|
||||
date.setTime(date.getTime() + minutes * 60 * 1000);
|
||||
expires = '; expires=' + date.toUTCString();
|
||||
} else expires = '';
|
||||
cookieOptions.expires = new Date();
|
||||
cookieOptions.expires.setTime(cookieOptions.expires.getTime() + minutes * 60 * 1000);
|
||||
}
|
||||
|
||||
domain = domain ? 'domain=' + domain + ';' : '';
|
||||
cookieOptions = Object.keys(cookieOptions).reduce(function (acc, key) {
|
||||
return acc + ';' + key.replace(/([A-Z])/g, function ($1) {
|
||||
return '-' + $1.toLowerCase();
|
||||
}) + '=' + cookieOptions[key];
|
||||
}, '');
|
||||
document.cookie = name + '=' + encodeURIComponent(value) + expires + ';' + domain + cookieOptions;
|
||||
if (domain) cookieOptions.domain = domain;
|
||||
document.cookie = serializeCookie(name, encodeURIComponent(value), cookieOptions);
|
||||
},
|
||||
read: function read(name) {
|
||||
var nameEQ = name + '=';
|
||||
@@ -101,23 +165,29 @@ var querystring = {
|
||||
}
|
||||
};
|
||||
|
||||
var hasLocalStorageSupport;
|
||||
var hasLocalStorageSupport = null;
|
||||
|
||||
try {
|
||||
hasLocalStorageSupport = window !== 'undefined' && window.localStorage !== null;
|
||||
var testKey = 'i18next.translate.boo';
|
||||
window.localStorage.setItem(testKey, 'foo');
|
||||
window.localStorage.removeItem(testKey);
|
||||
} catch (e) {
|
||||
hasLocalStorageSupport = false;
|
||||
}
|
||||
var localStorageAvailable = function localStorageAvailable() {
|
||||
if (hasLocalStorageSupport !== null) return hasLocalStorageSupport;
|
||||
|
||||
try {
|
||||
hasLocalStorageSupport = window !== 'undefined' && window.localStorage !== null;
|
||||
var testKey = 'i18next.translate.boo';
|
||||
window.localStorage.setItem(testKey, 'foo');
|
||||
window.localStorage.removeItem(testKey);
|
||||
} catch (e) {
|
||||
hasLocalStorageSupport = false;
|
||||
}
|
||||
|
||||
return hasLocalStorageSupport;
|
||||
};
|
||||
|
||||
var localStorage = {
|
||||
name: 'localStorage',
|
||||
lookup: function lookup(options) {
|
||||
var found;
|
||||
|
||||
if (options.lookupLocalStorage && hasLocalStorageSupport) {
|
||||
if (options.lookupLocalStorage && localStorageAvailable()) {
|
||||
var lng = window.localStorage.getItem(options.lookupLocalStorage);
|
||||
if (lng) found = lng;
|
||||
}
|
||||
@@ -125,38 +195,44 @@ var localStorage = {
|
||||
return found;
|
||||
},
|
||||
cacheUserLanguage: function cacheUserLanguage(lng, options) {
|
||||
if (options.lookupLocalStorage && hasLocalStorageSupport) {
|
||||
if (options.lookupLocalStorage && localStorageAvailable()) {
|
||||
window.localStorage.setItem(options.lookupLocalStorage, lng);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var hasSessionStorageSupport;
|
||||
var hasSessionStorageSupport = null;
|
||||
|
||||
try {
|
||||
hasSessionStorageSupport = window !== 'undefined' && window.sessionStorage !== null;
|
||||
var testKey$1 = 'i18next.translate.boo';
|
||||
window.sessionStorage.setItem(testKey$1, 'foo');
|
||||
window.sessionStorage.removeItem(testKey$1);
|
||||
} catch (e) {
|
||||
hasSessionStorageSupport = false;
|
||||
}
|
||||
var sessionStorageAvailable = function sessionStorageAvailable() {
|
||||
if (hasSessionStorageSupport !== null) return hasSessionStorageSupport;
|
||||
|
||||
try {
|
||||
hasSessionStorageSupport = window !== 'undefined' && window.sessionStorage !== null;
|
||||
var testKey = 'i18next.translate.boo';
|
||||
window.sessionStorage.setItem(testKey, 'foo');
|
||||
window.sessionStorage.removeItem(testKey);
|
||||
} catch (e) {
|
||||
hasSessionStorageSupport = false;
|
||||
}
|
||||
|
||||
return hasSessionStorageSupport;
|
||||
};
|
||||
|
||||
var sessionStorage = {
|
||||
name: 'sessionStorage',
|
||||
lookup: function lookup(options) {
|
||||
var found;
|
||||
|
||||
if (options.lookupsessionStorage && hasSessionStorageSupport) {
|
||||
var lng = window.sessionStorage.getItem(options.lookupsessionStorage);
|
||||
if (options.lookupSessionStorage && sessionStorageAvailable()) {
|
||||
var lng = window.sessionStorage.getItem(options.lookupSessionStorage);
|
||||
if (lng) found = lng;
|
||||
}
|
||||
|
||||
return found;
|
||||
},
|
||||
cacheUserLanguage: function cacheUserLanguage(lng, options) {
|
||||
if (options.lookupsessionStorage && hasSessionStorageSupport) {
|
||||
window.sessionStorage.setItem(options.lookupsessionStorage, lng);
|
||||
if (options.lookupSessionStorage && sessionStorageAvailable()) {
|
||||
window.sessionStorage.setItem(options.lookupSessionStorage, lng);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -253,13 +329,12 @@ function getDefaults() {
|
||||
lookupQuerystring: 'lng',
|
||||
lookupCookie: 'i18next',
|
||||
lookupLocalStorage: 'i18nextLng',
|
||||
lookupSessionStorage: 'i18nextLng',
|
||||
// cache user language
|
||||
caches: ['localStorage'],
|
||||
excludeCacheFor: ['cimode'],
|
||||
//cookieMinutes: 10,
|
||||
excludeCacheFor: ['cimode'] //cookieMinutes: 10,
|
||||
//cookieDomain: 'myDomain'
|
||||
checkWhitelist: true,
|
||||
checkForSimilarInWhitelist: false
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
@@ -282,9 +357,7 @@ function () {
|
||||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
||||
var i18nOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
||||
this.services = services;
|
||||
this.options = defaults(options, this.options || {}, getDefaults()); // if checking for similar, user needs to check whitelist
|
||||
|
||||
if (this.options.checkForSimilarInWhitelist) this.options.checkWhitelist = true; // backwards compatibility
|
||||
this.options = defaults(options, this.options || {}, getDefaults()); // backwards compatibility
|
||||
|
||||
if (this.options.lookupFromUrlIndex) this.options.lookupFromPathIndex = this.options.lookupFromUrlIndex;
|
||||
this.i18nOptions = i18nOptions;
|
||||
@@ -317,32 +390,9 @@ function () {
|
||||
if (lookup) detected = detected.concat(lookup);
|
||||
}
|
||||
});
|
||||
var found;
|
||||
detected.forEach(function (lng) {
|
||||
if (found) return;
|
||||
if (this.services.languageUtils.getBestMatchFromCodes) return detected; // new i18next v19.5.0
|
||||
|
||||
var cleanedLng = _this.services.languageUtils.formatLanguageCode(lng);
|
||||
|
||||
if (!_this.options.checkWhitelist || _this.services.languageUtils.isWhitelisted(cleanedLng)) found = cleanedLng;
|
||||
|
||||
if (!found && _this.options.checkForSimilarInWhitelist) {
|
||||
found = _this.getSimilarInWhitelist(cleanedLng);
|
||||
}
|
||||
});
|
||||
|
||||
if (!found) {
|
||||
var fallbacks = this.i18nOptions.fallbackLng;
|
||||
if (typeof fallbacks === 'string') fallbacks = [fallbacks];
|
||||
if (!fallbacks) fallbacks = [];
|
||||
|
||||
if (Object.prototype.toString.apply(fallbacks) === '[object Array]') {
|
||||
found = fallbacks[0];
|
||||
} else {
|
||||
found = fallbacks[0] || fallbacks["default"] && fallbacks["default"][0];
|
||||
}
|
||||
}
|
||||
|
||||
return found;
|
||||
return detected.length > 0 ? detected[0] : null; // a little backward compatibility
|
||||
}
|
||||
}, {
|
||||
key: "cacheUserLanguage",
|
||||
@@ -356,30 +406,6 @@ function () {
|
||||
if (_this2.detectors[cacheName]) _this2.detectors[cacheName].cacheUserLanguage(lng, _this2.options);
|
||||
});
|
||||
}
|
||||
}, {
|
||||
key: "getSimilarInWhitelist",
|
||||
value: function getSimilarInWhitelist(cleanedLng) {
|
||||
var _this3 = this;
|
||||
|
||||
if (!this.i18nOptions.whitelist) return;
|
||||
|
||||
if (cleanedLng.includes('-')) {
|
||||
// i.e. es-MX should check if es is in whitelist
|
||||
var prefix = cleanedLng.split('-')[0];
|
||||
var cleanedPrefix = this.services.languageUtils.formatLanguageCode(prefix);
|
||||
if (this.services.languageUtils.isWhitelisted(cleanedPrefix)) return cleanedPrefix; // if reached here, nothing found. continue to search for similar using only prefix
|
||||
|
||||
cleanedLng = cleanedPrefix;
|
||||
} // i.e. 'pt' should return 'pt-BR'. If multiple in whitelist with 'pt-', then use first one in whitelist
|
||||
|
||||
|
||||
var similar = this.i18nOptions.whitelist.find(function (whitelistLng) {
|
||||
var cleanedWhitelistLng = _this3.services.languageUtils.formatLanguageCode(whitelistLng);
|
||||
|
||||
if (cleanedWhitelistLng.startsWith(cleanedLng)) return cleanedWhitelistLng;
|
||||
});
|
||||
if (similar) return similar;
|
||||
}
|
||||
}]);
|
||||
|
||||
return Browser;
|
||||
|
Reference in New Issue
Block a user