@sveltekit-i18n/base 1.0.0 → 1.1.0
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.d.ts +10 -8
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var T=Object.defineProperty,
|
|
1
|
+
var T=Object.defineProperty,W=Object.defineProperties,D=Object.getOwnPropertyDescriptor,I=Object.getOwnPropertyDescriptors,$=Object.getOwnPropertyNames,y=Object.getOwnPropertySymbols;var S=Object.prototype.hasOwnProperty,K=Object.prototype.propertyIsEnumerable;var F=(a,t,e)=>t in a?T(a,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[t]=e,l=(a,t)=>{for(var e in t||(t={}))S.call(t,e)&&F(a,e,t[e]);if(y)for(var e of y(t))K.call(t,e)&&F(a,e,t[e]);return a},c=(a,t)=>W(a,I(t)),H=a=>T(a,"__esModule",{value:!0});var L=(a,t)=>{var e={};for(var r in a)S.call(a,r)&&t.indexOf(r)<0&&(e[r]=a[r]);if(a!=null&&y)for(var r of y(a))t.indexOf(r)<0&&K.call(a,r)&&(e[r]=a[r]);return e};var V=(a,t)=>{for(var e in t)T(a,e,{get:t[e],enumerable:!0})},M=(a,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of $(t))!S.call(a,s)&&(e||s!=="default")&&T(a,s,{get:()=>t[s],enumerable:!(r=D(t,s))||r.enumerable});return a};var q=(a=>(t,e)=>a&&a.get(t)||(e=M(H({}),t,1),a&&a.set(t,e),e))(typeof WeakMap!="undefined"?new WeakMap:0);var G={};V(G,{default:()=>R});var o=require("svelte/store");var C=({parser:a,key:t,params:e,translations:r,locale:s,fallbackLocale:i})=>{if(!(t&&s))return console.warn("[i18n]: No translation key or locale provided. Skipping translation..."),"";let n=(r[s]||{})[t];return i&&n===void 0&&(n=(r[i]||{})[t]),a.parse(n,e,s,t)},d=(...a)=>a.length?a.filter(t=>!!t).map(t=>{let e=`${t}`.toLowerCase();try{[e]=Intl.Collator.supportedLocalesOf(t)}catch{console.warn(`[i18n]: Non-standard locale provided: '${a}'. Check your 'translations' and 'loaders' in i18n config...`)}return e}):[],b=(a,t)=>Object.keys(a||{}).reduce((e,r)=>{let s=a[r],i=t?`${t}.${r}`:`${r}`;return s&&typeof s=="object"?l(l({},e),b(s,i)):c(l({},e),{[i]:s})},{}),j=async a=>{try{return(await Promise.all(a.map(s=>{var i=s,{loader:e}=i,r=L(i,["loader"]);return new Promise(async n=>{let p;try{p=await e()}catch(P){console.error(`[i18n]: Failed to load translation. Verify your '${r.locale}' > '${r.key}' Loader.`),console.error(P)}n(c(l({loader:e},r),{data:p}))})}))).reduce((e,{key:r,data:s,locale:i})=>{if(!s)return e;let[n]=d(i);return c(l({},e),{[n]:b(c(l({},e[n]||{}),{[r]:s}))})},{})}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(r=>a[r]!==void 0).every(r=>a[r]===t[r])}catch{}return e};var O=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=new Set;this.loading={subscribe:this.isLoading.subscribe,toPromise:(t,e)=>{let r=Array.from(this.promises).filter(s=>N({locale:d(t)[0],route:e},s)).map(({promise:s})=>s);return Promise.all(r)},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:r=[]}=t,s=r.map(({locale:n})=>d(n)[0]),i=Object.keys(e).map(n=>d(n)[0]);return Array.from(new Set([...s,...i]))},[])),{get:()=>(0,o.get)(this.locales)});this.internalLocale=(0,o.writable)();this.loaderTrigger=(0,o.derived)([this.internalLocale,this.currentRoute],([t,e],r)=>{var s,i;t!==void 0&&e!==void 0&&(t!==((s=(0,o.get)(this.loaderTrigger))==null?void 0:s[0])||e!==((i=(0,o.get)(this.loaderTrigger))==null?void 0:i[1]))&&r([t,e])},[]);this.localeHelper=(0,o.writable)();this.locale={subscribe:this.localeHelper.subscribe,forceSet:this.localeHelper.set,set:this.internalLocale.set,update:this.internalLocale.update,get:()=>(0,o.get)(this.locale)};this.initialized=(0,o.derived)([this.locale,this.currentRoute,this.privateTranslations],([t,e,r],s)=>{(0,o.get)(this.initialized)||s(t!==void 0&&e!==void 0&&!!Object.keys(r).length)});this.translation=(0,o.derived)([this.privateTranslations,this.locale,this.isLoading],([t,e,r],s)=>{let i=t[e];i&&Object.keys(i).length&&!r&&s(i)},{});this.t=c(l({},(0,o.derived)([this.config,this.translation],([{parser:t,fallbackLocale:e}])=>(r,...s)=>C({parser:t,key:r,params:s,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},r])=>(s,i,...n)=>C({parser:t,key:i,params:n,translations:r,locale:s,fallbackLocale:e}))),{get:(t,e,...r)=>(0,o.get)(this.l)(t,e,...r)});this.getLocale=t=>{if(!t)return"";let r=this.locales.get().find(s=>s===d(t)[0])||"";return d(r)[0]||""};this.setLocale=t=>{if(!t)return;let[e]=d(t);if(e!==(0,o.get)(this.internalLocale))return this.internalLocale.set(e),this.loading.toPromise(t,(0,o.get)(this.currentRoute))};this.setRoute=t=>{if(t!==(0,o.get)(this.currentRoute)){this.currentRoute.set(t);let e=(0,o.get)(this.internalLocale);return this.loading.toPromise(e,t)}};this.loadConfig=async t=>{await this.configLoader(t)};this.getTranslationProps=async(t=this.locale.get(),e=(0,o.get)(this.currentRoute))=>{let r=(0,o.get)(this.config);if(!r||!t)return[];let s=this.translations.get(),{loaders:i,fallbackLocale:n="",cache:p=O}=r||{},P=Number.isNaN(+p)?O:+p;this.cachedAt?Date.now()>P+this.cachedAt&&(this.loadedKeys={},this.cachedAt=0):this.cachedAt=Date.now();let[v,w]=d(t,n),E=s[v],A=s[w],x=(i||[]).map(z=>{var f=z,{locale:h}=f,u=L(f,["locale"]);return c(l({},u),{locale:d(h)[0]})}).filter(({routes:h})=>!h||(h||[]).some(k(e))).filter(({key:h,locale:u})=>u===v&&(!E||!(this.loadedKeys[v]||[]).includes(h))||n&&u===w&&(!A||!(this.loadedKeys[w]||[]).includes(h)));if(x.length){this.isLoading.set(!0);let h=await j(x);this.isLoading.set(!1);let u=Object.keys(h).reduce((f,g)=>c(l({},f),{[g]:Object.keys(h[g])}),{}),z=x.filter(({key:f,locale:g})=>(u[g]||[]).some(m=>`${m}`.startsWith(f))).reduce((f,{key:g,locale:m})=>c(l({},f),{[m]:[...f[m]||[],g]}),{});return[h,z]}return[]};this.addTranslations=(t,e)=>{if(!t)return;let r=Object.keys(t||{});this.privateTranslations.update(s=>r.reduce((i,n)=>c(l({},i),{[n]:l(l({},i[n]||{}),b(t[n]))}),s)),r.forEach(s=>{let i=Object.keys(t[s]).map(n=>`${n}`.split(".")[0]);e&&(i=e[s]),this.loadedKeys[s]=Array.from(new Set([...this.loadedKeys[s]||[],...i||[]]))})};this.loader=async([t,e])=>{let r=(async()=>{let s=await this.getTranslationProps(t,e);s.length&&this.addTranslations(...s)})();this.promises.add({locale:t,route:e,promise:r}),r.then(()=>{let s=this.getLocale(t);s&&this.locale.get()!==s&&this.locale.forceSet(s)})};this.loadTranslations=(t,e=(0,o.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=>{e&&this.promises.size&&(await this.loading.toPromise(),this.promises.clear(),console.info("[i18n]: Promises has been purged."))})}async configLoader(t){if(!t)throw new Error("[i18n]: No config provided!");let n=t,{initLocale:e,fallbackLocale:r,translations:s}=n,i=L(n,["initLocale","fallbackLocale","translations"]);e=d(e)[0],r=d(r)[0],this.config.set(l({initLocale:e,fallbackLocale:r,translations:s},i)),s&&this.addTranslations(s),await this.loadTranslations(e)}};module.exports=q(G);0&&(module.exports={});
|
package/dist/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ declare type ExtendedStore<T, Get = () => T, Store = Readable<T>> = Store & {
|
|
|
4
4
|
get: Get;
|
|
5
5
|
};
|
|
6
6
|
declare type LoadingStore = Readable<boolean> & {
|
|
7
|
-
toPromise: (
|
|
7
|
+
toPromise: (locale?: Config.Locale, route?: string) => Promise<void[] | void>;
|
|
8
8
|
get: () => boolean;
|
|
9
9
|
};
|
|
10
10
|
declare module DotNotation {
|
|
@@ -59,7 +59,7 @@ declare module Translations {
|
|
|
59
59
|
type FetchTranslations = (loaders: Loader.LoaderModule[]) => Promise<SerializedTranslations>;
|
|
60
60
|
type TranslationFunction<P extends Parser.Params = Parser.Params> = (key: string, ...restParams: P) => any;
|
|
61
61
|
type LocalTranslationFunction<P extends Parser.Params = Parser.Params> = (locale: Config.Locale, key: string, ...restParams: P) => any;
|
|
62
|
-
type Translate<P extends Parser.Params = Parser.Params>
|
|
62
|
+
type Translate = <P extends Parser.Params = Parser.Params>(props: {
|
|
63
63
|
parser: Parser.T<P>;
|
|
64
64
|
key: string;
|
|
65
65
|
params: P;
|
|
@@ -89,23 +89,25 @@ declare class I18n<ParserParams extends Parser.Params = any> {
|
|
|
89
89
|
translations: ExtendedStore<Translations.SerializedTranslations>;
|
|
90
90
|
locales: ExtendedStore<Config.Locale[]>;
|
|
91
91
|
private internalLocale;
|
|
92
|
-
locale: ExtendedStore<Config.Locale, () => Config.Locale, Writable<string>>;
|
|
93
92
|
private loaderTrigger;
|
|
93
|
+
private localeHelper;
|
|
94
|
+
locale: ExtendedStore<Config.Locale, () => Config.Locale, Writable<string>> & {
|
|
95
|
+
forceSet: any;
|
|
96
|
+
};
|
|
94
97
|
initialized: Readable<boolean>;
|
|
95
98
|
private translation;
|
|
96
|
-
private translate;
|
|
97
99
|
t: ExtendedStore<Translations.TranslationFunction<ParserParams>, Translations.TranslationFunction<ParserParams>>;
|
|
98
100
|
l: ExtendedStore<Translations.LocalTranslationFunction<ParserParams>, Translations.LocalTranslationFunction<ParserParams>>;
|
|
99
101
|
private getLocale;
|
|
100
|
-
setLocale: (locale?: string | undefined) => Promise<void
|
|
101
|
-
setRoute: (route: string) => Promise<void
|
|
102
|
+
setLocale: (locale?: string | undefined) => Promise<void | void[]> | undefined;
|
|
103
|
+
setRoute: (route: string) => Promise<void | void[]> | undefined;
|
|
102
104
|
configLoader(config: Config.T<ParserParams>): Promise<void>;
|
|
103
105
|
loadConfig: (config: Config.T<ParserParams>) => Promise<void>;
|
|
104
106
|
getTranslationProps: ($locale?: string, $route?: string) => Promise<[Translations.SerializedTranslations, Loader.IndexedKeys] | [
|
|
105
107
|
]>;
|
|
106
108
|
addTranslations: (translations?: Translations.SerializedTranslations | undefined, keys?: Loader.IndexedKeys | undefined) => void;
|
|
107
|
-
private
|
|
108
|
-
loadTranslations: (locale: Config.Locale, route?: string) => Promise<void
|
|
109
|
+
private loader;
|
|
110
|
+
loadTranslations: (locale: Config.Locale, route?: string) => Promise<void | void[]> | undefined;
|
|
109
111
|
}
|
|
110
112
|
|
|
111
113
|
export { Config, Loader, Parser, Translations, I18n as default };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var D=Object.defineProperty,I=Object.defineProperties;var $=Object.getOwnPropertyDescriptors;var b=Object.getOwnPropertySymbols;var F=Object.prototype.hasOwnProperty,K=Object.prototype.propertyIsEnumerable;var z=(a,t,e)=>t in a?D(a,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[t]=e,n=(a,t)=>{for(var e in t||(t={}))F.call(t,e)&&z(a,e,t[e]);if(b)for(var e of b(t))K.call(t,e)&&z(a,e,t[e]);return a},c=(a,t)=>I(a,$(t));var y=(a,t)=>{var e={};for(var r in a)F.call(a,r)&&t.indexOf(r)<0&&(e[r]=a[r]);if(a!=null&&b)for(var r of b(a))t.indexOf(r)<0&&K.call(a,r)&&(e[r]=a[r]);return e};import{derived as p,get as l,writable as L}from"svelte/store";var C=({parser:a,key:t,params:e,translations:r,locale:s,fallbackLocale:i})=>{if(!(t&&s))return console.warn("[i18n]: No translation key or locale provided. Skipping translation..."),"";let o=(r[s]||{})[t];return i&&o===void 0&&(o=(r[i]||{})[t]),a.parse(o,e,s,t)},d=(...a)=>a.length?a.filter(t=>!!t).map(t=>{let e=`${t}`.toLowerCase();try{[e]=Intl.Collator.supportedLocalesOf(t)}catch{console.warn(`[i18n]: Non-standard locale provided: '${a}'. Check your 'translations' and 'loaders' in i18n config...`)}return e}):[],P=(a,t)=>Object.keys(a||{}).reduce((e,r)=>{let s=a[r],i=t?`${t}.${r}`:`${r}`;return s&&typeof s=="object"?n(n({},e),P(s,i)):c(n({},e),{[i]:s})},{}),j=async a=>{try{return(await Promise.all(a.map(s=>{var i=s,{loader:e}=i,r=y(i,["loader"]);return new Promise(async o=>{let m;try{m=await e()}catch(v){console.error(`[i18n]: Failed to load translation. Verify your '${r.locale}' > '${r.key}' Loader.`),console.error(v)}o(c(n({loader:e},r),{data:m}))})}))).reduce((e,{key:r,data:s,locale:i})=>{if(!s)return e;let[o]=d(i);return c(n({},e),{[o]:P(c(n({},e[o]||{}),{[r]:s}))})},{})}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(r=>a[r]!==void 0).every(r=>a[r]===t[r])}catch{}return e};var O=1e3*60*60*24,E=class{constructor(t){this.cachedAt=0;this.loadedKeys={};this.currentRoute=L();this.config=L();this.isLoading=L(!1);this.promises=new Set;this.loading={subscribe:this.isLoading.subscribe,toPromise:(t,e)=>{let r=Array.from(this.promises).filter(s=>N({locale:d(t)[0],route:e},s)).map(({promise:s})=>s);return Promise.all(r)},get:()=>l(this.isLoading)};this.privateTranslations=L({});this.translations={subscribe:this.privateTranslations.subscribe,get:()=>l(this.translations)};this.locales=c(n({},p([this.config,this.privateTranslations],([t,e])=>{if(!t)return[];let{loaders:r=[]}=t,s=r.map(({locale:o})=>d(o)[0]),i=Object.keys(e).map(o=>d(o)[0]);return Array.from(new Set([...s,...i]))},[])),{get:()=>l(this.locales)});this.internalLocale=L();this.loaderTrigger=p([this.internalLocale,this.currentRoute],([t,e],r)=>{var s,i;t!==void 0&&e!==void 0&&(t!==((s=l(this.loaderTrigger))==null?void 0:s[0])||e!==((i=l(this.loaderTrigger))==null?void 0:i[1]))&&r([t,e])},[]);this.localeHelper=L();this.locale={subscribe:this.localeHelper.subscribe,forceSet:this.localeHelper.set,set:this.internalLocale.set,update:this.internalLocale.update,get:()=>l(this.locale)};this.initialized=p([this.locale,this.currentRoute,this.privateTranslations],([t,e,r],s)=>{l(this.initialized)||s(t!==void 0&&e!==void 0&&!!Object.keys(r).length)});this.translation=p([this.privateTranslations,this.locale,this.isLoading],([t,e,r],s)=>{let i=t[e];i&&Object.keys(i).length&&!r&&s(i)},{});this.t=c(n({},p([this.config,this.translation],([{parser:t,fallbackLocale:e}])=>(r,...s)=>C({parser:t,key:r,params:s,translations:this.translations.get(),locale:this.locale.get(),fallbackLocale:e}))),{get:(t,...e)=>l(this.t)(t,...e)});this.l=c(n({},p([this.config,this.translations],([{parser:t,fallbackLocale:e},r])=>(s,i,...o)=>C({parser:t,key:i,params:o,translations:r,locale:s,fallbackLocale:e}))),{get:(t,e,...r)=>l(this.l)(t,e,...r)});this.getLocale=t=>{if(!t)return"";let r=this.locales.get().find(s=>s===d(t)[0])||"";return d(r)[0]||""};this.setLocale=t=>{if(!t)return;let[e]=d(t);if(e!==l(this.internalLocale))return this.internalLocale.set(e),this.loading.toPromise(t,l(this.currentRoute))};this.setRoute=t=>{if(t!==l(this.currentRoute)){this.currentRoute.set(t);let e=l(this.internalLocale);return this.loading.toPromise(e,t)}};this.loadConfig=async t=>{await this.configLoader(t)};this.getTranslationProps=async(t=this.locale.get(),e=l(this.currentRoute))=>{let r=l(this.config);if(!r||!t)return[];let s=this.translations.get(),{loaders:i,fallbackLocale:o="",cache:m=O}=r||{},v=Number.isNaN(+m)?O:+m;this.cachedAt?Date.now()>v+this.cachedAt&&(this.loadedKeys={},this.cachedAt=0):this.cachedAt=Date.now();let[w,x]=d(t,o),A=s[w],W=s[x],S=(i||[]).map(R=>{var f=R,{locale:h}=f,u=y(f,["locale"]);return c(n({},u),{locale:d(h)[0]})}).filter(({routes:h})=>!h||(h||[]).some(k(e))).filter(({key:h,locale:u})=>u===w&&(!A||!(this.loadedKeys[w]||[]).includes(h))||o&&u===x&&(!W||!(this.loadedKeys[x]||[]).includes(h)));if(S.length){this.isLoading.set(!0);let h=await j(S);this.isLoading.set(!1);let u=Object.keys(h).reduce((f,g)=>c(n({},f),{[g]:Object.keys(h[g])}),{}),R=S.filter(({key:f,locale:g})=>(u[g]||[]).some(T=>`${T}`.startsWith(f))).reduce((f,{key:g,locale:T})=>c(n({},f),{[T]:[...f[T]||[],g]}),{});return[h,R]}return[]};this.addTranslations=(t,e)=>{if(!t)return;let r=Object.keys(t||{});this.privateTranslations.update(s=>r.reduce((i,o)=>c(n({},i),{[o]:n(n({},i[o]||{}),P(t[o]))}),s)),r.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.loader=async([t,e])=>{let r=(async()=>{let s=await this.getTranslationProps(t,e);s.length&&this.addTranslations(...s)})();this.promises.add({locale:t,route:e,promise:r}),r.then(()=>{let s=this.getLocale(t);s&&this.locale.get()!==s&&this.locale.forceSet(s)})};this.loadTranslations=(t,e=l(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=>{e&&this.promises.size&&(await this.loading.toPromise(),this.promises.clear(),console.info("[i18n]: Promises has been purged."))})}async configLoader(t){if(!t)throw new Error("[i18n]: No config provided!");let o=t,{initLocale:e,fallbackLocale:r,translations:s}=o,i=y(o,["initLocale","fallbackLocale","translations"]);e=d(e)[0],r=d(r)[0],this.config.set(n({initLocale:e,fallbackLocale:r,translations:s},i)),s&&this.addTranslations(s),await this.loadTranslations(e)}};export{E as default};
|
package/package.json
CHANGED