@sveltekit-i18n/base 1.0.0-beta.2 → 1.0.0-beta.3

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.
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- var T=Object.defineProperty,k=Object.defineProperties,E=Object.getOwnPropertyDescriptor,O=Object.getOwnPropertyDescriptors,W=Object.getOwnPropertyNames,y=Object.getOwnPropertySymbols;var v=Object.prototype.hasOwnProperty,P=Object.prototype.propertyIsEnumerable;var F=(t,e,o)=>e in t?T(t,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):t[e]=o,r=(t,e)=>{for(var o in e||(e={}))v.call(e,o)&&F(t,o,e[o]);if(y)for(var o of y(e))P.call(e,o)&&F(t,o,e[o]);return t},l=(t,e)=>k(t,O(e)),K=t=>T(t,"__esModule",{value:!0});var b=(t,e)=>{var o={};for(var s in t)v.call(t,s)&&e.indexOf(s)<0&&(o[s]=t[s]);if(t!=null&&y)for(var s of y(t))e.indexOf(s)<0&&P.call(t,s)&&(o[s]=t[s]);return o};var D=(t,e)=>{for(var o in e)T(t,o,{get:e[o],enumerable:!0})},N=(t,e,o,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of W(e))!v.call(t,i)&&(o||i!=="default")&&T(t,i,{get:()=>e[i],enumerable:!(s=E(e,i))||s.enumerable});return t};var M=(t=>(e,o)=>t&&t.get(e)||(o=N(K({}),e,1),t&&t.set(e,o),o))(typeof WeakMap!="undefined"?new WeakMap:0);var A={};D(A,{default:()=>j});var a=require("svelte/store");var c=(t,e={})=>t||e,m=(t,e)=>Object.keys(c(t)).reduce((o,s)=>{let i=t[s],n=e?`${e}.${s}`:`${s}`;return i&&typeof i=="object"?r(r({},o),m(i,n)):l(r({},o),{[n]:i})},{}),x=async t=>{try{return(await Promise.all(t.map(i=>{var n=i,{loader:o}=n,s=b(n,["loader"]);return new Promise(async p=>{let u;try{u=await o()}catch(w){console.error(`Failed to load translation. Verify your '${s.locale}' > '${s.key}' loader.`),console.error(w)}p(l(r({loader:o},s),{data:u}))})}))).reduce((o,{key:s,data:i,locale:n})=>i?l(r({},o),{[n]:m(l(r({},c(o[n])),{[s]:i}))}):o,{})}catch(e){console.error(e)}return{}},$=t=>e=>{try{if(typeof e=="string")return e===t;if(typeof e=="object")return e.test(t)}catch{throw new Error("Invalid route config!")}return!1};var j=class{constructor(t){this.loadedKeys={};this.currentRoute=(0,a.writable)();this.config=(0,a.writable)();this.isLoading=(0,a.writable)(!1);this.promises=[];this.loading={subscribe:this.isLoading.subscribe,toPromise:()=>Promise.all(this.promises),get:()=>(0,a.get)(this.isLoading)};this.privateTranslations=(0,a.writable)({});this.translations={subscribe:this.privateTranslations.subscribe,get:()=>(0,a.get)(this.translations)};this.locales=l(r({},(0,a.derived)([this.config,this.privateTranslations],([t,e])=>{if(!t)return[];let{loaders:o=[]}=t,s=o.map(({locale:n})=>`${n}`.toLowerCase()),i=Object.keys(e).map(n=>`${n}`.toLowerCase());return[...new Set([...s,...i])]},[])),{get:()=>(0,a.get)(this.locales)});this.internalLocale=(0,a.writable)();this.locale=l(r({set:this.internalLocale.set,update:this.internalLocale.update},(0,a.derived)(this.internalLocale,(t,e)=>{let o=t&&`${t}`.toLowerCase(),s=this.getLocale(o);s&&s!==this.locale.get()&&e(s)})),{get:()=>(0,a.get)(this.locale)});this.loaderTrigger=(0,a.derived)([this.internalLocale,this.currentRoute],([t,e],o)=>{t!==void 0&&e!==void 0&&o([t,e])},[]);this.initialized=(0,a.derived)([this.locale,this.currentRoute,this.privateTranslations],([t,e,o],s)=>{(0,a.get)(this.initialized)||s(t!==void 0&&e!==void 0&&!!Object.keys(o).length)});this.translation=(0,a.derived)([this.privateTranslations,this.locale,this.isLoading],([t,e,o],s)=>{let i=t[e];i&&Object.keys(i).length&&!o&&s(i)},{});this.t=l(r({},(0,a.derived)([this.config,this.translation],([{parser:t,fallbackLocale:e}])=>(o,s)=>t==null?void 0:t.parse({key:o,payload:s,translations:this.translations.get(),locale:this.locale.get(),fallbackLocale:e}))),{get:(...t)=>(0,a.get)(this.t)(...t)});this.l=l(r({},(0,a.derived)([this.config,this.translations],([{parser:t,fallbackLocale:e},o])=>(s,i,n)=>t.parse({key:i,payload:n,translations:o,locale:s,fallbackLocale:e}))),{get:(...t)=>(0,a.get)(this.l)(...t)});this.getLocale=t=>t?`${this.locales.get().find(s=>`${s}`.toLowerCase()===`${t}`.toLowerCase())||""}`.toLowerCase():"";this.setLocale=async t=>{!t||(this.internalLocale.set(t),await this.loading.toPromise())};this.setRoute=async t=>{t!==(0,a.get)(this.currentRoute)&&this.currentRoute.set(t),await this.loading.toPromise()};this.loadConfig=async t=>{if(!t)throw new Error("No config!");this.config.set(t);let{initLocale:e="",translations:o}=t;o&&this.addTranslations(o),await this.loadTranslations(e)};this.addTranslations=(t,e)=>{if(!t)return;let o=Object.keys(c(t));this.privateTranslations.update(s=>o.reduce((i,n)=>l(r({},i),{[n]:r(r({},c(i[n])),m(t[n]))}),s)),o.forEach(s=>{let i=Object.keys(t[s]).map(n=>`${n}`.split(".")[0]);e&&(i=e[s]),this.loadedKeys[s]=Array.from(new Set([...c(this.loadedKeys[s],[]),...c(i,[])]))})};this.getTranslationProps=async(t=this.locale.get(),e=(0,a.get)(this.currentRoute))=>{let o=(0,a.get)(this.config);if(!o||!t)return[];let s=this.translations.get(),{loaders:i,fallbackLocale:n=""}=c(o),p=`${t}`.toLowerCase(),u=n&&`${n}`.toLowerCase(),w=s[p],S=s[u],R=c(i,[]).map(C=>{var g=C,{locale:d}=g,h=b(g,["locale"]);return l(r({},h),{locale:`${d}`.toLowerCase()})}).filter(({routes:d})=>!d||c(d,[]).some($(e))).filter(({key:d,locale:h})=>h===p&&(!w||!c(this.loadedKeys[p],[]).includes(d))||n&&h===u&&(!S||!c(this.loadedKeys[u],[]).includes(d)));if(R.length){this.isLoading.set(!0);let d=await x(R);this.isLoading.set(!1);let h=Object.keys(d).reduce((g,f)=>l(r({},g),{[f]:Object.keys(d[f])}),{}),C=R.filter(({key:g,locale:f})=>c(h[f],[]).some(L=>`${L}`.startsWith(g))).reduce((g,{key:f,locale:L})=>l(r({},g),{[L]:[...g[L]||[],f]}),{});return[d,C]}return[]};this.translationLoader=async t=>{this.promises.push(new Promise(async e=>{let o=await this.getTranslationProps(t);o.length&&this.addTranslations(...o),e()})),await this.loading.toPromise()};this.loadTranslations=async(t,e=(0,a.get)(this.currentRoute)||"")=>{!t||(this.setRoute(e),this.setLocale(t),await this.loading.toPromise())};t&&this.loadConfig(t),this.loaderTrigger.subscribe(()=>this.translationLoader())}};module.exports=M(A);0&&(module.exports={});
1
+ var b=Object.defineProperty,E=Object.defineProperties,O=Object.getOwnPropertyDescriptor,W=Object.getOwnPropertyDescriptors,K=Object.getOwnPropertyNames,T=Object.getOwnPropertySymbols;var v=Object.prototype.hasOwnProperty,P=Object.prototype.propertyIsEnumerable;var F=(s,t,e)=>t in s?b(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e,r=(s,t)=>{for(var e in t||(t={}))v.call(t,e)&&F(s,e,t[e]);if(T)for(var e of T(t))P.call(t,e)&&F(s,e,t[e]);return s},c=(s,t)=>E(s,W(t)),D=s=>b(s,"__esModule",{value:!0});var m=(s,t)=>{var e={};for(var o in s)v.call(s,o)&&t.indexOf(o)<0&&(e[o]=s[o]);if(s!=null&&T)for(var o of T(s))t.indexOf(o)<0&&P.call(s,o)&&(e[o]=s[o]);return e};var N=(s,t)=>{for(var e in t)b(s,e,{get:t[e],enumerable:!0})},M=(s,t,e,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of K(t))!v.call(s,i)&&(e||i!=="default")&&b(s,i,{get:()=>t[i],enumerable:!(o=O(t,i))||o.enumerable});return s};var z=(s=>(t,e)=>s&&s.get(t)||(e=M(D({}),t,1),s&&s.set(t,e),e))(typeof WeakMap!="undefined"?new WeakMap:0);var V={};N(V,{default:()=>I});var a=require("svelte/store");var d=(s,t={})=>s||t,w=(s,t)=>Object.keys(d(s)).reduce((e,o)=>{let i=s[o],n=t?`${t}.${o}`:`${o}`;return i&&typeof i=="object"?r(r({},e),w(i,n)):c(r({},e),{[n]:i})},{}),x=async s=>{try{return(await Promise.all(s.map(i=>{var n=i,{loader:e}=n,o=m(n,["loader"]);return new Promise(async l=>{let h;try{h=await e()}catch(p){console.error(`Failed to load translation. Verify your '${o.locale}' > '${o.key}' loader.`),console.error(p)}l(c(r({loader:e},o),{data:h}))})}))).reduce((e,{key:o,data:i,locale:n})=>i?c(r({},e),{[n]:w(c(r({},d(e[n])),{[o]:i}))}):e,{})}catch(t){console.error(t)}return{}},$=s=>t=>{try{if(typeof t=="string")return t===s;if(typeof t=="object")return t.test(s)}catch{throw new Error("Invalid route config!")}return!1};var j=class{constructor(t){this.loadedKeys={};this.currentRoute=(0,a.writable)();this.config=(0,a.writable)();this.isLoading=(0,a.writable)(!1);this.promises=[];this.loading={subscribe:this.isLoading.subscribe,toPromise:()=>Promise.all(this.promises),get:()=>(0,a.get)(this.isLoading)};this.privateTranslations=(0,a.writable)({});this.translations={subscribe:this.privateTranslations.subscribe,get:()=>(0,a.get)(this.translations)};this.locales=c(r({},(0,a.derived)([this.config,this.privateTranslations],([t,e])=>{if(!t)return[];let{loaders:o=[]}=t,i=o.map(({locale:l})=>`${l}`.toLowerCase()),n=Object.keys(e).map(l=>`${l}`.toLowerCase());return[...new Set([...i,...n])]},[])),{get:()=>(0,a.get)(this.locales)});this.internalLocale=(0,a.writable)();this.locale=c(r({set:this.internalLocale.set,update:this.internalLocale.update},(0,a.derived)(this.internalLocale,(t,e)=>{let o=t&&`${t}`.toLowerCase(),i=this.getLocale(o);i&&i!==this.locale.get()&&e(i)})),{get:()=>(0,a.get)(this.locale)});this.loaderTrigger=(0,a.derived)([this.internalLocale,this.currentRoute],([t,e],o)=>{t!==void 0&&e!==void 0&&o([t,e])},[]);this.initialized=(0,a.derived)([this.locale,this.currentRoute,this.privateTranslations],([t,e,o],i)=>{(0,a.get)(this.initialized)||i(t!==void 0&&e!==void 0&&!!Object.keys(o).length)});this.translation=(0,a.derived)([this.privateTranslations,this.locale,this.isLoading],([t,e,o],i)=>{let n=t[e];n&&Object.keys(n).length&&!o&&i(n)},{});this.t=c(r({},(0,a.derived)([this.config,this.translation],([{parser:t,fallbackLocale:e}])=>(o,i)=>t==null?void 0:t.parse({key:o,payload:i,translations:this.translations.get(),locale:this.locale.get(),fallbackLocale:e}))),{get:(...t)=>(0,a.get)(this.t)(...t)});this.l=c(r({},(0,a.derived)([this.config,this.translations],([{parser:t,fallbackLocale:e},o])=>(i,n,l)=>t.parse({key:n,payload:l,translations:o,locale:i,fallbackLocale:e}))),{get:(...t)=>(0,a.get)(this.l)(...t)});this.getLocale=t=>t?`${this.locales.get().find(i=>`${i}`.toLowerCase()===`${t}`.toLowerCase())||""}`.toLowerCase():"";this.setLocale=async t=>{!t||(this.internalLocale.set(t),await this.loading.toPromise())};this.setRoute=async t=>{t!==(0,a.get)(this.currentRoute)&&this.currentRoute.set(t),await this.loading.toPromise()};this.loadConfig=async t=>{await this.configLoader(t)};this.addTranslations=(t,e)=>{if(!t)return;let o=Object.keys(d(t));this.privateTranslations.update(i=>o.reduce((n,l)=>c(r({},n),{[l]:r(r({},d(n[l])),w(t[l]))}),i)),o.forEach(i=>{let n=Object.keys(t[i]).map(l=>`${l}`.split(".")[0]);e&&(n=e[i]),this.loadedKeys[i]=Array.from(new Set([...d(this.loadedKeys[i],[]),...d(n,[])]))})};this.getTranslationProps=async(t=this.locale.get(),e=(0,a.get)(this.currentRoute))=>{let o=(0,a.get)(this.config);if(!o||!t)return[];let i=this.translations.get(),{loaders:n,fallbackLocale:l=""}=d(o),h=`${t}`.toLowerCase(),p=l&&`${l}`.toLowerCase(),S=i[h],k=i[p],R=d(n,[]).map(C=>{var u=C,{locale:g}=u,f=m(u,["locale"]);return c(r({},f),{locale:`${g}`.toLowerCase()})}).filter(({routes:g})=>!g||d(g,[]).some($(e))).filter(({key:g,locale:f})=>f===h&&(!S||!d(this.loadedKeys[h],[]).includes(g))||l&&f===p&&(!k||!d(this.loadedKeys[p],[]).includes(g)));if(R.length){this.isLoading.set(!0);let g=await x(R);this.isLoading.set(!1);let f=Object.keys(g).reduce((u,L)=>c(r({},u),{[L]:Object.keys(g[L])}),{}),C=R.filter(({key:u,locale:L})=>d(f[L],[]).some(y=>`${y}`.startsWith(u))).reduce((u,{key:L,locale:y})=>c(r({},u),{[y]:[...u[y]||[],L]}),{});return[g,C]}return[]};this.translationLoader=async t=>{this.promises.push(new Promise(async e=>{let o=await this.getTranslationProps(t);o.length&&this.addTranslations(...o),e()})),await this.loading.toPromise()};this.loadTranslations=async(t,e=(0,a.get)(this.currentRoute)||"")=>{!t||(this.setRoute(e),this.setLocale(t),await this.loading.toPromise())};t&&this.loadConfig(t),this.loaderTrigger.subscribe(()=>this.translationLoader())}async configLoader(t){if(!t)throw new Error("No config!");this.config.set(t);let{initLocale:e="",translations:o}=t;o&&this.addTranslations(o),await this.loadTranslations(e)}};var I=j;module.exports=z(V);0&&(module.exports={});
package/dist/index.d.ts CHANGED
@@ -41,7 +41,7 @@ type Config = {
41
41
  fallbackLocale?: string;
42
42
  };
