@sveltekit-i18n/base 1.1.2 → 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 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 z=(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)&&z(a,e,t[e]);if(L)for(var e of L(t))K.call(t,e)&&z(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{if([e]=Intl.Collator.supportedLocalesOf(t),!e)throw new Error(`[i18n]: '${t}' is non-standard.`)}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(F=>{var u=F,{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])}),{}),F=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,F]}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={});
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.js CHANGED
@@ -1 +1 @@
1
- var D=Object.defineProperty,I=Object.defineProperties;var H=Object.getOwnPropertyDescriptors;var T=Object.getOwnPropertySymbols;var z=Object.prototype.hasOwnProperty,K=Object.prototype.propertyIsEnumerable;var F=(i,t,e)=>t in i?D(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e,l=(i,t)=>{for(var e in t||(t={}))z.call(t,e)&&F(i,e,t[e]);if(T)for(var e of T(t))K.call(t,e)&&F(i,e,t[e]);return i},d=(i,t)=>I(i,H(t));var L=(i,t)=>{var e={};for(var s in i)z.call(i,s)&&t.indexOf(s)<0&&(e[s]=i[s]);if(i!=null&&T)for(var s of T(i))t.indexOf(s)<0&&K.call(i,s)&&(e[s]=i[s]);return e};import{derived as b,get as o,writable as m}from"svelte/store";var C=({parser:i,key:t,params:e,translations:s,locale:r,fallbackLocale:a})=>{if(!(t&&r))return console.warn("[i18n]: No translation key or locale provided. Skipping translation..."),"";let n=(s[r]||{})[t];return a&&n===void 0&&(n=(s[a]||{})[t]),i.parse(n,e,r,t)},h=(...i)=>i.length?i.filter(t=>!!t).map(t=>{let e=`${t}`.toLowerCase();try{if([e]=Intl.Collator.supportedLocalesOf(t),!e)throw new Error(`[i18n]: '${t}' is non-standard.`)}catch{console.warn(`[i18n]: Non-standard locale provided: '${t}'. Check your 'translations' and 'loaders' in i18n config...`)}return e}):[],P=(i,t)=>Object.keys(i||{}).reduce((e,s)=>{let r=i[s],a=t?`${t}.${s}`:`${s}`;return r&&typeof r=="object"?l(l({},e),P(r,a)):d(l({},e),{[a]:r})},{}),j=async i=>{try{return(await Promise.all(i.map(r=>{var a=r,{loader:e}=a,s=L(a,["loader"]);return new Promise(async n=>{let c;try{c=await e()}catch(v){console.error(`[i18n]: Failed to load translation. Verify your '${s.locale}' > '${s.key}' Loader.`),console.error(v)}n(d(l({loader:e},s),{data:c}))})}))).reduce((e,{key:s,data:r,locale:a})=>{if(!r)return e;let[n]=h(a);return d(l({},e),{[n]:P(d(l({},e[n]||{}),{[s]: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 e=!0;try{e=Object.keys(i).filter(s=>i[s]!==void 0).every(s=>i[s]===t[s])}catch{}return e};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,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:()=>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,e])=>{if(!t)return[];let{loaders:s=[]}=t,r=s.map(({locale:n})=>h(n)[0]),a=Object.keys(e).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,e],s)=>{var r,a,n;t!==void 0&&e!==void 0&&(t!==((r=o(this.loaderTrigger))==null?void 0:r[0])||e!==((a=o(this.loaderTrigger))==null?void 0:a[1]))&&((n=o(this.config))!=null&&n.debug&&console.debug("[i18n]: Triggering translation load..."),s([t,e]))},[]);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,e,s],r)=>{o(this.initialized)||r(t!==void 0&&e!==void 0&&!!Object.keys(s).length)});this.translation=b([this.privateTranslations,this.locale,this.isLoading],([t,e,s],r)=>{let a=t[e];a&&Object.keys(a).length&&!s&&r(a)},{});this.t=d(l({},b([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)=>o(this.t)(t,...e)});this.l=d(l({},b([this.config,this.translations],([{parser:t,fallbackLocale:e},s])=>(r,a,...n)=>C({parser:t,key:a,params:n,translations:s,locale:r,fallbackLocale:e}))),{get:(t,e,...s)=>o(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!==o(this.internalLocale))return(s=o(this.config))!=null&&s.debug&&console.debug(`[i18n]: Setting '${e}' locale.`),this.internalLocale.set(e),this.loading.toPromise(t,o(this.currentRoute))};this.setRoute=t=>{var e;if(t!==o(this.currentRoute)){(e=o(this.config))!=null&&e.debug&&console.debug(`[i18n]: Setting '${t}' route.`),this.currentRoute.set(t);let s=o(this.internalLocale);return this.loading.toPromise(s,t)}};this.loadConfig=async t=>{await this.configLoader(t)};this.getTranslationProps=async(t=this.locale.get(),e=o(this.currentRoute))=>{let s=o(this.config);if(!s||!t)return[];let r=this.translations.get(),{loaders:a,fallbackLocale:n="",cache:c=A}=s||{},v=Number.isNaN(+c)?A:+c;this.cachedAt?Date.now()>v+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[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(e))).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),s!=null&&s.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,e)=>{var r;if(!t)return;(r=o(this.config))!=null&&r.debug&&console.debug("[i18n]: Adding translations...");let s=Object.keys(t||{});this.privateTranslations.update(a=>s.reduce((n,c)=>d(l({},n),{[c]:l(l({},n[c]||{}),P(t[c]))}),a)),s.forEach(a=>{let n=Object.keys(t[a]).map(c=>`${c}`.split(".")[0]);e&&(n=e[a]),this.loadedKeys[a]=Array.from(new Set([...this.loadedKeys[a]||[],...n||[]]))})};this.loader=async([t,e])=>{var r;(r=o(this.config))!=null&&r.debug&&console.debug("[i18n]: Adding loader promise.");let s=(async()=>{let a=await this.getTranslationProps(t,e);a.length&&this.addTranslations(...a)})();this.promises.add({locale:t,route:e,promise:s}),s.then(()=>{let a=this.getLocale(t);a&&this.locale.get()!==a&&this.locale.forceSet(a)})};this.loadTranslations=(t,e=o(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=o(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:a}=c,n=L(c,["initLocale","fallbackLocale","translations","debug"]);[e]=h(e),[s]=h(s),a&&console.debug("[i18n]: Setting config."),this.config.set(l({initLocale:e,fallbackLocale:s,translations:r,debug:a},n)),r&&this.addTranslations(r),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.2",
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",