@sveltekit-i18n/base 1.3.1 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.cjs +1 -1
  2. package/package.json +1 -1
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var w=Object.defineProperty,I=Object.defineProperties,H=Object.getOwnPropertyDescriptor,q=Object.getOwnPropertyDescriptors,B=Object.getOwnPropertyNames,v=Object.getOwnPropertySymbols;var C=Object.prototype.hasOwnProperty,j=Object.prototype.propertyIsEnumerable;var $=(s,t,e)=>t in s?w(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e,l=(s,t)=>{for(var e in t||(t={}))C.call(t,e)&&$(s,e,t[e]);if(v)for(var e of v(t))j.call(t,e)&&$(s,e,t[e]);return s},u=(s,t)=>I(s,q(t));var T=(s,t)=>{var e={};for(var a in s)C.call(s,a)&&t.indexOf(a)<0&&(e[a]=s[a]);if(s!=null&&v)for(var a of v(s))t.indexOf(a)<0&&j.call(s,a)&&(e[a]=s[a]);return e};var G=(s,t)=>{for(var e in t)w(s,e,{get:t[e],enumerable:!0})},J=(s,t,e,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of B(t))!C.call(s,r)&&r!==e&&w(s,r,{get:()=>t[r],enumerable:!(a=H(t,r))||a.enumerable});return s};var M=s=>J(w({},"__esModule",{value:!0}),s);var U={};G(U,{default:()=>x});module.exports=M(U);var n=require("svelte/store");var z=["error","warn","debug"],F=({logger:s=console,level:t=z[1],prefix:e="[i18n]: "})=>z.reduce((a,r,i)=>u(l({},a),{[r]:o=>z.indexOf(t)>=i&&s[r](`${e}${o}`)}),{}),g=F({}),A=s=>{g=s};var O=c=>{var d=c,{parser:s,key:t,params:e,translations:a,locale:r,fallbackLocale:i}=d,o=T(d,["parser","key","params","translations","locale","fallbackLocale"]);if(!t)return g.warn(`No translation key provided ('${r}' locale). Skipping translation...`),"";if(!r)return g.warn(`No locale provided for '${t}' key. Skipping translation...`),"";let f=(a[r]||{})[t];return i&&f===void 0&&(f=(a[i]||{})[t]),o.hasOwnProperty("fallbackValue")&&f===void 0?o.fallbackValue:s.parse(f,e,r,t)},p=(...s)=>s.length?s.filter(t=>!!t).map(t=>{let e=`${t}`.toLowerCase();try{let[a]=Intl.Collator.supportedLocalesOf(t);if(!a)throw new Error;e=a}catch(a){g.warn(`'${t}' locale is non-standard.`)}return e}):[],S=(s,t,e)=>Object.keys(s||{}).reduce((a,r)=>{let i=s[r],o=e?`${e}.${r}`:`${r}`;return t&&Array.isArray(i)?u(l({},a),{[o]:i.map(c=>S(c,t))}):i&&typeof i=="object"?l(l({},a),S(i,t,o)):u(l({},a),{[o]:i})},{}),Q=s=>s.reduce((t,{key:e,data:a,locale:r})=>{if(!a)return t;let[i]=p(r),o=u(l({},t[i]||{}),{[e]:a});return u(l({},t),{[i]:o})},{}),N=async s=>{try{let t=await Promise.all(s.map(r=>{var i=r,{loader:e}=i,a=T(i,["loader"]);return new Promise(async o=>{let c;try{c=await e()}catch(d){g.error(`Failed to load translation. Verify your '${a.locale}' > '${a.key}' Loader.`),g.error(d)}o(u(l({loader:e},a),{data:c}))})}));return Q(t)}catch(t){g.error(t)}return{}},V=s=>t=>{try{if(typeof t=="string")return t===s;if(typeof t=="object")return t.test(s)}catch(e){g.error("Invalid route config!")}return!1},E=(s,t)=>{let e=!0;try{e=Object.keys(s).filter(a=>s[a]!==void 0).every(a=>s[a]===t[a])}catch(a){}return e};var W=1e3*60*60*24,x=class{constructor(t){this.cachedAt=0;this.loadedKeys={};this.currentRoute=(0,n.writable)();this.config=(0,n.writable)();this.isLoading=(0,n.writable)(!1);this.promises=new Set;this.loading={subscribe:this.isLoading.subscribe,toPromise:(t,e)=>{let a=Array.from(this.promises).filter(r=>E({locale:p(t)[0],route:e},r)).map(({promise:r})=>r);return Promise.all(a)},get:()=>(0,n.get)(this.isLoading)};this.privateRawTranslations=(0,n.writable)({});this.rawTranslations={subscribe:this.privateRawTranslations.subscribe,get:()=>(0,n.get)(this.rawTranslations)};this.privateTranslations=(0,n.writable)({});this.translations={subscribe:this.privateTranslations.subscribe,get:()=>(0,n.get)(this.translations)};this.locales=u(l({},(0,n.derived)([this.config,this.privateTranslations],([t,e])=>{if(!t)return[];let{loaders:a=[]}=t,r=a.map(({locale:o})=>o),i=Object.keys(e).map(o=>o);return Array.from(new Set([...p(...r),...p(...i)]))},[])),{get:()=>(0,n.get)(this.locales)});this.internalLocale=(0,n.writable)();this.loaderTrigger=(0,n.derived)([this.internalLocale,this.currentRoute],([t,e],a)=>{var r,i;t!==void 0&&e!==void 0&&!(t===((r=(0,n.get)(this.loaderTrigger))==null?void 0:r[0])&&e===((i=(0,n.get)(this.loaderTrigger))==null?void 0:i[1]))&&(g.debug("Triggering translation load..."),a([t,e]))},[]);this.localeHelper=(0,n.writable)();this.locale={subscribe:this.localeHelper.subscribe,forceSet:this.localeHelper.set,set:this.internalLocale.set,update:this.internalLocale.update,get:()=>(0,n.get)(this.locale)};this.initialized=(0,n.derived)([this.locale,this.currentRoute,this.privateTranslations],([t,e,a],r)=>{(0,n.get)(this.initialized)||r(t!==void 0&&e!==void 0&&!!Object.keys(a).length)});this.translation=(0,n.derived)([this.privateTranslations,this.locale,this.isLoading],([t,e,a],r)=>{let i=t[e];i&&Object.keys(i).length&&!a&&r(i)},{});this.t=u(l({},(0,n.derived)([this.config,this.translation],r=>{var[i]=r,o=i,{parser:t,fallbackLocale:e}=o,a=T(o,["parser","fallbackLocale"]);return(c,...d)=>O(l({parser:t,key:c,params:d,translations:this.translations.get(),locale:this.locale.get(),fallbackLocale:e},a.hasOwnProperty("fallbackValue")?{fallbackValue:a.fallbackValue}:{}))})),{get:(t,...e)=>(0,n.get)(this.t)(t,...e)});this.l=u(l({},(0,n.derived)([this.config,this.translations],i=>{var[o,...c]=i,d=o,{parser:t,fallbackLocale:e}=d,a=T(d,["parser","fallbackLocale"]),[r]=c;return(f,b,...R)=>O(l({parser:t,key:b,params:R,translations:r,locale:f,fallbackLocale:e},a.hasOwnProperty("fallbackValue")?{fallbackValue:a.fallbackValue}:{}))})),{get:(t,e,...a)=>(0,n.get)(this.l)(t,e,...a)});this.getLocale=t=>{let{fallbackLocale:e=""}=(0,n.get)(this.config)||{},a=t||e;if(!a)return"";let r=this.locales.get();return r.find(o=>p(a).includes(o))||r.find(o=>p(e).includes(o))||""};this.setLocale=t=>{if(t&&t!==(0,n.get)(this.internalLocale))return g.debug(`Setting '${t}' locale.`),this.internalLocale.set(t),this.loading.toPromise(t,(0,n.get)(this.currentRoute))};this.setRoute=t=>{if(t!==(0,n.get)(this.currentRoute)){g.debug(`Setting '${t}' route.`),this.currentRoute.set(t);let e=(0,n.get)(this.internalLocale);return this.loading.toPromise(e,t)}};this.loadConfig=async t=>{await this.configLoader(t)};this.getTranslationProps=async(t=this.locale.get(),e=(0,n.get)(this.currentRoute))=>{let a=(0,n.get)(this.config);if(!a||!t)return[];let r=this.translations.get(),{loaders:i,fallbackLocale:o="",cache:c=W}=a||{},d=Number.isNaN(+c)?W:+c;this.cachedAt?Date.now()>d+this.cachedAt&&(g.debug("Refreshing cache."),this.loadedKeys={},this.cachedAt=0):(g.debug("Setting cache timestamp."),this.cachedAt=Date.now());let[f,b]=p(t,o),R=r[f],D=r[b],k=(i||[]).map(K=>{var L=K,{locale:h}=L,m=T(L,["locale"]);return u(l({},m),{locale:p(h)[0]})}).filter(({routes:h})=>!h||(h||[]).some(V(e))).filter(({key:h,locale:m})=>m===f&&(!R||!(this.loadedKeys[f]||[]).includes(h))||o&&m===b&&(!D||!(this.loadedKeys[b]||[]).includes(h)));if(k.length){this.isLoading.set(!0),g.debug("Fetching translations...");let h=await N(k);this.isLoading.set(!1);let m=Object.keys(h).reduce((L,y)=>u(l({},L),{[y]:Object.keys(h[y])}),{}),K=k.filter(({key:L,locale:y})=>(m[y]||[]).some(P=>`${P}`.startsWith(L))).reduce((L,{key:y,locale:P})=>u(l({},L),{[P]:[...L[P]||[],y]}),{});return[h,K]}return[]};this.addTranslations=(t,e)=>{if(!t)return;let a=(0,n.get)(this.config),{preprocess:r}=a||{};g.debug("Adding translations...");let i=Object.keys(t||{});this.privateRawTranslations.update(o=>i.reduce((c,d)=>u(l({},c),{[d]:l(l({},c[d]||{}),t[d])}),o)),this.privateTranslations.update(o=>i.reduce((c,d)=>{let f=!0,b=t[d];return typeof r=="function"&&(b=r(b)),(typeof r=="function"||r==="none")&&(f=!1),u(l({},c),{[d]:l(l({},c[d]||{}),f?S(b,r==="preserveArrays"):b)})},o)),i.forEach(o=>{let c=Object.keys(t[o]).map(d=>`${d}`.split(".")[0]);e&&(c=e[o]),this.loadedKeys[o]=Array.from(new Set([...this.loadedKeys[o]||[],...c||[]]))})};this.loader=async([t,e])=>{let a=this.getLocale(t)||void 0;g.debug(`Adding loader promise for '${a}' locale and '${e}' route.`);let r=(async()=>{let i=await this.getTranslationProps(a,e);i.length&&this.addTranslations(...i)})();this.promises.add({locale:a,route:e,promise:r}),r.then(()=>{a&&this.locale.get()!==a&&this.locale.forceSet(a)})};this.loadTranslations=(t,e=(0,n.get)(this.currentRoute)||"")=>{let a=this.getLocale(t);if(a)return this.setRoute(e),this.setLocale(a),this.loading.toPromise(a,e)};this.loaderTrigger.subscribe(this.loader),this.isLoading.subscribe(async e=>{e&&this.promises.size&&(await this.loading.toPromise(),this.promises.clear(),g.debug("Loader promises have been purged."))}),t&&this.loadConfig(t)}async configLoader(t){if(!t)return g.error("No config provided!");let c=t,{initLocale:e,fallbackLocale:a,translations:r,log:i}=c,o=T(c,["initLocale","fallbackLocale","translations","log"]);i&&A(F(i)),[e]=p(e),[a]=p(a),g.debug("Setting config."),this.config.set(l({initLocale:e,fallbackLocale:a,translations:r},o)),r&&this.addTranslations(r),await this.loadTranslations(e)}};
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var H=Object.defineProperty,q=Object.defineProperties;var B=Object.getOwnPropertyDescriptors;var S=Object.getOwnPropertySymbols;var j=Object.prototype.hasOwnProperty,A=Object.prototype.propertyIsEnumerable;var $=(s,t,e)=>t in s?H(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e,n=(s,t)=>{for(var e in t||(t={}))j.call(t,e)&&$(s,e,t[e]);if(S)for(var e of S(t))A.call(t,e)&&$(s,e,t[e]);return s},u=(s,t)=>q(s,B(t));var T=(s,t)=>{var e={};for(var a in s)j.call(s,a)&&t.indexOf(a)<0&&(e[a]=s[a]);if(s!=null&&S)for(var a of S(s))t.indexOf(a)<0&&A.call(s,a)&&(e[a]=s[a]);return e};var _store = require('svelte/store');var C=["error","warn","debug"],z=({logger:s=console,level:t=C[1],prefix:e="[i18n]: "})=>C.reduce((a,r,i)=>u(n({},a),{[r]:o=>C.indexOf(t)>=i&&s[r](`${e}${o}`)}),{}),d=z({}),N=s=>{d=s};var F=l=>{var c=l,{parser:s,key:t,params:e,translations:a,locale:r,fallbackLocale:i}=c,o=T(c,["parser","key","params","translations","locale","fallbackLocale"]);if(!t)return d.warn(`No translation key provided ('${r}' locale). Skipping translation...`),"";if(!r)return d.warn(`No locale provided for '${t}' key. Skipping translation...`),"";let f=(a[r]||{})[t];return i&&f===void 0&&(f=(a[i]||{})[t]),o.hasOwnProperty("fallbackValue")&&f===void 0?o.fallbackValue:s.parse(f,e,r,t)},p=(...s)=>s.length?s.filter(t=>!!t).map(t=>{let e=`${t}`.toLowerCase();try{let[a]=Intl.Collator.supportedLocalesOf(t);if(!a)throw new Error;e=a}catch(a){d.warn(`'${t}' locale is non-standard.`)}return e}):[],x=(s,t,e)=>Object.keys(s||{}).reduce((a,r)=>{let i=s[r],o=e?`${e}.${r}`:`${r}`;return t&&Array.isArray(i)?u(n({},a),{[o]:i.map(l=>x(l,t))}):i&&typeof i=="object"?n(n({},a),x(i,t,o)):u(n({},a),{[o]:i})},{}),G=s=>s.reduce((t,{key:e,data:a,locale:r})=>{if(!a)return t;let[i]=p(r),o=u(n({},t[i]||{}),{[e]:a});return u(n({},t),{[i]:o})},{}),V=async s=>{try{let t=await Promise.all(s.map(r=>{var i=r,{loader:e}=i,a=T(i,["loader"]);return new Promise(async o=>{let l;try{l=await e()}catch(c){d.error(`Failed to load translation. Verify your '${a.locale}' > '${a.key}' Loader.`),d.error(c)}o(u(n({loader:e},a),{data:l}))})}));return G(t)}catch(t){d.error(t)}return{}},E=s=>t=>{try{if(typeof t=="string")return t===s;if(typeof t=="object")return t.test(s)}catch(e){d.error("Invalid route config!")}return!1},W=(s,t)=>{let e=!0;try{e=Object.keys(s).filter(a=>s[a]!==void 0).every(a=>s[a]===t[a])}catch(a){}return e};var D=1e3*60*60*24,O= exports.default =class{constructor(t){this.cachedAt=0;this.loadedKeys={};this.currentRoute=_store.writable.call(void 0, );this.config=_store.writable.call(void 0, );this.isLoading=_store.writable.call(void 0, !1);this.promises=new Set;this.loading={subscribe:this.isLoading.subscribe,toPromise:(t,e)=>{let a=Array.from(this.promises).filter(r=>W({locale:p(t)[0],route:e},r)).map(({promise:r})=>r);return Promise.all(a)},get:()=>_store.get.call(void 0, this.isLoading)};this.privateRawTranslations=_store.writable.call(void 0, {});this.rawTranslations={subscribe:this.privateRawTranslations.subscribe,get:()=>_store.get.call(void 0, this.rawTranslations)};this.privateTranslations=_store.writable.call(void 0, {});this.translations={subscribe:this.privateTranslations.subscribe,get:()=>_store.get.call(void 0, this.translations)};this.locales=u(n({},_store.derived.call(void 0, [this.config,this.privateTranslations],([t,e])=>{if(!t)return[];let{loaders:a=[]}=t,r=a.map(({locale:o})=>o),i=Object.keys(e).map(o=>o);return Array.from(new Set([...p(...r),...p(...i)]))},[])),{get:()=>_store.get.call(void 0, this.locales)});this.internalLocale=_store.writable.call(void 0, );this.loaderTrigger=_store.derived.call(void 0, [this.internalLocale,this.currentRoute],([t,e],a)=>{var r,i;t!==void 0&&e!==void 0&&!(t===((r=_store.get.call(void 0, this.loaderTrigger))==null?void 0:r[0])&&e===((i=_store.get.call(void 0, this.loaderTrigger))==null?void 0:i[1]))&&(d.debug("Triggering translation load..."),a([t,e]))},[]);this.localeHelper=_store.writable.call(void 0, );this.locale={subscribe:this.localeHelper.subscribe,forceSet:this.localeHelper.set,set:this.internalLocale.set,update:this.internalLocale.update,get:()=>_store.get.call(void 0, this.locale)};this.initialized=_store.derived.call(void 0, [this.locale,this.currentRoute,this.privateTranslations],([t,e,a],r)=>{_store.get.call(void 0, this.initialized)||r(t!==void 0&&e!==void 0&&!!Object.keys(a).length)});this.translation=_store.derived.call(void 0, [this.privateTranslations,this.locale,this.isLoading],([t,e,a],r)=>{let i=t[e];i&&Object.keys(i).length&&!a&&r(i)},{});this.t=u(n({},_store.derived.call(void 0, [this.config,this.translation],r=>{var[i]=r,o=i,{parser:t,fallbackLocale:e}=o,a=T(o,["parser","fallbackLocale"]);return(l,...c)=>F(n({parser:t,key:l,params:c,translations:this.translations.get(),locale:this.locale.get(),fallbackLocale:e},a.hasOwnProperty("fallbackValue")?{fallbackValue:a.fallbackValue}:{}))})),{get:(t,...e)=>_store.get.call(void 0, this.t)(t,...e)});this.l=u(n({},_store.derived.call(void 0, [this.config,this.translations],i=>{var[o,...l]=i,c=o,{parser:t,fallbackLocale:e}=c,a=T(c,["parser","fallbackLocale"]),[r]=l;return(f,b,...R)=>F(n({parser:t,key:b,params:R,translations:r,locale:f,fallbackLocale:e},a.hasOwnProperty("fallbackValue")?{fallbackValue:a.fallbackValue}:{}))})),{get:(t,e,...a)=>_store.get.call(void 0, this.l)(t,e,...a)});this.getLocale=t=>{let{fallbackLocale:e=""}=_store.get.call(void 0, this.config)||{},a=t||e;if(!a)return"";let r=this.locales.get();return r.find(o=>p(a).includes(o))||r.find(o=>p(e).includes(o))||""};this.setLocale=t=>{if(t&&t!==_store.get.call(void 0, this.internalLocale))return d.debug(`Setting '${t}' locale.`),this.internalLocale.set(t),this.loading.toPromise(t,_store.get.call(void 0, this.currentRoute))};this.setRoute=t=>{if(t!==_store.get.call(void 0, this.currentRoute)){d.debug(`Setting '${t}' route.`),this.currentRoute.set(t);let e=_store.get.call(void 0, this.internalLocale);return this.loading.toPromise(e,t)}};this.loadConfig=async t=>{await this.configLoader(t)};this.getTranslationProps=async(t=this.locale.get(),e=_store.get.call(void 0, this.currentRoute))=>{let a=_store.get.call(void 0, this.config);if(!a||!t)return[];let r=this.translations.get(),{loaders:i,fallbackLocale:o="",cache:l=D}=a||{},c=Number.isNaN(+l)?D:+l;this.cachedAt?Date.now()>c+this.cachedAt&&(d.debug("Refreshing cache."),this.loadedKeys={},this.cachedAt=0):(d.debug("Setting cache timestamp."),this.cachedAt=Date.now());let[f,b]=p(t,o),R=r[f],I=r[b],k=(i||[]).map(K=>{var L=K,{locale:h}=L,y=T(L,["locale"]);return u(n({},y),{locale:p(h)[0]})}).filter(({routes:h})=>!h||(h||[]).some(E(e))).filter(({key:h,locale:y})=>y===f&&(!R||!(this.loadedKeys[f]||[]).includes(h))||o&&y===b&&(!I||!(this.loadedKeys[b]||[]).includes(h)));if(k.length){this.isLoading.set(!0),d.debug("Fetching translations...");let h=await V(k);this.isLoading.set(!1);let y=Object.keys(h).reduce((L,P)=>u(n({},L),{[P]:Object.keys(h[P])}),{}),K=k.filter(({key:L,locale:P})=>(y[P]||[]).some(w=>`${w}`.startsWith(L))).reduce((L,{key:P,locale:w})=>u(n({},L),{[w]:[...L[w]||[],P]}),{});return[h,K]}return[]};this.addTranslations=(t,e)=>{if(!t)return;let a=_store.get.call(void 0, this.config),{preprocess:r}=a||{};d.debug("Adding translations...");let i=Object.keys(t||{});this.privateRawTranslations.update(o=>i.reduce((l,c)=>u(n({},l),{[c]:n(n({},l[c]||{}),t[c])}),o)),this.privateTranslations.update(o=>i.reduce((l,c)=>{let f=!0,b=t[c];return typeof r=="function"&&(b=r(b)),(typeof r=="function"||r==="none")&&(f=!1),u(n({},l),{[c]:n(n({},l[c]||{}),f?x(b,r==="preserveArrays"):b)})},o)),i.forEach(o=>{let l=Object.keys(t[o]).map(c=>`${c}`.split(".")[0]);e&&(l=e[o]),this.loadedKeys[o]=Array.from(new Set([...this.loadedKeys[o]||[],...l||[]]))})};this.loader=async([t,e])=>{let a=this.getLocale(t)||void 0;d.debug(`Adding loader promise for '${a}' locale and '${e}' route.`);let r=(async()=>{let i=await this.getTranslationProps(a,e);i.length&&this.addTranslations(...i)})();this.promises.add({locale:a,route:e,promise:r}),r.then(()=>{a&&this.locale.get()!==a&&this.locale.forceSet(a)})};this.loadTranslations=(t,e=_store.get.call(void 0, this.currentRoute)||"")=>{let a=this.getLocale(t);if(a)return this.setRoute(e),this.setLocale(a),this.loading.toPromise(a,e)};this.loaderTrigger.subscribe(this.loader),this.isLoading.subscribe(async e=>{e&&this.promises.size&&(await this.loading.toPromise(),this.promises.clear(),d.debug("Loader promises have been purged."))}),t&&this.loadConfig(t)}async configLoader(t){if(!t)return d.error("No config provided!");let l=t,{initLocale:e,fallbackLocale:a,translations:r,log:i}=l,o=T(l,["initLocale","fallbackLocale","translations","log"]);i&&N(z(i)),[e]=p(e),[a]=p(a),d.debug("Setting config."),this.config.set(n({initLocale:e,fallbackLocale:a,translations:r},o)),r&&this.addTranslations(r),await this.loadTranslations(e)}};exports.default = O;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltekit-i18n/base",
3
- "version": "1.3.1",
3
+ "version": "1.3.2",
4
4
  "description": "Base functionality of sveltekit-i18n library with a support for external message parsers.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",