-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
i18nextLocizeBackend.min.js
1 lines (1 loc) · 17.7 KB
/
i18nextLocizeBackend.min.js
1
!function(e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).i18nextLocizeBackend=e()}(function(){return function n(i,s,r){function a(t,e){if(!s[t]){if(!i[t]){var o="function"==typeof require&&require;if(!e&&o)return o(t,!0);if(c)return c(t,!0);throw(o=new Error("Cannot find module '"+t+"'")).code="MODULE_NOT_FOUND",o}o=s[t]={exports:{}},i[t][0].call(o.exports,function(e){return a(i[t][1][e]||e)},o,o.exports,n,i,s,r)}return s[t].exports}for(var c="function"==typeof require&&require,e=0;e<r.length;e++)a(r[e]);return a}({1:[function(o,n,i){!function(t){!function(){var e;"function"==typeof fetch&&(void 0!==t&&t.fetch?e=t.fetch:"undefined"!=typeof window&&window.fetch&&(e=window.fetch)),void 0===o||"undefined"!=typeof window&&void 0!==window.document||((e=e||o("cross-fetch")).default&&(e=e.default),i.default=e,n.exports=i.default)}.call(this)}.call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{"cross-fetch":5}],2:[function(e,t,o){"use strict";Object.defineProperty(o,"__esModule",{value:!0}),o.default=void 0;var d=e("./utils.js"),f=(e=e("./request.js"))&&e.__esModule?e:{default:e};function n(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)}}try{var c="undefined"!=typeof window&&null!==window.localStorage,i="notExistingLocizeProject";window.localStorage.setItem(i,"foo"),window.localStorage.removeItem(i)}catch(e){c=!1}var s,e=(e=r,(i=[{key:"init",value:function(e){var o,t,n=this,i=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},s=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{},r=3<arguments.length?arguments[3]:void 0;this.services=e,void 0!==i.whitelistThreshold&&void 0===i.translatedPercentageThreshold&&(e&&e.logger&&e.logger.deprecate("whitelistThreshold",'option "whitelistThreshold" will be renamed to "translatedPercentageThreshold" in the next major - please make sure to rename this option asap.'),i.translatedPercentageThreshold=i.whitelistThreshold),this.options=(0,d.defaults)(i,this.options||{},{loadPath:"https://api.locize.app/{{projectId}}/{{version}}/{{lng}}/{{ns}}",privatePath:"https://api.locize.app/private/{{projectId}}/{{version}}/{{lng}}/{{ns}}",getLanguagesPath:"https://api.locize.app/languages/{{projectId}}",addPath:"https://api.locize.app/missing/{{projectId}}/{{version}}/{{lng}}/{{ns}}",updatePath:"https://api.locize.app/update/{{projectId}}/{{version}}/{{lng}}/{{ns}}",referenceLng:"en",crossDomain:!0,setContentTypeJSON:!1,version:"latest",private:!1,translatedPercentageThreshold:.9,whitelistThreshold:.9,failLoadingOnEmptyJSON:!1,allowedAddOrUpdateHosts:["localhost"],onSaved:!1,reloadInterval:"undefined"==typeof window&&36e5,checkForProjectTimeout:3e3,storageExpiration:36e5,writeDebounce:5e3}),this.allOptions=s,this.somethingLoaded=!1,this.isProjectNotExisting=!1,this.storage=(o=this.options.storageExpiration,t=function(){},a=function(){},c?(t=function(e){window.localStorage.setItem("notExistingLocizeProject_".concat(e),Date.now())},a=function(e){var t=window.localStorage.getItem("notExistingLocizeProject_".concat(e));return!!t&&(!(Date.now()-t>o)||(window.localStorage.removeItem("notExistingLocizeProject_".concat(e)),!1))}):"undefined"!=typeof document&&(t=function(e){var t=new Date;t.setTime(t.getTime()+o);t="; expires=".concat(t.toGMTString()),e="notExistingLocizeProject_".concat(e);try{document.cookie="".concat(e,"=").concat(Date.now()).concat(t,";path=/")}catch(e){}},a=function(e){var e="notExistingLocizeProject_".concat(e),t="".concat(e,"=");try{for(var o=document.cookie.split(";"),n=0;n<o.length;n++){for(var i=o[n];" "===i.charAt(0);)i=i.substring(1,i.length);if(0===i.indexOf(t))return!0}}catch(e){}return!1}),{setProjectNotExisting:t,isProjectNotExisting:a}),this.options.pull&&console.warn('The pull API was removed use "private: true" option instead: https://docs.locize.com/integration/api#fetch-private-namespace-resources');var a="undefined"!=typeof window&&window.location&&window.location.hostname;a?(this.isAddOrUpdateAllowed="function"==typeof this.options.allowedAddOrUpdateHosts?this.options.allowedAddOrUpdateHosts(a):-1<this.options.allowedAddOrUpdateHosts.indexOf(a),e&&e.logger&&(s.saveMissing||s.updateMissing)&&(this.isAddOrUpdateAllowed?"localhost"!==a&&e.logger.warn('locize-backend: you are using the save or update missings feature from this host "'.concat(a,'".\nMake sure you will not use it in production!\nhttps://docs.locize.com/guides-tips-and-tricks/going-production')):e.logger.warn("function"==typeof this.options.allowedAddOrUpdateHosts?'locize-backend: will not save or update missings because allowedAddOrUpdateHosts returned false for the host "'.concat(a,'".'):'locize-backend: will not save or update missings because the host "'.concat(a,'" was not in the list of allowedAddOrUpdateHosts: ').concat(this.options.allowedAddOrUpdateHosts.join(", ")," (matches need to be exact).")))):this.isAddOrUpdateAllowed=!0,"function"==typeof r&&this.getOptions(function(e,t,o){return e?r(e):(n.options.referenceLng=i.referenceLng||t.referenceLng||n.options.referenceLng,void r(null,t,o))}),this.queuedWrites={pending:{}},this.debouncedProcess=(0,d.debounce)(this.process,this.options.writeDebounce),this.interval&&clearInterval(this.interval),this.options.reloadInterval&&this.options.projectId&&(this.interval=setInterval(function(){return n.reload()},this.options.reloadInterval))}},{key:"reload",value:function(){var e,t,o=this,n=this.services||{logger:console},i=n.backendConnector,s=n.languageUtils,r=n.logger;i&&((e=i.language)&&"cimode"===e.toLowerCase()||(t=[],(n=function(e){s.toResolveHierarchy(e).forEach(function(e){t.indexOf(e)<0&&t.push(e)})})(e),this.allOptions.preload&&this.allOptions.preload.forEach(n),t.forEach(function(n){o.allOptions.ns.forEach(function(o){i.read(n,o,"read",null,null,function(e,t){e&&r.warn("loading namespace ".concat(o," for language ").concat(n," failed"),e),!e&&t&&r.log("loaded namespace ".concat(o," for language ").concat(n),t),i.loaded("".concat(n,"|").concat(o),e,t)})})})))}},{key:"getLanguages",value:function(n){var i=this,e=(0,d.isMissingOption)(this.options,["projectId"]);if(e)return n(new Error(e));e=(0,d.interpolate)(this.options.getLanguagesPath,{projectId:this.options.projectId});if(!this.isProjectNotExisting&&this.storage.isProjectNotExisting(this.options.projectId)&&(this.isProjectNotExisting=!0),this.isProjectNotExisting)return n(new Error("locize project ".concat(this.options.projectId," does not exist!")));this.loadUrl({},e,function(e,t,o){return!i.somethingLoaded&&o&&o.resourceNotExisting?(i.isProjectNotExisting=!0,i.storage.setProjectNotExisting(i.options.projectId),n(new Error("locize project ".concat(i.options.projectId," does not exist!")))):(i.somethingLoaded=!0,void n(e,t))})}},{key:"getOptions",value:function(i){var s=this;this.getLanguages(function(e,n){if(e)return i(e);var t=Object.keys(n);if(!t.length)return i(new Error("was unable to load languages via API"));var o=t.reduce(function(e,t){return e=n[t].isReferenceLanguage?t:e},""),e=t.reduce(function(e,t){var o=n[t];return o.translated[s.options.version]&&o.translated[s.options.version]>=s.options.translatedPercentageThreshold&&e.push(t),e},[]),t=t.reduce(function(e,t){return-1<t.indexOf("-")||e},!1);i(null,{fallbackLng:o,referenceLng:o,supportedLngs:e,whitelist:e,load:t?"all":"languageOnly"},n)})}},{key:"checkIfProjectExists",value:function(t){var e=this,o=(this.services||{logger:console}).logger;this.somethingLoaded?t&&t(null):this.alreadyRequestedCheckIfProjectExists?setTimeout(function(){return e.checkIfProjectExists(t)},this.options.checkForProjectTimeout):(this.alreadyRequestedCheckIfProjectExists=!0,this.getLanguages(function(e){e&&e.message&&0<e.message.indexOf("does not exist")&&o&&o.error(e.message),t&&t(e)}))}},{key:"read",value:function(e,t,n){var o,i=this,s=(this.services||{logger:console}).logger,r={};if(this.options.private){var a=(0,d.isMissingOption)(this.options,["projectId","version","apiKey"]);if(a)return n(new Error(a),!1);o=(0,d.interpolate)(this.options.privatePath,{lng:e,ns:t,projectId:this.options.projectId,version:this.options.version}),r={authorize:!0}}else{a=(0,d.isMissingOption)(this.options,["projectId","version"]);if(a)return n(new Error(a),!1);o=(0,d.interpolate)(this.options.loadPath,{lng:e,ns:t,projectId:this.options.projectId,version:this.options.version})}if(!this.isProjectNotExisting&&this.storage.isProjectNotExisting(this.options.projectId)&&(this.isProjectNotExisting=!0),this.isProjectNotExisting){t=new Error("locize project ".concat(this.options.projectId," does not exist!"));return s&&s.error(t.message),void(n&&n(t))}this.loadUrl(r,o,function(e,t,o){i.somethingLoaded||(o&&o.resourceNotExisting?setTimeout(function(){return i.checkIfProjectExists()},i.options.checkForProjectTimeout):i.somethingLoaded=!0),n(e,t)})}},{key:"loadUrl",value:function(e,s,t,r){var a=this;e=(0,d.defaults)(e,this.options),"function"==typeof t&&(r=t,t=void 0),r=r||function(){},(0,f.default)(e,s,t,function(e,t){var o,n,i=t&&t.resourceNotExisting;if(t&&(408===t.status||400===t.status))return r("failed loading "+s,!0,{resourceNotExisting:i});if(t&&(500<=t.status&&t.status<600||!t.status))return r("failed loading "+s,!0,{resourceNotExisting:i});if(t&&400<=t.status&&t.status<500)return r("failed loading "+s,!1,{resourceNotExisting:i});if(!t&&e&&e.message&&-1<e.message.indexOf("Failed to fetch"))return r("failed loading "+s,!0,{resourceNotExisting:i});if(e)return r(e,!1);try{o=JSON.parse(t.data)}catch(e){n="failed parsing "+s+" to json"}return n?r(n,!1):a.options.failLoadingOnEmptyJSON&&!Object.keys(o).length?r("loaded result empty for "+s,!1,{resourceNotExisting:i}):void r(null,o,{resourceNotExisting:i})})}},{key:"create",value:function(t,o,n,i,s,r){var a=this;s=s||function(){},this.checkIfProjectExists(function(e){if(e)return s(e);e=(0,d.isMissingOption)(a.options,["projectId","version","apiKey","referenceLng"]);return e?s(new Error(e)):a.isAddOrUpdateAllowed?((t="string"==typeof t?[t]:t).filter(function(e){return e===a.options.referenceLng}).length<1&&a.services&&a.services.logger&&a.services.logger.warn('locize-backend: will not save missings because the reference language "'.concat(a.options.referenceLng,'" was not in the list of to save languages: ').concat(t.join(", ")," (open your site in the reference language to save missings).")),void t.forEach(function(e){e===a.options.referenceLng&&a.queue.call(a,a.options.referenceLng,o,n,i,s,r)})):s("host is not allowed to create key.")})}},{key:"update",value:function(t,o,n,i,s,r){var a=this;s=s||function(){},this.checkIfProjectExists(function(e){if(e)return s(e);e=(0,d.isMissingOption)(a.options,["projectId","version","apiKey","referenceLng"]);return e?s(new Error(e)):a.isAddOrUpdateAllowed?("string"==typeof t&&(t=[t]),(r=r||{}).isUpdate=!0,void t.forEach(function(e){e===a.options.referenceLng&&a.queue.call(a,a.options.referenceLng,o,n,i,s,r)})):s("host is not allowed to update key.")})}},{key:"writePage",value:function(e,t,o,n){var i=(0,d.interpolate)(this.options.addPath,{lng:e,ns:t,projectId:this.options.projectId,version:this.options.version}),t=(0,d.interpolate)(this.options.updatePath,{lng:e,ns:t,projectId:this.options.projectId,version:this.options.version}),s=!1,r=!1,a={},c={};o.forEach(function(e){var t=e.options&&e.options.tDescription?{value:e.fallbackValue||"",context:{text:e.options.tDescription}}:e.fallbackValue||"";e.options&&e.options.isUpdate?(r=r||!0,c[e.key]=t):(s=s||!0,a[e.key]=t)});var u=0;s&&u++,r&&u++;function l(e){--u||n(e)}u||l(),s&&(0,f.default)((0,d.defaults)({authorize:!0},this.options),i,a,l),r&&(0,f.default)((0,d.defaults)({authorize:!0},this.options),t,c,l)}},{key:"write",value:function(i,s){var r,a,c=this;(0,d.getPath)(this.queuedWrites,["locks",i,s])||(r=(0,d.getPath)(this.queuedWrites,[i,s]),(0,d.setPath)(this.queuedWrites,[i,s],[]),a=r.filter(function(e){return e.callback}).map(function(e){return e.callback}),r.length&&function(){(0,d.setPath)(c.queuedWrites,["locks",i,s],!0);function e(){(0,d.setPath)(c.queuedWrites,["locks",i,s],!1),a.forEach(function(e){return e()}),c.options.onSaved&&c.options.onSaved(i,s),c.debouncedProcess(i,s)}var t=r.length/1e3,o=0,n=r.splice(0,1e3);for(c.writePage(i,s,n,function(){t<=++o&&e()});1e3===n.length;)(n=r.splice(0,1e3)).length&&c.writePage(i,s,n,function(){t<=++o&&e()})}())}},{key:"process",value:function(){var o=this;Object.keys(this.queuedWrites).forEach(function(t){"locks"!==t&&Object.keys(o.queuedWrites[t]).forEach(function(e){o.queuedWrites[t][e].length&&o.write(t,e)})})}},{key:"queue",value:function(e,t,o,n,i,s){(0,d.pushPath)(this.queuedWrites,[e,t],{key:o,fallbackValue:n||"",callback:i,options:s}),this.debouncedProcess()}}])&&n(e.prototype,i),s&&n(e,s),Object.defineProperty(e,"prototype",{writable:!1}),r);function r(e){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},o=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{},n=3<arguments.length?arguments[3]:void 0;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,r),this.services=e,this.options=t,this.allOptions=o,this.type="backend",e&&e.projectId?this.init(null,e,o,t):this.init(e,t,o,n)}e.type="backend",o.default=e,t.exports=o.default},{"./request.js":3,"./utils.js":4}],3:[function(o,n,i){!function(t){!function(){"use strict";Object.defineProperty(i,"__esModule",{value:!0}),i.default=void 0;var a,c,u,e=function(e,t){if(!t&&e&&e.__esModule)return e;if(null===e||"object"!==l(e)&&"function"!=typeof e)return{default:e};t=r(t);if(t&&t.has(e))return t.get(e);var o,n={},i=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(o in e){var s;"default"!==o&&Object.prototype.hasOwnProperty.call(e,o)&&((s=i?Object.getOwnPropertyDescriptor(e,o):null)&&(s.get||s.set)?Object.defineProperty(n,o,s):n[o]=e[o])}n.default=e,t&&t.set(e,n);return n}(o("./getFetch.js"));function r(e){if("function"!=typeof WeakMap)return null;var t=new WeakMap,o=new WeakMap;return(r=function(e){return e?o:t})(e)}function l(e){return(l="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})(e)}"function"==typeof fetch&&(void 0!==t&&t.fetch?a=t.fetch:"undefined"!=typeof window&&window.fetch&&(a=window.fetch)),"function"!=typeof XMLHttpRequest&&"object"!==("undefined"==typeof XMLHttpRequest?"undefined":l(XMLHttpRequest))||(void 0!==t&&t.XMLHttpRequest?c=t.XMLHttpRequest:"undefined"!=typeof window&&window.XMLHttpRequest&&(c=window.XMLHttpRequest)),"function"==typeof ActiveXObject&&(void 0!==t&&t.ActiveXObject?u=t.ActiveXObject:"undefined"!=typeof window&&window.ActiveXObject&&(u=window.ActiveXObject)),"function"!=typeof(a=!a&&e&&!c&&!u?e.default||e:a)&&(a=void 0);i.default=function(e,t,o,n){return"function"==typeof o&&(n=o,o=void 0),n=n||function(){},a?(i=e,r=n,void a(t,{method:(s=o)?"POST":"GET",body:s?JSON.stringify(s):void 0,headers:{Authorization:i.authorize&&i.apiKey?i.apiKey:void 0,"Content-Type":"application/json"}}).then(function(t){var o=t.headers&&"Error from cloudfront"===t.headers.get("x-cache");if(!t.ok)return r(t.statusText||"Error",{status:t.status,resourceNotExisting:o});t.text().then(function(e){r(null,{status:t.status,data:e,resourceNotExisting:o})}).catch(r)}).catch(r)):"function"==typeof XMLHttpRequest||"object"===("undefined"==typeof XMLHttpRequest?"undefined":l(XMLHttpRequest))||"function"==typeof ActiveXObject?function(e,t,o,n){try{var i=c?new c:new u("MSXML2.XMLHTTP.3.0");i.open(o?"POST":"GET",t,1),e.crossDomain||i.setRequestHeader("X-Requested-With","XMLHttpRequest"),e.authorize&&e.apiKey&&i.setRequestHeader("Authorization",e.apiKey),(o||e.setContentTypeJSON)&&i.setRequestHeader("Content-Type","application/json"),i.onreadystatechange=function(){var e="Error from cloudfront"===i.getResponseHeader("x-cache");3<i.readyState&&n(400<=i.status?i.statusText:null,{status:i.status,data:i.responseText,resourceNotExisting:e})},i.send(JSON.stringify(o))}catch(e){console&&console.log(e)}}(e,t,o,n):void 0;var i,s,r},n.exports=i.default}.call(this)}.call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{"./getFetch.js":1}],4:[function(e,t,o){"use strict";Object.defineProperty(o,"__esModule",{value:!0}),o.debounce=function(n,i,s){var r;return function(){var e=this,t=arguments,o=s&&!r;clearTimeout(r),r=setTimeout(function(){r=null,s||n.apply(e,t)},i),o&&n.apply(e,t)}},o.defaults=function(o){return n.call(i.call(arguments,1),function(e){if(e)for(var t in e)void 0===o[t]&&(o[t]=e[t])}),o},o.getPath=function(e,t){e=s(e,t),t=e.obj,e=e.k;return t?t[e]:void 0},o.interpolate=function(e,t,o){var n,i;for(;n=r.exec(e);)i=(i=(i="string"!=typeof(i=n[1].trim())?function(e){return null==e?"":""+e}(i):i)||"").replace(/\$/g,"$$$$"),e=e.replace(n[0],t[i]||i),r.lastIndex=0;return e},o.isMissingOption=a,o.optionExist=function(e,t){return!a(e,t)},o.pushPath=function(e,t,o,n){e=s(e,t,Object),t=e.obj,e=e.k;t[e]=t[e]||[],n&&(t[e]=t[e].concat(o));n||t[e].push(o)},o.setPath=function(e,t,o){e=s(e,t,Object),t=e.obj,e=e.k;t[e]=o};var o=[],n=o.forEach,i=o.slice;function s(e,t,o){function n(e){return e&&-1<e.indexOf("###")?e.replace(/###/g,"."):e}for(var i="string"!=typeof t?[].concat(t):t.split(".");1<i.length;){if(!e)return{};var s=n(i.shift());!e[s]&&o&&(e[s]=new o),e=e[s]}return e?{obj:e,k:n(i.shift())}:{}}var r=new RegExp("{{(.+?)}}","g");function a(o,e){return e.reduce(function(e,t){if(e)return e;if(o&&o[t]&&"string"==typeof o[t]&&!o[t].toLowerCase()!==t.toLowerCase())return!1;t='i18next-locize-backend :: got "'.concat(o[t],'" in options for ').concat(t," which is invalid.");return console.warn(t),t},!1)}},{}],5:[function(e,t,o){},{}]},{},[2])(2)});