43
43
 
44
- declare class export_default{
44
+ declare class I18n {
45
45
  constructor(config?: Config);
46
46
  private loadedKeys;
47
47
  private currentRoute;
@@ -62,6 +62,7 @@ declare class export_default{
62
62
  private getLocale;
63
63
  setLocale: (locale?: string | undefined) => Promise<void>;
64
64
  setRoute: (route: string) => Promise<void>;
65
+ configLoader(config: Config): Promise<void>;
65
66
  loadConfig: (config: Config) => Promise<void>;
66
67
  addTranslations: (translations?: ConfigTranslations | undefined, keys?: Record<string, string[]> | undefined) => void;
67
68
  getTranslationProps: ($locale?: string, $route?: string) => Promise<[ConfigTranslations, Record<string, string[]>] | [
@@ -70,4 +71,4 @@ declare class export_default{
70
71
  loadTranslations: (locale: string, route?: string) => Promise<void>;
71
72
  }
72
73
 
73
- export { Config, Parser, export_default as default };
74
+ export { Config, Parser, I18n as default };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- var k=Object.defineProperty,E=Object.defineProperties;var O=Object.getOwnPropertyDescriptors;var b=Object.getOwnPropertySymbols;var P=Object.prototype.hasOwnProperty,x=Object.prototype.propertyIsEnumerable;var F=(t,e,o)=>e in t?k(t,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):t[e]=o,n=(t,e)=>{for(var o in e||(e={}))P.call(e,o)&&F(t,o,e[o]);if(b)for(var o of b(e))x.call(e,o)&&F(t,o,e[o]);return t},r=(t,e)=>E(t,O(e));var m=(t,e)=>{var o={};for(var s in t)P.call(t,s)&&e.indexOf(s)<0&&(o[s]=t[s]);if(t!=null&&b)for(var s of b(t))e.indexOf(s)<0&&x.call(t,s)&&(o[s]=t[s]);return o};import{derived as u,get as d,writable as y}from"svelte/store";var l=(t,e={})=>t||e,w=(t,e)=>Object.keys(l(t)).reduce((o,s)=>{let i=t[s],a=e?`${e}.${s}`:`${s}`;return i&&typeof i=="object"?n(n({},o),w(i,a)):r(n({},o),{[a]:i})},{}),$=async t=>{try{return(await Promise.all(t.map(i=>{var a=i,{loader:o}=a,s=m(a,["loader"]);return new Promise(async L=>{let h;try{h=await o()}catch(R){console.error(`Failed to load translation. Verify your '${s.locale}' > '${s.key}' loader.`),console.error(R)}L(r(n({loader:o},s),{data:h}))})}))).reduce((o,{key:s,data:i,locale:a})=>i?r(n({},o),{[a]:w(r(n({},l(o[a])),{[s]:i}))}):o,{})}catch(e){console.error(e)}return{}},j=t=>e=>{try{if(typeof e=="string")return e===t;if(typeof e=="object")return e.test(t)}catch{throw new Error("Invalid route config!")}return!1};var D=class{constructor(t){this.loadedKeys={};this.currentRoute=y();this.config=y();this.isLoading=y(!1);this.promises=[];this.loading={subscribe:this.isLoading.subscribe,toPromise:()=>Promise.all(this.promises),get:()=>d(this.isLoading)};this.privateTranslations=y({});this.translations={subscribe:this.privateTranslations.subscribe,get:()=>d(this.translations)};this.locales=r(n({},u([this.config,this.privateTranslations],([t,e])=>{if(!t)return[];let{loaders:o=[]}=t,s=o.map(({locale:a})=>`${a}`.toLowerCase()),i=Object.keys(e).map(a=>`${a}`.toLowerCase());return[...new Set([...s,...i])]},[])),{get:()=>d(this.locales)});this.internalLocale=y();this.locale=r(n({set:this.internalLocale.set,update:this.internalLocale.update},u(this.internalLocale,(t,e)=>{let o=t&&`${t}`.toLowerCase(),s=this.getLocale(o);s&&s!==this.locale.get()&&e(s)})),{get:()=>d(this.locale)});this.loaderTrigger=u([this.internalLocale,this.currentRoute],([t,e],o)=>{t!==void 0&&e!==void 0&&o([t,e])},[]);this.initialized=u([this.locale,this.currentRoute,this.privateTranslations],([t,e,o],s)=>{d(this.initialized)||s(t!==void 0&&e!==void 0&&!!Object.keys(o).length)});this.translation=u([this.privateTranslations,this.locale,this.isLoading],([t,e,o],s)=>{let i=t[e];i&&Object.keys(i).length&&!o&&s(i)},{});this.t=r(n({},u([this.config,this.translation],([{parser:t,fallbackLocale:e}])=>(o,s)=>t==null?void 0:t.parse({key:o,payload:s,translations:this.translations.get(),locale:this.locale.get(),fallbackLocale:e}))),{get:(...t)=>d(this.t)(...t)});this.l=r(n({},u([this.config,this.translations],([{parser:t,fallbackLocale:e},o])=>(s,i,a)=>t.parse({key:i,payload:a,translations:o,locale:s,fallbackLocale:e}))),{get:(...t)=>d(this.l)(...t)});this.getLocale=t=>t?`${this.locales.get().find(s=>`${s}`.toLowerCase()===`${t}`.toLowerCase())||""}`.toLowerCase():"";this.setLocale=async t=>{!t||(this.internalLocale.set(t),await this.loading.toPromise())};this.setRoute=async t=>{t!==d(this.currentRoute)&&this.currentRoute.set(t),await this.loading.toPromise()};this.loadConfig=async t=>{if(!t)throw new Error("No config!");this.config.set(t);let{initLocale:e="",translations:o}=t;o&&this.addTranslations(o),await this.loadTranslations(e)};this.addTranslations=(t,e)=>{if(!t)return;let o=Object.keys(l(t));this.privateTranslations.update(s=>o.reduce((i,a)=>r(n({},i),{[a]:n(n({},l(i[a])),w(t[a]))}),s)),o.forEach(s=>{let i=Object.keys(t[s]).map(a=>`${a}`.split(".")[0]);e&&(i=e[s]),this.loadedKeys[s]=Array.from(new Set([...l(this.loadedKeys[s],[]),...l(i,[])]))})};this.getTranslationProps=async(t=this.locale.get(),e=d(this.currentRoute))=>{let o=d(this.config);if(!o||!t)return[];let s=this.translations.get(),{loaders:i,fallbackLocale:a=""}=l(o),L=`${t}`.toLowerCase(),h=a&&`${a}`.toLowerCase(),R=s[L],S=s[h],v=l(i,[]).map(C=>{var g=C,{locale:c}=g,f=m(g,["locale"]);return r(n({},f),{locale:`${c}`.toLowerCase()})}).filter(({routes:c})=>!c||l(c,[]).some(j(e))).filter(({key:c,locale:f})=>f===L&&(!R||!l(this.loadedKeys[L],[]).includes(c))||a&&f===h&&(!S||!l(this.loadedKeys[h],[]).includes(c)));if(v.length){this.isLoading.set(!0);let c=await $(v);this.isLoading.set(!1);let f=Object.keys(c).reduce((g,p)=>r(n({},g),{[p]:Object.keys(c[p])}),{}),C=v.filter(({key:g,locale:p})=>l(f[p],[]).some(T=>`${T}`.startsWith(g))).reduce((g,{key:p,locale:T})=>r(n({},g),{[T]:[...g[T]||[],p]}),{});return[c,C]}return[]};this.translationLoader=async t=>{this.promises.push(new Promise(async e=>{let o=await this.getTranslationProps(t);o.length&&this.addTranslations(...o),e()})),await this.loading.toPromise()};this.loadTranslations=async(t,e=d(this.currentRoute)||"")=>{!t||(this.setRoute(e),this.setLocale(t),await this.loading.toPromise())};t&&this.loadConfig(t),this.loaderTrigger.subscribe(()=>this.translationLoader())}};export{D as default};
1
+ var O=Object.defineProperty,W=Object.defineProperties;var K=Object.getOwnPropertyDescriptors;var m=Object.getOwnPropertySymbols;var P=Object.prototype.hasOwnProperty,x=Object.prototype.propertyIsEnumerable;var F=(i,t,e)=>t in i?O(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e,n=(i,t)=>{for(var e in t||(t={}))P.call(t,e)&&F(i,e,t[e]);if(m)for(var e of m(t))x.call(t,e)&&F(i,e,t[e]);return i},l=(i,t)=>W(i,K(t));var w=(i,t)=>{var e={};for(var o in i)P.call(i,o)&&t.indexOf(o)<0&&(e[o]=i[o]);if(i!=null&&m)for(var o of m(i))t.indexOf(o)<0&&x.call(i,o)&&(e[o]=i[o]);return e};import{derived as h,get as g,writable as T}from"svelte/store";var c=(i,t={})=>i||t,R=(i,t)=>Object.keys(c(i)).reduce((e,o)=>{let s=i[o],a=t?`${t}.${o}`:`${o}`;return s&&typeof s=="object"?n(n({},e),R(s,a)):l(n({},e),{[a]:s})},{}),$=async i=>{try{return(await Promise.all(i.map(s=>{var a=s,{loader:e}=a,o=w(a,["loader"]);return new Promise(async r=>{let f;try{f=await e()}catch(y){console.error(`Failed to load translation. Verify your '${o.locale}' > '${o.key}' loader.`),console.error(y)}r(l(n({loader:e},o),{data:f}))})}))).reduce((e,{key:o,data:s,locale:a})=>s?l(n({},e),{[a]:R(l(n({},c(e[a])),{[o]:s}))}):e,{})}catch(t){console.error(t)}return{}},j=i=>t=>{try{if(typeof t=="string")return t===i;if(typeof t=="object")return t.test(i)}catch{throw new Error("Invalid route config!")}return!1};var S=class{constructor(t){this.loadedKeys={};this.currentRoute=T();this.config=T();this.isLoading=T(!1);this.promises=[];this.loading={subscribe:this.isLoading.subscribe,toPromise:()=>Promise.all(this.promises),get:()=>g(this.isLoading)};this.privateTranslations=T({});this.translations={subscribe:this.privateTranslations.subscribe,get:()=>g(this.translations)};this.locales=l(n({},h([this.config,this.privateTranslations],([t,e])=>{if(!t)return[];let{loaders:o=[]}=t,s=o.map(({locale:r})=>`${r}`.toLowerCase()),a=Object.keys(e).map(r=>`${r}`.toLowerCase());return[...new Set([...s,...a])]},[])),{get:()=>g(this.locales)});this.internalLocale=T();this.locale=l(n({set:this.internalLocale.set,update:this.internalLocale.update},h(this.internalLocale,(t,e)=>{let o=t&&`${t}`.toLowerCase(),s=this.getLocale(o);s&&s!==this.locale.get()&&e(s)})),{get:()=>g(this.locale)});this.loaderTrigger=h([this.internalLocale,this.currentRoute],([t,e],o)=>{t!==void 0&&e!==void 0&&o([t,e])},[]);this.initialized=h([this.locale,this.currentRoute,this.privateTranslations],([t,e,o],s)=>{g(this.initialized)||s(t!==void 0&&e!==void 0&&!!Object.keys(o).length)});this.translation=h([this.privateTranslations,this.locale,this.isLoading],([t,e,o],s)=>{let a=t[e];a&&Object.keys(a).length&&!o&&s(a)},{});this.t=l(n({},h([this.config,this.translation],([{parser:t,fallbackLocale:e}])=>(o,s)=>t==null?void 0:t.parse({key:o,payload:s,translations:this.translations.get(),locale:this.locale.get(),fallbackLocale:e}))),{get:(...t)=>g(this.t)(...t)});this.l=l(n({},h([this.config,this.translations],([{parser:t,fallbackLocale:e},o])=>(s,a,r)=>t.parse({key:a,payload:r,translations:o,locale:s,fallbackLocale:e}))),{get:(...t)=>g(this.l)(...t)});this.getLocale=t=>t?`${this.locales.get().find(s=>`${s}`.toLowerCase()===`${t}`.toLowerCase())||""}`.toLowerCase():"";this.setLocale=async t=>{!t||(this.internalLocale.set(t),await this.loading.toPromise())};this.setRoute=async t=>{t!==g(this.currentRoute)&&this.currentRoute.set(t),await this.loading.toPromise()};this.loadConfig=async t=>{await this.configLoader(t)};this.addTranslations=(t,e)=>{if(!t)return;let o=Object.keys(c(t));this.privateTranslations.update(s=>o.reduce((a,r)=>l(n({},a),{[r]:n(n({},c(a[r])),R(t[r]))}),s)),o.forEach(s=>{let a=Object.keys(t[s]).map(r=>`${r}`.split(".")[0]);e&&(a=e[s]),this.loadedKeys[s]=Array.from(new Set([...c(this.loadedKeys[s],[]),...c(a,[])]))})};this.getTranslationProps=async(t=this.locale.get(),e=g(this.currentRoute))=>{let o=g(this.config);if(!o||!t)return[];let s=this.translations.get(),{loaders:a,fallbackLocale:r=""}=c(o),f=`${t}`.toLowerCase(),y=r&&`${r}`.toLowerCase(),k=s[f],E=s[y],v=c(a,[]).map(C=>{var u=C,{locale:d}=u,L=w(u,["locale"]);return l(n({},L),{locale:`${d}`.toLowerCase()})}).filter(({routes:d})=>!d||c(d,[]).some(j(e))).filter(({key:d,locale:L})=>L===f&&(!k||!c(this.loadedKeys[f],[]).includes(d))||r&&L===y&&(!E||!c(this.loadedKeys[y],[]).includes(d)));if(v.length){this.isLoading.set(!0);let d=await $(v);this.isLoading.set(!1);let L=Object.keys(d).reduce((u,p)=>l(n({},u),{[p]:Object.keys(d[p])}),{}),C=v.filter(({key:u,locale:p})=>c(L[p],[]).some(b=>`${b}`.startsWith(u))).reduce((u,{key:p,locale:b})=>l(n({},u),{[b]:[...u[b]||[],p]}),{});return[d,C]}return[]};this.translationLoader=async t=>{this.promises.push(new Promise(async e=>{let o=await this.getTranslationProps(t);o.length&&this.addTranslations(...o),e()})),await this.loading.toPromise()};this.loadTranslations=async(t,e=g(this.currentRoute)||"")=>{!t||(this.setRoute(e),this.setLocale(t),await this.loading.toPromise())};t&&this.loadConfig(t),this.loaderTrigger.subscribe(()=>this.translationLoader())}async configLoader(t){if(!t)throw new Error("No config!");this.config.set(t);let{initLocale:e="",translations:o}=t;o&&this.addTranslations(o),await this.loadTranslations(e)}};var V=S;export{V as default};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltekit-i18n/base",
3
- "version": "1.0.0-beta.2",
3
+ "version": "1.0.0-beta.3",
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",