UNPKG

@resk/core

Version:

An innovative TypeScript framework that empowers developers to build applications with a fully decorator-based architecture for efficient resource management. By combining the power of decorators with a resource-oriented design, DecorRes enhances code cla

1 lines 7.86 kB
'use strict';require('reflect-metadata');var a=require('../utils/isNonNullString'),object=require('../utils/object'),observable=require('../observable'),i18nJs=require('i18n-js'),u=require('../utils/defaultStr'),v=require('../utils/stringify'),index=require('../session/index'),j=require('moment'),decorators=require('../resources/decorators'),O=require('../utils/isPrimitive'),isNullable=require('../utils/isNullable');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var a__default=/*#__PURE__*/_interopDefault(a);var u__default=/*#__PURE__*/_interopDefault(u);var v__default=/*#__PURE__*/_interopDefault(v);var j__default=/*#__PURE__*/_interopDefault(j);var O__default=/*#__PURE__*/_interopDefault(O);var b=Object.getOwnPropertySymbols;var N=Object.prototype.hasOwnProperty,A=Object.prototype.propertyIsEnumerable;var y=(l,f)=>{var t={};for(var e in l)N.call(l,e)&&f.indexOf(e)<0&&(t[e]=l[e]);if(l!=null&&b)for(var e of b(l))f.indexOf(e)<0&&A.call(l,e)&&(t[e]=l[e]);return t};const p=Symbol("TRANSLATION_KEY");function it(l){return decorators.createPropertyDecorator(p,l)}const i=class i extends i18nJs.I18n{constructor(t={},e={}){super(t,e);this._isLoading=false;this._locales=[];this.namespaceResolvers={};this._observableFactory=observable.observableFactory();this._____isObservable=true;this._namespacesLoaded={};this.loadNamespaces();}translate(t,e){return this.isPluralizeOptions(e)&&this.canPluralize(t)?(typeof e.count=="number"&&(e.countStr=e.count.formatNumber()),this.pluralize(e.count,t,e)):super.translate(t,e)}translateTarget(t,e){const r=i.getTargetTanslationKeys(t);for(let s in r)a__default.default(r[s])&&(r[s]=this.translate(r[s],e));return r}translateObject(t,e){if(!object.isObj(t))return {};const r={};for(const s in t){const n=t[s];a__default.default(n)&&(r[s]=R.translate(n,e));}return r}static getTargetTanslationKeys(t){return decorators.getDecoratedProperties(t,p)}on(t,e){return this._observableFactory.on.call(this,t,e)}finally(t,e){return this._observableFactory.finally.call(this,t,e)}off(t,e){var r;return (r=this._observableFactory)==null?void 0:r.off.call(this,t,e)}trigger(t,...e){var r;return (r=this._observableFactory)==null?void 0:r.trigger.call(this,t,...e)}offAll(){var t;return (t=this._observableFactory)==null?void 0:t.offAll.call(this)}once(t,e){var r;return (r=this._observableFactory)==null?void 0:r.once.call(this,t,e)}getEventCallBacks(){var t;return (t=this._observableFactory)==null?void 0:t.getEventCallBacks.call(this)}static getInstance(t){if(!i.instance){const e=i.getLocaleFromSession();i.instance=this.createInstance({},Object.assign({},e?{locale:e}:{},t));}return i.instance}isDefaultInstance(){return this===i.instance}static setLocaleToSession(t){index.Session.set("i18n.locale",t);}static getLocaleFromSession(){return index.Session.get("i18n.locale")}canPluralize(t,e){e=u__default.default(e,this.getLocale());const r=this.getNestedTranslation(t,e);return !object.isObj(r)||!r?false:a__default.default(r==null?void 0:r.one)&&a__default.default(r==null?void 0:r.other)}getNestedTranslation(t,e){e=u__default.default(e,this.getLocale());const r=a__default.default(t)?t.trim().split("."):Array.isArray(t)?t:[];if(!r.length)return;let s=this.getTranslations(e);for(const g of r)if(object.isObj(s))s=s[g];else return;return s}isPluralizeOptions(t){return !!(object.isObj(t)&&t&&typeof t.count=="number")}static RegisterTranslations(t){return i.getInstance().registerTranslations(t)}static createInstance(t={},e={}){const g=Object.assign({},e),{interpolate:r}=g,s=y(g,["interpolate"]),n=new i(t,s);return n.interpolate=(h,c,I)=>{const S=i.flattenObject(I),m=this.defaultInterpolator(h,c,S);return a__default.default(m)&&m!==c&&(c=m),typeof r=="function"?r(h,c,I):c},n}getTranslations(t){const e=object.isObj(this.translations)?this.translations:{};return a__default.default(t)?object.isObj(e[t])?e[t]:{}:e}static registerMomentLocale(t,e){return a__default.default(t)&&object.isObj(e)&&Array.isArray(e.months)&&(this.momentLocales[t]=object.extendObj({},this.momentLocales[t],e)),this.momentLocales}static getMomentLocale(t){return this.momentLocales[t]}static setMomentLocale(t){try{return j__default.default.updateLocale(t,this.getMomentLocale(t)),!0}catch(e){console.error(e," setting moment locale : ",t);}return false}registerTranslations(t){return this.store(t),this.getTranslations()}store(t){super.store(t),this.trigger("translations-changed",this.getLocale(),this.getTranslations());}resolveTranslations(t){try{const e=Object.getOwnPropertyNames(t);for(const r of e){const s=Reflect.getMetadata(p,t,r);if(s)try{t[r]=this.translate(s);}catch(n){console.error(n," resolving translation for key : ",s);}}}catch(e){console.error(e," resolving translations for target : ",t);}}getMissingPlaceholderString(t,e,r){return typeof this.missingPlaceholder=="function"?this.missingPlaceholder(this,t,u__default.default(e),Object.assign({},r)):t}getLocale(){return super.locale}setLocales(t){return this._locales=Array.isArray(t)?t:["en"],this._locales.includes("en")||this._locales.push("en"),this.getLocales()}hasLocale(t){return a__default.default(t)&&this.getLocales().includes(t)}getLocales(){const t=Object.keys(this.getTranslations()),e=Array.isArray(this._locales)?this._locales:["en"],r=[...t,...e.filter(s=>!t.includes(s))];return r.includes("en")||r.push("en"),r}isLocaleSupported(t){return a__default.default(t)?this.getLocales().includes(t):false}isLoading(){return this._isLoading}setLocale(t,e=false){return super.locale===t&&e!==true&&this._namespacesLoaded[t]?Promise.resolve(this.getLocale()):new Promise((r,s)=>(this._isLoading=true,this.trigger("namespaces-before-load",t),this.loadNamespaces(t).then(n=>{this.isDefaultInstance()&&this.isLocaleSupported(t)&&i.setLocaleToSession(t),super.locale=t,i.setMomentLocale(t),this.trigger("locale-changed",t,n),r(t);}).catch(s).finally(()=>{this._isLoading=false;})))}registerNamespaceResolver(t,e){if(!a__default.default(t)||typeof e!="function"){console.warn("Invalid arguments for registerNamespaceResolver.",t,e);return}this.namespaceResolvers[t]=e;}static RegisterNamespaceResolver(t,e){return i.getInstance().registerNamespaceResolver(t,e)}loadNamespace(t,e,r=true){return !a__default.default(t)||!this.namespaceResolvers[t]?Promise.reject(new Error(`Invalid namespace or resolver for namespace "${t}".`)):(e=u__default.default(e,this.getLocale()),a__default.default(e)?this.namespaceResolvers[t](e).then(s=>{const n={};return n[e]=Object.assign({},s),object.isObj(s)&&(r!==false&&this.store(n),this.trigger("namespace-loaded",t,e,n)),n}):Promise.reject(new Error(`Locale is not set. Cannot load namespace "${t}".`)))}static LoadNamespace(t,e,r=true){return i.getInstance().loadNamespace(t,e,r)}loadNamespaces(t,e=true){const r=[],s={};t=u__default.default(t,this.getLocale()),this._isLoading=true;for(const n in this.namespaceResolvers)this.namespaceResolvers.hasOwnProperty(n)&&typeof this.namespaceResolvers[n]=="function"&&r.push(new Promise((g,h)=>{this.namespaceResolvers[n](t).then(c=>{object.extendObj(s,c);}).finally(()=>{g(true);});}));return Promise.all(r).then(()=>{const n={};return n[t]=s,e!==false&&this.store(n),setTimeout(()=>{this.trigger("namespaces-loaded",t,n);},100),n}).finally(()=>{this._isLoading=false;})}static LoadNamespaces(t,e=true){return i.getInstance().loadNamespaces(t,e)}static flattenObject(t){return object.isObj(t)?Object.flatten(t):t}static defaultInterpolator(t,e,r){return isNullable.isNullable(e)?"":O__default.default(e)?(e=String(e),!object.isObj(r)||!r||!object.isObj(r)||!r?e:e.replace(/%{(.*?)}/g,(s,n)=>isNullable.isNullable(r[n])?"":O__default.default(r[n])?String(r[n]):v__default.default(r[n],{escapeString:false}))):v__default.default(e,{escapeString:false})}};i.momentLocales={};let d=i;const R=d.getInstance();exports.I18n=d;exports.Translate=it;exports.i18n=R;