@sveltekit-i18n/base 1.3.5 → 1.3.6

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
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});var H=Object.defineProperty,q=Object.defineProperties;var B=Object.getOwnPropertyDescriptors;var S=Object.getOwnPropertySymbols;var A=Object.prototype.hasOwnProperty,N=Object.prototype.propertyIsEnumerable;var j=(o,t,e)=>t in o?H(o,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):o[t]=e,n=(o,t)=>{for(var e in t||(t={}))A.call(t,e)&&j(o,e,t[e]);if(S)for(var e of S(t))N.call(t,e)&&j(o,e,t[e]);return o},u=(o,t)=>q(o,B(t));var T=(o,t)=>{var e={};for(var a in o)A.call(o,a)&&t.indexOf(a)<0&&(e[a]=o[a]);if(o!=null&&S)for(var a of S(o))t.indexOf(a)<0&&N.call(o,a)&&(e[a]=o[a]);return e};var _store = require('svelte/store');var C=["error","warn","debug"],z=({logger:o=console,level:t=C[1],prefix:e="[i18n]: "})=>C.reduce((a,r,s)=>u(n({},a),{[r]:i=>C.indexOf(t)>=s&&o[r](`${e}${i}`)}),{}),d=z({}),V=o=>{d=o};var F=l=>{var c=l,{parser:o,key:t,params:e,translations:a,locale:r,fallbackLocale:s}=c,i=T(c,["parser","key","params","translations","locale","fallbackLocale"]);if(!t)return d.warn(`No translation key provided ('${r}' locale). Skipping translation...`),"";if(!r)return d.warn(`No locale provided for '${t}' key. Skipping translation...`),"";let f=(a[r]||{})[t];return s&&f===void 0&&(f=(a[s]||{})[t]),i.hasOwnProperty("fallbackValue")&&f===void 0?i.fallbackValue:o.parse(f,e,r,t)},h=(...o)=>o.length?o.filter(t=>!!t).map(t=>{let e=`${t}`.toLowerCase();try{let[a]=Intl.Collator.supportedLocalesOf(t);if(!a)throw new Error;e=a}catch(a){d.warn(`'${t}' locale is non-standard.`)}return e}):[],x=(o,t,e)=>Object.keys(o||{}).reduce((a,r)=>{let s=o[r],i=e?`${e}.${r}`:`${r}`;return t&&Array.isArray(s)?u(n({},a),{[i]:s.map(l=>x(l,t))}):s&&typeof s=="object"?n(n({},a),x(s,t,i)):u(n({},a),{[i]:s})},{}),G=o=>o.reduce((t,{key:e,data:a,locale:r})=>{if(!a)return t;let[s]=h(r),i=u(n({},t[s]||{}),{[e]:a});return u(n({},t),{[s]:i})},{}),E=async o=>{try{let t=await Promise.all(o.map(r=>{var s=r,{loader:e}=s,a=T(s,["loader"]);return new Promise(async i=>{let l;try{l=await e()}catch(c){d.error(`Failed to load translation. Verify your '${a.locale}' > '${a.key}' Loader.`),d.error(c)}i(u(n({loader:e},a),{data:l}))})}));return G(t)}catch(t){d.error(t)}return{}},W=o=>t=>{try{if(typeof t=="string")return t===o;if(typeof t=="object")return t.test(o)}catch(e){d.error("Invalid route config!")}return!1},O=(o,t)=>{let e=!0;try{e=Object.keys(o).filter(a=>o[a]!==void 0).every(a=>o[a]===t[a])}catch(a){}return e};var D=1e3*60*60*24,K= exports.default =class{constructor(t){this.cachedAt=0;this.loadedKeys={};this.currentRoute=_store.writable.call(void 0, );this.config=_store.writable.call(void 0, );this.isLoading=_store.writable.call(void 0, !1);this.promises=new Set;this.loading={subscribe:this.isLoading.subscribe,toPromise:(t,e)=>{let{fallbackLocale:a}=_store.get.call(void 0, this.config),r=Array.from(this.promises).filter(s=>{let i=O({locale:h(t)[0],route:e},s);return a&&(i=i||O({locale:h(a)[0],route:e},s)),i}).map(({promise:s})=>s);return Promise.all(r)},get:()=>_store.get.call(void 0, this.isLoading)};this.privateRawTranslations=_store.writable.call(void 0, {});this.rawTranslations={subscribe:this.privateRawTranslations.subscribe,get:()=>_store.get.call(void 0, this.rawTranslations)};this.privateTranslations=_store.writable.call(void 0, {});this.translations={subscribe:this.privateTranslations.subscribe,get:()=>_store.get.call(void 0, this.translations)};this.locales=u(n({},_store.derived.call(void 0, [this.config,this.privateTranslations],([t,e])=>{if(!t)return[];let{loaders:a=[]}=t,r=a.map(({locale:i})=>i),s=Object.keys(e).map(i=>i);return Array.from(new Set([...h(...r),...h(...s)]))},[])),{get:()=>_store.get.call(void 0, this.locales)});this.internalLocale=_store.writable.call(void 0, );this.loaderTrigger=_store.derived.call(void 0, [this.internalLocale,this.currentRoute],([t,e],a)=>{var r,s;t!==void 0&&e!==void 0&&!(t===((r=_store.get.call(void 0, this.loaderTrigger))==null?void 0:r[0])&&e===((s=_store.get.call(void 0, this.loaderTrigger))==null?void 0:s[1]))&&(d.debug("Triggering translation load..."),a([t,e]))},[]);this.localeHelper=_store.writable.call(void 0, );this.locale={subscribe:this.localeHelper.subscribe,forceSet:this.localeHelper.set,set:this.internalLocale.set,update:this.internalLocale.update,get:()=>_store.get.call(void 0, this.locale)};this.initialized=_store.derived.call(void 0, [this.locale,this.currentRoute,this.privateTranslations],([t,e,a],r)=>{_store.get.call(void 0, this.initialized)||r(t!==void 0&&e!==void 0&&!!Object.keys(a).length)});this.translation=_store.derived.call(void 0, [this.privateTranslations,this.locale,this.isLoading],([t,e,a],r)=>{let s=t[e];s&&Object.keys(s).length&&!a&&r(s)},{});this.t=u(n({},_store.derived.call(void 0, [this.config,this.translation],r=>{var[s]=r,i=s,{parser:t,fallbackLocale:e}=i,a=T(i,["parser","fallbackLocale"]);return(l,...c)=>F(n({parser:t,key:l,params:c,translations:this.translations.get(),locale:this.locale.get(),fallbackLocale:e},a.hasOwnProperty("fallbackValue")?{fallbackValue:a.fallbackValue}:{}))})),{get:(t,...e)=>_store.get.call(void 0, this.t)(t,...e)});this.l=u(n({},_store.derived.call(void 0, [this.config,this.translations],s=>{var[i,...l]=s,c=i,{parser:t,fallbackLocale:e}=c,a=T(c,["parser","fallbackLocale"]),[r]=l;return(f,b,...R)=>F(n({parser:t,key:b,params:R,translations:r,locale:f,fallbackLocale:e},a.hasOwnProperty("fallbackValue")?{fallbackValue:a.fallbackValue}:{}))})),{get:(t,e,...a)=>_store.get.call(void 0, this.l)(t,e,...a)});this.getLocale=t=>{let{fallbackLocale:e}=_store.get.call(void 0, this.config)||{},a=t||e;if(!a)return;let r=this.locales.get();return r.find(i=>h(a).includes(i))||r.find(i=>h(e).includes(i))};this.setLocale=t=>{if(t&&t!==_store.get.call(void 0, this.internalLocale))return d.debug(`Setting '${t}' locale.`),this.internalLocale.set(t),this.loading.toPromise(t,_store.get.call(void 0, this.currentRoute))};this.setRoute=t=>{if(t!==_store.get.call(void 0, this.currentRoute)){d.debug(`Setting '${t}' route.`),this.currentRoute.set(t);let e=_store.get.call(void 0, this.internalLocale);return this.loading.toPromise(e,t)}};this.loadConfig=async t=>{await this.configLoader(t)};this.getTranslationProps=async(t=this.locale.get(),e=_store.get.call(void 0, this.currentRoute))=>{let a=_store.get.call(void 0, this.config);if(!a||!t)return[];let r=this.translations.get(),{loaders:s,fallbackLocale:i="",cache:l=D}=a||{},c=Number.isNaN(+l)?D:+l;this.cachedAt?Date.now()>c+this.cachedAt&&(d.debug("Refreshing cache."),this.loadedKeys={},this.cachedAt=0):(d.debug("Setting cache timestamp."),this.cachedAt=Date.now());let[f,b]=h(t,i),R=r[f],I=r[b],k=(s||[]).map($=>{var L=$,{locale:p}=L,y=T(L,["locale"]);return u(n({},y),{locale:h(p)[0]})}).filter(({routes:p})=>!p||(p||[]).some(W(e))).filter(({key:p,locale:y})=>y===f&&(!R||!(this.loadedKeys[f]||[]).includes(p))||i&&y===b&&(!I||!(this.loadedKeys[b]||[]).includes(p)));if(k.length){this.isLoading.set(!0),d.debug("Fetching translations...");let p=await E(k);this.isLoading.set(!1);let y=Object.keys(p).reduce((L,P)=>u(n({},L),{[P]:Object.keys(p[P])}),{}),$=k.filter(({key:L,locale:P})=>(y[P]||[]).some(w=>`${w}`.startsWith(L))).reduce((L,{key:P,locale:w})=>u(n({},L),{[w]:[...L[w]||[],P]}),{});return[p,$]}return[]};this.addTranslations=(t,e)=>{if(!t)return;let a=_store.get.call(void 0, this.config),{preprocess:r}=a||{};d.debug("Adding translations...");let s=Object.keys(t||{});this.privateRawTranslations.update(i=>s.reduce((l,c)=>u(n({},l),{[c]:n(n({},l[c]||{}),t[c])}),i)),this.privateTranslations.update(i=>s.reduce((l,c)=>{let f=!0,b=t[c];return typeof r=="function"&&(b=r(b)),(typeof r=="function"||r==="none")&&(f=!1),u(n({},l),{[c]:n(n({},l[c]||{}),f?x(b,r==="preserveArrays"):b)})},i)),s.forEach(i=>{let l=Object.keys(t[i]).map(c=>`${c}`.split(".")[0]);e&&(l=e[i]),this.loadedKeys[i]=Array.from(new Set([...this.loadedKeys[i]||[],...l||[]]))})};this.loader=async([t,e])=>{let a=this.getLocale(t)||void 0;d.debug(`Adding loader promise for '${a}' locale and '${e}' route.`);let r=(async()=>{let s=await this.getTranslationProps(a,e);s.length&&this.addTranslations(...s)})();this.promises.add({locale:a,route:e,promise:r}),r.then(()=>{a&&this.locale.get()!==a&&this.locale.forceSet(a)})};this.loadTranslations=(t,e=_store.get.call(void 0, this.currentRoute)||"")=>{let a=this.getLocale(t);if(a)return this.setRoute(e),this.setLocale(a),this.loading.toPromise(a,e)};this.loaderTrigger.subscribe(this.loader),this.isLoading.subscribe(async e=>{e&&this.promises.size&&(await this.loading.toPromise(),this.promises.clear(),d.debug("Loader promises have been purged."))}),t&&this.loadConfig(t)}async configLoader(t){if(!t)return d.error("No config provided!");let l=t,{initLocale:e,fallbackLocale:a,translations:r,log:s}=l,i=T(l,["initLocale","fallbackLocale","translations","log"]);s&&V(z(s)),[e]=h(e),[a]=h(a),d.debug("Setting config."),this.config.set(n({initLocale:e,fallbackLocale:a,translations:r},i)),r&&this.addTranslations(r),e&&await this.loadTranslations(e)}};exports.default = K;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var H=Object.defineProperty,q=Object.defineProperties;var B=Object.getOwnPropertyDescriptors;var x=Object.getOwnPropertySymbols;var A=Object.prototype.hasOwnProperty,N=Object.prototype.propertyIsEnumerable;var $=(r,t,e)=>t in r?H(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,l=(r,t)=>{for(var e in t||(t={}))A.call(t,e)&&$(r,e,t[e]);if(x)for(var e of x(t))N.call(t,e)&&$(r,e,t[e]);return r},u=(r,t)=>q(r,B(t));var T=(r,t)=>{var e={};for(var a in r)A.call(r,a)&&t.indexOf(a)<0&&(e[a]=r[a]);if(r!=null&&x)for(var a of x(r))t.indexOf(a)<0&&N.call(r,a)&&(e[a]=r[a]);return e};var _store = require('svelte/store');var C=["error","warn","debug"],z=({logger:r=console,level:t=C[1],prefix:e="[i18n]: "})=>C.reduce((a,s,o)=>u(l({},a),{[s]:i=>C.indexOf(t)>=o&&r[s](`${e}${i}`)}),{}),d=z({}),V=r=>{d=r};var F=n=>{var c=n,{parser:r,key:t,params:e,translations:a,locale:s,fallbackLocale:o}=c,i=T(c,["parser","key","params","translations","locale","fallbackLocale"]);if(!t)return d.warn(`No translation key provided ('${s}' locale). Skipping translation...`),"";if(!s)return d.warn(`No locale provided for '${t}' key. Skipping translation...`),"";let f=(a[s]||{})[t];return o&&f===void 0&&(f=(a[o]||{})[t]),i.hasOwnProperty("fallbackValue")&&f===void 0?i.fallbackValue:r.parse(f,e,s,t)},h=(...r)=>r.length?r.filter(t=>!!t).map(t=>{let e=`${t}`.toLowerCase();try{let[a]=Intl.Collator.supportedLocalesOf(t);if(!a)throw new Error;e=a}catch(a){d.warn(`'${t}' locale is non-standard.`)}return e}):[],w=(r,t,e)=>{if(t&&Array.isArray(r))return r.map(a=>w(a,t));if(r&&typeof r=="object"){let a=Object.keys(r).reduce((s,o)=>{let i=r[o],n=e?`${e}.${o}`:`${o}`;return i&&typeof i=="object"&&!(t&&Array.isArray(i))?l(l({},s),w(i,t,n)):u(l({},s),{[n]:w(i,t)})},{});return Object.keys(a).length?a:null}return r},G=r=>r.reduce((t,{key:e,data:a,locale:s})=>{if(!a)return t;let[o]=h(s),i=u(l({},t[o]||{}),{[e]:a});return u(l({},t),{[o]:i})},{}),E=async r=>{try{let t=await Promise.all(r.map(s=>{var o=s,{loader:e}=o,a=T(o,["loader"]);return new Promise(async i=>{let n;try{n=await e()}catch(c){d.error(`Failed to load translation. Verify your '${a.locale}' > '${a.key}' Loader.`),d.error(c)}i(u(l({loader:e},a),{data:n}))})}));return G(t)}catch(t){d.error(t)}return{}},W=r=>t=>{try{if(typeof t=="string")return t===r;if(typeof t=="object")return t.test(r)}catch(e){d.error("Invalid route config!")}return!1},O=(r,t)=>{let e=!0;try{e=Object.keys(r).filter(a=>r[a]!==void 0).every(a=>r[a]===t[a])}catch(a){}return e};var D=1e3*60*60*24,j= exports.default =class{constructor(t){this.cachedAt=0;this.loadedKeys={};this.currentRoute=_store.writable.call(void 0, );this.config=_store.writable.call(void 0, );this.isLoading=_store.writable.call(void 0, !1);this.promises=new Set;this.loading={subscribe:this.isLoading.subscribe,toPromise:(t,e)=>{let{fallbackLocale:a}=_store.get.call(void 0, this.config),s=Array.from(this.promises).filter(o=>{let i=O({locale:h(t)[0],route:e},o);return a&&(i=i||O({locale:h(a)[0],route:e},o)),i}).map(({promise:o})=>o);return Promise.all(s)},get:()=>_store.get.call(void 0, this.isLoading)};this.privateRawTranslations=_store.writable.call(void 0, {});this.rawTranslations={subscribe:this.privateRawTranslations.subscribe,get:()=>_store.get.call(void 0, this.rawTranslations)};this.privateTranslations=_store.writable.call(void 0, {});this.translations={subscribe:this.privateTranslations.subscribe,get:()=>_store.get.call(void 0, this.translations)};this.locales=u(l({},_store.derived.call(void 0, [this.config,this.privateTranslations],([t,e])=>{if(!t)return[];let{loaders:a=[]}=t,s=a.map(({locale:i})=>i),o=Object.keys(e).map(i=>i);return Array.from(new Set([...h(...s),...h(...o)]))},[])),{get:()=>_store.get.call(void 0, this.locales)});this.internalLocale=_store.writable.call(void 0, );this.loaderTrigger=_store.derived.call(void 0, [this.internalLocale,this.currentRoute],([t,e],a)=>{var s,o;t!==void 0&&e!==void 0&&!(t===((s=_store.get.call(void 0, this.loaderTrigger))==null?void 0:s[0])&&e===((o=_store.get.call(void 0, this.loaderTrigger))==null?void 0:o[1]))&&(d.debug("Triggering translation load..."),a([t,e]))},[]);this.localeHelper=_store.writable.call(void 0, );this.locale={subscribe:this.localeHelper.subscribe,forceSet:this.localeHelper.set,set:this.internalLocale.set,update:this.internalLocale.update,get:()=>_store.get.call(void 0, this.locale)};this.initialized=_store.derived.call(void 0, [this.locale,this.currentRoute,this.privateTranslations],([t,e,a],s)=>{_store.get.call(void 0, this.initialized)||s(t!==void 0&&e!==void 0&&!!Object.keys(a).length)});this.translation=_store.derived.call(void 0, [this.privateTranslations,this.locale,this.isLoading],([t,e,a],s)=>{let o=t[e];o&&Object.keys(o).length&&!a&&s(o)},{});this.t=u(l({},_store.derived.call(void 0, [this.config,this.translation],s=>{var[o]=s,i=o,{parser:t,fallbackLocale:e}=i,a=T(i,["parser","fallbackLocale"]);return(n,...c)=>F(l({parser:t,key:n,params:c,translations:this.translations.get(),locale:this.locale.get(),fallbackLocale:e},a.hasOwnProperty("fallbackValue")?{fallbackValue:a.fallbackValue}:{}))})),{get:(t,...e)=>_store.get.call(void 0, this.t)(t,...e)});this.l=u(l({},_store.derived.call(void 0, [this.config,this.translations],o=>{var[i,...n]=o,c=i,{parser:t,fallbackLocale:e}=c,a=T(c,["parser","fallbackLocale"]),[s]=n;return(f,b,...k)=>F(l({parser:t,key:b,params:k,translations:s,locale:f,fallbackLocale:e},a.hasOwnProperty("fallbackValue")?{fallbackValue:a.fallbackValue}:{}))})),{get:(t,e,...a)=>_store.get.call(void 0, this.l)(t,e,...a)});this.getLocale=t=>{let{fallbackLocale:e}=_store.get.call(void 0, this.config)||{},a=t||e;if(!a)return;let s=this.locales.get();return s.find(i=>h(a).includes(i))||s.find(i=>h(e).includes(i))};this.setLocale=t=>{if(t&&t!==_store.get.call(void 0, this.internalLocale))return d.debug(`Setting '${t}' locale.`),this.internalLocale.set(t),this.loading.toPromise(t,_store.get.call(void 0, this.currentRoute))};this.setRoute=t=>{if(t!==_store.get.call(void 0, this.currentRoute)){d.debug(`Setting '${t}' route.`),this.currentRoute.set(t);let e=_store.get.call(void 0, this.internalLocale);return this.loading.toPromise(e,t)}};this.loadConfig=async t=>{await this.configLoader(t)};this.getTranslationProps=async(t=this.locale.get(),e=_store.get.call(void 0, this.currentRoute))=>{let a=_store.get.call(void 0, this.config);if(!a||!t)return[];let s=this.translations.get(),{loaders:o,fallbackLocale:i="",cache:n=D}=a||{},c=Number.isNaN(+n)?D:+n;this.cachedAt?Date.now()>c+this.cachedAt&&(d.debug("Refreshing cache."),this.loadedKeys={},this.cachedAt=0):(d.debug("Setting cache timestamp."),this.cachedAt=Date.now());let[f,b]=h(t,i),k=s[f],I=s[b],R=(o||[]).map(K=>{var L=K,{locale:p}=L,y=T(L,["locale"]);return u(l({},y),{locale:h(p)[0]})}).filter(({routes:p})=>!p||(p||[]).some(W(e))).filter(({key:p,locale:y})=>y===f&&(!k||!(this.loadedKeys[f]||[]).includes(p))||i&&y===b&&(!I||!(this.loadedKeys[b]||[]).includes(p)));if(R.length){this.isLoading.set(!0),d.debug("Fetching translations...");let p=await E(R);this.isLoading.set(!1);let y=Object.keys(p).reduce((L,P)=>u(l({},L),{[P]:Object.keys(p[P])}),{}),K=R.filter(({key:L,locale:P})=>(y[P]||[]).some(S=>`${S}`.startsWith(L))).reduce((L,{key:P,locale:S})=>u(l({},L),{[S]:[...L[S]||[],P]}),{});return[p,K]}return[]};this.addTranslations=(t,e)=>{if(!t)return;let a=_store.get.call(void 0, this.config),{preprocess:s}=a||{};d.debug("Adding translations...");let o=Object.keys(t||{});this.privateRawTranslations.update(i=>o.reduce((n,c)=>u(l({},n),{[c]:l(l({},n[c]||{}),t[c])}),i)),this.privateTranslations.update(i=>o.reduce((n,c)=>{let f=!0,b=t[c];return typeof s=="function"&&(b=s(b)),(typeof s=="function"||s==="none")&&(f=!1),u(l({},n),{[c]:l(l({},n[c]||{}),f?w(b,s==="preserveArrays"):b)})},i)),o.forEach(i=>{let n=Object.keys(t[i]).map(c=>`${c}`.split(".")[0]);e&&(n=e[i]),this.loadedKeys[i]=Array.from(new Set([...this.loadedKeys[i]||[],...n||[]]))})};this.loader=async([t,e])=>{let a=this.getLocale(t)||void 0;d.debug(`Adding loader promise for '${a}' locale and '${e}' route.`);let s=(async()=>{let o=await this.getTranslationProps(a,e);o.length&&this.addTranslations(...o)})();this.promises.add({locale:a,route:e,promise:s}),s.then(()=>{a&&this.locale.get()!==a&&this.locale.forceSet(a)})};this.loadTranslations=(t,e=_store.get.call(void 0, this.currentRoute)||"")=>{let a=this.getLocale(t);if(a)return this.setRoute(e),this.setLocale(a),this.loading.toPromise(a,e)};this.loaderTrigger.subscribe(this.loader),this.isLoading.subscribe(async e=>{e&&this.promises.size&&(await this.loading.toPromise(),this.promises.clear(),d.debug("Loader promises have been purged."))}),t&&this.loadConfig(t)}async configLoader(t){if(!t)return d.error("No config provided!");let n=t,{initLocale:e,fallbackLocale:a,translations:s,log:o}=n,i=T(n,["initLocale","fallbackLocale","translations","log"]);o&&V(z(o)),[e]=h(e),[a]=h(a),d.debug("Setting config."),this.config.set(l({initLocale:e,fallbackLocale:a,translations:s},i)),s&&this.addTranslations(s),e&&await this.loadTranslations(e)}};exports.default = j;
package/dist/index.d.ts CHANGED
@@ -7,14 +7,14 @@ type LoadingStore = Readable<boolean> & {
7
7
  toPromise: (locale?: Config.Locale, route?: string) => Promise<void[] | void>;
8
8
  get: () => boolean;
9
9
  };
10
- declare module DotNotation {
11
- type Input = Translations.Input;
10
+ declare namespace DotNotation {
11
+ type Input = any;
12
12
  type Output<V = any, K extends keyof V = keyof V> = {
13
13
  [P in K]?: V[K];
14
- };
14
+ } | null | V;
15
15
  type T = <I = Input>(input: I, preserveArrays?: boolean, parentKey?: string) => Output<I>;
16
16
  }
17
- declare module Logger {
17
+ declare namespace Logger {
18
18
  type Level = 'error' | 'warn' | 'debug';
19
19
  type Prefix = string;
20
20
  type T = {
@@ -41,7 +41,7 @@ declare module Logger {
41
41
  prefix?: Logger.Prefix;
42
42
  };
43
43
  }
44
- declare module Config {
44
+ declare namespace Config {
45
45
  type Loader = Loader.LoaderModule;
46
46
  type Translations = Translations.T;
47
47
  type Locale = Translations.Locales[number];
@@ -106,7 +106,7 @@ declare module Config {
106
106
  log?: Logger.FactoryProps;
107
107
  };
108
108
  }
109
- declare module Loader {
109
+ declare namespace Loader {
110
110
  type Key = string;
111
111
  type Locale = Config.Locale;
112
112
  type Route = string | RegExp;
@@ -131,7 +131,7 @@ declare module Loader {
131
131
  };
132
132
  type T = () => Promise<Translations.Input>;
133
133
  }
134
- declare module Parser {
134
+ declare namespace Parser {
135
135
  type Value = any;
136
136
  type Params = Array<unknown>;
137
137
  type Locale = Config.Locale;
@@ -161,7 +161,7 @@ declare module Parser {
161
161
  parse: Parse<P>;
162
162
  };
163
163
  }
164
- declare module Translations {
164
+ declare namespace Translations {
165
165
  type Locales<T = string> = T[];
166
166
  type SerializedTranslations = LocaleIndexed<DotNotation.Input>;
167
167
  type TranslationData<T = any> = Loader.LoaderModule & {
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- var H=Object.defineProperty,q=Object.defineProperties;var B=Object.getOwnPropertyDescriptors;var S=Object.getOwnPropertySymbols;var A=Object.prototype.hasOwnProperty,N=Object.prototype.propertyIsEnumerable;var j=(o,t,e)=>t in o?H(o,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):o[t]=e,n=(o,t)=>{for(var e in t||(t={}))A.call(t,e)&&j(o,e,t[e]);if(S)for(var e of S(t))N.call(t,e)&&j(o,e,t[e]);return o},u=(o,t)=>q(o,B(t));var T=(o,t)=>{var e={};for(var a in o)A.call(o,a)&&t.indexOf(a)<0&&(e[a]=o[a]);if(o!=null&&S)for(var a of S(o))t.indexOf(a)<0&&N.call(o,a)&&(e[a]=o[a]);return e};import{derived as v,get as g,writable as m}from"svelte/store";var C=["error","warn","debug"],z=({logger:o=console,level:t=C[1],prefix:e="[i18n]: "})=>C.reduce((a,r,s)=>u(n({},a),{[r]:i=>C.indexOf(t)>=s&&o[r](`${e}${i}`)}),{}),d=z({}),V=o=>{d=o};var F=l=>{var c=l,{parser:o,key:t,params:e,translations:a,locale:r,fallbackLocale:s}=c,i=T(c,["parser","key","params","translations","locale","fallbackLocale"]);if(!t)return d.warn(`No translation key provided ('${r}' locale). Skipping translation...`),"";if(!r)return d.warn(`No locale provided for '${t}' key. Skipping translation...`),"";let f=(a[r]||{})[t];return s&&f===void 0&&(f=(a[s]||{})[t]),i.hasOwnProperty("fallbackValue")&&f===void 0?i.fallbackValue:o.parse(f,e,r,t)},h=(...o)=>o.length?o.filter(t=>!!t).map(t=>{let e=`${t}`.toLowerCase();try{let[a]=Intl.Collator.supportedLocalesOf(t);if(!a)throw new Error;e=a}catch(a){d.warn(`'${t}' locale is non-standard.`)}return e}):[],x=(o,t,e)=>Object.keys(o||{}).reduce((a,r)=>{let s=o[r],i=e?`${e}.${r}`:`${r}`;return t&&Array.isArray(s)?u(n({},a),{[i]:s.map(l=>x(l,t))}):s&&typeof s=="object"?n(n({},a),x(s,t,i)):u(n({},a),{[i]:s})},{}),G=o=>o.reduce((t,{key:e,data:a,locale:r})=>{if(!a)return t;let[s]=h(r),i=u(n({},t[s]||{}),{[e]:a});return u(n({},t),{[s]:i})},{}),E=async o=>{try{let t=await Promise.all(o.map(r=>{var s=r,{loader:e}=s,a=T(s,["loader"]);return new Promise(async i=>{let l;try{l=await e()}catch(c){d.error(`Failed to load translation. Verify your '${a.locale}' > '${a.key}' Loader.`),d.error(c)}i(u(n({loader:e},a),{data:l}))})}));return G(t)}catch(t){d.error(t)}return{}},W=o=>t=>{try{if(typeof t=="string")return t===o;if(typeof t=="object")return t.test(o)}catch(e){d.error("Invalid route config!")}return!1},O=(o,t)=>{let e=!0;try{e=Object.keys(o).filter(a=>o[a]!==void 0).every(a=>o[a]===t[a])}catch(a){}return e};var D=1e3*60*60*24,K=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{fallbackLocale:a}=g(this.config),r=Array.from(this.promises).filter(s=>{let i=O({locale:h(t)[0],route:e},s);return a&&(i=i||O({locale:h(a)[0],route:e},s)),i}).map(({promise:s})=>s);return Promise.all(r)},get:()=>g(this.isLoading)};this.privateRawTranslations=m({});this.rawTranslations={subscribe:this.privateRawTranslations.subscribe,get:()=>g(this.rawTranslations)};this.privateTranslations=m({});this.translations={subscribe:this.privateTranslations.subscribe,get:()=>g(this.translations)};this.locales=u(n({},v([this.config,this.privateTranslations],([t,e])=>{if(!t)return[];let{loaders:a=[]}=t,r=a.map(({locale:i})=>i),s=Object.keys(e).map(i=>i);return Array.from(new Set([...h(...r),...h(...s)]))},[])),{get:()=>g(this.locales)});this.internalLocale=m();this.loaderTrigger=v([this.internalLocale,this.currentRoute],([t,e],a)=>{var r,s;t!==void 0&&e!==void 0&&!(t===((r=g(this.loaderTrigger))==null?void 0:r[0])&&e===((s=g(this.loaderTrigger))==null?void 0:s[1]))&&(d.debug("Triggering translation load..."),a([t,e]))},[]);this.localeHelper=m();this.locale={subscribe:this.localeHelper.subscribe,forceSet:this.localeHelper.set,set:this.internalLocale.set,update:this.internalLocale.update,get:()=>g(this.locale)};this.initialized=v([this.locale,this.currentRoute,this.privateTranslations],([t,e,a],r)=>{g(this.initialized)||r(t!==void 0&&e!==void 0&&!!Object.keys(a).length)});this.translation=v([this.privateTranslations,this.locale,this.isLoading],([t,e,a],r)=>{let s=t[e];s&&Object.keys(s).length&&!a&&r(s)},{});this.t=u(n({},v([this.config,this.translation],r=>{var[s]=r,i=s,{parser:t,fallbackLocale:e}=i,a=T(i,["parser","fallbackLocale"]);return(l,...c)=>F(n({parser:t,key:l,params:c,translations:this.translations.get(),locale:this.locale.get(),fallbackLocale:e},a.hasOwnProperty("fallbackValue")?{fallbackValue:a.fallbackValue}:{}))})),{get:(t,...e)=>g(this.t)(t,...e)});this.l=u(n({},v([this.config,this.translations],s=>{var[i,...l]=s,c=i,{parser:t,fallbackLocale:e}=c,a=T(c,["parser","fallbackLocale"]),[r]=l;return(f,b,...R)=>F(n({parser:t,key:b,params:R,translations:r,locale:f,fallbackLocale:e},a.hasOwnProperty("fallbackValue")?{fallbackValue:a.fallbackValue}:{}))})),{get:(t,e,...a)=>g(this.l)(t,e,...a)});this.getLocale=t=>{let{fallbackLocale:e}=g(this.config)||{},a=t||e;if(!a)return;let r=this.locales.get();return r.find(i=>h(a).includes(i))||r.find(i=>h(e).includes(i))};this.setLocale=t=>{if(t&&t!==g(this.internalLocale))return d.debug(`Setting '${t}' locale.`),this.internalLocale.set(t),this.loading.toPromise(t,g(this.currentRoute))};this.setRoute=t=>{if(t!==g(this.currentRoute)){d.debug(`Setting '${t}' route.`),this.currentRoute.set(t);let e=g(this.internalLocale);return this.loading.toPromise(e,t)}};this.loadConfig=async t=>{await this.configLoader(t)};this.getTranslationProps=async(t=this.locale.get(),e=g(this.currentRoute))=>{let a=g(this.config);if(!a||!t)return[];let r=this.translations.get(),{loaders:s,fallbackLocale:i="",cache:l=D}=a||{},c=Number.isNaN(+l)?D:+l;this.cachedAt?Date.now()>c+this.cachedAt&&(d.debug("Refreshing cache."),this.loadedKeys={},this.cachedAt=0):(d.debug("Setting cache timestamp."),this.cachedAt=Date.now());let[f,b]=h(t,i),R=r[f],I=r[b],k=(s||[]).map($=>{var L=$,{locale:p}=L,y=T(L,["locale"]);return u(n({},y),{locale:h(p)[0]})}).filter(({routes:p})=>!p||(p||[]).some(W(e))).filter(({key:p,locale:y})=>y===f&&(!R||!(this.loadedKeys[f]||[]).includes(p))||i&&y===b&&(!I||!(this.loadedKeys[b]||[]).includes(p)));if(k.length){this.isLoading.set(!0),d.debug("Fetching translations...");let p=await E(k);this.isLoading.set(!1);let y=Object.keys(p).reduce((L,P)=>u(n({},L),{[P]:Object.keys(p[P])}),{}),$=k.filter(({key:L,locale:P})=>(y[P]||[]).some(w=>`${w}`.startsWith(L))).reduce((L,{key:P,locale:w})=>u(n({},L),{[w]:[...L[w]||[],P]}),{});return[p,$]}return[]};this.addTranslations=(t,e)=>{if(!t)return;let a=g(this.config),{preprocess:r}=a||{};d.debug("Adding translations...");let s=Object.keys(t||{});this.privateRawTranslations.update(i=>s.reduce((l,c)=>u(n({},l),{[c]:n(n({},l[c]||{}),t[c])}),i)),this.privateTranslations.update(i=>s.reduce((l,c)=>{let f=!0,b=t[c];return typeof r=="function"&&(b=r(b)),(typeof r=="function"||r==="none")&&(f=!1),u(n({},l),{[c]:n(n({},l[c]||{}),f?x(b,r==="preserveArrays"):b)})},i)),s.forEach(i=>{let l=Object.keys(t[i]).map(c=>`${c}`.split(".")[0]);e&&(l=e[i]),this.loadedKeys[i]=Array.from(new Set([...this.loadedKeys[i]||[],...l||[]]))})};this.loader=async([t,e])=>{let a=this.getLocale(t)||void 0;d.debug(`Adding loader promise for '${a}' locale and '${e}' route.`);let r=(async()=>{let s=await this.getTranslationProps(a,e);s.length&&this.addTranslations(...s)})();this.promises.add({locale:a,route:e,promise:r}),r.then(()=>{a&&this.locale.get()!==a&&this.locale.forceSet(a)})};this.loadTranslations=(t,e=g(this.currentRoute)||"")=>{let a=this.getLocale(t);if(a)return this.setRoute(e),this.setLocale(a),this.loading.toPromise(a,e)};this.loaderTrigger.subscribe(this.loader),this.isLoading.subscribe(async e=>{e&&this.promises.size&&(await this.loading.toPromise(),this.promises.clear(),d.debug("Loader promises have been purged."))}),t&&this.loadConfig(t)}async configLoader(t){if(!t)return d.error("No config provided!");let l=t,{initLocale:e,fallbackLocale:a,translations:r,log:s}=l,i=T(l,["initLocale","fallbackLocale","translations","log"]);s&&V(z(s)),[e]=h(e),[a]=h(a),d.debug("Setting config."),this.config.set(n({initLocale:e,fallbackLocale:a,translations:r},i)),r&&this.addTranslations(r),e&&await this.loadTranslations(e)}};export{K as default};
1
+ var H=Object.defineProperty,q=Object.defineProperties;var B=Object.getOwnPropertyDescriptors;var x=Object.getOwnPropertySymbols;var A=Object.prototype.hasOwnProperty,N=Object.prototype.propertyIsEnumerable;var $=(r,t,e)=>t in r?H(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,l=(r,t)=>{for(var e in t||(t={}))A.call(t,e)&&$(r,e,t[e]);if(x)for(var e of x(t))N.call(t,e)&&$(r,e,t[e]);return r},u=(r,t)=>q(r,B(t));var T=(r,t)=>{var e={};for(var a in r)A.call(r,a)&&t.indexOf(a)<0&&(e[a]=r[a]);if(r!=null&&x)for(var a of x(r))t.indexOf(a)<0&&N.call(r,a)&&(e[a]=r[a]);return e};import{derived as v,get as g,writable as m}from"svelte/store";var C=["error","warn","debug"],z=({logger:r=console,level:t=C[1],prefix:e="[i18n]: "})=>C.reduce((a,s,o)=>u(l({},a),{[s]:i=>C.indexOf(t)>=o&&r[s](`${e}${i}`)}),{}),d=z({}),V=r=>{d=r};var F=n=>{var c=n,{parser:r,key:t,params:e,translations:a,locale:s,fallbackLocale:o}=c,i=T(c,["parser","key","params","translations","locale","fallbackLocale"]);if(!t)return d.warn(`No translation key provided ('${s}' locale). Skipping translation...`),"";if(!s)return d.warn(`No locale provided for '${t}' key. Skipping translation...`),"";let f=(a[s]||{})[t];return o&&f===void 0&&(f=(a[o]||{})[t]),i.hasOwnProperty("fallbackValue")&&f===void 0?i.fallbackValue:r.parse(f,e,s,t)},h=(...r)=>r.length?r.filter(t=>!!t).map(t=>{let e=`${t}`.toLowerCase();try{let[a]=Intl.Collator.supportedLocalesOf(t);if(!a)throw new Error;e=a}catch(a){d.warn(`'${t}' locale is non-standard.`)}return e}):[],w=(r,t,e)=>{if(t&&Array.isArray(r))return r.map(a=>w(a,t));if(r&&typeof r=="object"){let a=Object.keys(r).reduce((s,o)=>{let i=r[o],n=e?`${e}.${o}`:`${o}`;return i&&typeof i=="object"&&!(t&&Array.isArray(i))?l(l({},s),w(i,t,n)):u(l({},s),{[n]:w(i,t)})},{});return Object.keys(a).length?a:null}return r},G=r=>r.reduce((t,{key:e,data:a,locale:s})=>{if(!a)return t;let[o]=h(s),i=u(l({},t[o]||{}),{[e]:a});return u(l({},t),{[o]:i})},{}),E=async r=>{try{let t=await Promise.all(r.map(s=>{var o=s,{loader:e}=o,a=T(o,["loader"]);return new Promise(async i=>{let n;try{n=await e()}catch(c){d.error(`Failed to load translation. Verify your '${a.locale}' > '${a.key}' Loader.`),d.error(c)}i(u(l({loader:e},a),{data:n}))})}));return G(t)}catch(t){d.error(t)}return{}},W=r=>t=>{try{if(typeof t=="string")return t===r;if(typeof t=="object")return t.test(r)}catch(e){d.error("Invalid route config!")}return!1},O=(r,t)=>{let e=!0;try{e=Object.keys(r).filter(a=>r[a]!==void 0).every(a=>r[a]===t[a])}catch(a){}return e};var D=1e3*60*60*24,j=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{fallbackLocale:a}=g(this.config),s=Array.from(this.promises).filter(o=>{let i=O({locale:h(t)[0],route:e},o);return a&&(i=i||O({locale:h(a)[0],route:e},o)),i}).map(({promise:o})=>o);return Promise.all(s)},get:()=>g(this.isLoading)};this.privateRawTranslations=m({});this.rawTranslations={subscribe:this.privateRawTranslations.subscribe,get:()=>g(this.rawTranslations)};this.privateTranslations=m({});this.translations={subscribe:this.privateTranslations.subscribe,get:()=>g(this.translations)};this.locales=u(l({},v([this.config,this.privateTranslations],([t,e])=>{if(!t)return[];let{loaders:a=[]}=t,s=a.map(({locale:i})=>i),o=Object.keys(e).map(i=>i);return Array.from(new Set([...h(...s),...h(...o)]))},[])),{get:()=>g(this.locales)});this.internalLocale=m();this.loaderTrigger=v([this.internalLocale,this.currentRoute],([t,e],a)=>{var s,o;t!==void 0&&e!==void 0&&!(t===((s=g(this.loaderTrigger))==null?void 0:s[0])&&e===((o=g(this.loaderTrigger))==null?void 0:o[1]))&&(d.debug("Triggering translation load..."),a([t,e]))},[]);this.localeHelper=m();this.locale={subscribe:this.localeHelper.subscribe,forceSet:this.localeHelper.set,set:this.internalLocale.set,update:this.internalLocale.update,get:()=>g(this.locale)};this.initialized=v([this.locale,this.currentRoute,this.privateTranslations],([t,e,a],s)=>{g(this.initialized)||s(t!==void 0&&e!==void 0&&!!Object.keys(a).length)});this.translation=v([this.privateTranslations,this.locale,this.isLoading],([t,e,a],s)=>{let o=t[e];o&&Object.keys(o).length&&!a&&s(o)},{});this.t=u(l({},v([this.config,this.translation],s=>{var[o]=s,i=o,{parser:t,fallbackLocale:e}=i,a=T(i,["parser","fallbackLocale"]);return(n,...c)=>F(l({parser:t,key:n,params:c,translations:this.translations.get(),locale:this.locale.get(),fallbackLocale:e},a.hasOwnProperty("fallbackValue")?{fallbackValue:a.fallbackValue}:{}))})),{get:(t,...e)=>g(this.t)(t,...e)});this.l=u(l({},v([this.config,this.translations],o=>{var[i,...n]=o,c=i,{parser:t,fallbackLocale:e}=c,a=T(c,["parser","fallbackLocale"]),[s]=n;return(f,b,...k)=>F(l({parser:t,key:b,params:k,translations:s,locale:f,fallbackLocale:e},a.hasOwnProperty("fallbackValue")?{fallbackValue:a.fallbackValue}:{}))})),{get:(t,e,...a)=>g(this.l)(t,e,...a)});this.getLocale=t=>{let{fallbackLocale:e}=g(this.config)||{},a=t||e;if(!a)return;let s=this.locales.get();return s.find(i=>h(a).includes(i))||s.find(i=>h(e).includes(i))};this.setLocale=t=>{if(t&&t!==g(this.internalLocale))return d.debug(`Setting '${t}' locale.`),this.internalLocale.set(t),this.loading.toPromise(t,g(this.currentRoute))};this.setRoute=t=>{if(t!==g(this.currentRoute)){d.debug(`Setting '${t}' route.`),this.currentRoute.set(t);let e=g(this.internalLocale);return this.loading.toPromise(e,t)}};this.loadConfig=async t=>{await this.configLoader(t)};this.getTranslationProps=async(t=this.locale.get(),e=g(this.currentRoute))=>{let a=g(this.config);if(!a||!t)return[];let s=this.translations.get(),{loaders:o,fallbackLocale:i="",cache:n=D}=a||{},c=Number.isNaN(+n)?D:+n;this.cachedAt?Date.now()>c+this.cachedAt&&(d.debug("Refreshing cache."),this.loadedKeys={},this.cachedAt=0):(d.debug("Setting cache timestamp."),this.cachedAt=Date.now());let[f,b]=h(t,i),k=s[f],I=s[b],R=(o||[]).map(K=>{var L=K,{locale:p}=L,y=T(L,["locale"]);return u(l({},y),{locale:h(p)[0]})}).filter(({routes:p})=>!p||(p||[]).some(W(e))).filter(({key:p,locale:y})=>y===f&&(!k||!(this.loadedKeys[f]||[]).includes(p))||i&&y===b&&(!I||!(this.loadedKeys[b]||[]).includes(p)));if(R.length){this.isLoading.set(!0),d.debug("Fetching translations...");let p=await E(R);this.isLoading.set(!1);let y=Object.keys(p).reduce((L,P)=>u(l({},L),{[P]:Object.keys(p[P])}),{}),K=R.filter(({key:L,locale:P})=>(y[P]||[]).some(S=>`${S}`.startsWith(L))).reduce((L,{key:P,locale:S})=>u(l({},L),{[S]:[...L[S]||[],P]}),{});return[p,K]}return[]};this.addTranslations=(t,e)=>{if(!t)return;let a=g(this.config),{preprocess:s}=a||{};d.debug("Adding translations...");let o=Object.keys(t||{});this.privateRawTranslations.update(i=>o.reduce((n,c)=>u(l({},n),{[c]:l(l({},n[c]||{}),t[c])}),i)),this.privateTranslations.update(i=>o.reduce((n,c)=>{let f=!0,b=t[c];return typeof s=="function"&&(b=s(b)),(typeof s=="function"||s==="none")&&(f=!1),u(l({},n),{[c]:l(l({},n[c]||{}),f?w(b,s==="preserveArrays"):b)})},i)),o.forEach(i=>{let n=Object.keys(t[i]).map(c=>`${c}`.split(".")[0]);e&&(n=e[i]),this.loadedKeys[i]=Array.from(new Set([...this.loadedKeys[i]||[],...n||[]]))})};this.loader=async([t,e])=>{let a=this.getLocale(t)||void 0;d.debug(`Adding loader promise for '${a}' locale and '${e}' route.`);let s=(async()=>{let o=await this.getTranslationProps(a,e);o.length&&this.addTranslations(...o)})();this.promises.add({locale:a,route:e,promise:s}),s.then(()=>{a&&this.locale.get()!==a&&this.locale.forceSet(a)})};this.loadTranslations=(t,e=g(this.currentRoute)||"")=>{let a=this.getLocale(t);if(a)return this.setRoute(e),this.setLocale(a),this.loading.toPromise(a,e)};this.loaderTrigger.subscribe(this.loader),this.isLoading.subscribe(async e=>{e&&this.promises.size&&(await this.loading.toPromise(),this.promises.clear(),d.debug("Loader promises have been purged."))}),t&&this.loadConfig(t)}async configLoader(t){if(!t)return d.error("No config provided!");let n=t,{initLocale:e,fallbackLocale:a,translations:s,log:o}=n,i=T(n,["initLocale","fallbackLocale","translations","log"]);o&&V(z(o)),[e]=h(e),[a]=h(a),d.debug("Setting config."),this.config.set(l({initLocale:e,fallbackLocale:a,translations:s},i)),s&&this.addTranslations(s),e&&await this.loadTranslations(e)}};export{j as default};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltekit-i18n/base",
3
- "version": "1.3.5",
3
+ "version": "1.3.6",
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",