@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 +1 -1
- package/dist/index.d.ts +11 -11
- package/dist/index.js +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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 =
|
|
59
|
-
type LocalTranslationFunction<P extends Parser.Params =
|
|
60
|
-
type Translate<P extends Parser.Params =
|
|
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<
|
|
69
|
-
[locale: string]: Record<string,
|
|
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<
|
|
83
|
+
locales: ExtendedStore<Config.Locale[]>;
|
|
84
84
|
private internalLocale;
|
|
85
|
-
locale: ExtendedStore<
|
|
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:
|
|
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
|
|
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.
|
|
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
|
+
}
|