-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18nextServiceBackend.min.js
1 lines (1 loc) · 4.9 KB
/
i18nextServiceBackend.min.js
1
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.i18nextServiceBackend=t()}(this,function(){"use strict";function e(e,t,n){var o;return function(){var i=this,s=arguments,r=function(){o=null,n||e.apply(i,s)},a=n&&!o;clearTimeout(o),o=setTimeout(r,t),a&&e.apply(i,s)}}function t(e,t,n){function o(e){return e&&e.indexOf("###")>-1?e.replace(/###/g,"."):e}for(var i="string"!=typeof t?[].concat(t):t.split(".");i.length>1;){if(!e)return{};var s=o(i.shift());!e[s]&&n&&(e[s]=new n),e=e[s]}return e?{obj:e,k:o(i.shift())}:{}}function n(e,n,o){var i=t(e,n,Object);i.obj[i.k]=o}function o(e,n,o,i){var s=t(e,n,Object),r=s.obj,a=s.k;r[a]=r[a]||[],i&&(r[a]=r[a].concat(o)),i||r[a].push(o)}function i(e,n){var o=t(e,n),i=o.obj,s=o.k;if(i)return i[s]}function s(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function r(e,t,n,o,i){try{var s=new(XMLHttpRequest||ActiveXObject)("MSXML2.XMLHTTP.3.0");s.open(o?"POST":"GET",e,1),t.crossDomain||s.setRequestHeader("X-Requested-With","XMLHttpRequest"),t.authorize&&t.apiKey&&s.setRequestHeader("Authorization",t.apiKey),(o||t.setContentTypeJSON)&&s.setRequestHeader("Content-type","application/json"),s.onreadystatechange=function(){s.readyState>3&&n&&n(s.responseText,s)},s.send(JSON.stringify(o))}catch(e){window.console&&console.log(e)}}function a(e){if(e)return{loadPath:e+"/{{projectId}}/{{version}}/{{lng}}/{{ns}}",getLanguagesPath:e+"/languages/{{projectId}}",addPath:e+"/missing/{{projectId}}/{{version}}/{{lng}}/{{ns}}",updatePath:e+"/update/{{projectId}}/{{version}}/{{lng}}/{{ns}}",referenceLng:"en",crossDomain:!0,setContentTypeJSON:!1,version:"latest"}}var c=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o])}return e},u=function(){function e(e,t){for(var n=0;n<t.length;n++){var o=t[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}return function(t,n,o){return n&&e(t.prototype,n),o&&e(t,o),t}}(),l=function(){function t(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};s(this,t),this.init(e,n),this.type="backend"}return u(t,[{key:"init",value:function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};this.services=t,this.options=c({},a(n.service),this.options,n),this.queuedWrites={},this.debouncedProcess=e(this.process,1e4)}},{key:"getLanguages",value:function(e){var t=this.services.interpolator.interpolate(this.options.getLanguagesPath,{projectId:this.options.projectId});this.loadUrl(t,e)}},{key:"read",value:function(e,t,n){var o=this.services.interpolator.interpolate(this.options.loadPath,{lng:e,ns:t,projectId:this.options.projectId,version:this.options.version});this.loadUrl(o,n)}},{key:"loadUrl",value:function(e,t){r(e,this.options,function(n,o){if(o.status>=500&&o.status<600)return t("failed loading "+e,!0);if(o.status>=400&&o.status<500)return t("failed loading "+e,!1);var i=void 0,s=void 0;try{i=JSON.parse(n)}catch(t){s="failed parsing "+e+" to json"}if(s)return t(s,!1);t(null,i)})}},{key:"create",value:function(e,t,n,o,i,s){var r=this;i||(i=function(){}),"string"==typeof e&&(e=[e]),e.forEach(function(e){e===r.options.referenceLng&&r.queue.call(r,r.options.referenceLng,t,n,o,i,s)})}},{key:"update",value:function(e,t,n,o,i,s){var r=this;i||(i=function(){}),s||(s={}),"string"==typeof e&&(e=[e]),s.isUpdate=!0,e.forEach(function(e){e===r.options.referenceLng&&r.queue.call(r,r.options.referenceLng,t,n,o,i,s)})}},{key:"write",value:function(e,t){var o=this;if(!i(this.queuedWrites,["locks",e,t])){var s=this.services.interpolator.interpolate(this.options.addPath,{lng:e,ns:t,projectId:this.options.projectId,version:this.options.version}),a=this.services.interpolator.interpolate(this.options.updatePath,{lng:e,ns:t,projectId:this.options.projectId,version:this.options.version}),u=i(this.queuedWrites,[e,t]);if(n(this.queuedWrites,[e,t],[]),u.length){n(this.queuedWrites,["locks",e,t],!0);var l=!1,p=!1,f={},h={};u.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?(p||(p=!0),h[e.key]=t):(l||(l=!0),f[e.key]=t)});var d=0;l&&d++,p&&d++;var v=function(){--d||(n(o.queuedWrites,["locks",e,t],!1),u.forEach(function(e){e.callback&&e.callback()}),o.debouncedProcess(e,t))};d||v(),l&&r(s,c({authorize:!0},this.options),function(e,t){v()},f),p&&r(a,c({authorize:!0},this.options),function(e,t){v()},h)}}}},{key:"process",value:function(){var e=this;Object.keys(this.queuedWrites).forEach(function(t){"locks"!==t&&Object.keys(e.queuedWrites[t]).forEach(function(n){e.queuedWrites[t][n].length&&e.write(t,n)})})}},{key:"queue",value:function(e,t,n,i,s,r){o(this.queuedWrites,[e,t],{key:n,fallbackValue:i||"",callback:s,options:r}),this.debouncedProcess()}}]),t}();return l.type="backend",l});