turbocommons-ts
Version:
General purpose library that implements frequently used and generic software development tasks
1 lines • 8.84 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.LocalizationManager=void 0;var StringUtils_1=require("../utils/StringUtils"),ArrayUtils_1=require("../utils/ArrayUtils"),JavaPropertiesObject_1=require("../model/JavaPropertiesObject"),LocalizationManager=function(){function t(){this.missingKeyFormat="$exception",this.wildCardsFormat="{N}",this.isBundleMandatory=!1,this._initialized=!1,this._locales=[],this._languages=[],this._activeBundle="",this._activeLocation="",this._definedLocations=[],this._loadedTranslations={},this._filesManager=null,this._httpManager=null}return t.prototype.isInitialized=function(){return this._initialized},t.prototype.isLocaleLoaded=function(t){return this._locales.indexOf(t)>=0},t.prototype.isLanguageLoaded=function(t){if(2!==t.length)throw new Error("language must be a valid 2 digit value");return this._languages.indexOf(t)>=0},t.prototype.initialize=function(t,i,e,r,a){var n=this;if(void 0===r&&(r=null),void 0===a&&(a=null),t?this._httpManager=t:this._filesManager=t,this._locales=[],this._languages=[],this._activeBundle="",this._activeLocation="",this._definedLocations=e,this._loadedTranslations={},!ArrayUtils_1.ArrayUtils.isArray(e)||e.length<=0)throw new Error("Locations must be an array of objects");for(var o=0,l=e;o<l.length;o++){var s=l[o];if(!s.label||StringUtils_1.StringUtils.isEmpty(s.label)||!s.path||StringUtils_1.StringUtils.isEmpty(s.path)||!s.bundles||!ArrayUtils_1.ArrayUtils.isArray(s.bundles))throw new Error("Invalid locations specified")}this._loadData(i,e.map(function(t){return{label:t.label,bundles:t.bundles}}),function(t){n._initialized=!0,null!==r&&r(t)},a)},t.prototype.loadLocales=function(t,i,e){if(void 0===i&&(i=null),void 0===e&&(e=null),!this._initialized)throw new Error("LocalizationManager not initialized. Call initialize() before loading more locales");this._loadData(t,this._definedLocations.map(function(t){return{label:t.label,bundles:t.bundles}}),i,e)},t.prototype.loadBundles=function(t,i,e,r){if(void 0===i&&(i=""),void 0===e&&(e=null),void 0===r&&(r=null),!ArrayUtils_1.ArrayUtils.isArray(t)||0===t.length)throw new Error("no bundles specified to load on "+i+" location");if(!this._initialized)throw new Error("LocalizationManager not initialized. Call initialize() before loading more bundles to a location");""===i&&(i=this._activeLocation),this._loadData(this._locales,[{label:i,bundles:t}],e,r)},t.prototype._loadData=function(t,i,e,r){if(void 0===e&&(e=null),void 0===r&&(r=null),!ArrayUtils_1.ArrayUtils.isArray(t)||t.length<=0)throw new Error("no locales defined");for(var a=[],n=[],o=0,l=i;o<l.length;o++)for(var s=l[o],d=0,h=s.bundles;d<h.length;d++)for(var c=h[d],u=0,g=t;u<g.length;u++){for(var _=g[u],f=!1,p=0,y=this._definedLocations;p<y.length;p++){var v=y[p];if(v.label===s.label){f=!0,n.push({locale:_,bundle:c,location:s.label}),a.push(StringUtils_1.StringUtils.replace(v.path,["$locale","$bundle"],[_,c]));break}}if(!f)throw new Error("Undefined location: "+s.label)}this._locales=this._locales.concat(t),this._locales=ArrayUtils_1.ArrayUtils.removeDuplicateElements(this._locales),this._languages=this._locales.map(function(t){return t.substr(0,2)}),""===this._activeLocation&&(this._activeLocation=i[i.length-1].label),null!==this._filesManager||this._loadDataFromUrls(a,n,e,r)},t.prototype._loadDataFromUrls=function(t,i,e,r){var a=this;void 0===e&&(e=null),void 0===r&&(r=null);var n=function(t){void 0===t&&(t=[]),i.length>0&&(a._activeBundle=i[i.length-1].bundle),null!==e&&e(t)};t.length<=0?n():this._httpManager.execute(t,function(e,r){for(var o=[],l=0;l<e.length;l++)if(e[l].isError)o.push({path:e[l].url,errorMsg:e[l].errorMsg,errorCode:e[l].code});else{var s=i[l].locale,d=i[l].bundle,h=i[l].location,c=StringUtils_1.StringUtils.getPathExtension(t[l]);a._loadedTranslations.hasOwnProperty(h)||(a._loadedTranslations[h]={}),a._loadedTranslations[h].hasOwnProperty(s)||(a._loadedTranslations[h][s]={}),a._loadedTranslations[h][s][d]="json"===c?a.parseJson(e[l].response):a.parseProperties(e[l].response)}n(o)},function(t,i){null!==r&&r(t,i)})},t.prototype.locales=function(){return this._locales},t.prototype.languages=function(){return this._languages},t.prototype.activeBundle=function(){return this._activeBundle},t.prototype.primaryLocale=function(){if(!this._initialized)throw new Error("LocalizationManager not initialized");return this._locales[0]},t.prototype.primaryLanguage=function(){if(!this._initialized)throw new Error("LocalizationManager not initialized");return this._languages[0]},t.prototype.setActiveBundle=function(t){for(var i=0,e=Object.keys(this._loadedTranslations);i<e.length;i++)for(var r=e[i],a=0,n=Object.keys(this._loadedTranslations[r]);a<n.length;a++){var o=n[a];if(Object.keys(this._loadedTranslations[r][o]).indexOf(t)>=0)return this._activeBundle=t,void(this._activeLocation=r)}throw new Error("Bundle <"+t+"> not loaded")},t.prototype.setPrimaryLocale=function(t){if(!StringUtils_1.StringUtils.isString(t))throw new Error("Invalid locale value");if(!this.isLocaleLoaded(t))throw new Error(t+" not loaded");for(var i=[t],e=0,r=this._locales;e<r.length;e++){var a=r[e];a!==t&&i.push(a)}this._locales=i,this._languages=this._locales.map(function(t){return t.substr(0,2)})},t.prototype.setPrimaryLocales=function(t){if(!ArrayUtils_1.ArrayUtils.isArray(t)||ArrayUtils_1.ArrayUtils.hasDuplicateElements(t)||t.length<=0)throw new Error("locales must be non empty string array with no duplicate elements");for(var i=t.length-1;i>=0;i--)this.setPrimaryLocale(t[i])},t.prototype.setPrimaryLanguage=function(t){for(var i=0,e=this._locales;i<e.length;i++){var r=e[i];if(r.substr(0,2)===t)return this.setPrimaryLocale(r)}throw new Error(t+" not loaded")},t.prototype.setPrimaryLanguages=function(t){if(!ArrayUtils_1.ArrayUtils.isArray(t)||ArrayUtils_1.ArrayUtils.hasDuplicateElements(t)||t.length<=0)throw new Error("languages must be non empty string array with no duplicate elements");for(var i=t.length-1;i>=0;i--)this.setPrimaryLanguage(t[i])},t.prototype.setLocalesOrder=function(t){if(!ArrayUtils_1.ArrayUtils.isArray(t))throw new Error("locales must be an array");if(t.length!==this._locales.length)throw new Error("locales must contain all the currently loaded locales");for(var i=0,e=t;i<e.length;i++){var r=e[i];if(!this.isLocaleLoaded(r))throw new Error(r+" not loaded")}this._locales=t,this._languages=this._locales.map(function(t){return t.substr(0,2)})},t.prototype.get=function(t,i,e,r){if(void 0===i&&(i=""),void 0===e&&(e=""),void 0===r&&(r=[]),!this._initialized)throw new Error("LocalizationManager not initialized. Call initialize() before requesting translated texts");if(""===e&&(""!==i&&i!==this._activeBundle&&this.setActiveBundle(i),e=this._activeLocation),""===i){if(this.isBundleMandatory)throw new Error("bundle is mandatory for key "+t);i=this._activeBundle}if(-1===Object.keys(this._loadedTranslations).indexOf(e))throw new Error("Location <"+e+"> not loaded");for(var a=0,n=this._locales;a<n.length;a++){var o=n[a];if(Object.keys(this._loadedTranslations[e]).indexOf(o)>=0){if(-1===Object.keys(this._loadedTranslations[e][o]).indexOf(i))throw new Error("Bundle <"+i+"> not loaded");if(Object.keys(this._loadedTranslations[e][o][i]).indexOf(t)>=0){this._activeBundle=i,this._activeLocation=e;for(var l=this._loadedTranslations[e][o][i][t],s=StringUtils_1.StringUtils.isString(r)?[String(r)]:r,d=0;d<s.length;d++)l=StringUtils_1.StringUtils.replace(l,StringUtils_1.StringUtils.replace(this.wildCardsFormat,"N",String(d)),s[d]);return l}}}if(this.missingKeyFormat.indexOf("$exception")>=0)throw new Error("key <"+t+"> not found on "+i+" - "+e);return this.missingKeyFormat.replace("$key",t)},t.prototype.getStartCase=function(t,i,e,r){return void 0===i&&(i=""),void 0===e&&(e=""),void 0===r&&(r=[]),StringUtils_1.StringUtils.formatCase(this.get(t,i,e,r),StringUtils_1.StringUtils.FORMAT_START_CASE)},t.prototype.getAllUpperCase=function(t,i,e,r){return void 0===i&&(i=""),void 0===e&&(e=""),void 0===r&&(r=[]),StringUtils_1.StringUtils.formatCase(this.get(t,i,e,r),StringUtils_1.StringUtils.FORMAT_ALL_UPPER_CASE)},t.prototype.getAllLowerCase=function(t,i,e,r){return void 0===i&&(i=""),void 0===e&&(e=""),void 0===r&&(r=[]),StringUtils_1.StringUtils.formatCase(this.get(t,i,e,r),StringUtils_1.StringUtils.FORMAT_ALL_LOWER_CASE)},t.prototype.getFirstUpperRestLower=function(t,i,e,r){return void 0===i&&(i=""),void 0===e&&(e=""),void 0===r&&(r=[]),StringUtils_1.StringUtils.formatCase(this.get(t,i,e,r),StringUtils_1.StringUtils.FORMAT_FIRST_UPPER_REST_LOWER)},t.prototype.parseJson=function(t){return JSON.parse(t)},t.prototype.parseProperties=function(t){for(var i={},e=new JavaPropertiesObject_1.JavaPropertiesObject(t),r=0,a=e.getKeys();r<a.length;r++){var n=a[r];i[n]=e.get(n)}return i},t}();exports.LocalizationManager=LocalizationManager;