@sveltekit-i18n/base 1.2.0 → 1.2.1

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