i18next-browser-languagedetector
Advanced tools
Comparing version 7.2.1 to 8.0.0
@@ -0,1 +1,6 @@ | ||
### 8.0.0 | ||
- chore: set browsers target to defaults [286](https://github.com/i18next/i18next-browser-languageDetector/pull/286) | ||
- perf: use object deconstruction, optional chaining and hot path optimisations [287](https://github.com/i18next/i18next-browser-languageDetector/pull/287) | ||
### 7.2.1 | ||
@@ -2,0 +7,0 @@ |
'use strict'; | ||
var _classCallCheck = require('@babel/runtime/helpers/classCallCheck'); | ||
var _createClass = require('@babel/runtime/helpers/createClass'); | ||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
var _classCallCheck__default = /*#__PURE__*/_interopDefaultLegacy(_classCallCheck); | ||
var _createClass__default = /*#__PURE__*/_interopDefaultLegacy(_createClass); | ||
var arr = []; | ||
var each = arr.forEach; | ||
var slice = arr.slice; | ||
const { | ||
slice, | ||
forEach | ||
} = []; | ||
function defaults(obj) { | ||
each.call(slice.call(arguments, 1), function (source) { | ||
forEach.call(slice.call(arguments, 1), source => { | ||
if (source) { | ||
for (var prop in source) { | ||
for (const prop in source) { | ||
if (obj[prop] === undefined) obj[prop] = source[prop]; | ||
@@ -26,12 +19,12 @@ } | ||
// eslint-disable-next-line no-control-regex | ||
var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/; | ||
var serializeCookie = function serializeCookie(name, val, options) { | ||
var opt = options || {}; | ||
const fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/; | ||
const serializeCookie = (name, val, options) => { | ||
const opt = options || {}; | ||
opt.path = opt.path || '/'; | ||
var value = encodeURIComponent(val); | ||
var str = "".concat(name, "=").concat(value); | ||
const value = encodeURIComponent(val); | ||
let str = `${name}=${value}`; | ||
if (opt.maxAge > 0) { | ||
var maxAge = opt.maxAge - 0; | ||
const maxAge = opt.maxAge - 0; | ||
if (Number.isNaN(maxAge)) throw new Error('maxAge should be a Number'); | ||
str += "; Max-Age=".concat(Math.floor(maxAge)); | ||
str += `; Max-Age=${Math.floor(maxAge)}`; | ||
} | ||
@@ -42,3 +35,3 @@ if (opt.domain) { | ||
} | ||
str += "; Domain=".concat(opt.domain); | ||
str += `; Domain=${opt.domain}`; | ||
} | ||
@@ -49,3 +42,3 @@ if (opt.path) { | ||
} | ||
str += "; Path=".concat(opt.path); | ||
str += `; Path=${opt.path}`; | ||
} | ||
@@ -56,3 +49,3 @@ if (opt.expires) { | ||
} | ||
str += "; Expires=".concat(opt.expires.toUTCString()); | ||
str += `; Expires=${opt.expires.toUTCString()}`; | ||
} | ||
@@ -62,3 +55,3 @@ if (opt.httpOnly) str += '; HttpOnly'; | ||
if (opt.sameSite) { | ||
var sameSite = typeof opt.sameSite === 'string' ? opt.sameSite.toLowerCase() : opt.sameSite; | ||
const sameSite = typeof opt.sameSite === 'string' ? opt.sameSite.toLowerCase() : opt.sameSite; | ||
switch (sameSite) { | ||
@@ -83,5 +76,5 @@ case true: | ||
}; | ||
var cookie = { | ||
create: function create(name, value, minutes, domain) { | ||
var cookieOptions = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : { | ||
const cookie = { | ||
create(name, value, minutes, domain) { | ||
let cookieOptions = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : { | ||
path: '/', | ||
@@ -97,7 +90,7 @@ sameSite: 'strict' | ||
}, | ||
read: function read(name) { | ||
var nameEQ = "".concat(name, "="); | ||
var ca = document.cookie.split(';'); | ||
for (var i = 0; i < ca.length; i++) { | ||
var c = ca[i]; | ||
read(name) { | ||
const nameEQ = `${name}=`; | ||
const ca = document.cookie.split(';'); | ||
for (let i = 0; i < ca.length; i++) { | ||
let c = ca[i]; | ||
while (c.charAt(0) === ' ') c = c.substring(1, c.length); | ||
@@ -108,3 +101,3 @@ if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length); | ||
}, | ||
remove: function remove(name) { | ||
remove(name) { | ||
this.create(name, '', -1); | ||
@@ -115,13 +108,22 @@ } | ||
name: 'cookie', | ||
lookup: function lookup(options) { | ||
var found; | ||
if (options.lookupCookie && typeof document !== 'undefined') { | ||
var c = cookie.read(options.lookupCookie); | ||
if (c) found = c; | ||
// Deconstruct the options object and extract the lookupCookie property | ||
lookup(_ref) { | ||
let { | ||
lookupCookie | ||
} = _ref; | ||
if (lookupCookie && typeof document !== 'undefined') { | ||
return cookie.read(lookupCookie) || undefined; | ||
} | ||
return found; | ||
return undefined; | ||
}, | ||
cacheUserLanguage: function cacheUserLanguage(lng, options) { | ||
if (options.lookupCookie && typeof document !== 'undefined') { | ||
cookie.create(options.lookupCookie, lng, options.cookieMinutes, options.cookieDomain, options.cookieOptions); | ||
// Deconstruct the options object and extract the lookupCookie, cookieMinutes, cookieDomain, and cookieOptions properties | ||
cacheUserLanguage(lng, _ref2) { | ||
let { | ||
lookupCookie, | ||
cookieMinutes, | ||
cookieDomain, | ||
cookieOptions | ||
} = _ref2; | ||
if (lookupCookie && typeof document !== 'undefined') { | ||
cookie.create(lookupCookie, lng, cookieMinutes, cookieDomain, cookieOptions); | ||
} | ||
@@ -133,16 +135,22 @@ } | ||
name: 'querystring', | ||
lookup: function lookup(options) { | ||
var found; | ||
// Deconstruct the options object and extract the lookupQuerystring property | ||
lookup(_ref) { | ||
let { | ||
lookupQuerystring | ||
} = _ref; | ||
let found; | ||
if (typeof window !== 'undefined') { | ||
var search = window.location.search; | ||
if (!window.location.search && window.location.hash && window.location.hash.indexOf('?') > -1) { | ||
let { | ||
search | ||
} = window.location; | ||
if (!window.location.search && window.location.hash?.indexOf('?') > -1) { | ||
search = window.location.hash.substring(window.location.hash.indexOf('?')); | ||
} | ||
var query = search.substring(1); | ||
var params = query.split('&'); | ||
for (var i = 0; i < params.length; i++) { | ||
var pos = params[i].indexOf('='); | ||
const query = search.substring(1); | ||
const params = query.split('&'); | ||
for (let i = 0; i < params.length; i++) { | ||
const pos = params[i].indexOf('='); | ||
if (pos > 0) { | ||
var key = params[i].substring(0, pos); | ||
if (key === options.lookupQuerystring) { | ||
const key = params[i].substring(0, pos); | ||
if (key === lookupQuerystring) { | ||
found = params[i].substring(pos + 1); | ||
@@ -157,8 +165,8 @@ } | ||
var hasLocalStorageSupport = null; | ||
var localStorageAvailable = function localStorageAvailable() { | ||
let hasLocalStorageSupport = null; | ||
const localStorageAvailable = () => { | ||
if (hasLocalStorageSupport !== null) return hasLocalStorageSupport; | ||
try { | ||
hasLocalStorageSupport = window !== 'undefined' && window.localStorage !== null; | ||
var testKey = 'i18next.translate.boo'; | ||
const testKey = 'i18next.translate.boo'; | ||
window.localStorage.setItem(testKey, 'foo'); | ||
@@ -173,13 +181,20 @@ window.localStorage.removeItem(testKey); | ||
name: 'localStorage', | ||
lookup: function lookup(options) { | ||
var found; | ||
if (options.lookupLocalStorage && localStorageAvailable()) { | ||
var lng = window.localStorage.getItem(options.lookupLocalStorage); | ||
if (lng) found = lng; | ||
// Deconstruct the options object and extract the lookupLocalStorage property | ||
lookup(_ref) { | ||
let { | ||
lookupLocalStorage | ||
} = _ref; | ||
if (lookupLocalStorage && localStorageAvailable()) { | ||
return window.localStorage.getItem(lookupLocalStorage) || undefined; // Undefined ensures type consistency with the previous version of this function | ||
} | ||
return found; | ||
return undefined; | ||
}, | ||
cacheUserLanguage: function cacheUserLanguage(lng, options) { | ||
if (options.lookupLocalStorage && localStorageAvailable()) { | ||
window.localStorage.setItem(options.lookupLocalStorage, lng); | ||
// Deconstruct the options object and extract the lookupLocalStorage property | ||
cacheUserLanguage(lng, _ref2) { | ||
let { | ||
lookupLocalStorage | ||
} = _ref2; | ||
if (lookupLocalStorage && localStorageAvailable()) { | ||
window.localStorage.setItem(lookupLocalStorage, lng); | ||
} | ||
@@ -189,8 +204,8 @@ } | ||
var hasSessionStorageSupport = null; | ||
var sessionStorageAvailable = function sessionStorageAvailable() { | ||
let hasSessionStorageSupport = null; | ||
const sessionStorageAvailable = () => { | ||
if (hasSessionStorageSupport !== null) return hasSessionStorageSupport; | ||
try { | ||
hasSessionStorageSupport = window !== 'undefined' && window.sessionStorage !== null; | ||
var testKey = 'i18next.translate.boo'; | ||
const testKey = 'i18next.translate.boo'; | ||
window.sessionStorage.setItem(testKey, 'foo'); | ||
@@ -205,13 +220,17 @@ window.sessionStorage.removeItem(testKey); | ||
name: 'sessionStorage', | ||
lookup: function lookup(options) { | ||
var found; | ||
if (options.lookupSessionStorage && sessionStorageAvailable()) { | ||
var lng = window.sessionStorage.getItem(options.lookupSessionStorage); | ||
if (lng) found = lng; | ||
lookup(_ref) { | ||
let { | ||
lookupSessionStorage | ||
} = _ref; | ||
if (lookupSessionStorage && sessionStorageAvailable()) { | ||
return window.sessionStorage.getItem(lookupSessionStorage) || undefined; | ||
} | ||
return found; | ||
return undefined; | ||
}, | ||
cacheUserLanguage: function cacheUserLanguage(lng, options) { | ||
if (options.lookupSessionStorage && sessionStorageAvailable()) { | ||
window.sessionStorage.setItem(options.lookupSessionStorage, lng); | ||
cacheUserLanguage(lng, _ref2) { | ||
let { | ||
lookupSessionStorage | ||
} = _ref2; | ||
if (lookupSessionStorage && sessionStorageAvailable()) { | ||
window.sessionStorage.setItem(lookupSessionStorage, lng); | ||
} | ||
@@ -223,16 +242,21 @@ } | ||
name: 'navigator', | ||
lookup: function lookup(options) { | ||
var found = []; | ||
lookup(options) { | ||
const found = []; | ||
if (typeof navigator !== 'undefined') { | ||
if (navigator.languages) { | ||
const { | ||
languages, | ||
userLanguage, | ||
language | ||
} = navigator; | ||
if (languages) { | ||
// chrome only; not an array, so can't use .push.apply instead of iterating | ||
for (var i = 0; i < navigator.languages.length; i++) { | ||
found.push(navigator.languages[i]); | ||
for (let i = 0; i < languages.length; i++) { | ||
found.push(languages[i]); | ||
} | ||
} | ||
if (navigator.userLanguage) { | ||
found.push(navigator.userLanguage); | ||
if (userLanguage) { | ||
found.push(userLanguage); | ||
} | ||
if (navigator.language) { | ||
found.push(navigator.language); | ||
if (language) { | ||
found.push(language); | ||
} | ||
@@ -246,7 +270,11 @@ } | ||
name: 'htmlTag', | ||
lookup: function lookup(options) { | ||
var found; | ||
var htmlTag = options.htmlTag || (typeof document !== 'undefined' ? document.documentElement : null); | ||
if (htmlTag && typeof htmlTag.getAttribute === 'function') { | ||
found = htmlTag.getAttribute('lang'); | ||
// Deconstruct the options object and extract the htmlTag property | ||
lookup(_ref) { | ||
let { | ||
htmlTag | ||
} = _ref; | ||
let found; | ||
const internalHtmlTag = htmlTag || (typeof document !== 'undefined' ? document.documentElement : null); | ||
if (internalHtmlTag && typeof internalHtmlTag.getAttribute === 'function') { | ||
found = internalHtmlTag.getAttribute('lang'); | ||
} | ||
@@ -259,18 +287,12 @@ return found; | ||
name: 'path', | ||
lookup: function lookup(options) { | ||
var found; | ||
if (typeof window !== 'undefined') { | ||
var language = window.location.pathname.match(/\/([a-zA-Z-]*)/g); | ||
if (language instanceof Array) { | ||
if (typeof options.lookupFromPathIndex === 'number') { | ||
if (typeof language[options.lookupFromPathIndex] !== 'string') { | ||
return undefined; | ||
} | ||
found = language[options.lookupFromPathIndex].replace('/', ''); | ||
} else { | ||
found = language[0].replace('/', ''); | ||
} | ||
} | ||
} | ||
return found; | ||
// Deconstruct the options object and extract the lookupFromPathIndex property | ||
lookup(_ref) { | ||
let { | ||
lookupFromPathIndex | ||
} = _ref; | ||
if (typeof window === 'undefined') return undefined; | ||
const language = window.location.pathname.match(/\/([a-zA-Z-]*)/g); | ||
if (!Array.isArray(language)) return undefined; | ||
const index = typeof lookupFromPathIndex === 'number' ? lookupFromPathIndex : 0; | ||
return language[index]?.replace('/', ''); | ||
} | ||
@@ -281,9 +303,12 @@ }; | ||
name: 'subdomain', | ||
lookup: function lookup(options) { | ||
lookup(_ref) { | ||
let { | ||
lookupFromSubdomainIndex | ||
} = _ref; | ||
// If given get the subdomain index else 1 | ||
var lookupFromSubdomainIndex = typeof options.lookupFromSubdomainIndex === 'number' ? options.lookupFromSubdomainIndex + 1 : 1; | ||
const internalLookupFromSubdomainIndex = typeof lookupFromSubdomainIndex === 'number' ? lookupFromSubdomainIndex + 1 : 1; | ||
// get all matches if window.location. is existing | ||
// first item of match is the match itself and the second is the first group macht which sould be the first subdomain match | ||
// first item of match is the match itself and the second is the first group match which should be the first subdomain match | ||
// is the hostname no public domain get the or option of localhost | ||
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); | ||
const language = typeof window !== 'undefined' && window.location?.hostname?.match(/^(\w{2,5})\.(([a-z0-9-]{1,63}\.[a-z]{2,6})|localhost)/i); | ||
@@ -293,3 +318,3 @@ // if there is no match (null) return undefined | ||
// return the given group match | ||
return language[lookupFromSubdomainIndex]; | ||
return language[internalLookupFromSubdomainIndex]; | ||
} | ||
@@ -311,11 +336,8 @@ }; | ||
convertDetectedLanguage: function convertDetectedLanguage(l) { | ||
return l; | ||
} | ||
convertDetectedLanguage: l => l | ||
}; | ||
} | ||
var Browser = /*#__PURE__*/function () { | ||
function Browser(services) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
_classCallCheck__default["default"](this, Browser); | ||
class Browser { | ||
constructor(services) { | ||
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
this.type = 'languageDetector'; | ||
@@ -325,70 +347,55 @@ this.detectors = {}; | ||
} | ||
_createClass__default["default"](Browser, [{ | ||
key: "init", | ||
value: function init(services) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var i18nOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
this.services = services || { | ||
languageUtils: {} | ||
}; // this way the language detector can be used without i18next | ||
this.options = defaults(options, this.options || {}, getDefaults()); | ||
if (typeof this.options.convertDetectedLanguage === 'string' && this.options.convertDetectedLanguage.indexOf('15897') > -1) { | ||
this.options.convertDetectedLanguage = function (l) { | ||
return l.replace('-', '_'); | ||
}; | ||
init(services) { | ||
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
let i18nOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
this.services = services || { | ||
languageUtils: {} | ||
}; // this way the language detector can be used without i18next | ||
this.options = defaults(options, this.options || {}, getDefaults()); | ||
if (typeof this.options.convertDetectedLanguage === 'string' && this.options.convertDetectedLanguage.indexOf('15897') > -1) { | ||
this.options.convertDetectedLanguage = l => l.replace('-', '_'); | ||
} | ||
// backwards compatibility | ||
if (this.options.lookupFromUrlIndex) this.options.lookupFromPathIndex = this.options.lookupFromUrlIndex; | ||
this.i18nOptions = i18nOptions; | ||
this.addDetector(cookie$1); | ||
this.addDetector(querystring); | ||
this.addDetector(localStorage); | ||
this.addDetector(sessionStorage); | ||
this.addDetector(navigator$1); | ||
this.addDetector(htmlTag); | ||
this.addDetector(path); | ||
this.addDetector(subdomain); | ||
} | ||
addDetector(detector) { | ||
this.detectors[detector.name] = detector; | ||
return this; | ||
} | ||
detect(detectionOrder) { | ||
if (!detectionOrder) detectionOrder = this.options.order; | ||
let detected = []; | ||
detectionOrder.forEach(detectorName => { | ||
if (this.detectors[detectorName]) { | ||
let lookup = this.detectors[detectorName].lookup(this.options); | ||
if (lookup && typeof lookup === 'string') lookup = [lookup]; | ||
if (lookup) detected = detected.concat(lookup); | ||
} | ||
}); | ||
detected = detected.map(d => this.options.convertDetectedLanguage(d)); | ||
if (this.services.languageUtils.getBestMatchFromCodes) return detected; // new i18next v19.5.0 | ||
return detected.length > 0 ? detected[0] : null; // a little backward compatibility | ||
} | ||
// backwards compatibility | ||
if (this.options.lookupFromUrlIndex) this.options.lookupFromPathIndex = this.options.lookupFromUrlIndex; | ||
this.i18nOptions = i18nOptions; | ||
this.addDetector(cookie$1); | ||
this.addDetector(querystring); | ||
this.addDetector(localStorage); | ||
this.addDetector(sessionStorage); | ||
this.addDetector(navigator$1); | ||
this.addDetector(htmlTag); | ||
this.addDetector(path); | ||
this.addDetector(subdomain); | ||
} | ||
}, { | ||
key: "addDetector", | ||
value: function addDetector(detector) { | ||
this.detectors[detector.name] = detector; | ||
return this; | ||
} | ||
}, { | ||
key: "detect", | ||
value: function detect(detectionOrder) { | ||
var _this = this; | ||
if (!detectionOrder) detectionOrder = this.options.order; | ||
var detected = []; | ||
detectionOrder.forEach(function (detectorName) { | ||
if (_this.detectors[detectorName]) { | ||
var lookup = _this.detectors[detectorName].lookup(_this.options); | ||
if (lookup && typeof lookup === 'string') lookup = [lookup]; | ||
if (lookup) detected = detected.concat(lookup); | ||
} | ||
}); | ||
detected = detected.map(function (d) { | ||
return _this.options.convertDetectedLanguage(d); | ||
}); | ||
if (this.services.languageUtils.getBestMatchFromCodes) return detected; // new i18next v19.5.0 | ||
return detected.length > 0 ? detected[0] : null; // a little backward compatibility | ||
} | ||
}, { | ||
key: "cacheUserLanguage", | ||
value: function cacheUserLanguage(lng, caches) { | ||
var _this2 = this; | ||
if (!caches) caches = this.options.caches; | ||
if (!caches) return; | ||
if (this.options.excludeCacheFor && this.options.excludeCacheFor.indexOf(lng) > -1) return; | ||
caches.forEach(function (cacheName) { | ||
if (_this2.detectors[cacheName]) _this2.detectors[cacheName].cacheUserLanguage(lng, _this2.options); | ||
}); | ||
} | ||
}]); | ||
return Browser; | ||
}(); | ||
cacheUserLanguage(lng, caches) { | ||
if (!caches) caches = this.options.caches; | ||
if (!caches) return; | ||
if (this.options.excludeCacheFor && this.options.excludeCacheFor.indexOf(lng) > -1) return; | ||
caches.forEach(cacheName => { | ||
if (this.detectors[cacheName]) this.detectors[cacheName].cacheUserLanguage(lng, this.options); | ||
}); | ||
} | ||
} | ||
Browser.type = 'languageDetector'; | ||
module.exports = Browser; |
@@ -1,11 +0,9 @@ | ||
import _classCallCheck from '@babel/runtime/helpers/esm/classCallCheck'; | ||
import _createClass from '@babel/runtime/helpers/esm/createClass'; | ||
var arr = []; | ||
var each = arr.forEach; | ||
var slice = arr.slice; | ||
const { | ||
slice, | ||
forEach | ||
} = []; | ||
function defaults(obj) { | ||
each.call(slice.call(arguments, 1), function (source) { | ||
forEach.call(slice.call(arguments, 1), source => { | ||
if (source) { | ||
for (var prop in source) { | ||
for (const prop in source) { | ||
if (obj[prop] === undefined) obj[prop] = source[prop]; | ||
@@ -19,12 +17,12 @@ } | ||
// eslint-disable-next-line no-control-regex | ||
var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/; | ||
var serializeCookie = function serializeCookie(name, val, options) { | ||
var opt = options || {}; | ||
const fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/; | ||
const serializeCookie = (name, val, options) => { | ||
const opt = options || {}; | ||
opt.path = opt.path || '/'; | ||
var value = encodeURIComponent(val); | ||
var str = "".concat(name, "=").concat(value); | ||
const value = encodeURIComponent(val); | ||
let str = `${name}=${value}`; | ||
if (opt.maxAge > 0) { | ||
var maxAge = opt.maxAge - 0; | ||
const maxAge = opt.maxAge - 0; | ||
if (Number.isNaN(maxAge)) throw new Error('maxAge should be a Number'); | ||
str += "; Max-Age=".concat(Math.floor(maxAge)); | ||
str += `; Max-Age=${Math.floor(maxAge)}`; | ||
} | ||
@@ -35,3 +33,3 @@ if (opt.domain) { | ||
} | ||
str += "; Domain=".concat(opt.domain); | ||
str += `; Domain=${opt.domain}`; | ||
} | ||
@@ -42,3 +40,3 @@ if (opt.path) { | ||
} | ||
str += "; Path=".concat(opt.path); | ||
str += `; Path=${opt.path}`; | ||
} | ||
@@ -49,3 +47,3 @@ if (opt.expires) { | ||
} | ||
str += "; Expires=".concat(opt.expires.toUTCString()); | ||
str += `; Expires=${opt.expires.toUTCString()}`; | ||
} | ||
@@ -55,3 +53,3 @@ if (opt.httpOnly) str += '; HttpOnly'; | ||
if (opt.sameSite) { | ||
var sameSite = typeof opt.sameSite === 'string' ? opt.sameSite.toLowerCase() : opt.sameSite; | ||
const sameSite = typeof opt.sameSite === 'string' ? opt.sameSite.toLowerCase() : opt.sameSite; | ||
switch (sameSite) { | ||
@@ -76,5 +74,5 @@ case true: | ||
}; | ||
var cookie = { | ||
create: function create(name, value, minutes, domain) { | ||
var cookieOptions = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : { | ||
const cookie = { | ||
create(name, value, minutes, domain) { | ||
let cookieOptions = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : { | ||
path: '/', | ||
@@ -90,7 +88,7 @@ sameSite: 'strict' | ||
}, | ||
read: function read(name) { | ||
var nameEQ = "".concat(name, "="); | ||
var ca = document.cookie.split(';'); | ||
for (var i = 0; i < ca.length; i++) { | ||
var c = ca[i]; | ||
read(name) { | ||
const nameEQ = `${name}=`; | ||
const ca = document.cookie.split(';'); | ||
for (let i = 0; i < ca.length; i++) { | ||
let c = ca[i]; | ||
while (c.charAt(0) === ' ') c = c.substring(1, c.length); | ||
@@ -101,3 +99,3 @@ if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length); | ||
}, | ||
remove: function remove(name) { | ||
remove(name) { | ||
this.create(name, '', -1); | ||
@@ -108,13 +106,22 @@ } | ||
name: 'cookie', | ||
lookup: function lookup(options) { | ||
var found; | ||
if (options.lookupCookie && typeof document !== 'undefined') { | ||
var c = cookie.read(options.lookupCookie); | ||
if (c) found = c; | ||
// Deconstruct the options object and extract the lookupCookie property | ||
lookup(_ref) { | ||
let { | ||
lookupCookie | ||
} = _ref; | ||
if (lookupCookie && typeof document !== 'undefined') { | ||
return cookie.read(lookupCookie) || undefined; | ||
} | ||
return found; | ||
return undefined; | ||
}, | ||
cacheUserLanguage: function cacheUserLanguage(lng, options) { | ||
if (options.lookupCookie && typeof document !== 'undefined') { | ||
cookie.create(options.lookupCookie, lng, options.cookieMinutes, options.cookieDomain, options.cookieOptions); | ||
// Deconstruct the options object and extract the lookupCookie, cookieMinutes, cookieDomain, and cookieOptions properties | ||
cacheUserLanguage(lng, _ref2) { | ||
let { | ||
lookupCookie, | ||
cookieMinutes, | ||
cookieDomain, | ||
cookieOptions | ||
} = _ref2; | ||
if (lookupCookie && typeof document !== 'undefined') { | ||
cookie.create(lookupCookie, lng, cookieMinutes, cookieDomain, cookieOptions); | ||
} | ||
@@ -126,16 +133,22 @@ } | ||
name: 'querystring', | ||
lookup: function lookup(options) { | ||
var found; | ||
// Deconstruct the options object and extract the lookupQuerystring property | ||
lookup(_ref) { | ||
let { | ||
lookupQuerystring | ||
} = _ref; | ||
let found; | ||
if (typeof window !== 'undefined') { | ||
var search = window.location.search; | ||
if (!window.location.search && window.location.hash && window.location.hash.indexOf('?') > -1) { | ||
let { | ||
search | ||
} = window.location; | ||
if (!window.location.search && window.location.hash?.indexOf('?') > -1) { | ||
search = window.location.hash.substring(window.location.hash.indexOf('?')); | ||
} | ||
var query = search.substring(1); | ||
var params = query.split('&'); | ||
for (var i = 0; i < params.length; i++) { | ||
var pos = params[i].indexOf('='); | ||
const query = search.substring(1); | ||
const params = query.split('&'); | ||
for (let i = 0; i < params.length; i++) { | ||
const pos = params[i].indexOf('='); | ||
if (pos > 0) { | ||
var key = params[i].substring(0, pos); | ||
if (key === options.lookupQuerystring) { | ||
const key = params[i].substring(0, pos); | ||
if (key === lookupQuerystring) { | ||
found = params[i].substring(pos + 1); | ||
@@ -150,8 +163,8 @@ } | ||
var hasLocalStorageSupport = null; | ||
var localStorageAvailable = function localStorageAvailable() { | ||
let hasLocalStorageSupport = null; | ||
const localStorageAvailable = () => { | ||
if (hasLocalStorageSupport !== null) return hasLocalStorageSupport; | ||
try { | ||
hasLocalStorageSupport = window !== 'undefined' && window.localStorage !== null; | ||
var testKey = 'i18next.translate.boo'; | ||
const testKey = 'i18next.translate.boo'; | ||
window.localStorage.setItem(testKey, 'foo'); | ||
@@ -166,13 +179,20 @@ window.localStorage.removeItem(testKey); | ||
name: 'localStorage', | ||
lookup: function lookup(options) { | ||
var found; | ||
if (options.lookupLocalStorage && localStorageAvailable()) { | ||
var lng = window.localStorage.getItem(options.lookupLocalStorage); | ||
if (lng) found = lng; | ||
// Deconstruct the options object and extract the lookupLocalStorage property | ||
lookup(_ref) { | ||
let { | ||
lookupLocalStorage | ||
} = _ref; | ||
if (lookupLocalStorage && localStorageAvailable()) { | ||
return window.localStorage.getItem(lookupLocalStorage) || undefined; // Undefined ensures type consistency with the previous version of this function | ||
} | ||
return found; | ||
return undefined; | ||
}, | ||
cacheUserLanguage: function cacheUserLanguage(lng, options) { | ||
if (options.lookupLocalStorage && localStorageAvailable()) { | ||
window.localStorage.setItem(options.lookupLocalStorage, lng); | ||
// Deconstruct the options object and extract the lookupLocalStorage property | ||
cacheUserLanguage(lng, _ref2) { | ||
let { | ||
lookupLocalStorage | ||
} = _ref2; | ||
if (lookupLocalStorage && localStorageAvailable()) { | ||
window.localStorage.setItem(lookupLocalStorage, lng); | ||
} | ||
@@ -182,8 +202,8 @@ } | ||
var hasSessionStorageSupport = null; | ||
var sessionStorageAvailable = function sessionStorageAvailable() { | ||
let hasSessionStorageSupport = null; | ||
const sessionStorageAvailable = () => { | ||
if (hasSessionStorageSupport !== null) return hasSessionStorageSupport; | ||
try { | ||
hasSessionStorageSupport = window !== 'undefined' && window.sessionStorage !== null; | ||
var testKey = 'i18next.translate.boo'; | ||
const testKey = 'i18next.translate.boo'; | ||
window.sessionStorage.setItem(testKey, 'foo'); | ||
@@ -198,13 +218,17 @@ window.sessionStorage.removeItem(testKey); | ||
name: 'sessionStorage', | ||
lookup: function lookup(options) { | ||
var found; | ||
if (options.lookupSessionStorage && sessionStorageAvailable()) { | ||
var lng = window.sessionStorage.getItem(options.lookupSessionStorage); | ||
if (lng) found = lng; | ||
lookup(_ref) { | ||
let { | ||
lookupSessionStorage | ||
} = _ref; | ||
if (lookupSessionStorage && sessionStorageAvailable()) { | ||
return window.sessionStorage.getItem(lookupSessionStorage) || undefined; | ||
} | ||
return found; | ||
return undefined; | ||
}, | ||
cacheUserLanguage: function cacheUserLanguage(lng, options) { | ||
if (options.lookupSessionStorage && sessionStorageAvailable()) { | ||
window.sessionStorage.setItem(options.lookupSessionStorage, lng); | ||
cacheUserLanguage(lng, _ref2) { | ||
let { | ||
lookupSessionStorage | ||
} = _ref2; | ||
if (lookupSessionStorage && sessionStorageAvailable()) { | ||
window.sessionStorage.setItem(lookupSessionStorage, lng); | ||
} | ||
@@ -216,16 +240,21 @@ } | ||
name: 'navigator', | ||
lookup: function lookup(options) { | ||
var found = []; | ||
lookup(options) { | ||
const found = []; | ||
if (typeof navigator !== 'undefined') { | ||
if (navigator.languages) { | ||
const { | ||
languages, | ||
userLanguage, | ||
language | ||
} = navigator; | ||
if (languages) { | ||
// chrome only; not an array, so can't use .push.apply instead of iterating | ||
for (var i = 0; i < navigator.languages.length; i++) { | ||
found.push(navigator.languages[i]); | ||
for (let i = 0; i < languages.length; i++) { | ||
found.push(languages[i]); | ||
} | ||
} | ||
if (navigator.userLanguage) { | ||
found.push(navigator.userLanguage); | ||
if (userLanguage) { | ||
found.push(userLanguage); | ||
} | ||
if (navigator.language) { | ||
found.push(navigator.language); | ||
if (language) { | ||
found.push(language); | ||
} | ||
@@ -239,7 +268,11 @@ } | ||
name: 'htmlTag', | ||
lookup: function lookup(options) { | ||
var found; | ||
var htmlTag = options.htmlTag || (typeof document !== 'undefined' ? document.documentElement : null); | ||
if (htmlTag && typeof htmlTag.getAttribute === 'function') { | ||
found = htmlTag.getAttribute('lang'); | ||
// Deconstruct the options object and extract the htmlTag property | ||
lookup(_ref) { | ||
let { | ||
htmlTag | ||
} = _ref; | ||
let found; | ||
const internalHtmlTag = htmlTag || (typeof document !== 'undefined' ? document.documentElement : null); | ||
if (internalHtmlTag && typeof internalHtmlTag.getAttribute === 'function') { | ||
found = internalHtmlTag.getAttribute('lang'); | ||
} | ||
@@ -252,18 +285,12 @@ return found; | ||
name: 'path', | ||
lookup: function lookup(options) { | ||
var found; | ||
if (typeof window !== 'undefined') { | ||
var language = window.location.pathname.match(/\/([a-zA-Z-]*)/g); | ||
if (language instanceof Array) { | ||
if (typeof options.lookupFromPathIndex === 'number') { | ||
if (typeof language[options.lookupFromPathIndex] !== 'string') { | ||
return undefined; | ||
} | ||
found = language[options.lookupFromPathIndex].replace('/', ''); | ||
} else { | ||
found = language[0].replace('/', ''); | ||
} | ||
} | ||
} | ||
return found; | ||
// Deconstruct the options object and extract the lookupFromPathIndex property | ||
lookup(_ref) { | ||
let { | ||
lookupFromPathIndex | ||
} = _ref; | ||
if (typeof window === 'undefined') return undefined; | ||
const language = window.location.pathname.match(/\/([a-zA-Z-]*)/g); | ||
if (!Array.isArray(language)) return undefined; | ||
const index = typeof lookupFromPathIndex === 'number' ? lookupFromPathIndex : 0; | ||
return language[index]?.replace('/', ''); | ||
} | ||
@@ -274,9 +301,12 @@ }; | ||
name: 'subdomain', | ||
lookup: function lookup(options) { | ||
lookup(_ref) { | ||
let { | ||
lookupFromSubdomainIndex | ||
} = _ref; | ||
// If given get the subdomain index else 1 | ||
var lookupFromSubdomainIndex = typeof options.lookupFromSubdomainIndex === 'number' ? options.lookupFromSubdomainIndex + 1 : 1; | ||
const internalLookupFromSubdomainIndex = typeof lookupFromSubdomainIndex === 'number' ? lookupFromSubdomainIndex + 1 : 1; | ||
// get all matches if window.location. is existing | ||
// first item of match is the match itself and the second is the first group macht which sould be the first subdomain match | ||
// first item of match is the match itself and the second is the first group match which should be the first subdomain match | ||
// is the hostname no public domain get the or option of localhost | ||
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); | ||
const language = typeof window !== 'undefined' && window.location?.hostname?.match(/^(\w{2,5})\.(([a-z0-9-]{1,63}\.[a-z]{2,6})|localhost)/i); | ||
@@ -286,3 +316,3 @@ // if there is no match (null) return undefined | ||
// return the given group match | ||
return language[lookupFromSubdomainIndex]; | ||
return language[internalLookupFromSubdomainIndex]; | ||
} | ||
@@ -304,11 +334,8 @@ }; | ||
convertDetectedLanguage: function convertDetectedLanguage(l) { | ||
return l; | ||
} | ||
convertDetectedLanguage: l => l | ||
}; | ||
} | ||
var Browser = /*#__PURE__*/function () { | ||
function Browser(services) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
_classCallCheck(this, Browser); | ||
class Browser { | ||
constructor(services) { | ||
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
this.type = 'languageDetector'; | ||
@@ -318,70 +345,55 @@ this.detectors = {}; | ||
} | ||
_createClass(Browser, [{ | ||
key: "init", | ||
value: function init(services) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var i18nOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
this.services = services || { | ||
languageUtils: {} | ||
}; // this way the language detector can be used without i18next | ||
this.options = defaults(options, this.options || {}, getDefaults()); | ||
if (typeof this.options.convertDetectedLanguage === 'string' && this.options.convertDetectedLanguage.indexOf('15897') > -1) { | ||
this.options.convertDetectedLanguage = function (l) { | ||
return l.replace('-', '_'); | ||
}; | ||
init(services) { | ||
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
let i18nOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
this.services = services || { | ||
languageUtils: {} | ||
}; // this way the language detector can be used without i18next | ||
this.options = defaults(options, this.options || {}, getDefaults()); | ||
if (typeof this.options.convertDetectedLanguage === 'string' && this.options.convertDetectedLanguage.indexOf('15897') > -1) { | ||
this.options.convertDetectedLanguage = l => l.replace('-', '_'); | ||
} | ||
// backwards compatibility | ||
if (this.options.lookupFromUrlIndex) this.options.lookupFromPathIndex = this.options.lookupFromUrlIndex; | ||
this.i18nOptions = i18nOptions; | ||
this.addDetector(cookie$1); | ||
this.addDetector(querystring); | ||
this.addDetector(localStorage); | ||
this.addDetector(sessionStorage); | ||
this.addDetector(navigator$1); | ||
this.addDetector(htmlTag); | ||
this.addDetector(path); | ||
this.addDetector(subdomain); | ||
} | ||
addDetector(detector) { | ||
this.detectors[detector.name] = detector; | ||
return this; | ||
} | ||
detect(detectionOrder) { | ||
if (!detectionOrder) detectionOrder = this.options.order; | ||
let detected = []; | ||
detectionOrder.forEach(detectorName => { | ||
if (this.detectors[detectorName]) { | ||
let lookup = this.detectors[detectorName].lookup(this.options); | ||
if (lookup && typeof lookup === 'string') lookup = [lookup]; | ||
if (lookup) detected = detected.concat(lookup); | ||
} | ||
}); | ||
detected = detected.map(d => this.options.convertDetectedLanguage(d)); | ||
if (this.services.languageUtils.getBestMatchFromCodes) return detected; // new i18next v19.5.0 | ||
return detected.length > 0 ? detected[0] : null; // a little backward compatibility | ||
} | ||
// backwards compatibility | ||
if (this.options.lookupFromUrlIndex) this.options.lookupFromPathIndex = this.options.lookupFromUrlIndex; | ||
this.i18nOptions = i18nOptions; | ||
this.addDetector(cookie$1); | ||
this.addDetector(querystring); | ||
this.addDetector(localStorage); | ||
this.addDetector(sessionStorage); | ||
this.addDetector(navigator$1); | ||
this.addDetector(htmlTag); | ||
this.addDetector(path); | ||
this.addDetector(subdomain); | ||
} | ||
}, { | ||
key: "addDetector", | ||
value: function addDetector(detector) { | ||
this.detectors[detector.name] = detector; | ||
return this; | ||
} | ||
}, { | ||
key: "detect", | ||
value: function detect(detectionOrder) { | ||
var _this = this; | ||
if (!detectionOrder) detectionOrder = this.options.order; | ||
var detected = []; | ||
detectionOrder.forEach(function (detectorName) { | ||
if (_this.detectors[detectorName]) { | ||
var lookup = _this.detectors[detectorName].lookup(_this.options); | ||
if (lookup && typeof lookup === 'string') lookup = [lookup]; | ||
if (lookup) detected = detected.concat(lookup); | ||
} | ||
}); | ||
detected = detected.map(function (d) { | ||
return _this.options.convertDetectedLanguage(d); | ||
}); | ||
if (this.services.languageUtils.getBestMatchFromCodes) return detected; // new i18next v19.5.0 | ||
return detected.length > 0 ? detected[0] : null; // a little backward compatibility | ||
} | ||
}, { | ||
key: "cacheUserLanguage", | ||
value: function cacheUserLanguage(lng, caches) { | ||
var _this2 = this; | ||
if (!caches) caches = this.options.caches; | ||
if (!caches) return; | ||
if (this.options.excludeCacheFor && this.options.excludeCacheFor.indexOf(lng) > -1) return; | ||
caches.forEach(function (cacheName) { | ||
if (_this2.detectors[cacheName]) _this2.detectors[cacheName].cacheUserLanguage(lng, _this2.options); | ||
}); | ||
} | ||
}]); | ||
return Browser; | ||
}(); | ||
cacheUserLanguage(lng, caches) { | ||
if (!caches) caches = this.options.caches; | ||
if (!caches) return; | ||
if (this.options.excludeCacheFor && this.options.excludeCacheFor.indexOf(lng) > -1) return; | ||
caches.forEach(cacheName => { | ||
if (this.detectors[cacheName]) this.detectors[cacheName].cacheUserLanguage(lng, this.options); | ||
}); | ||
} | ||
} | ||
Browser.type = 'languageDetector'; | ||
export { Browser as default }; |
@@ -1,1 +0,1 @@ | ||
{"type":"module","version":"7.2.1"} | ||
{"type":"module","version":"8.0.0"} |
@@ -7,59 +7,10 @@ (function (global, factory) { | ||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
} | ||
function _typeof(o) { | ||
"@babel/helpers - typeof"; | ||
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { | ||
return typeof o; | ||
} : function (o) { | ||
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; | ||
}, _typeof(o); | ||
} | ||
function _toPrimitive(input, hint) { | ||
if (_typeof(input) !== "object" || input === null) return input; | ||
var prim = input[Symbol.toPrimitive]; | ||
if (prim !== undefined) { | ||
var res = prim.call(input, hint || "default"); | ||
if (_typeof(res) !== "object") return res; | ||
throw new TypeError("@@toPrimitive must return a primitive value."); | ||
} | ||
return (hint === "string" ? String : Number)(input); | ||
} | ||
function _toPropertyKey(arg) { | ||
var key = _toPrimitive(arg, "string"); | ||
return _typeof(key) === "symbol" ? key : String(key); | ||
} | ||
function _defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); | ||
} | ||
} | ||
function _createClass(Constructor, protoProps, staticProps) { | ||
if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) _defineProperties(Constructor, staticProps); | ||
Object.defineProperty(Constructor, "prototype", { | ||
writable: false | ||
}); | ||
return Constructor; | ||
} | ||
var arr = []; | ||
var each = arr.forEach; | ||
var slice = arr.slice; | ||
const { | ||
slice, | ||
forEach | ||
} = []; | ||
function defaults(obj) { | ||
each.call(slice.call(arguments, 1), function (source) { | ||
forEach.call(slice.call(arguments, 1), source => { | ||
if (source) { | ||
for (var prop in source) { | ||
for (const prop in source) { | ||
if (obj[prop] === undefined) obj[prop] = source[prop]; | ||
@@ -73,12 +24,12 @@ } | ||
// eslint-disable-next-line no-control-regex | ||
var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/; | ||
var serializeCookie = function serializeCookie(name, val, options) { | ||
var opt = options || {}; | ||
const fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/; | ||
const serializeCookie = (name, val, options) => { | ||
const opt = options || {}; | ||
opt.path = opt.path || '/'; | ||
var value = encodeURIComponent(val); | ||
var str = "".concat(name, "=").concat(value); | ||
const value = encodeURIComponent(val); | ||
let str = `${name}=${value}`; | ||
if (opt.maxAge > 0) { | ||
var maxAge = opt.maxAge - 0; | ||
const maxAge = opt.maxAge - 0; | ||
if (Number.isNaN(maxAge)) throw new Error('maxAge should be a Number'); | ||
str += "; Max-Age=".concat(Math.floor(maxAge)); | ||
str += `; Max-Age=${Math.floor(maxAge)}`; | ||
} | ||
@@ -89,3 +40,3 @@ if (opt.domain) { | ||
} | ||
str += "; Domain=".concat(opt.domain); | ||
str += `; Domain=${opt.domain}`; | ||
} | ||
@@ -96,3 +47,3 @@ if (opt.path) { | ||
} | ||
str += "; Path=".concat(opt.path); | ||
str += `; Path=${opt.path}`; | ||
} | ||
@@ -103,3 +54,3 @@ if (opt.expires) { | ||
} | ||
str += "; Expires=".concat(opt.expires.toUTCString()); | ||
str += `; Expires=${opt.expires.toUTCString()}`; | ||
} | ||
@@ -109,3 +60,3 @@ if (opt.httpOnly) str += '; HttpOnly'; | ||
if (opt.sameSite) { | ||
var sameSite = typeof opt.sameSite === 'string' ? opt.sameSite.toLowerCase() : opt.sameSite; | ||
const sameSite = typeof opt.sameSite === 'string' ? opt.sameSite.toLowerCase() : opt.sameSite; | ||
switch (sameSite) { | ||
@@ -130,5 +81,5 @@ case true: | ||
}; | ||
var cookie = { | ||
create: function create(name, value, minutes, domain) { | ||
var cookieOptions = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : { | ||
const cookie = { | ||
create(name, value, minutes, domain) { | ||
let cookieOptions = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : { | ||
path: '/', | ||
@@ -144,7 +95,7 @@ sameSite: 'strict' | ||
}, | ||
read: function read(name) { | ||
var nameEQ = "".concat(name, "="); | ||
var ca = document.cookie.split(';'); | ||
for (var i = 0; i < ca.length; i++) { | ||
var c = ca[i]; | ||
read(name) { | ||
const nameEQ = `${name}=`; | ||
const ca = document.cookie.split(';'); | ||
for (let i = 0; i < ca.length; i++) { | ||
let c = ca[i]; | ||
while (c.charAt(0) === ' ') c = c.substring(1, c.length); | ||
@@ -155,3 +106,3 @@ if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length); | ||
}, | ||
remove: function remove(name) { | ||
remove(name) { | ||
this.create(name, '', -1); | ||
@@ -162,13 +113,22 @@ } | ||
name: 'cookie', | ||
lookup: function lookup(options) { | ||
var found; | ||
if (options.lookupCookie && typeof document !== 'undefined') { | ||
var c = cookie.read(options.lookupCookie); | ||
if (c) found = c; | ||
// Deconstruct the options object and extract the lookupCookie property | ||
lookup(_ref) { | ||
let { | ||
lookupCookie | ||
} = _ref; | ||
if (lookupCookie && typeof document !== 'undefined') { | ||
return cookie.read(lookupCookie) || undefined; | ||
} | ||
return found; | ||
return undefined; | ||
}, | ||
cacheUserLanguage: function cacheUserLanguage(lng, options) { | ||
if (options.lookupCookie && typeof document !== 'undefined') { | ||
cookie.create(options.lookupCookie, lng, options.cookieMinutes, options.cookieDomain, options.cookieOptions); | ||
// Deconstruct the options object and extract the lookupCookie, cookieMinutes, cookieDomain, and cookieOptions properties | ||
cacheUserLanguage(lng, _ref2) { | ||
let { | ||
lookupCookie, | ||
cookieMinutes, | ||
cookieDomain, | ||
cookieOptions | ||
} = _ref2; | ||
if (lookupCookie && typeof document !== 'undefined') { | ||
cookie.create(lookupCookie, lng, cookieMinutes, cookieDomain, cookieOptions); | ||
} | ||
@@ -180,16 +140,22 @@ } | ||
name: 'querystring', | ||
lookup: function lookup(options) { | ||
var found; | ||
// Deconstruct the options object and extract the lookupQuerystring property | ||
lookup(_ref) { | ||
let { | ||
lookupQuerystring | ||
} = _ref; | ||
let found; | ||
if (typeof window !== 'undefined') { | ||
var search = window.location.search; | ||
if (!window.location.search && window.location.hash && window.location.hash.indexOf('?') > -1) { | ||
let { | ||
search | ||
} = window.location; | ||
if (!window.location.search && window.location.hash?.indexOf('?') > -1) { | ||
search = window.location.hash.substring(window.location.hash.indexOf('?')); | ||
} | ||
var query = search.substring(1); | ||
var params = query.split('&'); | ||
for (var i = 0; i < params.length; i++) { | ||
var pos = params[i].indexOf('='); | ||
const query = search.substring(1); | ||
const params = query.split('&'); | ||
for (let i = 0; i < params.length; i++) { | ||
const pos = params[i].indexOf('='); | ||
if (pos > 0) { | ||
var key = params[i].substring(0, pos); | ||
if (key === options.lookupQuerystring) { | ||
const key = params[i].substring(0, pos); | ||
if (key === lookupQuerystring) { | ||
found = params[i].substring(pos + 1); | ||
@@ -204,8 +170,8 @@ } | ||
var hasLocalStorageSupport = null; | ||
var localStorageAvailable = function localStorageAvailable() { | ||
let hasLocalStorageSupport = null; | ||
const localStorageAvailable = () => { | ||
if (hasLocalStorageSupport !== null) return hasLocalStorageSupport; | ||
try { | ||
hasLocalStorageSupport = window !== 'undefined' && window.localStorage !== null; | ||
var testKey = 'i18next.translate.boo'; | ||
const testKey = 'i18next.translate.boo'; | ||
window.localStorage.setItem(testKey, 'foo'); | ||
@@ -220,13 +186,20 @@ window.localStorage.removeItem(testKey); | ||
name: 'localStorage', | ||
lookup: function lookup(options) { | ||
var found; | ||
if (options.lookupLocalStorage && localStorageAvailable()) { | ||
var lng = window.localStorage.getItem(options.lookupLocalStorage); | ||
if (lng) found = lng; | ||
// Deconstruct the options object and extract the lookupLocalStorage property | ||
lookup(_ref) { | ||
let { | ||
lookupLocalStorage | ||
} = _ref; | ||
if (lookupLocalStorage && localStorageAvailable()) { | ||
return window.localStorage.getItem(lookupLocalStorage) || undefined; // Undefined ensures type consistency with the previous version of this function | ||
} | ||
return found; | ||
return undefined; | ||
}, | ||
cacheUserLanguage: function cacheUserLanguage(lng, options) { | ||
if (options.lookupLocalStorage && localStorageAvailable()) { | ||
window.localStorage.setItem(options.lookupLocalStorage, lng); | ||
// Deconstruct the options object and extract the lookupLocalStorage property | ||
cacheUserLanguage(lng, _ref2) { | ||
let { | ||
lookupLocalStorage | ||
} = _ref2; | ||
if (lookupLocalStorage && localStorageAvailable()) { | ||
window.localStorage.setItem(lookupLocalStorage, lng); | ||
} | ||
@@ -236,8 +209,8 @@ } | ||
var hasSessionStorageSupport = null; | ||
var sessionStorageAvailable = function sessionStorageAvailable() { | ||
let hasSessionStorageSupport = null; | ||
const sessionStorageAvailable = () => { | ||
if (hasSessionStorageSupport !== null) return hasSessionStorageSupport; | ||
try { | ||
hasSessionStorageSupport = window !== 'undefined' && window.sessionStorage !== null; | ||
var testKey = 'i18next.translate.boo'; | ||
const testKey = 'i18next.translate.boo'; | ||
window.sessionStorage.setItem(testKey, 'foo'); | ||
@@ -252,13 +225,17 @@ window.sessionStorage.removeItem(testKey); | ||
name: 'sessionStorage', | ||
lookup: function lookup(options) { | ||
var found; | ||
if (options.lookupSessionStorage && sessionStorageAvailable()) { | ||
var lng = window.sessionStorage.getItem(options.lookupSessionStorage); | ||
if (lng) found = lng; | ||
lookup(_ref) { | ||
let { | ||
lookupSessionStorage | ||
} = _ref; | ||
if (lookupSessionStorage && sessionStorageAvailable()) { | ||
return window.sessionStorage.getItem(lookupSessionStorage) || undefined; | ||
} | ||
return found; | ||
return undefined; | ||
}, | ||
cacheUserLanguage: function cacheUserLanguage(lng, options) { | ||
if (options.lookupSessionStorage && sessionStorageAvailable()) { | ||
window.sessionStorage.setItem(options.lookupSessionStorage, lng); | ||
cacheUserLanguage(lng, _ref2) { | ||
let { | ||
lookupSessionStorage | ||
} = _ref2; | ||
if (lookupSessionStorage && sessionStorageAvailable()) { | ||
window.sessionStorage.setItem(lookupSessionStorage, lng); | ||
} | ||
@@ -270,16 +247,21 @@ } | ||
name: 'navigator', | ||
lookup: function lookup(options) { | ||
var found = []; | ||
lookup(options) { | ||
const found = []; | ||
if (typeof navigator !== 'undefined') { | ||
if (navigator.languages) { | ||
const { | ||
languages, | ||
userLanguage, | ||
language | ||
} = navigator; | ||
if (languages) { | ||
// chrome only; not an array, so can't use .push.apply instead of iterating | ||
for (var i = 0; i < navigator.languages.length; i++) { | ||
found.push(navigator.languages[i]); | ||
for (let i = 0; i < languages.length; i++) { | ||
found.push(languages[i]); | ||
} | ||
} | ||
if (navigator.userLanguage) { | ||
found.push(navigator.userLanguage); | ||
if (userLanguage) { | ||
found.push(userLanguage); | ||
} | ||
if (navigator.language) { | ||
found.push(navigator.language); | ||
if (language) { | ||
found.push(language); | ||
} | ||
@@ -293,7 +275,11 @@ } | ||
name: 'htmlTag', | ||
lookup: function lookup(options) { | ||
var found; | ||
var htmlTag = options.htmlTag || (typeof document !== 'undefined' ? document.documentElement : null); | ||
if (htmlTag && typeof htmlTag.getAttribute === 'function') { | ||
found = htmlTag.getAttribute('lang'); | ||
// Deconstruct the options object and extract the htmlTag property | ||
lookup(_ref) { | ||
let { | ||
htmlTag | ||
} = _ref; | ||
let found; | ||
const internalHtmlTag = htmlTag || (typeof document !== 'undefined' ? document.documentElement : null); | ||
if (internalHtmlTag && typeof internalHtmlTag.getAttribute === 'function') { | ||
found = internalHtmlTag.getAttribute('lang'); | ||
} | ||
@@ -306,18 +292,12 @@ return found; | ||
name: 'path', | ||
lookup: function lookup(options) { | ||
var found; | ||
if (typeof window !== 'undefined') { | ||
var language = window.location.pathname.match(/\/([a-zA-Z-]*)/g); | ||
if (language instanceof Array) { | ||
if (typeof options.lookupFromPathIndex === 'number') { | ||
if (typeof language[options.lookupFromPathIndex] !== 'string') { | ||
return undefined; | ||
} | ||
found = language[options.lookupFromPathIndex].replace('/', ''); | ||
} else { | ||
found = language[0].replace('/', ''); | ||
} | ||
} | ||
} | ||
return found; | ||
// Deconstruct the options object and extract the lookupFromPathIndex property | ||
lookup(_ref) { | ||
let { | ||
lookupFromPathIndex | ||
} = _ref; | ||
if (typeof window === 'undefined') return undefined; | ||
const language = window.location.pathname.match(/\/([a-zA-Z-]*)/g); | ||
if (!Array.isArray(language)) return undefined; | ||
const index = typeof lookupFromPathIndex === 'number' ? lookupFromPathIndex : 0; | ||
return language[index]?.replace('/', ''); | ||
} | ||
@@ -328,9 +308,12 @@ }; | ||
name: 'subdomain', | ||
lookup: function lookup(options) { | ||
lookup(_ref) { | ||
let { | ||
lookupFromSubdomainIndex | ||
} = _ref; | ||
// If given get the subdomain index else 1 | ||
var lookupFromSubdomainIndex = typeof options.lookupFromSubdomainIndex === 'number' ? options.lookupFromSubdomainIndex + 1 : 1; | ||
const internalLookupFromSubdomainIndex = typeof lookupFromSubdomainIndex === 'number' ? lookupFromSubdomainIndex + 1 : 1; | ||
// get all matches if window.location. is existing | ||
// first item of match is the match itself and the second is the first group macht which sould be the first subdomain match | ||
// first item of match is the match itself and the second is the first group match which should be the first subdomain match | ||
// is the hostname no public domain get the or option of localhost | ||
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); | ||
const language = typeof window !== 'undefined' && window.location?.hostname?.match(/^(\w{2,5})\.(([a-z0-9-]{1,63}\.[a-z]{2,6})|localhost)/i); | ||
@@ -340,3 +323,3 @@ // if there is no match (null) return undefined | ||
// return the given group match | ||
return language[lookupFromSubdomainIndex]; | ||
return language[internalLookupFromSubdomainIndex]; | ||
} | ||
@@ -358,11 +341,8 @@ }; | ||
convertDetectedLanguage: function convertDetectedLanguage(l) { | ||
return l; | ||
} | ||
convertDetectedLanguage: l => l | ||
}; | ||
} | ||
var Browser = /*#__PURE__*/function () { | ||
function Browser(services) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
_classCallCheck(this, Browser); | ||
class Browser { | ||
constructor(services) { | ||
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
this.type = 'languageDetector'; | ||
@@ -372,68 +352,53 @@ this.detectors = {}; | ||
} | ||
_createClass(Browser, [{ | ||
key: "init", | ||
value: function init(services) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var i18nOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
this.services = services || { | ||
languageUtils: {} | ||
}; // this way the language detector can be used without i18next | ||
this.options = defaults(options, this.options || {}, getDefaults()); | ||
if (typeof this.options.convertDetectedLanguage === 'string' && this.options.convertDetectedLanguage.indexOf('15897') > -1) { | ||
this.options.convertDetectedLanguage = function (l) { | ||
return l.replace('-', '_'); | ||
}; | ||
init(services) { | ||
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
let i18nOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
this.services = services || { | ||
languageUtils: {} | ||
}; // this way the language detector can be used without i18next | ||
this.options = defaults(options, this.options || {}, getDefaults()); | ||
if (typeof this.options.convertDetectedLanguage === 'string' && this.options.convertDetectedLanguage.indexOf('15897') > -1) { | ||
this.options.convertDetectedLanguage = l => l.replace('-', '_'); | ||
} | ||
// backwards compatibility | ||
if (this.options.lookupFromUrlIndex) this.options.lookupFromPathIndex = this.options.lookupFromUrlIndex; | ||
this.i18nOptions = i18nOptions; | ||
this.addDetector(cookie$1); | ||
this.addDetector(querystring); | ||
this.addDetector(localStorage); | ||
this.addDetector(sessionStorage); | ||
this.addDetector(navigator$1); | ||
this.addDetector(htmlTag); | ||
this.addDetector(path); | ||
this.addDetector(subdomain); | ||
} | ||
addDetector(detector) { | ||
this.detectors[detector.name] = detector; | ||
return this; | ||
} | ||
detect(detectionOrder) { | ||
if (!detectionOrder) detectionOrder = this.options.order; | ||
let detected = []; | ||
detectionOrder.forEach(detectorName => { | ||
if (this.detectors[detectorName]) { | ||
let lookup = this.detectors[detectorName].lookup(this.options); | ||
if (lookup && typeof lookup === 'string') lookup = [lookup]; | ||
if (lookup) detected = detected.concat(lookup); | ||
} | ||
}); | ||
detected = detected.map(d => this.options.convertDetectedLanguage(d)); | ||
if (this.services.languageUtils.getBestMatchFromCodes) return detected; // new i18next v19.5.0 | ||
return detected.length > 0 ? detected[0] : null; // a little backward compatibility | ||
} | ||
// backwards compatibility | ||
if (this.options.lookupFromUrlIndex) this.options.lookupFromPathIndex = this.options.lookupFromUrlIndex; | ||
this.i18nOptions = i18nOptions; | ||
this.addDetector(cookie$1); | ||
this.addDetector(querystring); | ||
this.addDetector(localStorage); | ||
this.addDetector(sessionStorage); | ||
this.addDetector(navigator$1); | ||
this.addDetector(htmlTag); | ||
this.addDetector(path); | ||
this.addDetector(subdomain); | ||
} | ||
}, { | ||
key: "addDetector", | ||
value: function addDetector(detector) { | ||
this.detectors[detector.name] = detector; | ||
return this; | ||
} | ||
}, { | ||
key: "detect", | ||
value: function detect(detectionOrder) { | ||
var _this = this; | ||
if (!detectionOrder) detectionOrder = this.options.order; | ||
var detected = []; | ||
detectionOrder.forEach(function (detectorName) { | ||
if (_this.detectors[detectorName]) { | ||
var lookup = _this.detectors[detectorName].lookup(_this.options); | ||
if (lookup && typeof lookup === 'string') lookup = [lookup]; | ||
if (lookup) detected = detected.concat(lookup); | ||
} | ||
}); | ||
detected = detected.map(function (d) { | ||
return _this.options.convertDetectedLanguage(d); | ||
}); | ||
if (this.services.languageUtils.getBestMatchFromCodes) return detected; // new i18next v19.5.0 | ||
return detected.length > 0 ? detected[0] : null; // a little backward compatibility | ||
} | ||
}, { | ||
key: "cacheUserLanguage", | ||
value: function cacheUserLanguage(lng, caches) { | ||
var _this2 = this; | ||
if (!caches) caches = this.options.caches; | ||
if (!caches) return; | ||
if (this.options.excludeCacheFor && this.options.excludeCacheFor.indexOf(lng) > -1) return; | ||
caches.forEach(function (cacheName) { | ||
if (_this2.detectors[cacheName]) _this2.detectors[cacheName].cacheUserLanguage(lng, _this2.options); | ||
}); | ||
} | ||
}]); | ||
return Browser; | ||
}(); | ||
cacheUserLanguage(lng, caches) { | ||
if (!caches) caches = this.options.caches; | ||
if (!caches) return; | ||
if (this.options.excludeCacheFor && this.options.excludeCacheFor.indexOf(lng) > -1) return; | ||
caches.forEach(cacheName => { | ||
if (this.detectors[cacheName]) this.detectors[cacheName].cacheUserLanguage(lng, this.options); | ||
}); | ||
} | ||
} | ||
Browser.type = 'languageDetector'; | ||
@@ -440,0 +405,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?module.exports=o():"function"==typeof define&&define.amd?define(o):(e="undefined"!=typeof globalThis?globalThis:e||self).i18nextBrowserLanguageDetector=o()}(this,(function(){"use strict";function e(e,o){if(!(e instanceof o))throw new TypeError("Cannot call a class as a function")}function o(e){return o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},o(e)}function t(e){var t=function(e,t){if("object"!==o(e)||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var i=n.call(e,t||"default");if("object"!==o(i))return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"===o(t)?t:String(t)}function n(e,o){for(var n=0;n<o.length;n++){var i=o[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,t(i.key),i)}}var i=[],r=i.forEach,a=i.slice;function u(e){return r.call(a.call(arguments,1),(function(o){if(o)for(var t in o)void 0===e[t]&&(e[t]=o[t])})),e}var s=/^[\u0009\u0020-\u007e\u0080-\u00ff]+$/,c=function(e,o,t){var n=t||{};n.path=n.path||"/";var i=encodeURIComponent(o),r="".concat(e,"=").concat(i);if(n.maxAge>0){var a=n.maxAge-0;if(Number.isNaN(a))throw new Error("maxAge should be a Number");r+="; Max-Age=".concat(Math.floor(a))}if(n.domain){if(!s.test(n.domain))throw new TypeError("option domain is invalid");r+="; Domain=".concat(n.domain)}if(n.path){if(!s.test(n.path))throw new TypeError("option path is invalid");r+="; Path=".concat(n.path)}if(n.expires){if("function"!=typeof n.expires.toUTCString)throw new TypeError("option expires is invalid");r+="; Expires=".concat(n.expires.toUTCString())}if(n.httpOnly&&(r+="; HttpOnly"),n.secure&&(r+="; Secure"),n.sameSite)switch("string"==typeof n.sameSite?n.sameSite.toLowerCase():n.sameSite){case!0:r+="; SameSite=Strict";break;case"lax":r+="; SameSite=Lax";break;case"strict":r+="; SameSite=Strict";break;case"none":r+="; SameSite=None";break;default:throw new TypeError("option sameSite is invalid")}return r},l=function(e,o,t,n){var i=arguments.length>4&&void 0!==arguments[4]?arguments[4]:{path:"/",sameSite:"strict"};t&&(i.expires=new Date,i.expires.setTime(i.expires.getTime()+60*t*1e3)),n&&(i.domain=n),document.cookie=c(e,encodeURIComponent(o),i)},f=function(e){for(var o="".concat(e,"="),t=document.cookie.split(";"),n=0;n<t.length;n++){for(var i=t[n];" "===i.charAt(0);)i=i.substring(1,i.length);if(0===i.indexOf(o))return i.substring(o.length,i.length)}return null},d={name:"cookie",lookup:function(e){var o;if(e.lookupCookie&&"undefined"!=typeof document){var t=f(e.lookupCookie);t&&(o=t)}return o},cacheUserLanguage:function(e,o){o.lookupCookie&&"undefined"!=typeof document&&l(o.lookupCookie,e,o.cookieMinutes,o.cookieDomain,o.cookieOptions)}},g={name:"querystring",lookup:function(e){var o;if("undefined"!=typeof window){var t=window.location.search;!window.location.search&&window.location.hash&&window.location.hash.indexOf("?")>-1&&(t=window.location.hash.substring(window.location.hash.indexOf("?")));for(var n=t.substring(1).split("&"),i=0;i<n.length;i++){var r=n[i].indexOf("=");if(r>0)n[i].substring(0,r)===e.lookupQuerystring&&(o=n[i].substring(r+1))}}return o}},p=null,h=function(){if(null!==p)return p;try{p="undefined"!==window&&null!==window.localStorage;var e="i18next.translate.boo";window.localStorage.setItem(e,"foo"),window.localStorage.removeItem(e)}catch(e){p=!1}return p},m={name:"localStorage",lookup:function(e){var o;if(e.lookupLocalStorage&&h()){var t=window.localStorage.getItem(e.lookupLocalStorage);t&&(o=t)}return o},cacheUserLanguage:function(e,o){o.lookupLocalStorage&&h()&&window.localStorage.setItem(o.lookupLocalStorage,e)}},v=null,w=function(){if(null!==v)return v;try{v="undefined"!==window&&null!==window.sessionStorage;var e="i18next.translate.boo";window.sessionStorage.setItem(e,"foo"),window.sessionStorage.removeItem(e)}catch(e){v=!1}return v},y={name:"sessionStorage",lookup:function(e){var o;if(e.lookupSessionStorage&&w()){var t=window.sessionStorage.getItem(e.lookupSessionStorage);t&&(o=t)}return o},cacheUserLanguage:function(e,o){o.lookupSessionStorage&&w()&&window.sessionStorage.setItem(o.lookupSessionStorage,e)}},S={name:"navigator",lookup:function(e){var o=[];if("undefined"!=typeof navigator){if(navigator.languages)for(var t=0;t<navigator.languages.length;t++)o.push(navigator.languages[t]);navigator.userLanguage&&o.push(navigator.userLanguage),navigator.language&&o.push(navigator.language)}return o.length>0?o:void 0}},k={name:"htmlTag",lookup:function(e){var o,t=e.htmlTag||("undefined"!=typeof document?document.documentElement:null);return t&&"function"==typeof t.getAttribute&&(o=t.getAttribute("lang")),o}},b={name:"path",lookup:function(e){var o;if("undefined"!=typeof window){var t=window.location.pathname.match(/\/([a-zA-Z-]*)/g);if(t instanceof Array)if("number"==typeof e.lookupFromPathIndex){if("string"!=typeof t[e.lookupFromPathIndex])return;o=t[e.lookupFromPathIndex].replace("/","")}else o=t[0].replace("/","")}return o}},x={name:"subdomain",lookup:function(e){var o="number"==typeof e.lookupFromSubdomainIndex?e.lookupFromSubdomainIndex+1:1,t="undefined"!=typeof window&&window.location&&window.location.hostname&&window.location.hostname.match(/^(\w{2,5})\.(([a-z0-9-]{1,63}\.[a-z]{2,6})|localhost)/i);if(t)return t[o]}};var L=function(){function o(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};e(this,o),this.type="languageDetector",this.detectors={},this.init(t,n)}var t,i,r;return t=o,i=[{key:"init",value:function(e){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};this.services=e||{languageUtils:{}},this.options=u(o,this.options||{},{order:["querystring","cookie","localStorage","sessionStorage","navigator","htmlTag"],lookupQuerystring:"lng",lookupCookie:"i18next",lookupLocalStorage:"i18nextLng",lookupSessionStorage:"i18nextLng",caches:["localStorage"],excludeCacheFor:["cimode"],convertDetectedLanguage:function(e){return e}}),"string"==typeof this.options.convertDetectedLanguage&&this.options.convertDetectedLanguage.indexOf("15897")>-1&&(this.options.convertDetectedLanguage=function(e){return e.replace("-","_")}),this.options.lookupFromUrlIndex&&(this.options.lookupFromPathIndex=this.options.lookupFromUrlIndex),this.i18nOptions=t,this.addDetector(d),this.addDetector(g),this.addDetector(m),this.addDetector(y),this.addDetector(S),this.addDetector(k),this.addDetector(b),this.addDetector(x)}},{key:"addDetector",value:function(e){return this.detectors[e.name]=e,this}},{key:"detect",value:function(e){var o=this;e||(e=this.options.order);var t=[];return e.forEach((function(e){if(o.detectors[e]){var n=o.detectors[e].lookup(o.options);n&&"string"==typeof n&&(n=[n]),n&&(t=t.concat(n))}})),t=t.map((function(e){return o.options.convertDetectedLanguage(e)})),this.services.languageUtils.getBestMatchFromCodes?t:t.length>0?t[0]:null}},{key:"cacheUserLanguage",value:function(e,o){var t=this;o||(o=this.options.caches),o&&(this.options.excludeCacheFor&&this.options.excludeCacheFor.indexOf(e)>-1||o.forEach((function(o){t.detectors[o]&&t.detectors[o].cacheUserLanguage(e,t.options)})))}}],i&&n(t.prototype,i),r&&n(t,r),Object.defineProperty(t,"prototype",{writable:!1}),o}();return L.type="languageDetector",L})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).i18nextBrowserLanguageDetector=t()}(this,(function(){"use strict";const{slice:e,forEach:t}=[];const o=/^[\u0009\u0020-\u007e\u0080-\u00ff]+$/,n={create(e,t,n,i){let r=arguments.length>4&&void 0!==arguments[4]?arguments[4]:{path:"/",sameSite:"strict"};n&&(r.expires=new Date,r.expires.setTime(r.expires.getTime()+60*n*1e3)),i&&(r.domain=i),document.cookie=((e,t,n)=>{const i=n||{};i.path=i.path||"/";let r=`${e}=${encodeURIComponent(t)}`;if(i.maxAge>0){const e=i.maxAge-0;if(Number.isNaN(e))throw new Error("maxAge should be a Number");r+=`; Max-Age=${Math.floor(e)}`}if(i.domain){if(!o.test(i.domain))throw new TypeError("option domain is invalid");r+=`; Domain=${i.domain}`}if(i.path){if(!o.test(i.path))throw new TypeError("option path is invalid");r+=`; Path=${i.path}`}if(i.expires){if("function"!=typeof i.expires.toUTCString)throw new TypeError("option expires is invalid");r+=`; Expires=${i.expires.toUTCString()}`}if(i.httpOnly&&(r+="; HttpOnly"),i.secure&&(r+="; Secure"),i.sameSite)switch("string"==typeof i.sameSite?i.sameSite.toLowerCase():i.sameSite){case!0:r+="; SameSite=Strict";break;case"lax":r+="; SameSite=Lax";break;case"strict":r+="; SameSite=Strict";break;case"none":r+="; SameSite=None";break;default:throw new TypeError("option sameSite is invalid")}return r})(e,encodeURIComponent(t),r)},read(e){const t=`${e}=`,o=document.cookie.split(";");for(let e=0;e<o.length;e++){let n=o[e];for(;" "===n.charAt(0);)n=n.substring(1,n.length);if(0===n.indexOf(t))return n.substring(t.length,n.length)}return null},remove(e){this.create(e,"",-1)}};var i={name:"cookie",lookup(e){let{lookupCookie:t}=e;if(t&&"undefined"!=typeof document)return n.read(t)||void 0},cacheUserLanguage(e,t){let{lookupCookie:o,cookieMinutes:i,cookieDomain:r,cookieOptions:a}=t;o&&"undefined"!=typeof document&&n.create(o,e,i,r,a)}},r={name:"querystring",lookup(e){let t,{lookupQuerystring:o}=e;if("undefined"!=typeof window){let{search:e}=window.location;!window.location.search&&window.location.hash?.indexOf("?")>-1&&(e=window.location.hash.substring(window.location.hash.indexOf("?")));const n=e.substring(1).split("&");for(let e=0;e<n.length;e++){const i=n[e].indexOf("=");if(i>0){n[e].substring(0,i)===o&&(t=n[e].substring(i+1))}}}return t}};let a=null;const s=()=>{if(null!==a)return a;try{a="undefined"!==window&&null!==window.localStorage;const e="i18next.translate.boo";window.localStorage.setItem(e,"foo"),window.localStorage.removeItem(e)}catch(e){a=!1}return a};var l={name:"localStorage",lookup(e){let{lookupLocalStorage:t}=e;if(t&&s())return window.localStorage.getItem(t)||void 0},cacheUserLanguage(e,t){let{lookupLocalStorage:o}=t;o&&s()&&window.localStorage.setItem(o,e)}};let c=null;const u=()=>{if(null!==c)return c;try{c="undefined"!==window&&null!==window.sessionStorage;const e="i18next.translate.boo";window.sessionStorage.setItem(e,"foo"),window.sessionStorage.removeItem(e)}catch(e){c=!1}return c};var d={name:"sessionStorage",lookup(e){let{lookupSessionStorage:t}=e;if(t&&u())return window.sessionStorage.getItem(t)||void 0},cacheUserLanguage(e,t){let{lookupSessionStorage:o}=t;o&&u()&&window.sessionStorage.setItem(o,e)}},g={name:"navigator",lookup(e){const t=[];if("undefined"!=typeof navigator){const{languages:e,userLanguage:o,language:n}=navigator;if(e)for(let o=0;o<e.length;o++)t.push(e[o]);o&&t.push(o),n&&t.push(n)}return t.length>0?t:void 0}},h={name:"htmlTag",lookup(e){let t,{htmlTag:o}=e;const n=o||("undefined"!=typeof document?document.documentElement:null);return n&&"function"==typeof n.getAttribute&&(t=n.getAttribute("lang")),t}},p={name:"path",lookup(e){let{lookupFromPathIndex:t}=e;if("undefined"==typeof window)return;const o=window.location.pathname.match(/\/([a-zA-Z-]*)/g);if(!Array.isArray(o))return;return o["number"==typeof t?t:0]?.replace("/","")}},f={name:"subdomain",lookup(e){let{lookupFromSubdomainIndex:t}=e;const o="number"==typeof t?t+1:1,n="undefined"!=typeof window&&window.location?.hostname?.match(/^(\w{2,5})\.(([a-z0-9-]{1,63}\.[a-z]{2,6})|localhost)/i);if(n)return n[o]}};class m{constructor(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};this.type="languageDetector",this.detectors={},this.init(e,t)}init(o){let n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},a=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};this.services=o||{languageUtils:{}},this.options=function(o){return t.call(e.call(arguments,1),(e=>{if(e)for(const t in e)void 0===o[t]&&(o[t]=e[t])})),o}(n,this.options||{},{order:["querystring","cookie","localStorage","sessionStorage","navigator","htmlTag"],lookupQuerystring:"lng",lookupCookie:"i18next",lookupLocalStorage:"i18nextLng",lookupSessionStorage:"i18nextLng",caches:["localStorage"],excludeCacheFor:["cimode"],convertDetectedLanguage:e=>e}),"string"==typeof this.options.convertDetectedLanguage&&this.options.convertDetectedLanguage.indexOf("15897")>-1&&(this.options.convertDetectedLanguage=e=>e.replace("-","_")),this.options.lookupFromUrlIndex&&(this.options.lookupFromPathIndex=this.options.lookupFromUrlIndex),this.i18nOptions=a,this.addDetector(i),this.addDetector(r),this.addDetector(l),this.addDetector(d),this.addDetector(g),this.addDetector(h),this.addDetector(p),this.addDetector(f)}addDetector(e){return this.detectors[e.name]=e,this}detect(e){e||(e=this.options.order);let t=[];return e.forEach((e=>{if(this.detectors[e]){let o=this.detectors[e].lookup(this.options);o&&"string"==typeof o&&(o=[o]),o&&(t=t.concat(o))}})),t=t.map((e=>this.options.convertDetectedLanguage(e))),this.services.languageUtils.getBestMatchFromCodes?t:t.length>0?t[0]:null}cacheUserLanguage(e,t){t||(t=this.options.caches),t&&(this.options.excludeCacheFor&&this.options.excludeCacheFor.indexOf(e)>-1||t.forEach((t=>{this.detectors[t]&&this.detectors[t].cacheUserLanguage(e,this.options)})))}}return m.type="languageDetector",m})); |
@@ -7,59 +7,10 @@ (function (global, factory) { | ||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
} | ||
function _typeof(o) { | ||
"@babel/helpers - typeof"; | ||
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { | ||
return typeof o; | ||
} : function (o) { | ||
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; | ||
}, _typeof(o); | ||
} | ||
function _toPrimitive(input, hint) { | ||
if (_typeof(input) !== "object" || input === null) return input; | ||
var prim = input[Symbol.toPrimitive]; | ||
if (prim !== undefined) { | ||
var res = prim.call(input, hint || "default"); | ||
if (_typeof(res) !== "object") return res; | ||
throw new TypeError("@@toPrimitive must return a primitive value."); | ||
} | ||
return (hint === "string" ? String : Number)(input); | ||
} | ||
function _toPropertyKey(arg) { | ||
var key = _toPrimitive(arg, "string"); | ||
return _typeof(key) === "symbol" ? key : String(key); | ||
} | ||
function _defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); | ||
} | ||
} | ||
function _createClass(Constructor, protoProps, staticProps) { | ||
if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) _defineProperties(Constructor, staticProps); | ||
Object.defineProperty(Constructor, "prototype", { | ||
writable: false | ||
}); | ||
return Constructor; | ||
} | ||
var arr = []; | ||
var each = arr.forEach; | ||
var slice = arr.slice; | ||
const { | ||
slice, | ||
forEach | ||
} = []; | ||
function defaults(obj) { | ||
each.call(slice.call(arguments, 1), function (source) { | ||
forEach.call(slice.call(arguments, 1), source => { | ||
if (source) { | ||
for (var prop in source) { | ||
for (const prop in source) { | ||
if (obj[prop] === undefined) obj[prop] = source[prop]; | ||
@@ -73,12 +24,12 @@ } | ||
// eslint-disable-next-line no-control-regex | ||
var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/; | ||
var serializeCookie = function serializeCookie(name, val, options) { | ||
var opt = options || {}; | ||
const fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/; | ||
const serializeCookie = (name, val, options) => { | ||
const opt = options || {}; | ||
opt.path = opt.path || '/'; | ||
var value = encodeURIComponent(val); | ||
var str = "".concat(name, "=").concat(value); | ||
const value = encodeURIComponent(val); | ||
let str = `${name}=${value}`; | ||
if (opt.maxAge > 0) { | ||
var maxAge = opt.maxAge - 0; | ||
const maxAge = opt.maxAge - 0; | ||
if (Number.isNaN(maxAge)) throw new Error('maxAge should be a Number'); | ||
str += "; Max-Age=".concat(Math.floor(maxAge)); | ||
str += `; Max-Age=${Math.floor(maxAge)}`; | ||
} | ||
@@ -89,3 +40,3 @@ if (opt.domain) { | ||
} | ||
str += "; Domain=".concat(opt.domain); | ||
str += `; Domain=${opt.domain}`; | ||
} | ||
@@ -96,3 +47,3 @@ if (opt.path) { | ||
} | ||
str += "; Path=".concat(opt.path); | ||
str += `; Path=${opt.path}`; | ||
} | ||
@@ -103,3 +54,3 @@ if (opt.expires) { | ||
} | ||
str += "; Expires=".concat(opt.expires.toUTCString()); | ||
str += `; Expires=${opt.expires.toUTCString()}`; | ||
} | ||
@@ -109,3 +60,3 @@ if (opt.httpOnly) str += '; HttpOnly'; | ||
if (opt.sameSite) { | ||
var sameSite = typeof opt.sameSite === 'string' ? opt.sameSite.toLowerCase() : opt.sameSite; | ||
const sameSite = typeof opt.sameSite === 'string' ? opt.sameSite.toLowerCase() : opt.sameSite; | ||
switch (sameSite) { | ||
@@ -130,5 +81,5 @@ case true: | ||
}; | ||
var cookie = { | ||
create: function create(name, value, minutes, domain) { | ||
var cookieOptions = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : { | ||
const cookie = { | ||
create(name, value, minutes, domain) { | ||
let cookieOptions = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : { | ||
path: '/', | ||
@@ -144,7 +95,7 @@ sameSite: 'strict' | ||
}, | ||
read: function read(name) { | ||
var nameEQ = "".concat(name, "="); | ||
var ca = document.cookie.split(';'); | ||
for (var i = 0; i < ca.length; i++) { | ||
var c = ca[i]; | ||
read(name) { | ||
const nameEQ = `${name}=`; | ||
const ca = document.cookie.split(';'); | ||
for (let i = 0; i < ca.length; i++) { | ||
let c = ca[i]; | ||
while (c.charAt(0) === ' ') c = c.substring(1, c.length); | ||
@@ -155,3 +106,3 @@ if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length); | ||
}, | ||
remove: function remove(name) { | ||
remove(name) { | ||
this.create(name, '', -1); | ||
@@ -162,13 +113,22 @@ } | ||
name: 'cookie', | ||
lookup: function lookup(options) { | ||
var found; | ||
if (options.lookupCookie && typeof document !== 'undefined') { | ||
var c = cookie.read(options.lookupCookie); | ||
if (c) found = c; | ||
// Deconstruct the options object and extract the lookupCookie property | ||
lookup(_ref) { | ||
let { | ||
lookupCookie | ||
} = _ref; | ||
if (lookupCookie && typeof document !== 'undefined') { | ||
return cookie.read(lookupCookie) || undefined; | ||
} | ||
return found; | ||
return undefined; | ||
}, | ||
cacheUserLanguage: function cacheUserLanguage(lng, options) { | ||
if (options.lookupCookie && typeof document !== 'undefined') { | ||
cookie.create(options.lookupCookie, lng, options.cookieMinutes, options.cookieDomain, options.cookieOptions); | ||
// Deconstruct the options object and extract the lookupCookie, cookieMinutes, cookieDomain, and cookieOptions properties | ||
cacheUserLanguage(lng, _ref2) { | ||
let { | ||
lookupCookie, | ||
cookieMinutes, | ||
cookieDomain, | ||
cookieOptions | ||
} = _ref2; | ||
if (lookupCookie && typeof document !== 'undefined') { | ||
cookie.create(lookupCookie, lng, cookieMinutes, cookieDomain, cookieOptions); | ||
} | ||
@@ -180,16 +140,22 @@ } | ||
name: 'querystring', | ||
lookup: function lookup(options) { | ||
var found; | ||
// Deconstruct the options object and extract the lookupQuerystring property | ||
lookup(_ref) { | ||
let { | ||
lookupQuerystring | ||
} = _ref; | ||
let found; | ||
if (typeof window !== 'undefined') { | ||
var search = window.location.search; | ||
if (!window.location.search && window.location.hash && window.location.hash.indexOf('?') > -1) { | ||
let { | ||
search | ||
} = window.location; | ||
if (!window.location.search && window.location.hash?.indexOf('?') > -1) { | ||
search = window.location.hash.substring(window.location.hash.indexOf('?')); | ||
} | ||
var query = search.substring(1); | ||
var params = query.split('&'); | ||
for (var i = 0; i < params.length; i++) { | ||
var pos = params[i].indexOf('='); | ||
const query = search.substring(1); | ||
const params = query.split('&'); | ||
for (let i = 0; i < params.length; i++) { | ||
const pos = params[i].indexOf('='); | ||
if (pos > 0) { | ||
var key = params[i].substring(0, pos); | ||
if (key === options.lookupQuerystring) { | ||
const key = params[i].substring(0, pos); | ||
if (key === lookupQuerystring) { | ||
found = params[i].substring(pos + 1); | ||
@@ -204,8 +170,8 @@ } | ||
var hasLocalStorageSupport = null; | ||
var localStorageAvailable = function localStorageAvailable() { | ||
let hasLocalStorageSupport = null; | ||
const localStorageAvailable = () => { | ||
if (hasLocalStorageSupport !== null) return hasLocalStorageSupport; | ||
try { | ||
hasLocalStorageSupport = window !== 'undefined' && window.localStorage !== null; | ||
var testKey = 'i18next.translate.boo'; | ||
const testKey = 'i18next.translate.boo'; | ||
window.localStorage.setItem(testKey, 'foo'); | ||
@@ -220,13 +186,20 @@ window.localStorage.removeItem(testKey); | ||
name: 'localStorage', | ||
lookup: function lookup(options) { | ||
var found; | ||
if (options.lookupLocalStorage && localStorageAvailable()) { | ||
var lng = window.localStorage.getItem(options.lookupLocalStorage); | ||
if (lng) found = lng; | ||
// Deconstruct the options object and extract the lookupLocalStorage property | ||
lookup(_ref) { | ||
let { | ||
lookupLocalStorage | ||
} = _ref; | ||
if (lookupLocalStorage && localStorageAvailable()) { | ||
return window.localStorage.getItem(lookupLocalStorage) || undefined; // Undefined ensures type consistency with the previous version of this function | ||
} | ||
return found; | ||
return undefined; | ||
}, | ||
cacheUserLanguage: function cacheUserLanguage(lng, options) { | ||
if (options.lookupLocalStorage && localStorageAvailable()) { | ||
window.localStorage.setItem(options.lookupLocalStorage, lng); | ||
// Deconstruct the options object and extract the lookupLocalStorage property | ||
cacheUserLanguage(lng, _ref2) { | ||
let { | ||
lookupLocalStorage | ||
} = _ref2; | ||
if (lookupLocalStorage && localStorageAvailable()) { | ||
window.localStorage.setItem(lookupLocalStorage, lng); | ||
} | ||
@@ -236,8 +209,8 @@ } | ||
var hasSessionStorageSupport = null; | ||
var sessionStorageAvailable = function sessionStorageAvailable() { | ||
let hasSessionStorageSupport = null; | ||
const sessionStorageAvailable = () => { | ||
if (hasSessionStorageSupport !== null) return hasSessionStorageSupport; | ||
try { | ||
hasSessionStorageSupport = window !== 'undefined' && window.sessionStorage !== null; | ||
var testKey = 'i18next.translate.boo'; | ||
const testKey = 'i18next.translate.boo'; | ||
window.sessionStorage.setItem(testKey, 'foo'); | ||
@@ -252,13 +225,17 @@ window.sessionStorage.removeItem(testKey); | ||
name: 'sessionStorage', | ||
lookup: function lookup(options) { | ||
var found; | ||
if (options.lookupSessionStorage && sessionStorageAvailable()) { | ||
var lng = window.sessionStorage.getItem(options.lookupSessionStorage); | ||
if (lng) found = lng; | ||
lookup(_ref) { | ||
let { | ||
lookupSessionStorage | ||
} = _ref; | ||
if (lookupSessionStorage && sessionStorageAvailable()) { | ||
return window.sessionStorage.getItem(lookupSessionStorage) || undefined; | ||
} | ||
return found; | ||
return undefined; | ||
}, | ||
cacheUserLanguage: function cacheUserLanguage(lng, options) { | ||
if (options.lookupSessionStorage && sessionStorageAvailable()) { | ||
window.sessionStorage.setItem(options.lookupSessionStorage, lng); | ||
cacheUserLanguage(lng, _ref2) { | ||
let { | ||
lookupSessionStorage | ||
} = _ref2; | ||
if (lookupSessionStorage && sessionStorageAvailable()) { | ||
window.sessionStorage.setItem(lookupSessionStorage, lng); | ||
} | ||
@@ -270,16 +247,21 @@ } | ||
name: 'navigator', | ||
lookup: function lookup(options) { | ||
var found = []; | ||
lookup(options) { | ||
const found = []; | ||
if (typeof navigator !== 'undefined') { | ||
if (navigator.languages) { | ||
const { | ||
languages, | ||
userLanguage, | ||
language | ||
} = navigator; | ||
if (languages) { | ||
// chrome only; not an array, so can't use .push.apply instead of iterating | ||
for (var i = 0; i < navigator.languages.length; i++) { | ||
found.push(navigator.languages[i]); | ||
for (let i = 0; i < languages.length; i++) { | ||
found.push(languages[i]); | ||
} | ||
} | ||
if (navigator.userLanguage) { | ||
found.push(navigator.userLanguage); | ||
if (userLanguage) { | ||
found.push(userLanguage); | ||
} | ||
if (navigator.language) { | ||
found.push(navigator.language); | ||
if (language) { | ||
found.push(language); | ||
} | ||
@@ -293,7 +275,11 @@ } | ||
name: 'htmlTag', | ||
lookup: function lookup(options) { | ||
var found; | ||
var htmlTag = options.htmlTag || (typeof document !== 'undefined' ? document.documentElement : null); | ||
if (htmlTag && typeof htmlTag.getAttribute === 'function') { | ||
found = htmlTag.getAttribute('lang'); | ||
// Deconstruct the options object and extract the htmlTag property | ||
lookup(_ref) { | ||
let { | ||
htmlTag | ||
} = _ref; | ||
let found; | ||
const internalHtmlTag = htmlTag || (typeof document !== 'undefined' ? document.documentElement : null); | ||
if (internalHtmlTag && typeof internalHtmlTag.getAttribute === 'function') { | ||
found = internalHtmlTag.getAttribute('lang'); | ||
} | ||
@@ -306,18 +292,12 @@ return found; | ||
name: 'path', | ||
lookup: function lookup(options) { | ||
var found; | ||
if (typeof window !== 'undefined') { | ||
var language = window.location.pathname.match(/\/([a-zA-Z-]*)/g); | ||
if (language instanceof Array) { | ||
if (typeof options.lookupFromPathIndex === 'number') { | ||
if (typeof language[options.lookupFromPathIndex] !== 'string') { | ||
return undefined; | ||
} | ||
found = language[options.lookupFromPathIndex].replace('/', ''); | ||
} else { | ||
found = language[0].replace('/', ''); | ||
} | ||
} | ||
} | ||
return found; | ||
// Deconstruct the options object and extract the lookupFromPathIndex property | ||
lookup(_ref) { | ||
let { | ||
lookupFromPathIndex | ||
} = _ref; | ||
if (typeof window === 'undefined') return undefined; | ||
const language = window.location.pathname.match(/\/([a-zA-Z-]*)/g); | ||
if (!Array.isArray(language)) return undefined; | ||
const index = typeof lookupFromPathIndex === 'number' ? lookupFromPathIndex : 0; | ||
return language[index]?.replace('/', ''); | ||
} | ||
@@ -328,9 +308,12 @@ }; | ||
name: 'subdomain', | ||
lookup: function lookup(options) { | ||
lookup(_ref) { | ||
let { | ||
lookupFromSubdomainIndex | ||
} = _ref; | ||
// If given get the subdomain index else 1 | ||
var lookupFromSubdomainIndex = typeof options.lookupFromSubdomainIndex === 'number' ? options.lookupFromSubdomainIndex + 1 : 1; | ||
const internalLookupFromSubdomainIndex = typeof lookupFromSubdomainIndex === 'number' ? lookupFromSubdomainIndex + 1 : 1; | ||
// get all matches if window.location. is existing | ||
// first item of match is the match itself and the second is the first group macht which sould be the first subdomain match | ||
// first item of match is the match itself and the second is the first group match which should be the first subdomain match | ||
// is the hostname no public domain get the or option of localhost | ||
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); | ||
const language = typeof window !== 'undefined' && window.location?.hostname?.match(/^(\w{2,5})\.(([a-z0-9-]{1,63}\.[a-z]{2,6})|localhost)/i); | ||
@@ -340,3 +323,3 @@ // if there is no match (null) return undefined | ||
// return the given group match | ||
return language[lookupFromSubdomainIndex]; | ||
return language[internalLookupFromSubdomainIndex]; | ||
} | ||
@@ -358,11 +341,8 @@ }; | ||
convertDetectedLanguage: function convertDetectedLanguage(l) { | ||
return l; | ||
} | ||
convertDetectedLanguage: l => l | ||
}; | ||
} | ||
var Browser = /*#__PURE__*/function () { | ||
function Browser(services) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
_classCallCheck(this, Browser); | ||
class Browser { | ||
constructor(services) { | ||
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
this.type = 'languageDetector'; | ||
@@ -372,68 +352,53 @@ this.detectors = {}; | ||
} | ||
_createClass(Browser, [{ | ||
key: "init", | ||
value: function init(services) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var i18nOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
this.services = services || { | ||
languageUtils: {} | ||
}; // this way the language detector can be used without i18next | ||
this.options = defaults(options, this.options || {}, getDefaults()); | ||
if (typeof this.options.convertDetectedLanguage === 'string' && this.options.convertDetectedLanguage.indexOf('15897') > -1) { | ||
this.options.convertDetectedLanguage = function (l) { | ||
return l.replace('-', '_'); | ||
}; | ||
init(services) { | ||
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
let i18nOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
this.services = services || { | ||
languageUtils: {} | ||
}; // this way the language detector can be used without i18next | ||
this.options = defaults(options, this.options || {}, getDefaults()); | ||
if (typeof this.options.convertDetectedLanguage === 'string' && this.options.convertDetectedLanguage.indexOf('15897') > -1) { | ||
this.options.convertDetectedLanguage = l => l.replace('-', '_'); | ||
} | ||
// backwards compatibility | ||
if (this.options.lookupFromUrlIndex) this.options.lookupFromPathIndex = this.options.lookupFromUrlIndex; | ||
this.i18nOptions = i18nOptions; | ||
this.addDetector(cookie$1); | ||
this.addDetector(querystring); | ||
this.addDetector(localStorage); | ||
this.addDetector(sessionStorage); | ||
this.addDetector(navigator$1); | ||
this.addDetector(htmlTag); | ||
this.addDetector(path); | ||
this.addDetector(subdomain); | ||
} | ||
addDetector(detector) { | ||
this.detectors[detector.name] = detector; | ||
return this; | ||
} | ||
detect(detectionOrder) { | ||
if (!detectionOrder) detectionOrder = this.options.order; | ||
let detected = []; | ||
detectionOrder.forEach(detectorName => { | ||
if (this.detectors[detectorName]) { | ||
let lookup = this.detectors[detectorName].lookup(this.options); | ||
if (lookup && typeof lookup === 'string') lookup = [lookup]; | ||
if (lookup) detected = detected.concat(lookup); | ||
} | ||
}); | ||
detected = detected.map(d => this.options.convertDetectedLanguage(d)); | ||
if (this.services.languageUtils.getBestMatchFromCodes) return detected; // new i18next v19.5.0 | ||
return detected.length > 0 ? detected[0] : null; // a little backward compatibility | ||
} | ||
// backwards compatibility | ||
if (this.options.lookupFromUrlIndex) this.options.lookupFromPathIndex = this.options.lookupFromUrlIndex; | ||
this.i18nOptions = i18nOptions; | ||
this.addDetector(cookie$1); | ||
this.addDetector(querystring); | ||
this.addDetector(localStorage); | ||
this.addDetector(sessionStorage); | ||
this.addDetector(navigator$1); | ||
this.addDetector(htmlTag); | ||
this.addDetector(path); | ||
this.addDetector(subdomain); | ||
} | ||
}, { | ||
key: "addDetector", | ||
value: function addDetector(detector) { | ||
this.detectors[detector.name] = detector; | ||
return this; | ||
} | ||
}, { | ||
key: "detect", | ||
value: function detect(detectionOrder) { | ||
var _this = this; | ||
if (!detectionOrder) detectionOrder = this.options.order; | ||
var detected = []; | ||
detectionOrder.forEach(function (detectorName) { | ||
if (_this.detectors[detectorName]) { | ||
var lookup = _this.detectors[detectorName].lookup(_this.options); | ||
if (lookup && typeof lookup === 'string') lookup = [lookup]; | ||
if (lookup) detected = detected.concat(lookup); | ||
} | ||
}); | ||
detected = detected.map(function (d) { | ||
return _this.options.convertDetectedLanguage(d); | ||
}); | ||
if (this.services.languageUtils.getBestMatchFromCodes) return detected; // new i18next v19.5.0 | ||
return detected.length > 0 ? detected[0] : null; // a little backward compatibility | ||
} | ||
}, { | ||
key: "cacheUserLanguage", | ||
value: function cacheUserLanguage(lng, caches) { | ||
var _this2 = this; | ||
if (!caches) caches = this.options.caches; | ||
if (!caches) return; | ||
if (this.options.excludeCacheFor && this.options.excludeCacheFor.indexOf(lng) > -1) return; | ||
caches.forEach(function (cacheName) { | ||
if (_this2.detectors[cacheName]) _this2.detectors[cacheName].cacheUserLanguage(lng, _this2.options); | ||
}); | ||
} | ||
}]); | ||
return Browser; | ||
}(); | ||
cacheUserLanguage(lng, caches) { | ||
if (!caches) caches = this.options.caches; | ||
if (!caches) return; | ||
if (this.options.excludeCacheFor && this.options.excludeCacheFor.indexOf(lng) > -1) return; | ||
caches.forEach(cacheName => { | ||
if (this.detectors[cacheName]) this.detectors[cacheName].cacheUserLanguage(lng, this.options); | ||
}); | ||
} | ||
} | ||
Browser.type = 'languageDetector'; | ||
@@ -440,0 +405,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?module.exports=o():"function"==typeof define&&define.amd?define(o):(e="undefined"!=typeof globalThis?globalThis:e||self).i18nextBrowserLanguageDetector=o()}(this,(function(){"use strict";function e(e,o){if(!(e instanceof o))throw new TypeError("Cannot call a class as a function")}function o(e){return o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},o(e)}function t(e){var t=function(e,t){if("object"!==o(e)||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var i=n.call(e,t||"default");if("object"!==o(i))return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"===o(t)?t:String(t)}function n(e,o){for(var n=0;n<o.length;n++){var i=o[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,t(i.key),i)}}var i=[],r=i.forEach,a=i.slice;function u(e){return r.call(a.call(arguments,1),(function(o){if(o)for(var t in o)void 0===e[t]&&(e[t]=o[t])})),e}var s=/^[\u0009\u0020-\u007e\u0080-\u00ff]+$/,c=function(e,o,t){var n=t||{};n.path=n.path||"/";var i=encodeURIComponent(o),r="".concat(e,"=").concat(i);if(n.maxAge>0){var a=n.maxAge-0;if(Number.isNaN(a))throw new Error("maxAge should be a Number");r+="; Max-Age=".concat(Math.floor(a))}if(n.domain){if(!s.test(n.domain))throw new TypeError("option domain is invalid");r+="; Domain=".concat(n.domain)}if(n.path){if(!s.test(n.path))throw new TypeError("option path is invalid");r+="; Path=".concat(n.path)}if(n.expires){if("function"!=typeof n.expires.toUTCString)throw new TypeError("option expires is invalid");r+="; Expires=".concat(n.expires.toUTCString())}if(n.httpOnly&&(r+="; HttpOnly"),n.secure&&(r+="; Secure"),n.sameSite)switch("string"==typeof n.sameSite?n.sameSite.toLowerCase():n.sameSite){case!0:r+="; SameSite=Strict";break;case"lax":r+="; SameSite=Lax";break;case"strict":r+="; SameSite=Strict";break;case"none":r+="; SameSite=None";break;default:throw new TypeError("option sameSite is invalid")}return r},l=function(e,o,t,n){var i=arguments.length>4&&void 0!==arguments[4]?arguments[4]:{path:"/",sameSite:"strict"};t&&(i.expires=new Date,i.expires.setTime(i.expires.getTime()+60*t*1e3)),n&&(i.domain=n),document.cookie=c(e,encodeURIComponent(o),i)},f=function(e){for(var o="".concat(e,"="),t=document.cookie.split(";"),n=0;n<t.length;n++){for(var i=t[n];" "===i.charAt(0);)i=i.substring(1,i.length);if(0===i.indexOf(o))return i.substring(o.length,i.length)}return null},d={name:"cookie",lookup:function(e){var o;if(e.lookupCookie&&"undefined"!=typeof document){var t=f(e.lookupCookie);t&&(o=t)}return o},cacheUserLanguage:function(e,o){o.lookupCookie&&"undefined"!=typeof document&&l(o.lookupCookie,e,o.cookieMinutes,o.cookieDomain,o.cookieOptions)}},g={name:"querystring",lookup:function(e){var o;if("undefined"!=typeof window){var t=window.location.search;!window.location.search&&window.location.hash&&window.location.hash.indexOf("?")>-1&&(t=window.location.hash.substring(window.location.hash.indexOf("?")));for(var n=t.substring(1).split("&"),i=0;i<n.length;i++){var r=n[i].indexOf("=");if(r>0)n[i].substring(0,r)===e.lookupQuerystring&&(o=n[i].substring(r+1))}}return o}},p=null,h=function(){if(null!==p)return p;try{p="undefined"!==window&&null!==window.localStorage;var e="i18next.translate.boo";window.localStorage.setItem(e,"foo"),window.localStorage.removeItem(e)}catch(e){p=!1}return p},m={name:"localStorage",lookup:function(e){var o;if(e.lookupLocalStorage&&h()){var t=window.localStorage.getItem(e.lookupLocalStorage);t&&(o=t)}return o},cacheUserLanguage:function(e,o){o.lookupLocalStorage&&h()&&window.localStorage.setItem(o.lookupLocalStorage,e)}},v=null,w=function(){if(null!==v)return v;try{v="undefined"!==window&&null!==window.sessionStorage;var e="i18next.translate.boo";window.sessionStorage.setItem(e,"foo"),window.sessionStorage.removeItem(e)}catch(e){v=!1}return v},y={name:"sessionStorage",lookup:function(e){var o;if(e.lookupSessionStorage&&w()){var t=window.sessionStorage.getItem(e.lookupSessionStorage);t&&(o=t)}return o},cacheUserLanguage:function(e,o){o.lookupSessionStorage&&w()&&window.sessionStorage.setItem(o.lookupSessionStorage,e)}},S={name:"navigator",lookup:function(e){var o=[];if("undefined"!=typeof navigator){if(navigator.languages)for(var t=0;t<navigator.languages.length;t++)o.push(navigator.languages[t]);navigator.userLanguage&&o.push(navigator.userLanguage),navigator.language&&o.push(navigator.language)}return o.length>0?o:void 0}},k={name:"htmlTag",lookup:function(e){var o,t=e.htmlTag||("undefined"!=typeof document?document.documentElement:null);return t&&"function"==typeof t.getAttribute&&(o=t.getAttribute("lang")),o}},b={name:"path",lookup:function(e){var o;if("undefined"!=typeof window){var t=window.location.pathname.match(/\/([a-zA-Z-]*)/g);if(t instanceof Array)if("number"==typeof e.lookupFromPathIndex){if("string"!=typeof t[e.lookupFromPathIndex])return;o=t[e.lookupFromPathIndex].replace("/","")}else o=t[0].replace("/","")}return o}},x={name:"subdomain",lookup:function(e){var o="number"==typeof e.lookupFromSubdomainIndex?e.lookupFromSubdomainIndex+1:1,t="undefined"!=typeof window&&window.location&&window.location.hostname&&window.location.hostname.match(/^(\w{2,5})\.(([a-z0-9-]{1,63}\.[a-z]{2,6})|localhost)/i);if(t)return t[o]}};var L=function(){function o(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};e(this,o),this.type="languageDetector",this.detectors={},this.init(t,n)}var t,i,r;return t=o,i=[{key:"init",value:function(e){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};this.services=e||{languageUtils:{}},this.options=u(o,this.options||{},{order:["querystring","cookie","localStorage","sessionStorage","navigator","htmlTag"],lookupQuerystring:"lng",lookupCookie:"i18next",lookupLocalStorage:"i18nextLng",lookupSessionStorage:"i18nextLng",caches:["localStorage"],excludeCacheFor:["cimode"],convertDetectedLanguage:function(e){return e}}),"string"==typeof this.options.convertDetectedLanguage&&this.options.convertDetectedLanguage.indexOf("15897")>-1&&(this.options.convertDetectedLanguage=function(e){return e.replace("-","_")}),this.options.lookupFromUrlIndex&&(this.options.lookupFromPathIndex=this.options.lookupFromUrlIndex),this.i18nOptions=t,this.addDetector(d),this.addDetector(g),this.addDetector(m),this.addDetector(y),this.addDetector(S),this.addDetector(k),this.addDetector(b),this.addDetector(x)}},{key:"addDetector",value:function(e){return this.detectors[e.name]=e,this}},{key:"detect",value:function(e){var o=this;e||(e=this.options.order);var t=[];return e.forEach((function(e){if(o.detectors[e]){var n=o.detectors[e].lookup(o.options);n&&"string"==typeof n&&(n=[n]),n&&(t=t.concat(n))}})),t=t.map((function(e){return o.options.convertDetectedLanguage(e)})),this.services.languageUtils.getBestMatchFromCodes?t:t.length>0?t[0]:null}},{key:"cacheUserLanguage",value:function(e,o){var t=this;o||(o=this.options.caches),o&&(this.options.excludeCacheFor&&this.options.excludeCacheFor.indexOf(e)>-1||o.forEach((function(o){t.detectors[o]&&t.detectors[o].cacheUserLanguage(e,t.options)})))}}],i&&n(t.prototype,i),r&&n(t,r),Object.defineProperty(t,"prototype",{writable:!1}),o}();return L.type="languageDetector",L})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).i18nextBrowserLanguageDetector=t()}(this,(function(){"use strict";const{slice:e,forEach:t}=[];const o=/^[\u0009\u0020-\u007e\u0080-\u00ff]+$/,n={create(e,t,n,i){let r=arguments.length>4&&void 0!==arguments[4]?arguments[4]:{path:"/",sameSite:"strict"};n&&(r.expires=new Date,r.expires.setTime(r.expires.getTime()+60*n*1e3)),i&&(r.domain=i),document.cookie=((e,t,n)=>{const i=n||{};i.path=i.path||"/";let r=`${e}=${encodeURIComponent(t)}`;if(i.maxAge>0){const e=i.maxAge-0;if(Number.isNaN(e))throw new Error("maxAge should be a Number");r+=`; Max-Age=${Math.floor(e)}`}if(i.domain){if(!o.test(i.domain))throw new TypeError("option domain is invalid");r+=`; Domain=${i.domain}`}if(i.path){if(!o.test(i.path))throw new TypeError("option path is invalid");r+=`; Path=${i.path}`}if(i.expires){if("function"!=typeof i.expires.toUTCString)throw new TypeError("option expires is invalid");r+=`; Expires=${i.expires.toUTCString()}`}if(i.httpOnly&&(r+="; HttpOnly"),i.secure&&(r+="; Secure"),i.sameSite)switch("string"==typeof i.sameSite?i.sameSite.toLowerCase():i.sameSite){case!0:r+="; SameSite=Strict";break;case"lax":r+="; SameSite=Lax";break;case"strict":r+="; SameSite=Strict";break;case"none":r+="; SameSite=None";break;default:throw new TypeError("option sameSite is invalid")}return r})(e,encodeURIComponent(t),r)},read(e){const t=`${e}=`,o=document.cookie.split(";");for(let e=0;e<o.length;e++){let n=o[e];for(;" "===n.charAt(0);)n=n.substring(1,n.length);if(0===n.indexOf(t))return n.substring(t.length,n.length)}return null},remove(e){this.create(e,"",-1)}};var i={name:"cookie",lookup(e){let{lookupCookie:t}=e;if(t&&"undefined"!=typeof document)return n.read(t)||void 0},cacheUserLanguage(e,t){let{lookupCookie:o,cookieMinutes:i,cookieDomain:r,cookieOptions:a}=t;o&&"undefined"!=typeof document&&n.create(o,e,i,r,a)}},r={name:"querystring",lookup(e){let t,{lookupQuerystring:o}=e;if("undefined"!=typeof window){let{search:e}=window.location;!window.location.search&&window.location.hash?.indexOf("?")>-1&&(e=window.location.hash.substring(window.location.hash.indexOf("?")));const n=e.substring(1).split("&");for(let e=0;e<n.length;e++){const i=n[e].indexOf("=");if(i>0){n[e].substring(0,i)===o&&(t=n[e].substring(i+1))}}}return t}};let a=null;const s=()=>{if(null!==a)return a;try{a="undefined"!==window&&null!==window.localStorage;const e="i18next.translate.boo";window.localStorage.setItem(e,"foo"),window.localStorage.removeItem(e)}catch(e){a=!1}return a};var l={name:"localStorage",lookup(e){let{lookupLocalStorage:t}=e;if(t&&s())return window.localStorage.getItem(t)||void 0},cacheUserLanguage(e,t){let{lookupLocalStorage:o}=t;o&&s()&&window.localStorage.setItem(o,e)}};let c=null;const u=()=>{if(null!==c)return c;try{c="undefined"!==window&&null!==window.sessionStorage;const e="i18next.translate.boo";window.sessionStorage.setItem(e,"foo"),window.sessionStorage.removeItem(e)}catch(e){c=!1}return c};var d={name:"sessionStorage",lookup(e){let{lookupSessionStorage:t}=e;if(t&&u())return window.sessionStorage.getItem(t)||void 0},cacheUserLanguage(e,t){let{lookupSessionStorage:o}=t;o&&u()&&window.sessionStorage.setItem(o,e)}},g={name:"navigator",lookup(e){const t=[];if("undefined"!=typeof navigator){const{languages:e,userLanguage:o,language:n}=navigator;if(e)for(let o=0;o<e.length;o++)t.push(e[o]);o&&t.push(o),n&&t.push(n)}return t.length>0?t:void 0}},h={name:"htmlTag",lookup(e){let t,{htmlTag:o}=e;const n=o||("undefined"!=typeof document?document.documentElement:null);return n&&"function"==typeof n.getAttribute&&(t=n.getAttribute("lang")),t}},p={name:"path",lookup(e){let{lookupFromPathIndex:t}=e;if("undefined"==typeof window)return;const o=window.location.pathname.match(/\/([a-zA-Z-]*)/g);if(!Array.isArray(o))return;return o["number"==typeof t?t:0]?.replace("/","")}},f={name:"subdomain",lookup(e){let{lookupFromSubdomainIndex:t}=e;const o="number"==typeof t?t+1:1,n="undefined"!=typeof window&&window.location?.hostname?.match(/^(\w{2,5})\.(([a-z0-9-]{1,63}\.[a-z]{2,6})|localhost)/i);if(n)return n[o]}};class m{constructor(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};this.type="languageDetector",this.detectors={},this.init(e,t)}init(o){let n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},a=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};this.services=o||{languageUtils:{}},this.options=function(o){return t.call(e.call(arguments,1),(e=>{if(e)for(const t in e)void 0===o[t]&&(o[t]=e[t])})),o}(n,this.options||{},{order:["querystring","cookie","localStorage","sessionStorage","navigator","htmlTag"],lookupQuerystring:"lng",lookupCookie:"i18next",lookupLocalStorage:"i18nextLng",lookupSessionStorage:"i18nextLng",caches:["localStorage"],excludeCacheFor:["cimode"],convertDetectedLanguage:e=>e}),"string"==typeof this.options.convertDetectedLanguage&&this.options.convertDetectedLanguage.indexOf("15897")>-1&&(this.options.convertDetectedLanguage=e=>e.replace("-","_")),this.options.lookupFromUrlIndex&&(this.options.lookupFromPathIndex=this.options.lookupFromUrlIndex),this.i18nOptions=a,this.addDetector(i),this.addDetector(r),this.addDetector(l),this.addDetector(d),this.addDetector(g),this.addDetector(h),this.addDetector(p),this.addDetector(f)}addDetector(e){return this.detectors[e.name]=e,this}detect(e){e||(e=this.options.order);let t=[];return e.forEach((e=>{if(this.detectors[e]){let o=this.detectors[e].lookup(this.options);o&&"string"==typeof o&&(o=[o]),o&&(t=t.concat(o))}})),t=t.map((e=>this.options.convertDetectedLanguage(e))),this.services.languageUtils.getBestMatchFromCodes?t:t.length>0?t[0]:null}cacheUserLanguage(e,t){t||(t=this.options.caches),t&&(this.options.excludeCacheFor&&this.options.excludeCacheFor.indexOf(e)>-1||t.forEach((t=>{this.detectors[t]&&this.detectors[t].cacheUserLanguage(e,this.options)})))}}return m.type="languageDetector",m})); |
{ | ||
"name": "i18next-browser-languagedetector", | ||
"version": "7.2.1", | ||
"version": "8.0.0", | ||
"description": "language detector used in browser environment for i18next", | ||
@@ -5,0 +5,0 @@ "main": "./dist/cjs/i18nextBrowserLanguageDetector.js", |
@@ -5,3 +5,3 @@ # Introduction | ||
This is a i18next language detection plugin use to detect user language in the browser with support for: | ||
This is an i18next language detection plugin used to detect user language in the browser, with support for: | ||
@@ -37,7 +37,12 @@ - cookie (set cookie i18next=LANGUAGE) | ||
i18next.use(LanguageDetector).init(i18nextOptions); | ||
i18next.use(LanguageDetector).init({ | ||
supportedLngs: ['de', 'en', 'fr'], | ||
...i18nextOptions | ||
}); | ||
``` | ||
As with all modules you can either pass the constructor function (class) to the i18next.use or a concrete instance. | ||
As with all modules you can either pass the constructor function (class) to the `i18next.use` or to a concrete instance. | ||
[`supportedLngs`](https://www.i18next.com/overview/configuration-options#languages-namespaces-resources) is optional, but allows i18next to pick the best match from the list of detected languages. If it's not set then [`language`](https://www.i18next.com/overview/api#language) will be set to the first detected language, regardless of whether your application has translations for that language or not. | ||
## Detector Options | ||
@@ -63,3 +68,3 @@ *The default options can be found [here](https://github.com/i18next/i18next-browser-languageDetector/blob/9efebe6ca0271c3797bc09b84babf1ba2d9b4dbb/src/index.js#L11).* | ||
// optional expire and domain for set cookie | ||
// optional expiry and domain for set cookie | ||
cookieMinutes: 10, | ||
@@ -74,3 +79,3 @@ cookieDomain: 'myDomain', | ||
// optional conversion function to use to modify the detected language code | ||
// optional conversion function used to modify the detected language code | ||
convertDetectedLanguage: 'Iso15897', | ||
@@ -77,0 +82,0 @@ convertDetectedLanguage: (lng) => lng.replace('-', '_') |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
166
80894
1608