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.99 kB
'use strict';Object.defineProperty(exports,'__esModule',{value:true});require('reflect-metadata');var a=require('../utils/isNonNullString'),object=require('../utils/object'),observable=require('../observable'),i18nJs=require('i18n-js'),f=require('../utils/defaultStr'),v=require('../utils/stringify'),L=require('../session/index'),j=require('moment'),decorators=require('../decorators'),D=require('./locales/moment.locale.fr'),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 f__default=/*#__PURE__*/_interopDefault(f);var v__default=/*#__PURE__*/_interopDefault(v);var L__default=/*#__PURE__*/_interopDefault(L);var j__default=/*#__PURE__*/_interopDefault(j);var D__default=/*#__PURE__*/_interopDefault(D);var O__default=/*#__PURE__*/_interopDefault(O);var b=Object.getOwnPropertySymbols;var S=Object.prototype.hasOwnProperty,N=Object.prototype.propertyIsEnumerable;var T=(l,m)=>{var t={};for(var e in l)S.call(l,e)&&m.indexOf(e)<0&&(t[e]=l[e]);if(l!=null&&b)for(var e of b(l))m.indexOf(e)<0&&N.call(l,e)&&(t[e]=l[e]);return t};const I=Symbol("TRANSLATION_KEY");function ot(l){return decorators.createPropertyDecorator(I,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.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 n in r)a__default.default(r[n])&&(r[n]=this.t(r[n],e));return r}static getTargetTanslationKeys(t){return decorators.getDecoratedProperties(t,I)}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){L__default.default.set("i18n.locale",t);}static getLocaleFromSession(){return L__default.default.get("i18n.locale")}canPluralize(t,e){e=f__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=f__default.default(e,this.getLocale());const r=a__default.default(t)?t.trim().split("."):Array.isArray(t)?t:[];if(!r.length)return;let n=this.getTranslations(e);for(const c of r)if(object.isObj(n))n=n[c];else return;return n}isPluralizeOptions(t){return !!(object.isObj(t)&&t&&typeof t.count=="number")}static RegisterTranslations(t){return i.getInstance().registerTranslations(t)}static createInstance(t={},e={}){const c=Object.assign({},e),{interpolate:r}=c,n=T(c,["interpolate"]),s=new i(t,n);return s.interpolate=(u,g,d)=>{const R=i.flattenObject(d),p=this.defaultInterpolator(u,g,R);return a__default.default(p)&&p!==g&&(g=p),typeof r=="function"?r(u,g,d):g},s}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.locale(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 n=Reflect.getMetadata(I,t,r);if(n)try{t[r]=this.t(n);}catch(s){console.error(s," resolving translation for key : ",n);}}}catch(e){console.error(e," resolving translations for target : ",t);}}getMissingPlaceholderString(t,e,r){return typeof this.missingPlaceholder=="function"?this.missingPlaceholder(this,t,f__default.default(e),Object.assign({},r)):t}get locale(){return super.locale}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(n=>!t.includes(n))];return r.includes("en")||r.push("en"),r}isLocaleSupported(t){return a__default.default(t)?this.getLocales().includes(t):false}isLoading(){return this._isLoading}set locale(t){this.locale!=t&&(this._isLoading=true,i.setMomentLocale(t),this.trigger("namespaces-before-load",t),this.loadNamespaces(t).then(e=>{this.isDefaultInstance()&&this.isLocaleSupported(t)&&i.setLocaleToSession(t),super.locale=t,this.trigger("locale-changed",t,e);}));}setLocale(t){return this.locale=t,new Promise((e,r)=>{let n=false;const s=c=>{n||(n=true,e(this.getTranslations()));};this.isLoading()?this.once("locale-changed",(c,u)=>{s();}):e(this.getTranslations());})}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=f__default.default(e,this.getLocale()),a__default.default(e)?this.namespaceResolvers[t](e).then(n=>{const s={};return s[e]=Object.assign({},n),object.isObj(n)&&(r!==false&&this.store(s),this.trigger("namespace-loaded",t,e,s)),s}):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=[],n={};t=f__default.default(t,this.getLocale()),this._isLoading=true;for(const s in this.namespaceResolvers)this.namespaceResolvers.hasOwnProperty(s)&&typeof this.namespaceResolvers[s]=="function"&&r.push(new Promise((c,u)=>{this.namespaceResolvers[s](t).then(g=>{object.extendObj(n,g);}).finally(()=>{c(true);});}));return Promise.all(r).then(()=>{const s={};return s[t]=n,e!==false&&this.store(s),setTimeout(()=>{this.trigger("namespaces-loaded",t,s);},100),s}).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,(n,s)=>isNullable.isNullable(r[s])?"":O__default.default(r[s])?String(r[s]):v__default.default(r[s],{escapeString:false}))):v__default.default(e,{escapeString:false})}};i.momentLocales={};let h=i;const k=h.getInstance();var lt=k;h.registerMomentLocale("fr",D__default.default);exports.I18nClass=h;exports.Translate=ot;exports.default=lt;