@sveltekit-i18n/base 1.0.0-beta.5 → 1.0.0-beta.6
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 +21 -19
- 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,S=Object.defineProperties,O=Object.getOwnPropertyDescriptor,W=Object.getOwnPropertyDescriptors,K=Object.getOwnPropertyNames,m=Object.getOwnPropertySymbols;var w=Object.prototype.hasOwnProperty,C=Object.prototype.propertyIsEnumerable;var R=(a,t,e)=>t in a?P(a,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[t]=e,l=(a,t)=>{for(var e in t||(t={}))w.call(t,e)&&R(a,e,t[e]);if(m)for(var e of m(t))C.call(t,e)&&R(a,e,t[e]);return a},c=(a,t)=>S(a,W(t)),N=a=>P(a,"__esModule",{value:!0});var y=(a,t)=>{var e={};for(var s in a)w.call(a,s)&&t.indexOf(s)<0&&(e[s]=a[s]);if(a!=null&&m)for(var s of m(a))t.indexOf(s)<0&&C.call(a,s)&&(e[s]=a[s]);return e};var k=(a,t)=>{for(var e in t)P(a,e,{get:t[e],enumerable:!0})},D=(a,t,e,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of K(t))!w.call(a,r)&&(e||r!=="default")&&P(a,r,{get:()=>t[r],enumerable:!(s=O(t,r))||s.enumerable});return a};var z=(a=>(t,e)=>a&&a.get(t)||(e=D(N({}),t,1),a&&a.set(t,e),e))(typeof WeakMap!="undefined"?new WeakMap:0);var I={};k(I,{default:()=>A});var i=require("svelte/store");var T=(a,t)=>Object.keys(a||{}).reduce((e,s)=>{let r=a[s],o=t?`${t}.${s}`:`${s}`;return r&&typeof r=="object"?l(l({},e),T(r,o)):c(l({},e),{[o]:r})},{}),F=async a=>{try{return(await Promise.all(a.map(r=>{var o=r,{loader:e}=o,s=y(o,["loader"]);return new Promise(async n=>{let g;try{g=await e()}catch(p){console.error(`Failed to load translation. Verify your '${s.locale}' > '${s.key}' loader.`),console.error(p)}n(c(l({loader:e},s),{data:g}))})}))).reduce((e,{key:s,data:r,locale:o})=>r?c(l({},e),{[o]:T(c(l({},e[o]||{}),{[s]:r}))}):e,{})}catch(t){console.error(t)}return{}},x=a=>t=>{try{if(typeof t=="string")return t===a;if(typeof t=="object")return t.test(a)}catch{throw new Error("Invalid route config!")}return!1};var $=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,e])=>{if(!t)return[];let{loaders:s=[]}=t,r=s.map(({locale:n})=>`${n}`.toLowerCase()),o=Object.keys(e).map(n=>`${n}`.toLowerCase());return[...new Set([...r,...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,e)=>{let s=t&&`${t}`.toLowerCase(),r=this.getLocale(s);r&&r!==this.locale.get()&&e(r)})),{get:()=>(0,i.get)(this.locale)});this.loaderTrigger=(0,i.derived)([this.internalLocale,this.currentRoute],([t,e],s)=>{t!==void 0&&e!==void 0&&s([t,e])},[]);this.initialized=(0,i.derived)([this.locale,this.currentRoute,this.privateTranslations],([t,e,s],r)=>{(0,i.get)(this.initialized)||r(t!==void 0&&e!==void 0&&!!Object.keys(s).length)});this.translation=(0,i.derived)([this.privateTranslations,this.locale,this.isLoading],([t,e,s],r)=>{let o=t[e];o&&Object.keys(o).length&&!s&&r(o)},{});this.translate=({parser:t,key:e,params:s,translations:r,locale:o,fallbackLocale:n})=>{if(!e)throw new Error("No key provided to $t()");if(!o)throw new Error("No locale set!");let g=(r[o]||{})[e];return n&&g===void 0&&(g=(r[n]||{})[e]),t.parse(g,s,o,e)};this.t=c(l({},(0,i.derived)([this.config,this.translation],([{parser:t,fallbackLocale:e}])=>(s,...r)=>this.translate({parser:t,key:s,params:r,translations:this.translations.get(),locale:this.locale.get(),fallbackLocale:e}))),{get:(t,...e)=>(0,i.get)(this.t)(t,...e)});this.l=c(l({},(0,i.derived)([this.config,this.translations],([{parser:t,fallbackLocale:e},s])=>(r,o,...n)=>this.translate({parser:t,key:o,params:n,translations:s,locale:r,fallbackLocale:e}))),{get:(t,e,...s)=>(0,i.get)(this.l)(t,e,...s)});this.getLocale=t=>t?`${this.locales.get().find(r=>`${r}`.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,e)=>{if(!t)return;let s=Object.keys(t||{});this.privateTranslations.update(r=>s.reduce((o,n)=>c(l({},o),{[n]:l(l({},o[n]||{}),T(t[n]))}),r)),s.forEach(r=>{let o=Object.keys(t[r]).map(n=>`${n}`.split(".")[0]);e&&(o=e[r]),this.loadedKeys[r]=Array.from(new Set([...this.loadedKeys[r]||[],...o||[]]))})};this.getTranslationProps=async(t=this.locale.get(),e=(0,i.get)(this.currentRoute))=>{let s=(0,i.get)(this.config);if(!s||!t)return[];let r=this.translations.get(),{loaders:o,fallbackLocale:n=""}=s||{},g=`${t}`.toLowerCase(),p=n&&`${n}`.toLowerCase(),E=r[g],j=r[p],b=(o||[]).map(v=>{var h=v,{locale:d}=h,u=y(h,["locale"]);return c(l({},u),{locale:`${d}`.toLowerCase()})}).filter(({routes:d})=>!d||(d||[]).some(x(e))).filter(({key:d,locale:u})=>u===g&&(!E||!(this.loadedKeys[g]||[]).includes(d))||n&&u===p&&(!j||!(this.loadedKeys[p]||[]).includes(d)));if(b.length){this.isLoading.set(!0);let d=await F(b);this.isLoading.set(!1);let u=Object.keys(d).reduce((h,f)=>c(l({},h),{[f]:Object.keys(d[f])}),{}),v=b.filter(({key:h,locale:f})=>(u[f]||[]).some(L=>`${L}`.startsWith(h))).reduce((h,{key:f,locale:L})=>c(l({},h),{[L]:[...h[L]||[],f]}),{});return[d,v]}return[]};this.translationLoader=async t=>{this.promises.push(new Promise(async e=>{let s=await this.getTranslationProps(t);s.length&&this.addTranslations(...s),e()})),await this.loading.toPromise()};this.loadTranslations=async(t,e=(0,i.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!");this.config.set(t);let{initLocale:e="",translations:s}=t;s&&this.addTranslations(s),await this.loadTranslations(e)}};var A=$;module.exports=z(I);0&&(module.exports={});
|
package/dist/index.d.ts
CHANGED
|
@@ -13,9 +13,11 @@ type LoaderModule = {
|
|
|
13
13
|
loader: Loader;
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
-
type
|
|
16
|
+
type ParserParamsDefault = any[] | [];
|
|
17
17
|
|
|
18
|
-
type
|
|
18
|
+
type TranslationFunction<ParserParams extends ParserParamsDefault> = (key: string, ...restParams: ParserParams) => string;
|
|
19
|
+
|
|
20
|
+
type LocalTranslationFunction<ParserParams extends ParserParamsDefault> = (locale: string, key: string, ...restParams: ParserParams) => string;
|
|
19
21
|
|
|
20
22
|
type ExtendedStore<T, Get = () => T, Store = Readable<T>> = Store & { get: Get };
|
|
21
23
|
|
|
@@ -23,28 +25,27 @@ type ConfigTranslations = { [locale: string]: Record<string, any> };
|
|
|
23
25
|
|
|
24
26
|
type Translations = { [locale: string]: Record<string, string> };
|
|
25
27
|
|
|
26
|
-
type Parse<
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}) => string;
|
|
28
|
+
type Parse<ParserParams = ParserParamsDefault> = (
|
|
29
|
+
text: string | undefined,
|
|
30
|
+
params: ParserParams,
|
|
31
|
+
locale: string,
|
|
32
|
+
key: string,
|
|
33
|
+
) => string;
|
|
33
34
|
|
|
34
|
-
type Parser = {
|
|
35
|
-
parse: Parse
|
|
35
|
+
type Parser<ParserParams = ParserParamsDefault> = {
|
|
36
|
+
parse: Parse<ParserParams>;
|
|
36
37
|
};
|
|
37
38
|
|
|
38
|
-
type Config = {
|
|
39
|
-
parser: Parser;
|
|
39
|
+
type Config<ParserParams = ParserParamsDefault> = {
|
|
40
40
|
loaders?: LoaderModule[];
|
|
41
41
|
translations?: ConfigTranslations;
|
|
42
42
|
initLocale?: string;
|
|
43
43
|
fallbackLocale?: string;
|
|
44
|
+
parser: Parser<ParserParams>;
|
|
44
45
|
};
|
|
45
46
|
|
|
46
|
-
declare class I18n {
|
|
47
|
-
constructor(config?: Config);
|
|
47
|
+
declare class I18n<ParserParams extends ParserParamsDefault> {
|
|
48
|
+
constructor(config?: Config<ParserParams>);
|
|
48
49
|
private loadedKeys;
|
|
49
50
|
private currentRoute;
|
|
50
51
|
private config;
|
|
@@ -59,13 +60,14 @@ declare class I18n {
|
|
|
59
60
|
private loaderTrigger;
|
|
60
61
|
initialized: Readable<boolean>;
|
|
61
62
|
private translation;
|
|
62
|
-
|
|
63
|
-
|
|
63
|
+
private translate;
|
|
64
|
+
t: ExtendedStore<TranslationFunction<ParserParams>, TranslationFunction<ParserParams>>;
|
|
65
|
+
l: ExtendedStore<LocalTranslationFunction<ParserParams>, LocalTranslationFunction<ParserParams>>;
|
|
64
66
|
private getLocale;
|
|
65
67
|
setLocale: (locale?: string | undefined) => Promise<void>;
|
|
66
68
|
setRoute: (route: string) => Promise<void>;
|
|
67
|
-
configLoader(config: Config): Promise<void>;
|
|
68
|
-
loadConfig: (config: Config) => Promise<void>;
|
|
69
|
+
configLoader(config: Config<ParserParams>): Promise<void>;
|
|
70
|
+
loadConfig: (config: Config<ParserParams>) => Promise<void>;
|
|
69
71
|
addTranslations: (translations?: ConfigTranslations | undefined, keys?: Record<string, string[]> | undefined) => void;
|
|
70
72
|
getTranslationProps: ($locale?: string, $route?: string) => Promise<[ConfigTranslations, Record<string, string[]>] | [
|
|
71
73
|
]>;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var O=Object.defineProperty,W=Object.defineProperties;var K=Object.getOwnPropertyDescriptors;var
|
|
1
|
+
var O=Object.defineProperty,W=Object.defineProperties;var K=Object.getOwnPropertyDescriptors;var y=Object.getOwnPropertySymbols;var C=Object.prototype.hasOwnProperty,F=Object.prototype.propertyIsEnumerable;var R=(o,t,e)=>t in o?O(o,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):o[t]=e,n=(o,t)=>{for(var e in t||(t={}))C.call(t,e)&&R(o,e,t[e]);if(y)for(var e of y(t))F.call(t,e)&&R(o,e,t[e]);return o},l=(o,t)=>W(o,K(t));var T=(o,t)=>{var e={};for(var s in o)C.call(o,s)&&t.indexOf(s)<0&&(e[s]=o[s]);if(o!=null&&y)for(var s of y(o))t.indexOf(s)<0&&F.call(o,s)&&(e[s]=o[s]);return e};import{derived as u,get as d,writable as m}from"svelte/store";var b=(o,t)=>Object.keys(o||{}).reduce((e,s)=>{let r=o[s],a=t?`${t}.${s}`:`${s}`;return r&&typeof r=="object"?n(n({},e),b(r,a)):l(n({},e),{[a]:r})},{}),x=async o=>{try{return(await Promise.all(o.map(r=>{var a=r,{loader:e}=a,s=T(a,["loader"]);return new Promise(async i=>{let g;try{g=await e()}catch(L){console.error(`Failed to load translation. Verify your '${s.locale}' > '${s.key}' loader.`),console.error(L)}i(l(n({loader:e},s),{data:g}))})}))).reduce((e,{key:s,data:r,locale:a})=>r?l(n({},e),{[a]:b(l(n({},e[a]||{}),{[s]:r}))}):e,{})}catch(t){console.error(t)}return{}},$=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 E=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({},u([this.config,this.privateTranslations],([t,e])=>{if(!t)return[];let{loaders:s=[]}=t,r=s.map(({locale:i})=>`${i}`.toLowerCase()),a=Object.keys(e).map(i=>`${i}`.toLowerCase());return[...new Set([...r,...a])]},[])),{get:()=>d(this.locales)});this.internalLocale=m();this.locale=l(n({set:this.internalLocale.set,update:this.internalLocale.update},u(this.internalLocale,(t,e)=>{let s=t&&`${t}`.toLowerCase(),r=this.getLocale(s);r&&r!==this.locale.get()&&e(r)})),{get:()=>d(this.locale)});this.loaderTrigger=u([this.internalLocale,this.currentRoute],([t,e],s)=>{t!==void 0&&e!==void 0&&s([t,e])},[]);this.initialized=u([this.locale,this.currentRoute,this.privateTranslations],([t,e,s],r)=>{d(this.initialized)||r(t!==void 0&&e!==void 0&&!!Object.keys(s).length)});this.translation=u([this.privateTranslations,this.locale,this.isLoading],([t,e,s],r)=>{let a=t[e];a&&Object.keys(a).length&&!s&&r(a)},{});this.translate=({parser:t,key:e,params:s,translations:r,locale:a,fallbackLocale:i})=>{if(!e)throw new Error("No key provided to $t()");if(!a)throw new Error("No locale set!");let g=(r[a]||{})[e];return i&&g===void 0&&(g=(r[i]||{})[e]),t.parse(g,s,a,e)};this.t=l(n({},u([this.config,this.translation],([{parser:t,fallbackLocale:e}])=>(s,...r)=>this.translate({parser:t,key:s,params:r,translations:this.translations.get(),locale:this.locale.get(),fallbackLocale:e}))),{get:(t,...e)=>d(this.t)(t,...e)});this.l=l(n({},u([this.config,this.translations],([{parser:t,fallbackLocale:e},s])=>(r,a,...i)=>this.translate({parser:t,key:a,params:i,translations:s,locale:r,fallbackLocale:e}))),{get:(t,e,...s)=>d(this.l)(t,e,...s)});this.getLocale=t=>t?`${this.locales.get().find(r=>`${r}`.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,e)=>{if(!t)return;let s=Object.keys(t||{});this.privateTranslations.update(r=>s.reduce((a,i)=>l(n({},a),{[i]:n(n({},a[i]||{}),b(t[i]))}),r)),s.forEach(r=>{let a=Object.keys(t[r]).map(i=>`${i}`.split(".")[0]);e&&(a=e[r]),this.loadedKeys[r]=Array.from(new Set([...this.loadedKeys[r]||[],...a||[]]))})};this.getTranslationProps=async(t=this.locale.get(),e=d(this.currentRoute))=>{let s=d(this.config);if(!s||!t)return[];let r=this.translations.get(),{loaders:a,fallbackLocale:i=""}=s||{},g=`${t}`.toLowerCase(),L=i&&`${i}`.toLowerCase(),j=r[g],S=r[L],w=(a||[]).map(v=>{var h=v,{locale:c}=h,f=T(h,["locale"]);return l(n({},f),{locale:`${c}`.toLowerCase()})}).filter(({routes:c})=>!c||(c||[]).some($(e))).filter(({key:c,locale:f})=>f===g&&(!j||!(this.loadedKeys[g]||[]).includes(c))||i&&f===L&&(!S||!(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((h,p)=>l(n({},h),{[p]:Object.keys(c[p])}),{}),v=w.filter(({key:h,locale:p})=>(f[p]||[]).some(P=>`${P}`.startsWith(h))).reduce((h,{key:p,locale:P})=>l(n({},h),{[P]:[...h[P]||[],p]}),{});return[c,v]}return[]};this.translationLoader=async t=>{this.promises.push(new Promise(async e=>{let s=await this.getTranslationProps(t);s.length&&this.addTranslations(...s),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!");this.config.set(t);let{initLocale:e="",translations:s}=t;s&&this.addTranslations(s),await this.loadTranslations(e)}};var I=E;export{I as default};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sveltekit-i18n/base",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.6",
|
|
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",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"./package.json": "./package.json"
|
|
16
16
|
},
|
|
17
17
|
"scripts": {
|
|
18
|
-
"dev": "tsup src/index.ts --watch",
|
|
18
|
+
"dev": "tsup src/index.ts --dts --format cjs,esm --sourcemap --watch",
|
|
19
19
|
"test": "jest",
|
|
20
20
|
"build": "tsup src/index.ts --dts --format cjs,esm --minify",
|
|
21
21
|
"prepublishOnly": "rm -rf dist && npm run build",
|