@sveltekit-i18n/base 1.1.0 → 1.1.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,W=Object.defineProperties,D=Object.getOwnPropertyDescriptor,I=Object.getOwnPropertyDescriptors,$=Object.getOwnPropertyNames,y=Object.getOwnPropertySymbols;var S=Object.prototype.hasOwnProperty,K=Object.prototype.propertyIsEnumerable;var F=(a,t,e)=>t in a?T(a,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[t]=e,l=(a,t)=>{for(var e in t||(t={}))S.call(t,e)&&F(a,e,t[e]);if(y)for(var e of y(t))K.call(t,e)&&F(a,e,t[e]);return a},c=(a,t)=>W(a,I(t)),H=a=>T(a,"__esModule",{value:!0});var L=(a,t)=>{var e={};for(var r in a)S.call(a,r)&&t.indexOf(r)<0&&(e[r]=a[r]);if(a!=null&&y)for(var r of y(a))t.indexOf(r)<0&&K.call(a,r)&&(e[r]=a[r]);return e};var V=(a,t)=>{for(var e in t)T(a,e,{get:t[e],enumerable:!0})},M=(a,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of $(t))!S.call(a,s)&&(e||s!=="default")&&T(a,s,{get:()=>t[s],enumerable:!(r=D(t,s))||r.enumerable});return a};var q=(a=>(t,e)=>a&&a.get(t)||(e=M(H({}),t,1),a&&a.set(t,e),e))(typeof WeakMap!="undefined"?new WeakMap:0);var G={};V(G,{default:()=>R});var o=require("svelte/store");var C=({parser:a,key:t,params:e,translations:r,locale:s,fallbackLocale:i})=>{if(!(t&&s))return console.warn("[i18n]: No translation key or locale provided. Skipping translation..."),"";let n=(r[s]||{})[t];return i&&n===void 0&&(n=(r[i]||{})[t]),a.parse(n,e,s,t)},d=(...a)=>a.length?a.filter(t=>!!t).map(t=>{let e=`${t}`.toLowerCase();try{[e]=Intl.Collator.supportedLocalesOf(t)}catch{console.warn(`[i18n]: Non-standard locale provided: '${a}'. Check your 'translations' and 'loaders' in i18n config...`)}return e}):[],b=(a,t)=>Object.keys(a||{}).reduce((e,r)=>{let s=a[r],i=t?`${t}.${r}`:`${r}`;return s&&typeof s=="object"?l(l({},e),b(s,i)):c(l({},e),{[i]:s})},{}),j=async a=>{try{return(await Promise.all(a.map(s=>{var i=s,{loader:e}=i,r=L(i,["loader"]);return new Promise(async n=>{let p;try{p=await e()}catch(P){console.error(`[i18n]: Failed to load translation. Verify your '${r.locale}' > '${r.key}' Loader.`),console.error(P)}n(c(l({loader:e},r),{data:p}))})}))).reduce((e,{key:r,data:s,locale:i})=>{if(!s)return e;let[n]=d(i);return c(l({},e),{[n]:b(c(l({},e[n]||{}),{[r]:s}))})},{})}catch(t){console.error(t)}return{}},k=a=>t=>{try{if(typeof t=="string")return t===a;if(typeof t=="object")return t.test(a)}catch{throw new Error("[i18n]: Invalid route config!")}return!1},N=(a,t)=>{let e=!0;try{e=Object.keys(a).filter(r=>a[r]!==void 0).every(r=>a[r]===t[r])}catch{}return e};var O=1e3*60*60*24,R=class{constructor(t){this.cachedAt=0;this.loadedKeys={};this.currentRoute=(0,o.writable)();this.config=(0,o.writable)();this.isLoading=(0,o.writable)(!1);this.promises=new Set;this.loading={subscribe:this.isLoading.subscribe,toPromise:(t,e)=>{let r=Array.from(this.promises).filter(s=>N({locale:d(t)[0],route:e},s)).map(({promise:s})=>s);return Promise.all(r)},get:()=>(0,o.get)(this.isLoading)};this.privateTranslations=(0,o.writable)({});this.translations={subscribe:this.privateTranslations.subscribe,get:()=>(0,o.get)(this.translations)};this.locales=c(l({},(0,o.derived)([this.config,this.privateTranslations],([t,e])=>{if(!t)return[];let{loaders:r=[]}=t,s=r.map(({locale:n})=>d(n)[0]),i=Object.keys(e).map(n=>d(n)[0]);return Array.from(new Set([...s,...i]))},[])),{get:()=>(0,o.get)(this.locales)});this.internalLocale=(0,o.writable)();this.loaderTrigger=(0,o.derived)([this.internalLocale,this.currentRoute],([t,e],r)=>{var s,i;t!==void 0&&e!==void 0&&(t!==((s=(0,o.get)(this.loaderTrigger))==null?void 0:s[0])||e!==((i=(0,o.get)(this.loaderTrigger))==null?void 0:i[1]))&&r([t,e])},[]);this.localeHelper=(0,o.writable)();this.locale={subscribe:this.localeHelper.subscribe,forceSet:this.localeHelper.set,set:this.internalLocale.set,update:this.internalLocale.update,get:()=>(0,o.get)(this.locale)};this.initialized=(0,o.derived)([this.locale,this.currentRoute,this.privateTranslations],([t,e,r],s)=>{(0,o.get)(this.initialized)||s(t!==void 0&&e!==void 0&&!!Object.keys(r).length)});this.translation=(0,o.derived)([this.privateTranslations,this.locale,this.isLoading],([t,e,r],s)=>{let i=t[e];i&&Object.keys(i).length&&!r&&s(i)},{});this.t=c(l({},(0,o.derived)([this.config,this.translation],([{parser:t,fallbackLocale:e}])=>(r,...s)=>C({parser:t,key:r,params:s,translations:this.translations.get(),locale:this.locale.get(),fallbackLocale:e}))),{get:(t,...e)=>(0,o.get)(this.t)(t,...e)});this.l=c(l({},(0,o.derived)([this.config,this.translations],([{parser:t,fallbackLocale:e},r])=>(s,i,...n)=>C({parser:t,key:i,params:n,translations:r,locale:s,fallbackLocale:e}))),{get:(t,e,...r)=>(0,o.get)(this.l)(t,e,...r)});this.getLocale=t=>{if(!t)return"";let r=this.locales.get().find(s=>s===d(t)[0])||"";return d(r)[0]||""};this.setLocale=t=>{if(!t)return;let[e]=d(t);if(e!==(0,o.get)(this.internalLocale))return this.internalLocale.set(e),this.loading.toPromise(t,(0,o.get)(this.currentRoute))};this.setRoute=t=>{if(t!==(0,o.get)(this.currentRoute)){this.currentRoute.set(t);let e=(0,o.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,o.get)(this.currentRoute))=>{let r=(0,o.get)(this.config);if(!r||!t)return[];let s=this.translations.get(),{loaders:i,fallbackLocale:n="",cache:p=O}=r||{},P=Number.isNaN(+p)?O:+p;this.cachedAt?Date.now()>P+this.cachedAt&&(this.loadedKeys={},this.cachedAt=0):this.cachedAt=Date.now();let[v,w]=d(t,n),E=s[v],A=s[w],x=(i||[]).map(z=>{var f=z,{locale:h}=f,u=L(f,["locale"]);return c(l({},u),{locale:d(h)[0]})}).filter(({routes:h})=>!h||(h||[]).some(k(e))).filter(({key:h,locale:u})=>u===v&&(!E||!(this.loadedKeys[v]||[]).includes(h))||n&&u===w&&(!A||!(this.loadedKeys[w]||[]).includes(h)));if(x.length){this.isLoading.set(!0);let h=await j(x);this.isLoading.set(!1);let u=Object.keys(h).reduce((f,g)=>c(l({},f),{[g]:Object.keys(h[g])}),{}),z=x.filter(({key:f,locale:g})=>(u[g]||[]).some(m=>`${m}`.startsWith(f))).reduce((f,{key:g,locale:m})=>c(l({},f),{[m]:[...f[m]||[],g]}),{});return[h,z]}return[]};this.addTranslations=(t,e)=>{if(!t)return;let r=Object.keys(t||{});this.privateTranslations.update(s=>r.reduce((i,n)=>c(l({},i),{[n]:l(l({},i[n]||{}),b(t[n]))}),s)),r.forEach(s=>{let i=Object.keys(t[s]).map(n=>`${n}`.split(".")[0]);e&&(i=e[s]),this.loadedKeys[s]=Array.from(new Set([...this.loadedKeys[s]||[],...i||[]]))})};this.loader=async([t,e])=>{let r=(async()=>{let s=await this.getTranslationProps(t,e);s.length&&this.addTranslations(...s)})();this.promises.add({locale:t,route:e,promise:r}),r.then(()=>{let s=this.getLocale(t);s&&this.locale.get()!==s&&this.locale.forceSet(s)})};this.loadTranslations=(t,e=(0,o.get)(this.currentRoute)||"")=>{if(!!t)return this.setRoute(e),this.setLocale(t),this.loading.toPromise(t,e)};t&&this.loadConfig(t),this.loaderTrigger.subscribe(this.loader),this.isLoading.subscribe(async e=>{e&&this.promises.size&&(await this.loading.toPromise(),this.promises.clear(),console.info("[i18n]: Promises has been purged."))})}async configLoader(t){if(!t)throw new Error("[i18n]: No config provided!");let n=t,{initLocale:e,fallbackLocale:r,translations:s}=n,i=L(n,["initLocale","fallbackLocale","translations"]);e=d(e)[0],r=d(r)[0],this.config.set(l({initLocale:e,fallbackLocale:r,translations:s},i)),s&&this.addTranslations(s),await this.loadTranslations(e)}};module.exports=q(G);0&&(module.exports={});
1
+ var y=Object.defineProperty,W=Object.defineProperties,D=Object.getOwnPropertyDescriptor,I=Object.getOwnPropertyDescriptors,H=Object.getOwnPropertyNames,L=Object.getOwnPropertySymbols;var x=Object.prototype.hasOwnProperty,K=Object.prototype.propertyIsEnumerable;var F=(a,t,e)=>t in a?y(a,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[t]=e,l=(a,t)=>{for(var e in t||(t={}))x.call(t,e)&&F(a,e,t[e]);if(L)for(var e of L(t))K.call(t,e)&&F(a,e,t[e]);return a},d=(a,t)=>W(a,I(t));var b=(a,t)=>{var e={};for(var s in a)x.call(a,s)&&t.indexOf(s)<0&&(e[s]=a[s]);if(a!=null&&L)for(var s of L(a))t.indexOf(s)<0&&K.call(a,s)&&(e[s]=a[s]);return e};var V=(a,t)=>{for(var e in t)y(a,e,{get:t[e],enumerable:!0})},M=(a,t,e,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of H(t))!x.call(a,r)&&r!==e&&y(a,r,{get:()=>t[r],enumerable:!(s=D(t,r))||s.enumerable});return a};var $=a=>M(y({},"__esModule",{value:!0}),a);var B={};V(B,{default:()=>R});module.exports=$(B);var n=require("svelte/store");var C=({parser:a,key:t,params:e,translations:s,locale:r,fallbackLocale:i})=>{if(!(t&&r))return console.warn("[i18n]: No translation key or locale provided. Skipping translation..."),"";let o=(s[r]||{})[t];return i&&o===void 0&&(o=(s[i]||{})[t]),a.parse(o,e,r,t)},h=(...a)=>a.length?a.filter(t=>!!t).map(t=>{let e=`${t}`.toLowerCase();try{let[s]=Intl.Collator.supportedLocalesOf(t);if(!s)throw new Error(`[i18n]: '${t}' is non-standard.`);e=s}catch{console.warn(`[i18n]: Non-standard locale provided: '${t}'. Check your 'translations' and 'loaders' in i18n config...`)}return e}):[],T=(a,t)=>Object.keys(a||{}).reduce((e,s)=>{let r=a[s],i=t?`${t}.${s}`:`${s}`;return r&&typeof r=="object"?l(l({},e),T(r,i)):d(l({},e),{[i]:r})},{}),j=async a=>{try{return(await Promise.all(a.map(r=>{var i=r,{loader:e}=i,s=b(i,["loader"]);return new Promise(async o=>{let c;try{c=await e()}catch(P){console.error(`[i18n]: Failed to load translation. Verify your '${s.locale}' > '${s.key}' Loader.`),console.error(P)}o(d(l({loader:e},s),{data:c}))})}))).reduce((e,{key:s,data:r,locale:i})=>{if(!r)return e;let[o]=h(i);return d(l({},e),{[o]:T(d(l({},e[o]||{}),{[s]:r}))})},{})}catch(t){console.error(t)}return{}},k=a=>t=>{try{if(typeof t=="string")return t===a;if(typeof t=="object")return t.test(a)}catch{throw new Error("[i18n]: Invalid route config!")}return!1},N=(a,t)=>{let e=!0;try{e=Object.keys(a).filter(s=>a[s]!==void 0).every(s=>a[s]===t[s])}catch{}return e};var A=1e3*60*60*24,R=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 s=Array.from(this.promises).filter(r=>N({locale:h(t)[0],route:e},r)).map(({promise:r})=>r);return Promise.all(s)},get:()=>(0,n.get)(this.isLoading)};this.privateTranslations=(0,n.writable)({});this.translations={subscribe:this.privateTranslations.subscribe,get:()=>(0,n.get)(this.translations)};this.locales=d(l({},(0,n.derived)([this.config,this.privateTranslations],([t,e])=>{if(!t)return[];let{loaders:s=[]}=t,r=s.map(({locale:o})=>h(o)[0]),i=Object.keys(e).map(o=>h(o)[0]);return Array.from(new Set([...r,...i]))},[])),{get:()=>(0,n.get)(this.locales)});this.internalLocale=(0,n.writable)();this.loaderTrigger=(0,n.derived)([this.internalLocale,this.currentRoute],([t,e],s)=>{var r,i,o;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]))&&((o=(0,n.get)(this.config))!=null&&o.debug&&console.debug("[i18n]: Triggering translation load..."),s([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,s],r)=>{(0,n.get)(this.initialized)||r(t!==void 0&&e!==void 0&&!!Object.keys(s).length)});this.translation=(0,n.derived)([this.privateTranslations,this.locale,this.isLoading],([t,e,s],r)=>{let i=t[e];i&&Object.keys(i).length&&!s&&r(i)},{});this.t=d(l({},(0,n.derived)([this.config,this.translation],([{parser:t,fallbackLocale:e}])=>(s,...r)=>C({parser:t,key:s,params:r,translations:this.translations.get(),locale:this.locale.get(),fallbackLocale:e}))),{get:(t,...e)=>(0,n.get)(this.t)(t,...e)});this.l=d(l({},(0,n.derived)([this.config,this.translations],([{parser:t,fallbackLocale:e},s])=>(r,i,...o)=>C({parser:t,key:i,params:o,translations:s,locale:r,fallbackLocale:e}))),{get:(t,e,...s)=>(0,n.get)(this.l)(t,e,...s)});this.getLocale=t=>{if(!t)return"";let s=this.locales.get().find(r=>r===h(t)[0])||"";return h(s)[0]||""};this.setLocale=t=>{var s;if(!t)return;let[e]=h(t);if(e!==(0,n.get)(this.internalLocale))return(s=(0,n.get)(this.config))!=null&&s.debug&&console.debug(`[i18n]: Setting '${e}' locale.`),this.internalLocale.set(e),this.loading.toPromise(t,(0,n.get)(this.currentRoute))};this.setRoute=t=>{var e;if(t!==(0,n.get)(this.currentRoute)){(e=(0,n.get)(this.config))!=null&&e.debug&&console.debug(`[i18n]: Setting '${t}' route.`),this.currentRoute.set(t);let s=(0,n.get)(this.internalLocale);return this.loading.toPromise(s,t)}};this.loadConfig=async t=>{await this.configLoader(t)};this.getTranslationProps=async(t=this.locale.get(),e=(0,n.get)(this.currentRoute))=>{let s=(0,n.get)(this.config);if(!s||!t)return[];let r=this.translations.get(),{loaders:i,fallbackLocale:o="",cache:c=A}=s||{},P=Number.isNaN(+c)?A:+c;this.cachedAt?Date.now()>P+this.cachedAt&&(s!=null&&s.debug&&console.debug("[i18n]: Refreshing cache."),this.loadedKeys={},this.cachedAt=0):(s!=null&&s.debug&&console.debug("[i18n]: Setting cache timestamp."),this.cachedAt=Date.now());let[v,S]=h(t,o),E=r[v],O=r[S],w=(i||[]).map(z=>{var u=z,{locale:g}=u,f=b(u,["locale"]);return d(l({},f),{locale:h(g)[0]})}).filter(({routes:g})=>!g||(g||[]).some(k(e))).filter(({key:g,locale:f})=>f===v&&(!E||!(this.loadedKeys[v]||[]).includes(g))||o&&f===S&&(!O||!(this.loadedKeys[S]||[]).includes(g)));if(w.length){this.isLoading.set(!0),s!=null&&s.debug&&console.debug("[i18n]: Fetching translations...");let g=await j(w);this.isLoading.set(!1);let f=Object.keys(g).reduce((u,p)=>d(l({},u),{[p]:Object.keys(g[p])}),{}),z=w.filter(({key:u,locale:p})=>(f[p]||[]).some(m=>`${m}`.startsWith(u))).reduce((u,{key:p,locale:m})=>d(l({},u),{[m]:[...u[m]||[],p]}),{});return[g,z]}return[]};this.addTranslations=(t,e)=>{var r;if(!t)return;(r=(0,n.get)(this.config))!=null&&r.debug&&console.debug("[i18n]: Adding translations...");let s=Object.keys(t||{});this.privateTranslations.update(i=>s.reduce((o,c)=>d(l({},o),{[c]:l(l({},o[c]||{}),T(t[c]))}),i)),s.forEach(i=>{let o=Object.keys(t[i]).map(c=>`${c}`.split(".")[0]);e&&(o=e[i]),this.loadedKeys[i]=Array.from(new Set([...this.loadedKeys[i]||[],...o||[]]))})};this.loader=async([t,e])=>{var r;(r=(0,n.get)(this.config))!=null&&r.debug&&console.debug("[i18n]: Adding loader promise.");let s=(async()=>{let i=await this.getTranslationProps(t,e);i.length&&this.addTranslations(...i)})();this.promises.add({locale:t,route:e,promise:s}),s.then(()=>{let i=this.getLocale(t);i&&this.locale.get()!==i&&this.locale.forceSet(i)})};this.loadTranslations=(t,e=(0,n.get)(this.currentRoute)||"")=>{if(!!t)return this.setRoute(e),this.setLocale(t),this.loading.toPromise(t,e)};t&&this.loadConfig(t),this.loaderTrigger.subscribe(this.loader),this.isLoading.subscribe(async e=>{var s;e&&this.promises.size&&(await this.loading.toPromise(),this.promises.clear(),(s=(0,n.get)(this.config))!=null&&s.debug&&console.debug("[i18n]: Loader promises have been purged."))})}async configLoader(t){if(!t)throw new Error("[i18n]: No config provided!");let c=t,{initLocale:e,fallbackLocale:s,translations:r,debug:i}=c,o=b(c,["initLocale","fallbackLocale","translations","debug"]);[e]=h(e),[s]=h(s),i&&console.debug("[i18n]: Setting config."),this.config.set(l({initLocale:e,fallbackLocale:s,translations:r,debug:i},o)),r&&this.addTranslations(r),await this.loadTranslations(e)}};0&&(module.exports={});
package/dist/index.d.ts CHANGED
@@ -27,6 +27,7 @@ declare module Config {
27
27
  fallbackLocale?: FallbackLocale;
28
28
  parser: Parser.T<P>;
29
29
  cache?: number;
30
+ debug?: boolean;
30
31
  };
