@sveltekit-i18n/base 1.0.0-rc.3 → 1.0.0-rc.4
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 +22 -17
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var P=Object.defineProperty,
|
|
1
|
+
var P=Object.defineProperty,k=Object.defineProperties,E=Object.getOwnPropertyDescriptor,O=Object.getOwnPropertyDescriptors,z=Object.getOwnPropertyNames,m=Object.getOwnPropertySymbols;var w=Object.prototype.hasOwnProperty,S=Object.prototype.propertyIsEnumerable;var R=(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)&&R(r,e,t[e]);if(m)for(var e of m(t))S.call(t,e)&&R(r,e,t[e]);return r},c=(r,t)=>k(r,O(t)),W=r=>P(r,"__esModule",{value:!0});var T=(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 I=(r,t)=>{for(var e in t)P(r,e,{get:t[e],enumerable:!0})},N=(r,t,e,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of z(t))!w.call(r,s)&&(e||s!=="default")&&P(r,s,{get:()=>t[s],enumerable:!(a=E(t,s))||a.enumerable});return r};var D=(r=>(t,e)=>r&&r.get(t)||(e=N(W({}),t,1),r&&r.set(t,e),e))(typeof WeakMap!="undefined"?new WeakMap:0);var M={};I(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=T(o,["loader"]);return new Promise(async i=>{let h;try{h=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:h}))})}))).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 h=(s[o]||{})[e];return i&&h===void 0&&(h=(s[i]||{})[e]),t.parse(h,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||{},h=`${t}`.toLowerCase(),L=i&&`${i}`.toLowerCase(),K=s[h],j=s[L],v=(o||[]).map(x=>{var u=x,{locale:d}=u,g=T(u,["locale"]);return c(l({},g),{locale:`${d}`.toLowerCase()})}).filter(({routes:d})=>!d||(d||[]).some($(e))).filter(({key:d,locale:g})=>g===h&&(!K||!(this.loadedKeys[h]||[]).includes(d))||i&&g===L&&(!j||!(this.loadedKeys[L]||[]).includes(d)));if(v.length){this.isLoading.set(!0);let d=await F(v);this.isLoading.set(!1);let g=Object.keys(d).reduce((u,p)=>c(l({},u),{[p]:Object.keys(d[p])}),{}),x=v.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,x]}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=T(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=D(M);0&&(module.exports={});
|
package/dist/index.d.ts
CHANGED
|
@@ -8,21 +8,21 @@ declare type LoadingStore = Readable<boolean> & {
|
|
|
8
8
|
get: () => boolean;
|
|
9
9
|
};
|
|
10
10
|
declare module DotNotation {
|
|
11
|
-
type Input =
|
|
11
|
+
type Input = Translations.Input;
|
|
12
12
|
type Output<V = any, K extends keyof V = keyof V> = {
|
|
13
13
|
[P in K]?: V[K];
|
|
14
|
-
}
|
|
14
|
+
};
|
|
15
15
|
type T = <I = Input>(input: I, parentKey?: string) => Output<I>;
|
|
16
16
|
}
|
|
17
17
|
declare module Config {
|
|
18
|
-
type
|
|
19
|
-
type Translations = Translations.T
|
|
20
|
-
type Locale =
|
|
18
|
+
type Loader = Loader.LoaderModule;
|
|
19
|
+
type Translations = Translations.T;
|
|
20
|
+
type Locale = Translations.Locales[number];
|
|
21
21
|
type InitLocale = Locale | undefined;
|
|
22
22
|
type FallbackLocale = Locale | undefined;
|
|
23
23
|
type T<P extends Parser.Params = Parser.Params> = {
|
|
24
|
-
loaders?:
|
|
25
|
-
translations?: Translations;
|
|
24
|
+
loaders?: Loader[];
|
|
25
|
+
translations?: Translations.T;
|
|
26
26
|
initLocale?: InitLocale;
|
|
27
27
|
fallbackLocale?: FallbackLocale;
|
|
28
28
|
parser: Parser.T<P>;
|
|
@@ -32,13 +32,14 @@ declare module Loader {
|
|
|
32
32
|
type Key = string;
|
|
33
33
|
type Locale = Config.Locale;
|
|
34
34
|
type Route = string | RegExp;
|
|
35
|
+
type IndexedKeys = Translations.LocaleIndexed<Key[]>;
|
|
35
36
|
type LoaderModule = {
|
|
36
37
|
key: Key;
|
|
37
38
|
locale: Locale;
|
|
38
39
|
routes?: Route[];
|
|
39
40
|
loader: T;
|
|
40
41
|
};
|
|
41
|
-
type T = () => Promise<
|
|
42
|
+
type T = () => Promise<Translations.Input>;
|
|
42
43
|
}
|
|
43
44
|
declare module Parser {
|
|
44
45
|
type Value = any;
|
|
@@ -47,27 +48,31 @@ declare module Parser {
|
|
|
47
48
|
type Key = Loader.Key;
|
|
48
49
|
type Output = any;
|
|
49
50
|
type Parse<P extends Parser.Params = Parser.Params> = (value: Value, params: P, locale: Locale, key: Key) => Output;
|
|
50
|
-
type ParserFactory<ParserConfig = any, P extends Parser.Params = any> = (config?: ParserConfig) => Parser.T<P>;
|
|
51
51
|
type T<P extends Parser.Params = Parser.Params> = {
|
|
52
52
|
parse: Parse<P>;
|
|
53
53
|
};
|
|
54
54
|
}
|
|
55
55
|
declare module Translations {
|
|
56
|
-
type
|
|
56
|
+
type Locales = string[];
|
|
57
|
+
type SerializedTranslations = LocaleIndexed<DotNotation.Output>;
|
|
57
58
|
type FetchTranslations = (loaders: Loader.LoaderModule[]) => Promise<SerializedTranslations>;
|
|
58
|
-
type TranslationFunction<P extends Parser.Params = Parser.Params> = (key: string, ...restParams: P) =>
|
|
59
|
-
type LocalTranslationFunction<P extends Parser.Params = Parser.Params> = (locale:
|
|
59
|
+
type TranslationFunction<P extends Parser.Params = Parser.Params> = (key: string, ...restParams: P) => any;
|
|
60
|
+
type LocalTranslationFunction<P extends Parser.Params = Parser.Params> = (locale: Config.Locale, key: string, ...restParams: P) => any;
|
|
60
61
|
type Translate<P extends Parser.Params = Parser.Params> = (props: {
|
|
61
62
|
parser: Parser.T<P>;
|
|
62
63
|
key: string;
|
|
63
64
|
params: P;
|
|
64
65
|
translations: SerializedTranslations;
|
|
65
|
-
locale:
|
|
66
|
+
locale: Locales[number];
|
|
66
67
|
fallbackLocale?: string;
|
|
67
68
|
}) => string;
|
|
68
|
-
type
|
|
69
|
-
[
|
|
69
|
+
type Input<V = any> = {
|
|
70
|
+
[key: string]: Input<V> | V;
|
|
71
|
+
};
|
|
72
|
+
type LocaleIndexed<V> = {
|
|
73
|
+
[locale in Locales[number]]: V;
|
|
70
74
|
};
|
|
75
|
+
type T<V = any> = LocaleIndexed<Input<V>>;
|
|
71
76
|
}
|
|
72
77
|
|
|
73
78
|
declare class I18n<ParserParams extends Parser.Params = any> {
|
|
@@ -94,8 +99,8 @@ declare class I18n<ParserParams extends Parser.Params = any> {
|
|
|
94
99
|
setRoute: (route: string) => Promise<void>;
|
|
95
100
|
configLoader(config: Config.T<ParserParams>): Promise<void>;
|
|
96
101
|
loadConfig: (config: Config.T<ParserParams>) => Promise<void>;
|
|
97
|
-
addTranslations: (translations?:
|
|
98
|
-
getTranslationProps: ($locale?: string, $route?: string) => Promise<[
|
|
102
|
+
addTranslations: (translations?: Translations.SerializedTranslations | undefined, keys?: Loader.IndexedKeys | undefined) => void;
|
|
103
|
+
getTranslationProps: ($locale?: string, $route?: string) => Promise<[Translations.SerializedTranslations, Loader.IndexedKeys] | [
|
|
99
104
|
]>;
|
|
100
105
|
private translationLoader;
|
|
101
106
|
loadTranslations: (locale: Config.Locale, route?: string) => Promise<void>;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var E=Object.defineProperty,O=Object.defineProperties;var z=Object.getOwnPropertyDescriptors;var b=Object.getOwnPropertySymbols;var R=Object.prototype.hasOwnProperty,S=Object.prototype.propertyIsEnumerable;var x=(r,t,e)=>t in r?E(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,n=(r,t)=>{for(var e in t||(t={}))R.call(t,e)&&x(r,e,t[e]);if(b)for(var e of b(t))S.call(t,e)&&x(r,e,t[e]);return r},l=(r,t)=>O(r,z(t));var y=(r,t)=>{var e={};for(var a in r)R.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 g,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=y(o,["loader"]);return new Promise(async i=>{let h;try{h=await e()}catch(T){console.error(`Failed to load translation. Verify your '${a.locale}' > '${a.key}' Loader.`),console.error(T)}i(l(n({loader:e},a),{data:h}))})}))).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 K=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({},g([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},g(this.internalLocale,(t,e)=>{let a=this.getLocale(t);a&&a!==this.locale.get()&&e(a)})),{get:()=>d(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)=>{d(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 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 h=(s[o]||{})[e];return i&&h===void 0&&(h=(s[i]||{})[e]),t.parse(h,a,o,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)=>d(this.t)(t,...e)});this.l=l(n({},g([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||{},h=`${t}`.toLowerCase(),T=i&&`${i}`.toLowerCase(),j=s[h],k=s[T],w=(o||[]).map(C=>{var u=C,{locale:c}=u,p=y(u,["locale"]);return l(n({},p),{locale:`${c}`.toLowerCase()})}).filter(({routes:c})=>!c||(c||[]).some($(e))).filter(({key:c,locale:p})=>p===h&&(!j||!(this.loadedKeys[h]||[]).includes(c))||i&&p===T&&(!k||!(this.loadedKeys[T]||[]).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=y(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{K as default};
|
package/package.json
CHANGED