@sveltekit-i18n/base 1.0.0-rc.2 → 1.0.0-rc.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 m=Object.defineProperty,j=Object.defineProperties,K=Object.getOwnPropertyDescriptor,N=Object.getOwnPropertyDescriptors,O=Object.getOwnPropertyNames,T=Object.getOwnPropertySymbols;var w=Object.prototype.hasOwnProperty,C=Object.prototype.propertyIsEnumerable;var R=(r,t,s)=>t in r?m(r,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):r[t]=s,l=(r,t)=>{for(var s in t||(t={}))w.call(t,s)&&R(r,s,t[s]);if(T)for(var s of T(t))C.call(t,s)&&R(r,s,t[s]);return r},c=(r,t)=>j(r,N(t)),W=r=>m(r,"__esModule",{value:!0});var y=(r,t)=>{var s={};for(var e in r)w.call(r,e)&&t.indexOf(e)<0&&(s[e]=r[e]);if(r!=null&&T)for(var e of T(r))t.indexOf(e)<0&&C.call(r,e)&&(s[e]=r[e]);return s};var k=(r,t)=>{for(var s in t)m(r,s,{get:t[s],enumerable:!0})},D=(r,t,s,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of O(t))!w.call(r,a)&&(s||a!=="default")&&m(r,a,{get:()=>t[a],enumerable:!(e=K(t,a))||e.enumerable});return r};var z=(r=>(t,s)=>r&&r.get(t)||(s=D(W({}),t,1),r&&r.set(t,s),s))(typeof WeakMap!="undefined"?new WeakMap:0);var A={};k(A,{default:()=>F});var i=require("svelte/store");var P=(r,t)=>Object.keys(r||{}).reduce((s,e)=>{let a=r[e],o=t?`${t}.${e}`:`${e}`;return a&&typeof a=="object"?l(l({},s),P(a,o)):c(l({},s),{[o]:a})},{}),$=async r=>{try{return(await Promise.all(r.map(a=>{var o=a,{loader:s}=o,e=y(o,["loader"]);return new Promise(async n=>{let h;try{h=await s()}catch(p){console.error(`Failed to load translation. Verify your '${e.locale}' > '${e.key}' Loader.`),console.error(p)}n(c(l({loader:s},e),{data:h}))})}))).reduce((s,{key:e,data:a,locale:o})=>a?c(l({},s),{[o]:P(c(l({},s[o]||{}),{[e]:a}))}):s,{})}catch(t){console.error(t)}return{}},x=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 F=class{constructor(t){this.loadedKeys={};this.currentRoute=(0,i.writable)();this.config=(0,i.writable)();this.isLoading=(0,i.writable)(!1);this.promises=[];this.loading={subscribe:this.isLoading.subscribe,toPromise:()=>Promise.all(this.promises),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=c(l({},(0,i.derived)([this.config,this.privateTranslations],([t,s])=>{if(!t)return[];let{loaders:e=[]}=t,a=e.map(({locale:n})=>`${n}`.toLowerCase()),o=Object.keys(s).map(n=>`${n}`.toLowerCase());return[...new Set([...a,...o])]},[])),{get:()=>(0,i.get)(this.locales)});this.internalLocale=(0,i.writable)();this.locale=c(l({set:this.internalLocale.set,update:this.internalLocale.update},(0,i.derived)(this.internalLocale,(t,s)=>{let e=t&&`${t}`.toLowerCase(),a=this.getLocale(e);a&&a!==this.locale.get()&&s(a)})),{get:()=>(0,i.get)(this.locale)});this.loaderTrigger=(0,i.derived)([this.internalLocale,this.currentRoute],([t,s],e)=>{t!==void 0&&s!==void 0&&e([t,s])},[]);this.initialized=(0,i.derived)([this.locale,this.currentRoute,this.privateTranslations],([t,s,e],a)=>{(0,i.get)(this.initialized)||a(t!==void 0&&s!==void 0&&!!Object.keys(e).length)});this.translation=(0,i.derived)([this.privateTranslations,this.locale,this.isLoading],([t,s,e],a)=>{let o=t[s];o&&Object.keys(o).length&&!e&&a(o)},{});this.translate=({parser:t,key:s,params:e,translations:a,locale:o,fallbackLocale:n})=>{if(!s)throw new Error("No key provided to $t()");if(!o)throw new Error("No locale set!");let h=(a[o]||{})[s];return n&&h===void 0&&(h=(a[n]||{})[s]),t.parse(h,e,o,s)};this.t=c(l({},(0,i.derived)([this.config,this.translation],([{parser:t,fallbackLocale:s}])=>(e,...a)=>this.translate({parser:t,key:e,params:a,translations:this.translations.get(),locale:this.locale.get(),fallbackLocale:s}))),{get:(t,...s)=>(0,i.get)(this.t)(t,...s)});this.l=c(l({},(0,i.derived)([this.config,this.translations],([{parser:t,fallbackLocale:s},e])=>(a,o,...n)=>this.translate({parser:t,key:o,params:n,translations:e,locale:a,fallbackLocale:s}))),{get:(t,s,...e)=>(0,i.get)(this.l)(t,s,...e)});this.getLocale=t=>t?`${this.locales.get().find(a=>`${a}`.toLowerCase()===`${t}`.toLowerCase())||""}`.toLowerCase():"";this.setLocale=async t=>{!t||(this.internalLocale.set(t),await this.loading.toPromise())};this.setRoute=async t=>{t!==(0,i.get)(this.currentRoute)&&this.currentRoute.set(t),await this.loading.toPromise()};this.loadConfig=async t=>{await this.configLoader(t)};this.addTranslations=(t,s)=>{if(!t)return;let e=Object.keys(t||{});this.privateTranslations.update(a=>e.reduce((o,n)=>c(l({},o),{[n]:l(l({},o[n]||{}),P(t[n]))}),a)),e.forEach(a=>{let o=Object.keys(t[a]).map(n=>`${n}`.split(".")[0]);s&&(o=s[a]),this.loadedKeys[a]=Array.from(new Set([...this.loadedKeys[a]||[],...o||[]]))})};this.getTranslationProps=async(t=this.locale.get(),s=(0,i.get)(this.currentRoute))=>{let e=(0,i.get)(this.config);if(!e||!t)return[];let a=this.translations.get(),{loaders:o,fallbackLocale:n=""}=e||{},h=`${t}`.toLowerCase(),p=n&&`${n}`.toLowerCase(),S=a[h],E=a[p],b=(o||[]).map(v=>{var g=v,{locale:d}=g,u=y(g,["locale"]);return c(l({},u),{locale:`${d}`.toLowerCase()})}).filter(({routes:d})=>!d||(d||[]).some(x(s))).filter(({key:d,locale:u})=>u===h&&(!S||!(this.loadedKeys[h]||[]).includes(d))||n&&u===p&&(!E||!(this.loadedKeys[p]||[]).includes(d)));if(b.length){this.isLoading.set(!0);let d=await $(b);this.isLoading.set(!1);let u=Object.keys(d).reduce((g,f)=>c(l({},g),{[f]:Object.keys(d[f])}),{}),v=b.filter(({key:g,locale:f})=>(u[f]||[]).some(L=>`${L}`.startsWith(g))).reduce((g,{key:f,locale:L})=>c(l({},g),{[L]:[...g[L]||[],f]}),{});return[d,v]}return[]};this.translationLoader=async t=>{this.promises.push(new Promise(async s=>{let e=await this.getTranslationProps(t);e.length&&this.addTranslations(...e),s()})),await this.loading.toPromise()};this.loadTranslations=async(t,s=(0,i.get)(this.currentRoute)||"")=>{!t||(this.setRoute(s),this.setLocale(t),await this.loading.toPromise())};t&&this.loadConfig(t),this.loaderTrigger.subscribe(()=>this.translationLoader())}async configLoader(t){if(!t)throw new Error("No config!");this.config.set(t);let{initLocale:s="",translations:e}=t;e&&this.addTranslations(e),await this.loadTranslations(s)}};module.exports=z(A);0&&(module.exports={});
1
+ var P=Object.defineProperty,E=Object.defineProperties,O=Object.getOwnPropertyDescriptor,K=Object.getOwnPropertyDescriptors,W=Object.getOwnPropertyNames,m=Object.getOwnPropertySymbols;var w=Object.prototype.hasOwnProperty,S=Object.prototype.propertyIsEnumerable;var x=(r,t,e)=>t in r?P(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,l=(r,t)=>{for(var e in t||(t={}))w.call(t,e)&&x(r,e,t[e]);if(m)for(var e of m(t))S.call(t,e)&&x(r,e,t[e]);return r},c=(r,t)=>E(r,K(t)),N=r=>P(r,"__esModule",{value:!0});var y=(r,t)=>{var e={};for(var a in r)w.call(r,a)&&t.indexOf(a)<0&&(e[a]=r[a]);if(r!=null&&m)for(var a of m(r))t.indexOf(a)<0&&S.call(r,a)&&(e[a]=r[a]);return e};var z=(r,t)=>{for(var e in t)P(r,e,{get:t[e],enumerable:!0})},D=(r,t,e,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of W(t))!w.call(r,s)&&(e||s!=="default")&&P(r,s,{get:()=>t[s],enumerable:!(a=O(t,s))||a.enumerable});return r};var A=(r=>(t,e)=>r&&r.get(t)||(e=D(N({}),t,1),r&&r.set(t,e),e))(typeof WeakMap!="undefined"?new WeakMap:0);var M={};z(M,{default:()=>C});var n=require("svelte/store");var f=r=>{if(!r)return[];let t=r;return Array.isArray(t)||(t=[t]),t.map(e=>{let a=`${e}`.toLowerCase();try{[a]=Intl.Collator.supportedLocalesOf(e)}catch{console.warn("Unstandard locale provided! Check your `translations` and `loaders` in config...")}return a})},b=(r,t)=>Object.keys(r||{}).reduce((e,a)=>{let s=r[a],o=t?`${t}.${a}`:`${a}`;return s&&typeof s=="object"?l(l({},e),b(s,o)):c(l({},e),{[o]:s})},{}),F=async r=>{try{return(await Promise.all(r.map(s=>{var o=s,{loader:e}=o,a=y(o,["loader"]);return new Promise(async i=>{let g;try{g=await e()}catch(L){console.error(`Failed to load translation. Verify your '${a.locale}' > '${a.key}' Loader.`),console.error(L)}i(c(l({loader:e},a),{data:g}))})}))).reduce((e,{key:a,data:s,locale:o})=>{if(!s)return e;let i=f(o)[0];return c(l({},e),{[i]:b(c(l({},e[i]||{}),{[a]:s}))})},{})}catch(t){console.error(t)}return{}},$=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 C=class{constructor(t){this.loadedKeys={};this.currentRoute=(0,n.writable)();this.config=(0,n.writable)();this.isLoading=(0,n.writable)(!1);this.promises=[];this.loading={subscribe:this.isLoading.subscribe,toPromise:()=>Promise.all(this.promises),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=c(l({},(0,n.derived)([this.config,this.privateTranslations],([t,e])=>{if(!t)return[];let{loaders:a=[]}=t,s=a.map(({locale:i})=>f(i)[0]),o=Object.keys(e).map(i=>f(i)[0]);return[...new Set([...s,...o])]},[])),{get:()=>(0,n.get)(this.locales)});this.internalLocale=(0,n.writable)();this.locale=c(l({set:this.internalLocale.set,update:this.internalLocale.update},(0,n.derived)(this.internalLocale,(t,e)=>{let a=this.getLocale(t);a&&a!==this.locale.get()&&e(a)})),{get:()=>(0,n.get)(this.locale)});this.loaderTrigger=(0,n.derived)([this.internalLocale,this.currentRoute],([t,e],a)=>{t!==void 0&&e!==void 0&&a([t,e])},[]);this.initialized=(0,n.derived)([this.locale,this.currentRoute,this.privateTranslations],([t,e,a],s)=>{(0,n.get)(this.initialized)||s(t!==void 0&&e!==void 0&&!!Object.keys(a).length)});this.translation=(0,n.derived)([this.privateTranslations,this.locale,this.isLoading],([t,e,a],s)=>{let o=t[e];o&&Object.keys(o).length&&!a&&s(o)},{});this.translate=({parser:t,key:e,params:a,translations:s,locale:o,fallbackLocale:i})=>{if(!(e&&o))return console.warn("No translation key or locale provided. Skipping translation..."),"";let g=(s[o]||{})[e];return i&&g===void 0&&(g=(s[i]||{})[e]),t.parse(g,a,o,e)};this.t=c(l({},(0,n.derived)([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)=>(0,n.get)(this.t)(t,...e)});this.l=c(l({},(0,n.derived)([this.config,this.translations],([{parser:t,fallbackLocale:e},a])=>(s,o,...i)=>this.translate({parser:t,key:o,params:i,translations:a,locale:s,fallbackLocale:e}))),{get:(t,e,...a)=>(0,n.get)(this.l)(t,e,...a)});this.getLocale=t=>{if(!t)return"";let a=this.locales.get().find(s=>`${f(s)}`==`${f(t)}`)||"";return`${f(a)}`};this.setLocale=async t=>{!t||(this.internalLocale.set(`${f(t)}`),await this.loading.toPromise())};this.setRoute=async t=>{t!==(0,n.get)(this.currentRoute)&&this.currentRoute.set(t),await this.loading.toPromise()};this.loadConfig=async t=>{await this.configLoader(t)};this.addTranslations=(t,e)=>{if(!t)return;let a=Object.keys(t||{});this.privateTranslations.update(s=>a.reduce((o,i)=>c(l({},o),{[i]:l(l({},o[i]||{}),b(t[i]))}),s)),a.forEach(s=>{let o=Object.keys(t[s]).map(i=>`${i}`.split(".")[0]);e&&(o=e[s]),this.loadedKeys[s]=Array.from(new Set([...this.loadedKeys[s]||[],...o||[]]))})};this.getTranslationProps=async(t=this.locale.get(),e=(0,n.get)(this.currentRoute))=>{let a=(0,n.get)(this.config);if(!a||!t)return[];let s=this.translations.get(),{loaders:o,fallbackLocale:i=""}=a||{},g=`${t}`.toLowerCase(),L=i&&`${i}`.toLowerCase(),j=s[g],k=s[L],v=(o||[]).map(R=>{var u=R,{locale:d}=u,h=y(u,["locale"]);return c(l({},h),{locale:`${d}`.toLowerCase()})}).filter(({routes:d})=>!d||(d||[]).some($(e))).filter(({key:d,locale:h})=>h===g&&(!j||!(this.loadedKeys[g]||[]).includes(d))||i&&h===L&&(!k||!(this.loadedKeys[L]||[]).includes(d)));if(v.length){this.isLoading.set(!0);let d=await F(v);this.isLoading.set(!1);let h=Object.keys(d).reduce((u,p)=>c(l({},u),{[p]:Object.keys(d[p])}),{}),R=v.filter(({key:u,locale:p})=>(h[p]||[]).some(T=>`${T}`.startsWith(u))).reduce((u,{key:p,locale:T})=>c(l({},u),{[T]:[...u[T]||[],p]}),{});return[d,R]}return[]};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,n.get)(this.currentRoute)||"")=>{!t||(this.setRoute(e),this.setLocale(t),await this.loading.toPromise())};t&&this.loadConfig(t),this.loaderTrigger.subscribe(()=>this.translationLoader())}async configLoader(t){if(!t)throw new Error("No config provided!");let i=t,{initLocale:e,fallbackLocale:a,translations:s}=i,o=y(i,["initLocale","fallbackLocale","translations"]);e=f(e)[0],a=f(a)[0],this.config.set(l({initLocale:e,fallbackLocale:a,translations:s},o)),s&&this.addTranslations(s),await this.loadTranslations(e)}};module.exports=A(M);0&&(module.exports={});
package/dist/index.d.ts CHANGED
@@ -20,7 +20,7 @@ declare module Config {
20
20
  type Locale = string;
21
21
  type InitLocale = Locale | undefined;
22
22
  type FallbackLocale = Locale | undefined;
23
- type T<P extends Parser.Params = any> = {
23
+ type T<P extends Parser.Params = Parser.Params> = {
24
24
  loaders?: Loaders;
25
25
  translations?: Translations;
26
26
  initLocale?: InitLocale;
@@ -46,18 +46,18 @@ declare module Parser {
46
46
  type Locale = Config.Locale;
47
47
  type Key = Loader.Key;
48
48
  type Output = any;
49
- type Parse<P extends Parser.Params = any> = (value: Value, params: P, locale: Locale, key: Key) => Output;
49
+ type Parse<P extends Parser.Params = Parser.Params> = (value: Value, params: P, locale: Locale, key: Key) => Output;
50
50
  type ParserFactory<ParserConfig = any, P extends Parser.Params = any> = (config?: ParserConfig) => Parser.T<P>;
51
- type T<P extends Parser.Params = any> = {
51
+ type T<P extends Parser.Params = Parser.Params> = {
52
52
  parse: Parse<P>;
53
53
  };
54
54
  }
55
55
  declare module Translations {
56
56
  type SerializedTranslations = T<DotNotation.Output>;
57
57
  type FetchTranslations = (loaders: Loader.LoaderModule[]) => Promise<SerializedTranslations>;
58
- type TranslationFunction<P extends Parser.Params = any> = (key: string, ...restParams: P) => string;
59
- type LocalTranslationFunction<P extends Parser.Params = any> = (locale: string, key: string, ...restParams: P) => string;
60
- type Translate<P extends Parser.Params = any> = (props: {
58
+ type TranslationFunction<P extends Parser.Params = Parser.Params> = (key: string, ...restParams: P) => string;
59
+ type LocalTranslationFunction<P extends Parser.Params = Parser.Params> = (locale: string, key: string, ...restParams: P) => string;
60
+ type Translate<P extends Parser.Params = Parser.Params> = (props: {
61
61
  parser: Parser.T<P>;
62
62
  key: string;
63
63
  params: P;
@@ -65,8 +65,8 @@ declare module Translations {
65
65
  locale: string;
66
66
  fallbackLocale?: string;
67
67
  }) => string;
68
- type T<T = any> = {
69
- [locale: string]: Record<string, T>;
68
+ type T<V = any> = {
69
+ [locale: string]: Record<string, V>;
70
70
  };
71
71
  }
72
72
 
@@ -80,9 +80,9 @@ declare class I18n<ParserParams extends Parser.Params = any> {
80
80
  loading: LoadingStore;
81
81
  private privateTranslations;
82
82
  translations: ExtendedStore<Translations.SerializedTranslations>;
83
- locales: ExtendedStore<string[]>;
83
+ locales: ExtendedStore<Config.Locale[]>;
84
84
  private internalLocale;
85
- locale: ExtendedStore<string, () => string, Writable<string>>;
85
+ locale: ExtendedStore<Config.Locale, () => Config.Locale, Writable<string>>;
86
86
  private loaderTrigger;
87
87
  initialized: Readable<boolean>;
88
88
  private translation;
@@ -98,7 +98,7 @@ declare class I18n<ParserParams extends Parser.Params = any> {
98
98
  getTranslationProps: ($locale?: string, $route?: string) => Promise<[Config.Translations, Record<string, string[]>] | [
99
99
  ]>;
100
100
  private translationLoader;
101
- loadTranslations: (locale: string, route?: string) => Promise<void>;
101
+ loadTranslations: (locale: Config.Locale, route?: string) => Promise<void>;
102
102
  }
103
103
 
104
104
  export { Config, Loader, Parser, Translations, I18n as default };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- var j=Object.defineProperty,K=Object.defineProperties;var N=Object.getOwnPropertyDescriptors;var y=Object.getOwnPropertySymbols;var C=Object.prototype.hasOwnProperty,$=Object.prototype.propertyIsEnumerable;var R=(o,t,s)=>t in o?j(o,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):o[t]=s,n=(o,t)=>{for(var s in t||(t={}))C.call(t,s)&&R(o,s,t[s]);if(y)for(var s of y(t))$.call(t,s)&&R(o,s,t[s]);return o},l=(o,t)=>K(o,N(t));var P=(o,t)=>{var s={};for(var e in o)C.call(o,e)&&t.indexOf(e)<0&&(s[e]=o[e]);if(o!=null&&y)for(var e of y(o))t.indexOf(e)<0&&$.call(o,e)&&(s[e]=o[e]);return s};import{derived as u,get as d,writable as T}from"svelte/store";var b=(o,t)=>Object.keys(o||{}).reduce((s,e)=>{let a=o[e],r=t?`${t}.${e}`:`${e}`;return a&&typeof a=="object"?n(n({},s),b(a,r)):l(n({},s),{[r]:a})},{}),x=async o=>{try{return(await Promise.all(o.map(a=>{var r=a,{loader:s}=r,e=P(r,["loader"]);return new Promise(async i=>{let h;try{h=await s()}catch(L){console.error(`Failed to load translation. Verify your '${e.locale}' > '${e.key}' Loader.`),console.error(L)}i(l(n({loader:s},e),{data:h}))})}))).reduce((s,{key:e,data:a,locale:r})=>a?l(n({},s),{[r]:b(l(n({},s[r]||{}),{[e]:a}))}):s,{})}catch(t){console.error(t)}return{}},F=o=>t=>{try{if(typeof t=="string")return t===o;if(typeof t=="object")return t.test(o)}catch{throw new Error("Invalid route config!")}return!1};var k=class{constructor(t){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:()=>d(this.isLoading)};this.privateTranslations=T({});this.translations={subscribe:this.privateTranslations.subscribe,get:()=>d(this.translations)};this.locales=l(n({},u([this.config,this.privateTranslations],([t,s])=>{if(!t)return[];let{loaders:e=[]}=t,a=e.map(({locale:i})=>`${i}`.toLowerCase()),r=Object.keys(s).map(i=>`${i}`.toLowerCase());return[...new Set([...a,...r])]},[])),{get:()=>d(this.locales)});this.internalLocale=T();this.locale=l(n({set:this.internalLocale.set,update:this.internalLocale.update},u(this.internalLocale,(t,s)=>{let e=t&&`${t}`.toLowerCase(),a=this.getLocale(e);a&&a!==this.locale.get()&&s(a)})),{get:()=>d(this.locale)});this.loaderTrigger=u([this.internalLocale,this.currentRoute],([t,s],e)=>{t!==void 0&&s!==void 0&&e([t,s])},[]);this.initialized=u([this.locale,this.currentRoute,this.privateTranslations],([t,s,e],a)=>{d(this.initialized)||a(t!==void 0&&s!==void 0&&!!Object.keys(e).length)});this.translation=u([this.privateTranslations,this.locale,this.isLoading],([t,s,e],a)=>{let r=t[s];r&&Object.keys(r).length&&!e&&a(r)},{});this.translate=({parser:t,key:s,params:e,translations:a,locale:r,fallbackLocale:i})=>{if(!s)throw new Error("No key provided to $t()");if(!r)throw new Error("No locale set!");let h=(a[r]||{})[s];return i&&h===void 0&&(h=(a[i]||{})[s]),t.parse(h,e,r,s)};this.t=l(n({},u([this.config,this.translation],([{parser:t,fallbackLocale:s}])=>(e,...a)=>this.translate({parser:t,key:e,params:a,translations:this.translations.get(),locale:this.locale.get(),fallbackLocale:s}))),{get:(t,...s)=>d(this.t)(t,...s)});this.l=l(n({},u([this.config,this.translations],([{parser:t,fallbackLocale:s},e])=>(a,r,...i)=>this.translate({parser:t,key:r,params:i,translations:e,locale:a,fallbackLocale:s}))),{get:(t,s,...e)=>d(this.l)(t,s,...e)});this.getLocale=t=>t?`${this.locales.get().find(a=>`${a}`.toLowerCase()===`${t}`.toLowerCase())||""}`.toLowerCase():"";this.setLocale=async t=>{!t||(this.internalLocale.set(t),await this.loading.toPromise())};this.setRoute=async t=>{t!==d(this.currentRoute)&&this.currentRoute.set(t),await this.loading.toPromise()};this.loadConfig=async t=>{await this.configLoader(t)};this.addTranslations=(t,s)=>{if(!t)return;let e=Object.keys(t||{});this.privateTranslations.update(a=>e.reduce((r,i)=>l(n({},r),{[i]:n(n({},r[i]||{}),b(t[i]))}),a)),e.forEach(a=>{let r=Object.keys(t[a]).map(i=>`${i}`.split(".")[0]);s&&(r=s[a]),this.loadedKeys[a]=Array.from(new Set([...this.loadedKeys[a]||[],...r||[]]))})};this.getTranslationProps=async(t=this.locale.get(),s=d(this.currentRoute))=>{let e=d(this.config);if(!e||!t)return[];let a=this.translations.get(),{loaders:r,fallbackLocale:i=""}=e||{},h=`${t}`.toLowerCase(),L=i&&`${i}`.toLowerCase(),S=a[h],E=a[L],w=(r||[]).map(v=>{var g=v,{locale:c}=g,f=P(g,["locale"]);return l(n({},f),{locale:`${c}`.toLowerCase()})}).filter(({routes:c})=>!c||(c||[]).some(F(s))).filter(({key:c,locale:f})=>f===h&&(!S||!(this.loadedKeys[h]||[]).includes(c))||i&&f===L&&(!E||!(this.loadedKeys[L]||[]).includes(c)));if(w.length){this.isLoading.set(!0);let c=await x(w);this.isLoading.set(!1);let f=Object.keys(c).reduce((g,p)=>l(n({},g),{[p]:Object.keys(c[p])}),{}),v=w.filter(({key:g,locale:p})=>(f[p]||[]).some(m=>`${m}`.startsWith(g))).reduce((g,{key:p,locale:m})=>l(n({},g),{[m]:[...g[m]||[],p]}),{});return[c,v]}return[]};this.translationLoader=async t=>{this.promises.push(new Promise(async s=>{let e=await this.getTranslationProps(t);e.length&&this.addTranslations(...e),s()})),await this.loading.toPromise()};this.loadTranslations=async(t,s=d(this.currentRoute)||"")=>{!t||(this.setRoute(s),this.setLocale(t),await this.loading.toPromise())};t&&this.loadConfig(t),this.loaderTrigger.subscribe(()=>this.translationLoader())}async configLoader(t){if(!t)throw new Error("No config!");this.config.set(t);let{initLocale:s="",translations:e}=t;e&&this.addTranslations(e),await this.loadTranslations(s)}};export{k as default};
1
+ var O=Object.defineProperty,K=Object.defineProperties;var W=Object.getOwnPropertyDescriptors;var b=Object.getOwnPropertySymbols;var x=Object.prototype.hasOwnProperty,S=Object.prototype.propertyIsEnumerable;var R=(r,t,e)=>t in r?O(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,n=(r,t)=>{for(var e in t||(t={}))x.call(t,e)&&R(r,e,t[e]);if(b)for(var e of b(t))S.call(t,e)&&R(r,e,t[e]);return r},l=(r,t)=>K(r,W(t));var T=(r,t)=>{var e={};for(var a in r)x.call(r,a)&&t.indexOf(a)<0&&(e[a]=r[a]);if(r!=null&&b)for(var a of b(r))t.indexOf(a)<0&&S.call(r,a)&&(e[a]=r[a]);return e};import{derived as h,get as d,writable as m}from"svelte/store";var f=r=>{if(!r)return[];let t=r;return Array.isArray(t)||(t=[t]),t.map(e=>{let a=`${e}`.toLowerCase();try{[a]=Intl.Collator.supportedLocalesOf(e)}catch{console.warn("Unstandard locale provided! Check your `translations` and `loaders` in config...")}return a})},v=(r,t)=>Object.keys(r||{}).reduce((e,a)=>{let s=r[a],o=t?`${t}.${a}`:`${a}`;return s&&typeof s=="object"?n(n({},e),v(s,o)):l(n({},e),{[o]:s})},{}),F=async r=>{try{return(await Promise.all(r.map(s=>{var o=s,{loader:e}=o,a=T(o,["loader"]);return new Promise(async i=>{let g;try{g=await e()}catch(y){console.error(`Failed to load translation. Verify your '${a.locale}' > '${a.key}' Loader.`),console.error(y)}i(l(n({loader:e},a),{data:g}))})}))).reduce((e,{key:a,data:s,locale:o})=>{if(!s)return e;let i=f(o)[0];return l(n({},e),{[i]:v(l(n({},e[i]||{}),{[a]:s}))})},{})}catch(t){console.error(t)}return{}},$=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 j=class{constructor(t){this.loadedKeys={};this.currentRoute=m();this.config=m();this.isLoading=m(!1);this.promises=[];this.loading={subscribe:this.isLoading.subscribe,toPromise:()=>Promise.all(this.promises),get:()=>d(this.isLoading)};this.privateTranslations=m({});this.translations={subscribe:this.privateTranslations.subscribe,get:()=>d(this.translations)};this.locales=l(n({},h([this.config,this.privateTranslations],([t,e])=>{if(!t)return[];let{loaders:a=[]}=t,s=a.map(({locale:i})=>f(i)[0]),o=Object.keys(e).map(i=>f(i)[0]);return[...new Set([...s,...o])]},[])),{get:()=>d(this.locales)});this.internalLocale=m();this.locale=l(n({set:this.internalLocale.set,update:this.internalLocale.update},h(this.internalLocale,(t,e)=>{let a=this.getLocale(t);a&&a!==this.locale.get()&&e(a)})),{get:()=>d(this.locale)});this.loaderTrigger=h([this.internalLocale,this.currentRoute],([t,e],a)=>{t!==void 0&&e!==void 0&&a([t,e])},[]);this.initialized=h([this.locale,this.currentRoute,this.privateTranslations],([t,e,a],s)=>{d(this.initialized)||s(t!==void 0&&e!==void 0&&!!Object.keys(a).length)});this.translation=h([this.privateTranslations,this.locale,this.isLoading],([t,e,a],s)=>{let o=t[e];o&&Object.keys(o).length&&!a&&s(o)},{});this.translate=({parser:t,key:e,params:a,translations:s,locale:o,fallbackLocale:i})=>{if(!(e&&o))return console.warn("No translation key or locale provided. Skipping translation..."),"";let g=(s[o]||{})[e];return i&&g===void 0&&(g=(s[i]||{})[e]),t.parse(g,a,o,e)};this.t=l(n({},h([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)=>d(this.t)(t,...e)});this.l=l(n({},h([this.config,this.translations],([{parser:t,fallbackLocale:e},a])=>(s,o,...i)=>this.translate({parser:t,key:o,params:i,translations:a,locale:s,fallbackLocale:e}))),{get:(t,e,...a)=>d(this.l)(t,e,...a)});this.getLocale=t=>{if(!t)return"";let a=this.locales.get().find(s=>`${f(s)}`==`${f(t)}`)||"";return`${f(a)}`};this.setLocale=async t=>{!t||(this.internalLocale.set(`${f(t)}`),await this.loading.toPromise())};this.setRoute=async t=>{t!==d(this.currentRoute)&&this.currentRoute.set(t),await this.loading.toPromise()};this.loadConfig=async t=>{await this.configLoader(t)};this.addTranslations=(t,e)=>{if(!t)return;let a=Object.keys(t||{});this.privateTranslations.update(s=>a.reduce((o,i)=>l(n({},o),{[i]:n(n({},o[i]||{}),v(t[i]))}),s)),a.forEach(s=>{let o=Object.keys(t[s]).map(i=>`${i}`.split(".")[0]);e&&(o=e[s]),this.loadedKeys[s]=Array.from(new Set([...this.loadedKeys[s]||[],...o||[]]))})};this.getTranslationProps=async(t=this.locale.get(),e=d(this.currentRoute))=>{let a=d(this.config);if(!a||!t)return[];let s=this.translations.get(),{loaders:o,fallbackLocale:i=""}=a||{},g=`${t}`.toLowerCase(),y=i&&`${i}`.toLowerCase(),k=s[g],E=s[y],w=(o||[]).map(C=>{var u=C,{locale:c}=u,p=T(u,["locale"]);return l(n({},p),{locale:`${c}`.toLowerCase()})}).filter(({routes:c})=>!c||(c||[]).some($(e))).filter(({key:c,locale:p})=>p===g&&(!k||!(this.loadedKeys[g]||[]).includes(c))||i&&p===y&&(!E||!(this.loadedKeys[y]||[]).includes(c)));if(w.length){this.isLoading.set(!0);let c=await F(w);this.isLoading.set(!1);let p=Object.keys(c).reduce((u,L)=>l(n({},u),{[L]:Object.keys(c[L])}),{}),C=w.filter(({key:u,locale:L})=>(p[L]||[]).some(P=>`${P}`.startsWith(u))).reduce((u,{key:L,locale:P})=>l(n({},u),{[P]:[...u[P]||[],L]}),{});return[c,C]}return[]};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=d(this.currentRoute)||"")=>{!t||(this.setRoute(e),this.setLocale(t),await this.loading.toPromise())};t&&this.loadConfig(t),this.loaderTrigger.subscribe(()=>this.translationLoader())}async configLoader(t){if(!t)throw new Error("No config provided!");let i=t,{initLocale:e,fallbackLocale:a,translations:s}=i,o=T(i,["initLocale","fallbackLocale","translations"]);e=f(e)[0],a=f(a)[0],this.config.set(n({initLocale:e,fallbackLocale:a,translations:s},o)),s&&this.addTranslations(s),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.0.0-rc.2",
3
+ "version": "1.0.0-rc.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",
@@ -65,4 +65,4 @@
65
65
  "tsup": "^5.11.13",
66
66
  "typescript": "^4.5.4"
67
67
  }
68
- }
68
+ }