i18next-browser-languagedetector-async
Advanced tools
Comparing version
@@ -208,2 +208,4 @@ (function (global, factory) { | ||
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -267,37 +269,133 @@ | ||
key: 'detect', | ||
value: function detect(detectionOrder) { | ||
var _this = this; | ||
value: function () { | ||
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(detectionOrder) { | ||
var _this = this; | ||
if (!detectionOrder) detectionOrder = this.options.order; | ||
var detected, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, detectorName, lookup, found, fallbacks; | ||
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); | ||
} | ||
}); | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
if (!detectionOrder) detectionOrder = this.options.order; | ||
var found = void 0; | ||
detected.forEach(function (lng) { | ||
if (found) return; | ||
var cleanedLng = _this.services.languageUtils.formatLanguageCode(lng); | ||
if (_this.services.languageUtils.isWhitelisted(cleanedLng)) found = cleanedLng; | ||
}); | ||
detected = []; | ||
_iteratorNormalCompletion = true; | ||
_didIteratorError = false; | ||
_iteratorError = undefined; | ||
_context.prev = 5; | ||
_iterator = detectionOrder[Symbol.iterator](); | ||
if (!found) { | ||
var fallbacks = this.i18nOptions.fallbackLng; | ||
if (typeof fallbacks === 'string') fallbacks = [fallbacks]; | ||
if (!fallbacks) fallbacks = []; | ||
case 7: | ||
if (_iteratorNormalCompletion = (_step = _iterator.next()).done) { | ||
_context.next = 23; | ||
break; | ||
} | ||
if (Object.prototype.toString.apply(fallbacks) === '[object Array]') { | ||
found = fallbacks[0]; | ||
} else { | ||
found = fallbacks[0] || fallbacks.default && fallbacks.default[0]; | ||
} | ||
}; | ||
detectorName = _step.value; | ||
return found; | ||
} | ||
if (!this.detectors[detectorName]) { | ||
_context.next = 20; | ||
break; | ||
} | ||
lookup = void 0; | ||
if (!this.detectors[detectorName].async) { | ||
_context.next = 17; | ||
break; | ||
} | ||
_context.next = 14; | ||
return this.detectors[detectorName].lookup(this.options); | ||
case 14: | ||
lookup = _context.sent; | ||
_context.next = 18; | ||
break; | ||
case 17: | ||
lookup = this.detectors[detectorName].lookup(this.options); | ||
case 18: | ||
if (lookup && typeof lookup === 'string') lookup = [lookup]; | ||
if (lookup) detected = detected.concat(lookup); | ||
case 20: | ||
_iteratorNormalCompletion = true; | ||
_context.next = 7; | ||
break; | ||
case 23: | ||
_context.next = 29; | ||
break; | ||
case 25: | ||
_context.prev = 25; | ||
_context.t0 = _context['catch'](5); | ||
_didIteratorError = true; | ||
_iteratorError = _context.t0; | ||
case 29: | ||
_context.prev = 29; | ||
_context.prev = 30; | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
case 32: | ||
_context.prev = 32; | ||
if (!_didIteratorError) { | ||
_context.next = 35; | ||
break; | ||
} | ||
throw _iteratorError; | ||
case 35: | ||
return _context.finish(32); | ||
case 36: | ||
return _context.finish(29); | ||
case 37: | ||
found = void 0; | ||
detected.forEach(function (lng) { | ||
if (found) return; | ||
var cleanedLng = _this.services.languageUtils.formatLanguageCode(lng); | ||
if (_this.services.languageUtils.isWhitelisted(cleanedLng)) found = cleanedLng; | ||
}); | ||
if (!found) { | ||
fallbacks = this.i18nOptions.fallbackLng; | ||
if (typeof fallbacks === 'string') fallbacks = [fallbacks]; | ||
if (!fallbacks) fallbacks = []; | ||
if (Object.prototype.toString.apply(fallbacks) === '[object Array]') { | ||
found = fallbacks[0]; | ||
} else { | ||
found = fallbacks[0] || fallbacks.default && fallbacks.default[0]; | ||
} | ||
}; | ||
return _context.abrupt('return', found); | ||
case 42: | ||
case 'end': | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, this, [[5, 25, 29, 37], [30,, 32, 36]]); | ||
})); | ||
function detect(_x4) { | ||
return _ref.apply(this, arguments); | ||
} | ||
return detect; | ||
}() | ||
}, { | ||
@@ -304,0 +402,0 @@ key: 'cacheUserLanguage', |
@@ -1,1 +0,1 @@ | ||
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?module.exports=o():"function"==typeof define&&define.amd?define(o):e.i18nextBrowserLanguageDetector=o()}(this,function(){"use strict";function e(e){return a.call(i.call(arguments,1),function(o){if(o)for(var t in o)void 0===e[t]&&(e[t]=o[t])}),e}function o(e,o){if(!(e instanceof o))throw new TypeError("Cannot call a class as a function")}function t(){return{order:["querystring","cookie","localStorage","navigator","htmlTag"],lookupQuerystring:"lng",lookupCookie:"i18next",lookupLocalStorage:"i18nextLng",caches:["localStorage"],excludeCacheFor:["cimode"]}}var n=[],a=n.forEach,i=n.slice,r={create:function(e,o,t,n){var a=void 0;if(t){var i=new Date;i.setTime(i.getTime()+60*t*1e3),a="; expires="+i.toGMTString()}else a="";n=n?"domain="+n+";":"",document.cookie=e+"="+o+a+";"+n+"path=/"},read:function(e){for(var o=e+"=",t=document.cookie.split(";"),n=0;n<t.length;n++){for(var a=t[n];" "===a.charAt(0);)a=a.substring(1,a.length);if(0===a.indexOf(o))return a.substring(o.length,a.length)}return null},remove:function(e){this.create(e,"",-1)}},u={name:"cookie",lookup:function(e){var o=void 0;if(e.lookupCookie&&"undefined"!=typeof document){var t=r.read(e.lookupCookie);t&&(o=t)}return o},cacheUserLanguage:function(e,o){o.lookupCookie&&"undefined"!=typeof document&&r.create(o.lookupCookie,e,o.cookieMinutes,o.cookieDomain)}},c={name:"querystring",lookup:function(e){var o=void 0;if("undefined"!=typeof window)for(var t=window.location.search.substring(1),n=t.split("&"),a=0;a<n.length;a++){var i=n[a].indexOf("=");if(i>0){var r=n[a].substring(0,i);r===e.lookupQuerystring&&(o=n[a].substring(i+1))}}return o}},l=void 0;try{l="undefined"!==window&&null!==window.localStorage;window.localStorage.setItem("i18next.translate.boo","foo"),window.localStorage.removeItem("i18next.translate.boo")}catch(e){l=!1}var s={name:"localStorage",lookup:function(e){var o=void 0;if(e.lookupLocalStorage&&l){var t=window.localStorage.getItem(e.lookupLocalStorage);t&&(o=t)}return o},cacheUserLanguage:function(e,o){o.lookupLocalStorage&&l&&window.localStorage.setItem(o.lookupLocalStorage,e)}},d={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}},f={name:"htmlTag",lookup:function(e){var o=void 0,t=e.htmlTag||("undefined"!=typeof document?document.documentElement:null);return t&&"function"==typeof t.getAttribute&&(o=t.getAttribute("lang")),o}},g={name:"path",lookup:function(e){var o=void 0;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}},p={name:"subdomain",lookup:function(e){var o=void 0;if("undefined"!=typeof window){var t=window.location.href.match(/(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})/gi);t instanceof Array&&(o="number"==typeof e.lookupFromSubdomainIndex?t[e.lookupFromSubdomainIndex].replace("http://","").replace("https://","").replace(".",""):t[0].replace("http://","").replace("https://","").replace(".",""))}return o}},h=function(){function e(e,o){for(var t=0;t<o.length;t++){var n=o[t];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(o,t,n){return t&&e(o.prototype,t),n&&e(o,n),o}}(),v=function(){function n(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};o(this,n),this.type="languageDetector",this.detectors={},this.init(e,t)}return h(n,[{key:"init",value:function(o){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},a=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};this.services=o,this.options=e(n,this.options||{},t()),this.options.lookupFromUrlIndex&&(this.options.lookupFromPathIndex=this.options.lookupFromUrlIndex),this.i18nOptions=a,this.addDetector(u),this.addDetector(c),this.addDetector(s),this.addDetector(d),this.addDetector(f),this.addDetector(g),this.addDetector(p)}},{key:"addDetector",value:function(e){this.detectors[e.name]=e}},{key:"detect",value:function(e){var o=this;e||(e=this.options.order);var t=[];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))}});var n=void 0;if(t.forEach(function(e){if(!n){var t=o.services.languageUtils.formatLanguageCode(e);o.services.languageUtils.isWhitelisted(t)&&(n=t)}}),!n){var a=this.i18nOptions.fallbackLng;"string"==typeof a&&(a=[a]),a||(a=[]),n="[object Array]"===Object.prototype.toString.apply(a)?a[0]:a[0]||a.default&&a.default[0]}return n}},{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)}))}}]),n}();return v.type="languageDetector",v}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.i18nextBrowserLanguageDetector=t()}(this,function(){"use strict";function e(e){return a.call(i.call(arguments,1),function(t){if(t)for(var o in t)void 0===e[o]&&(e[o]=t[o])}),e}function t(e){return function(){var t=e.apply(this,arguments);return new Promise(function(e,o){function n(r,a){try{var i=t[r](a),u=i.value}catch(e){return void o(e)}if(!i.done)return Promise.resolve(u).then(function(e){n("next",e)},function(e){n("throw",e)});e(u)}return n("next")})}}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function n(){return{order:["querystring","cookie","localStorage","navigator","htmlTag"],lookupQuerystring:"lng",lookupCookie:"i18next",lookupLocalStorage:"i18nextLng",caches:["localStorage"],excludeCacheFor:["cimode"]}}var r=[],a=r.forEach,i=r.slice,u={create:function(e,t,o,n){var r=void 0;if(o){var a=new Date;a.setTime(a.getTime()+60*o*1e3),r="; expires="+a.toGMTString()}else r="";n=n?"domain="+n+";":"",document.cookie=e+"="+t+r+";"+n+"path=/"},read:function(e){for(var t=e+"=",o=document.cookie.split(";"),n=0;n<o.length;n++){for(var r=o[n];" "===r.charAt(0);)r=r.substring(1,r.length);if(0===r.indexOf(t))return r.substring(t.length,r.length)}return null},remove:function(e){this.create(e,"",-1)}},c={name:"cookie",lookup:function(e){var t=void 0;if(e.lookupCookie&&"undefined"!=typeof document){var o=u.read(e.lookupCookie);o&&(t=o)}return t},cacheUserLanguage:function(e,t){t.lookupCookie&&"undefined"!=typeof document&&u.create(t.lookupCookie,e,t.cookieMinutes,t.cookieDomain)}},s={name:"querystring",lookup:function(e){var t=void 0;if("undefined"!=typeof window)for(var o=window.location.search.substring(1),n=o.split("&"),r=0;r<n.length;r++){var a=n[r].indexOf("=");if(a>0){var i=n[r].substring(0,a);i===e.lookupQuerystring&&(t=n[r].substring(a+1))}}return t}},l=void 0;try{l="undefined"!==window&&null!==window.localStorage;window.localStorage.setItem("i18next.translate.boo","foo"),window.localStorage.removeItem("i18next.translate.boo")}catch(e){l=!1}var d={name:"localStorage",lookup:function(e){var t=void 0;if(e.lookupLocalStorage&&l){var o=window.localStorage.getItem(e.lookupLocalStorage);o&&(t=o)}return t},cacheUserLanguage:function(e,t){t.lookupLocalStorage&&l&&window.localStorage.setItem(t.lookupLocalStorage,e)}},f={name:"navigator",lookup:function(e){var t=[];if("undefined"!=typeof navigator){if(navigator.languages)for(var o=0;o<navigator.languages.length;o++)t.push(navigator.languages[o]);navigator.userLanguage&&t.push(navigator.userLanguage),navigator.language&&t.push(navigator.language)}return t.length>0?t:void 0}},p={name:"htmlTag",lookup:function(e){var t=void 0,o=e.htmlTag||("undefined"!=typeof document?document.documentElement:null);return o&&"function"==typeof o.getAttribute&&(t=o.getAttribute("lang")),t}},g={name:"path",lookup:function(e){var t=void 0;if("undefined"!=typeof window){var o=window.location.pathname.match(/\/([a-zA-Z-]*)/g);if(o instanceof Array)if("number"==typeof e.lookupFromPathIndex){if("string"!=typeof o[e.lookupFromPathIndex])return;t=o[e.lookupFromPathIndex].replace("/","")}else t=o[0].replace("/","")}return t}},h={name:"subdomain",lookup:function(e){var t=void 0;if("undefined"!=typeof window){var o=window.location.href.match(/(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})/gi);o instanceof Array&&(t="number"==typeof e.lookupFromSubdomainIndex?o[e.lookupFromSubdomainIndex].replace("http://","").replace("https://","").replace(".",""):o[0].replace("http://","").replace("https://","").replace(".",""))}return t}},v=function(){function e(e,t){for(var o=0;o<t.length;o++){var n=t[o];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,o,n){return o&&e(t.prototype,o),n&&e(t,n),t}}(),m=function(){function r(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};o(this,r),this.type="languageDetector",this.detectors={},this.init(e,t)}return v(r,[{key:"init",value:function(t){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};this.services=t,this.options=e(o,this.options||{},n()),this.options.lookupFromUrlIndex&&(this.options.lookupFromPathIndex=this.options.lookupFromUrlIndex),this.i18nOptions=r,this.addDetector(c),this.addDetector(s),this.addDetector(d),this.addDetector(f),this.addDetector(p),this.addDetector(g),this.addDetector(h)}},{key:"addDetector",value:function(e){this.detectors[e.name]=e}},{key:"detect",value:function(){function e(e){return o.apply(this,arguments)}var o=t(regeneratorRuntime.mark(function e(t){var o,n,r,a,i,u,c,s,l,d,f=this;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:t||(t=this.options.order),o=[],n=!0,r=!1,a=void 0,e.prev=5,i=t[Symbol.iterator]();case 7:if(n=(u=i.next()).done){e.next=23;break}if(c=u.value,!this.detectors[c]){e.next=20;break}if(s=void 0,!this.detectors[c].async){e.next=17;break}return e.next=14,this.detectors[c].lookup(this.options);case 14:s=e.sent,e.next=18;break;case 17:s=this.detectors[c].lookup(this.options);case 18:s&&"string"==typeof s&&(s=[s]),s&&(o=o.concat(s));case 20:n=!0,e.next=7;break;case 23:e.next=29;break;case 25:e.prev=25,e.t0=e.catch(5),r=!0,a=e.t0;case 29:e.prev=29,e.prev=30,!n&&i.return&&i.return();case 32:if(e.prev=32,!r){e.next=35;break}throw a;case 35:return e.finish(32);case 36:return e.finish(29);case 37:return l=void 0,o.forEach(function(e){if(!l){var t=f.services.languageUtils.formatLanguageCode(e);f.services.languageUtils.isWhitelisted(t)&&(l=t)}}),l||(d=this.i18nOptions.fallbackLng,"string"==typeof d&&(d=[d]),d||(d=[]),l="[object Array]"===Object.prototype.toString.apply(d)?d[0]:d[0]||d.default&&d.default[0]),e.abrupt("return",l);case 42:case"end":return e.stop()}},e,this,[[5,25,29,37],[30,,32,36]])}));return e}()},{key:"cacheUserLanguage",value:function(e,t){var o=this;t||(t=this.options.caches),t&&(this.options.excludeCacheFor&&this.options.excludeCacheFor.indexOf(e)>-1||t.forEach(function(t){o.detectors[t]&&o.detectors[t].cacheUserLanguage(e,o.options)}))}}]),r}();return m.type="languageDetector",m}); |
{ | ||
"name": "i18next-browser-languagedetector-async", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "language detector used in browser environment for i18next", | ||
@@ -58,4 +58,4 @@ "main": "./index.js", | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.org/" | ||
"registry": "https://registry.npmjs.org/" | ||
} | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
80260
190.59%31
181.82%1528
306.38%1
Infinity%