31
32
  }
32
33
  declare module Loader {
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- var D=Object.defineProperty,I=Object.defineProperties;var $=Object.getOwnPropertyDescriptors;var b=Object.getOwnPropertySymbols;var F=Object.prototype.hasOwnProperty,K=Object.prototype.propertyIsEnumerable;var z=(a,t,e)=>t in a?D(a,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[t]=e,n=(a,t)=>{for(var e in t||(t={}))F.call(t,e)&&z(a,e,t[e]);if(b)for(var e of b(t))K.call(t,e)&&z(a,e,t[e]);return a},c=(a,t)=>I(a,$(t));var y=(a,t)=>{var e={};for(var r in a)F.call(a,r)&&t.indexOf(r)<0&&(e[r]=a[r]);if(a!=null&&b)for(var r of b(a))t.indexOf(r)<0&&K.call(a,r)&&(e[r]=a[r]);return e};import{derived as p,get as l,writable as L}from"svelte/store";var C=({parser:a,key:t,params:e,translations:r,locale:s,fallbackLocale:i})=>{if(!(t&&s))return console.warn("[i18n]: No translation key or locale provided. Skipping translation..."),"";let o=(r[s]||{})[t];return i&&o===void 0&&(o=(r[i]||{})[t]),a.parse(o,e,s,t)},d=(...a)=>a.length?a.filter(t=>!!t).map(t=>{let e=`${t}`.toLowerCase();try{[e]=Intl.Collator.supportedLocalesOf(t)}catch{console.warn(`[i18n]: Non-standard locale provided: '${a}'. Check your 'translations' and 'loaders' in i18n config...`)}return e}):[],P=(a,t)=>Object.keys(a||{}).reduce((e,r)=>{let s=a[r],i=t?`${t}.${r}`:`${r}`;return s&&typeof s=="object"?n(n({},e),P(s,i)):c(n({},e),{[i]:s})},{}),j=async a=>{try{return(await Promise.all(a.map(s=>{var i=s,{loader:e}=i,r=y(i,["loader"]);return new Promise(async o=>{let m;try{m=await e()}catch(v){console.error(`[i18n]: Failed to load translation. Verify your '${r.locale}' > '${r.key}' Loader.`),console.error(v)}o(c(n({loader:e},r),{data:m}))})}))).reduce((e,{key:r,data:s,locale:i})=>{if(!s)return e;let[o]=d(i);return c(n({},e),{[o]:P(c(n({},e[o]||{}),{[r]:s}))})},{})}catch(t){console.error(t)}return{}},k=a=>t=>{try{if(typeof t=="string")return t===a;if(typeof t=="object")return t.test(a)}catch{throw new Error("[i18n]: Invalid route config!")}return!1},N=(a,t)=>{let e=!0;try{e=Object.keys(a).filter(r=>a[r]!==void 0).every(r=>a[r]===t[r])}catch{}return e};var O=1e3*60*60*24,E=class{constructor(t){this.cachedAt=0;this.loadedKeys={};this.currentRoute=L();this.config=L();this.isLoading=L(!1);this.promises=new Set;this.loading={subscribe:this.isLoading.subscribe,toPromise:(t,e)=>{let r=Array.from(this.promises).filter(s=>N({locale:d(t)[0],route:e},s)).map(({promise:s})=>s);return Promise.all(r)},get:()=>l(this.isLoading)};this.privateTranslations=L({});this.translations={subscribe:this.privateTranslations.subscribe,get:()=>l(this.translations)};this.locales=c(n({},p([this.config,this.privateTranslations],([t,e])=>{if(!t)return[];let{loaders:r=[]}=t,s=r.map(({locale:o})=>d(o)[0]),i=Object.keys(e).map(o=>d(o)[0]);return Array.from(new Set([...s,...i]))},[])),{get:()=>l(this.locales)});this.internalLocale=L();this.loaderTrigger=p([this.internalLocale,this.currentRoute],([t,e],r)=>{var s,i;t!==void 0&&e!==void 0&&(t!==((s=l(this.loaderTrigger))==null?void 0:s[0])||e!==((i=l(this.loaderTrigger))==null?void 0:i[1]))&&r([t,e])},[]);this.localeHelper=L();this.locale={subscribe:this.localeHelper.subscribe,forceSet:this.localeHelper.set,set:this.internalLocale.set,update:this.internalLocale.update,get:()=>l(this.locale)};this.initialized=p([this.locale,this.currentRoute,this.privateTranslations],([t,e,r],s)=>{l(this.initialized)||s(t!==void 0&&e!==void 0&&!!Object.keys(r).length)});this.translation=p([this.privateTranslations,this.locale,this.isLoading],([t,e,r],s)=>{let i=t[e];i&&Object.keys(i).length&&!r&&s(i)},{});this.t=c(n({},p([this.config,this.translation],([{parser:t,fallbackLocale:e}])=>(r,...s)=>C({parser:t,key:r,params:s,translations:this.translations.get(),locale:this.locale.get(),fallbackLocale:e}))),{get:(t,...e)=>l(this.t)(t,...e)});this.l=c(n({},p([this.config,this.translations],([{parser:t,fallbackLocale:e},r])=>(s,i,...o)=>C({parser:t,key:i,params:o,translations:r,locale:s,fallbackLocale:e}))),{get:(t,e,...r)=>l(this.l)(t,e,...r)});this.getLocale=t=>{if(!t)return"";let r=this.locales.get().find(s=>s===d(t)[0])||"";return d(r)[0]||""};this.setLocale=t=>{if(!t)return;let[e]=d(t);if(e!==l(this.internalLocale))return this.internalLocale.set(e),this.loading.toPromise(t,l(this.currentRoute))};this.setRoute=t=>{if(t!==l(this.currentRoute)){this.currentRoute.set(t);let e=l(this.internalLocale);return this.loading.toPromise(e,t)}};this.loadConfig=async t=>{await this.configLoader(t)};this.getTranslationProps=async(t=this.locale.get(),e=l(this.currentRoute))=>{let r=l(this.config);if(!r||!t)return[];let s=this.translations.get(),{loaders:i,fallbackLocale:o="",cache:m=O}=r||{},v=Number.isNaN(+m)?O:+m;this.cachedAt?Date.now()>v+this.cachedAt&&(this.loadedKeys={},this.cachedAt=0):this.cachedAt=Date.now();let[w,x]=d(t,o),A=s[w],W=s[x],S=(i||[]).map(R=>{var f=R,{locale:h}=f,u=y(f,["locale"]);return c(n({},u),{locale:d(h)[0]})}).filter(({routes:h})=>!h||(h||[]).some(k(e))).filter(({key:h,locale:u})=>u===w&&(!A||!(this.loadedKeys[w]||[]).includes(h))||o&&u===x&&(!W||!(this.loadedKeys[x]||[]).includes(h)));if(S.length){this.isLoading.set(!0);let h=await j(S);this.isLoading.set(!1);let u=Object.keys(h).reduce((f,g)=>c(n({},f),{[g]:Object.keys(h[g])}),{}),R=S.filter(({key:f,locale:g})=>(u[g]||[]).some(T=>`${T}`.startsWith(f))).reduce((f,{key:g,locale:T})=>c(n({},f),{[T]:[...f[T]||[],g]}),{});return[h,R]}return[]};this.addTranslations=(t,e)=>{if(!t)return;let r=Object.keys(t||{});this.privateTranslations.update(s=>r.reduce((i,o)=>c(n({},i),{[o]:n(n({},i[o]||{}),P(t[o]))}),s)),r.forEach(s=>{let i=Object.keys(t[s]).map(o=>`${o}`.split(".")[0]);e&&(i=e[s]),this.loadedKeys[s]=Array.from(new Set([...this.loadedKeys[s]||[],...i||[]]))})};this.loader=async([t,e])=>{let r=(async()=>{let s=await this.getTranslationProps(t,e);s.length&&this.addTranslations(...s)})();this.promises.add({locale:t,route:e,promise:r}),r.then(()=>{let s=this.getLocale(t);s&&this.locale.get()!==s&&this.locale.forceSet(s)})};this.loadTranslations=(t,e=l(this.currentRoute)||"")=>{if(!!t)return this.setRoute(e),this.setLocale(t),this.loading.toPromise(t,e)};t&&this.loadConfig(t),this.loaderTrigger.subscribe(this.loader),this.isLoading.subscribe(async e=>{e&&this.promises.size&&(await this.loading.toPromise(),this.promises.clear(),console.info("[i18n]: Promises has been purged."))})}async configLoader(t){if(!t)throw new Error("[i18n]: No config provided!");let o=t,{initLocale:e,fallbackLocale:r,translations:s}=o,i=y(o,["initLocale","fallbackLocale","translations"]);e=d(e)[0],r=d(r)[0],this.config.set(n({initLocale:e,fallbackLocale:r,translations:s},i)),s&&this.addTranslations(s),await this.loadTranslations(e)}};export{E as default};
1
+ var D=Object.defineProperty,I=Object.defineProperties;var H=Object.getOwnPropertyDescriptors;var T=Object.getOwnPropertySymbols;var F=Object.prototype.hasOwnProperty,K=Object.prototype.propertyIsEnumerable;var z=(i,t,s)=>t in i?D(i,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):i[t]=s,l=(i,t)=>{for(var s in t||(t={}))F.call(t,s)&&z(i,s,t[s]);if(T)for(var s of T(t))K.call(t,s)&&z(i,s,t[s]);return i},d=(i,t)=>I(i,H(t));var L=(i,t)=>{var s={};for(var e in i)F.call(i,e)&&t.indexOf(e)<0&&(s[e]=i[e]);if(i!=null&&T)for(var e of T(i))t.indexOf(e)<0&&K.call(i,e)&&(s[e]=i[e]);return s};import{derived as b,get as o,writable as m}from"svelte/store";var C=({parser:i,key:t,params:s,translations:e,locale:r,fallbackLocale:a})=>{if(!(t&&r))return console.warn("[i18n]: No translation key or locale provided. Skipping translation..."),"";let n=(e[r]||{})[t];return a&&n===void 0&&(n=(e[a]||{})[t]),i.parse(n,s,r,t)},h=(...i)=>i.length?i.filter(t=>!!t).map(t=>{let s=`${t}`.toLowerCase();try{let[e]=Intl.Collator.supportedLocalesOf(t);if(!e)throw new Error(`[i18n]: '${t}' is non-standard.`);s=e}catch{console.warn(`[i18n]: Non-standard locale provided: '${t}'. Check your 'translations' and 'loaders' in i18n config...`)}return s}):[],P=(i,t)=>Object.keys(i||{}).reduce((s,e)=>{let r=i[e],a=t?`${t}.${e}`:`${e}`;return r&&typeof r=="object"?l(l({},s),P(r,a)):d(l({},s),{[a]:r})},{}),j=async i=>{try{return(await Promise.all(i.map(r=>{var a=r,{loader:s}=a,e=L(a,["loader"]);return new Promise(async n=>{let c;try{c=await s()}catch(v){console.error(`[i18n]: Failed to load translation. Verify your '${e.locale}' > '${e.key}' Loader.`),console.error(v)}n(d(l({loader:s},e),{data:c}))})}))).reduce((s,{key:e,data:r,locale:a})=>{if(!r)return s;let[n]=h(a);return d(l({},s),{[n]:P(d(l({},s[n]||{}),{[e]:r}))})},{})}catch(t){console.error(t)}return{}},k=i=>t=>{try{if(typeof t=="string")return t===i;if(typeof t=="object")return t.test(i)}catch{throw new Error("[i18n]: Invalid route config!")}return!1},N=(i,t)=>{let s=!0;try{s=Object.keys(i).filter(e=>i[e]!==void 0).every(e=>i[e]===t[e])}catch{}return s};var A=1e3*60*60*24,E=class{constructor(t){this.cachedAt=0;this.loadedKeys={};this.currentRoute=m();this.config=m();this.isLoading=m(!1);this.promises=new Set;this.loading={subscribe:this.isLoading.subscribe,toPromise:(t,s)=>{let e=Array.from(this.promises).filter(r=>N({locale:h(t)[0],route:s},r)).map(({promise:r})=>r);return Promise.all(e)},get:()=>o(this.isLoading)};this.privateTranslations=m({});this.translations={subscribe:this.privateTranslations.subscribe,get:()=>o(this.translations)};this.locales=d(l({},b([this.config,this.privateTranslations],([t,s])=>{if(!t)return[];let{loaders:e=[]}=t,r=e.map(({locale:n})=>h(n)[0]),a=Object.keys(s).map(n=>h(n)[0]);return Array.from(new Set([...r,...a]))},[])),{get:()=>o(this.locales)});this.internalLocale=m();this.loaderTrigger=b([this.internalLocale,this.currentRoute],([t,s],e)=>{var r,a,n;t!==void 0&&s!==void 0&&(t!==((r=o(this.loaderTrigger))==null?void 0:r[0])||s!==((a=o(this.loaderTrigger))==null?void 0:a[1]))&&((n=o(this.config))!=null&&n.debug&&console.debug("[i18n]: Triggering translation load..."),e([t,s]))},[]);this.localeHelper=m();this.locale={subscribe:this.localeHelper.subscribe,forceSet:this.localeHelper.set,set:this.internalLocale.set,update:this.internalLocale.update,get:()=>o(this.locale)};this.initialized=b([this.locale,this.currentRoute,this.privateTranslations],([t,s,e],r)=>{o(this.initialized)||r(t!==void 0&&s!==void 0&&!!Object.keys(e).length)});this.translation=b([this.privateTranslations,this.locale,this.isLoading],([t,s,e],r)=>{let a=t[s];a&&Object.keys(a).length&&!e&&r(a)},{});this.t=d(l({},b([this.config,this.translation],([{parser:t,fallbackLocale:s}])=>(e,...r)=>C({parser:t,key:e,params:r,translations:this.translations.get(),locale:this.locale.get(),fallbackLocale:s}))),{get:(t,...s)=>o(this.t)(t,...s)});this.l=d(l({},b([this.config,this.translations],([{parser:t,fallbackLocale:s},e])=>(r,a,...n)=>C({parser:t,key:a,params:n,translations:e,locale:r,fallbackLocale:s}))),{get:(t,s,...e)=>o(this.l)(t,s,...e)});this.getLocale=t=>{if(!t)return"";let e=this.locales.get().find(r=>r===h(t)[0])||"";return h(e)[0]||""};this.setLocale=t=>{var e;if(!t)return;let[s]=h(t);if(s!==o(this.internalLocale))return(e=o(this.config))!=null&&e.debug&&console.debug(`[i18n]: Setting '${s}' locale.`),this.internalLocale.set(s),this.loading.toPromise(t,o(this.currentRoute))};this.setRoute=t=>{var s;if(t!==o(this.currentRoute)){(s=o(this.config))!=null&&s.debug&&console.debug(`[i18n]: Setting '${t}' route.`),this.currentRoute.set(t);let e=o(this.internalLocale);return this.loading.toPromise(e,t)}};this.loadConfig=async t=>{await this.configLoader(t)};this.getTranslationProps=async(t=this.locale.get(),s=o(this.currentRoute))=>{let e=o(this.config);if(!e||!t)return[];let r=this.translations.get(),{loaders:a,fallbackLocale:n="",cache:c=A}=e||{},v=Number.isNaN(+c)?A:+c;this.cachedAt?Date.now()>v+this.cachedAt&&(e!=null&&e.debug&&console.debug("[i18n]: Refreshing cache."),this.loadedKeys={},this.cachedAt=0):(e!=null&&e.debug&&console.debug("[i18n]: Setting cache timestamp."),this.cachedAt=Date.now());let[S,w]=h(t,n),O=r[S],W=r[w],x=(a||[]).map(R=>{var u=R,{locale:g}=u,f=L(u,["locale"]);return d(l({},f),{locale:h(g)[0]})}).filter(({routes:g})=>!g||(g||[]).some(k(s))).filter(({key:g,locale:f})=>f===S&&(!O||!(this.loadedKeys[S]||[]).includes(g))||n&&f===w&&(!W||!(this.loadedKeys[w]||[]).includes(g)));if(x.length){this.isLoading.set(!0),e!=null&&e.debug&&console.debug("[i18n]: Fetching translations...");let g=await j(x);this.isLoading.set(!1);let f=Object.keys(g).reduce((u,p)=>d(l({},u),{[p]:Object.keys(g[p])}),{}),R=x.filter(({key:u,locale:p})=>(f[p]||[]).some(y=>`${y}`.startsWith(u))).reduce((u,{key:p,locale:y})=>d(l({},u),{[y]:[...u[y]||[],p]}),{});return[g,R]}return[]};this.addTranslations=(t,s)=>{var r;if(!t)return;(r=o(this.config))!=null&&r.debug&&console.debug("[i18n]: Adding translations...");let e=Object.keys(t||{});this.privateTranslations.update(a=>e.reduce((n,c)=>d(l({},n),{[c]:l(l({},n[c]||{}),P(t[c]))}),a)),e.forEach(a=>{let n=Object.keys(t[a]).map(c=>`${c}`.split(".")[0]);s&&(n=s[a]),this.loadedKeys[a]=Array.from(new Set([...this.loadedKeys[a]||[],...n||[]]))})};this.loader=async([t,s])=>{var r;(r=o(this.config))!=null&&r.debug&&console.debug("[i18n]: Adding loader promise.");let e=(async()=>{let a=await this.getTranslationProps(t,s);a.length&&this.addTranslations(...a)})();this.promises.add({locale:t,route:s,promise:e}),e.then(()=>{let a=this.getLocale(t);a&&this.locale.get()!==a&&this.locale.forceSet(a)})};this.loadTranslations=(t,s=o(this.currentRoute)||"")=>{if(!!t)return this.setRoute(s),this.setLocale(t),this.loading.toPromise(t,s)};t&&this.loadConfig(t),this.loaderTrigger.subscribe(this.loader),this.isLoading.subscribe(async s=>{var e;s&&this.promises.size&&(await this.loading.toPromise(),this.promises.clear(),(e=o(this.config))!=null&&e.debug&&console.debug("[i18n]: Loader promises have been purged."))})}async configLoader(t){if(!t)throw new Error("[i18n]: No config provided!");let c=t,{initLocale:s,fallbackLocale:e,translations:r,debug:a}=c,n=L(c,["initLocale","fallbackLocale","translations","debug"]);[s]=h(s),[e]=h(e),a&&console.debug("[i18n]: Setting config."),this.config.set(l({initLocale:s,fallbackLocale:e,translations:r,debug:a},n)),r&&this.addTranslations(r),await this.loadTranslations(s)}};export{E as default};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltekit-i18n/base",
3
- "version": "1.1.0",
3
+ "version": "1.1.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",