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