sveltekit-i18n
Advanced tools
Comparing version 1.5.3 to 1.5.4
@@ -15,2 +15,2 @@ import{writable,get,derived}from"svelte/store"; | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */function t(t,e){var s={};for(var i in t)Object.prototype.hasOwnProperty.call(t,i)&&e.indexOf(i)<0&&(s[i]=t[i]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var a=0;for(i=Object.getOwnPropertySymbols(t);a<i.length;a++)e.indexOf(i[a])<0&&Object.prototype.propertyIsEnumerable.call(t,i[a])&&(s[i[a]]=t[i[a]])}return s}const e=(t,e={})=>t||e,s=(t,e,s)=>{var i;return(null===(i=t.find((t=>t.key===e)))||void 0===i?void 0:i.value)||s},i=(t,s=[],i="")=>e(s.find((({key:e})=>`${e}`.toLowerCase()===`${t}`.toLowerCase()))).value||i,a=(t,s=[],i="")=>{const a=s.sort(((t,e)=>+t.key-+e.key));return e(a.find((({key:e})=>+t<+e))).value||i},o=(t,s=[],i="")=>{const a=s.sort(((t,e)=>+e.key-+t.key));return e(a.find((({key:e})=>+t>+e))).value||i},r=[{key:"second",multiplier:1e3},{key:"minute",multiplier:60},{key:"hour",multiplier:60},{key:"day",multiplier:24},{key:"week",multiplier:7},{key:"month",multiplier:13/3},{key:"year",multiplier:12}],n=t=>r.reduce((([t,e],{key:s,multiplier:i},a)=>{if(!e||a===(t=>r.indexOf(r.find((e=>e.key===t))))(e)+1){const a=Math.round(t/i);if(!e||Math.abs(a)>=1)return[a,s]}return[t,e]}),[t,""]);var l=Object.freeze({__proto__:null,eq:i,ne:(t,s=[],i="")=>e(s.find((({key:e})=>`${e}`.toLowerCase()!==`${t}`.toLowerCase()))).value||i,lt:a,gt:o,lte:(t,e=[],s="")=>i(t,e,a(t,e,s)),gte:(t,e=[],s="")=>i(t,e,o(t,e,s)),number:(t,e=[],i="",a="")=>a&&new Intl.NumberFormat(a,{maximumFractionDigits:s(e,"decimals",s(e,"maxDecimals","2")),minimumFractionDigits:s(e,"minDecimals")}).format(+t||+i),date:(t,e=[],i="",a="")=>a&&new Intl.DateTimeFormat(a,{dateStyle:s(e,"dateStyle","medium"),timeStyle:s(e,"timeStyle","short")}).format(+t||+i),ago:(t,e=[],i="",a="")=>{if(!a)return"";const o=s(e,"format","auto"),r=(+t||+i)-Date.now(),l="auto"===o?n(r):[r,o];return new Intl.RelativeTimeFormat(a,{numeric:s(e,"numeric","auto"),style:s(e,"style","long")}).format(...l)}});const c=(t,s)=>Object.keys(e(t)).reduce(((e,i)=>{const a=t[i],o=s?`${s}.${i}`:`${i}`;return a&&"object"==typeof a?Object.assign(Object.assign({},e),c(a,o)):Object.assign(Object.assign({},e),{[o]:a})}),{}),u=t=>t.replace(/\\(?=:|;|{|})/g,""),h=(t,s={},i,a)=>{if(((t="")=>/{{(?:(?!{{|}}).)+}}/.test(`${t}`))(t)){const o=((t,s={},i={},a)=>t.replace(/{{\s*(?:(?!{{|}}).)+\s*}}/g,(t=>{const o=u(`${t.match(/(?!{|\s).+?(?!\\[:;]).(?=\s*(?:[:;]|}}$))/)}`),r=null==s?void 0:s[o],[,n=""]=e(t.match(/.+?(?!\\;).;\s*default\s*:\s*([^\s:;].+?(?:\\[:;]|[^;\s}])*)(?=\s*(?:;|}}$))/i),[]);let[,c=""]=e(t.match(/{{\s*(?:[^;]|(?:\\;))+\s*(?:(?!\\:).[:])\s*(?!\s)((?:\\;|[^;])+?)(?=\s*(?:[;]|}}$))/i),[]);if(void 0===r&&"ne"!==c)return n;const h=!!c,d=Object.assign(Object.assign({},l),e(i));c=Object.keys(d).includes(c)?c:"eq";const g=d[c],y=e(t.match(/[^\s:;{](?:[^;]|\\[;])+[^\s:;}]/gi),[]).reduce(((t,e,s)=>{if(s>0){const s=u(`${e.match(/(?:(?:\\:)|[^:])+/)}`.trim()),i=`${e.match(/(?:(?:\\:)|[^:])+$/)}`.trim();if(s&&"default"!==s&&i)return[...t,{key:s,value:i}]}return t}),[]);return h||y.length?g(r,y,n,a):`${r}`})))(t,s,i,a);return h(o,s,i,a)}return u(`${t}`)},d=({translation:t,translations:s={},key:i,vars:a,customModifiers:o={},locale:r,fallbackLocale:n})=>{if(!i)throw new Error("no key provided to $t()");let l=e(t)[i];return n&&void 0===l&&(l=e(s[n])[i]),(null==a?void 0:a.default)&&void 0===l&&(l=`${a.default}`),void 0===l&&(l=`${i}`),h(l,a,o,r)};class g{constructor(s){this.loadedKeys={},this.currentRoute=writable(),this.config=writable({}),this.isLoading=writable(!1),this.promises=[],this.loading={subscribe:this.isLoading.subscribe,toPromise:()=>Promise.all(this.promises),get:()=>get(this.isLoading)},this.privateTranslations=writable({}),this.translations={subscribe:this.privateTranslations.subscribe,get:()=>get(this.translations)},this.locales=Object.assign(Object.assign({},derived([this.config,this.privateTranslations],(([t,e])=>{const{loaders:s=[]}=t,i=s.map((({locale:t})=>`${t}`.toLowerCase())),a=Object.keys(e).map((t=>`${t}`.toLowerCase()));return[...new Set([...i,...a])]}),[])),{get:()=>get(this.locales)}),this.internalLocale=writable(),this.locale=Object.assign(Object.assign({set:this.internalLocale.set,update:this.internalLocale.update},derived(this.internalLocale,((t,e)=>{const s=t&&`${t}`.toLowerCase(),i=this.getLocale(s);i&&i!==this.locale.get()&&e(i)}))),{get:()=>get(this.locale)}),this.loaderTrigger=derived([this.internalLocale,this.currentRoute],(([t,e],s)=>{void 0!==t&&void 0!==e&&s([t,e])}),[]),this.initialized=derived([this.locale,this.currentRoute,this.privateTranslations],(([t,e,s],i)=>{get(this.initialized)||i(void 0!==t&&void 0!==e&&!!Object.keys(s).length)})),this.translation=derived([this.privateTranslations,this.locale,this.isLoading],(([t,e,s],i)=>{const a=t[e];a&&Object.keys(a).length&&!s&&i(a)}),{}),this.t=Object.assign(Object.assign({},derived([this.translation,this.config],(([t,{customModifiers:s,fallbackLocale:i}])=>(a,o)=>d({translation:t,translations:this.translations.get(),key:a,vars:o,customModifiers:e(s),locale:this.locale.get(),fallbackLocale:i})))),{get:(...t)=>get(this.t)(...t)}),this.l=Object.assign(Object.assign({},derived([this.translations,this.config],(([t,{customModifiers:s,fallbackLocale:i}])=>(a,o,r)=>d({translation:t[a],translations:t,key:o,vars:r,customModifiers:e(s),locale:this.locale.get(),fallbackLocale:i})))),{get:(...t)=>get(this.l)(...t)}),this.getLocale=t=>{if(!t)return"";return`${this.locales.get().find((e=>`${e}`.toLowerCase()===`${t}`.toLowerCase()))||""}`.toLowerCase()},this.setLocale=async t=>{t&&(this.internalLocale.set(t),await this.loading.toPromise())},this.setRoute=async t=>{t!==get(this.currentRoute)&&this.currentRoute.set(t),await this.loading.toPromise()},this.loadConfig=async t=>{if(!t)throw new Error("No config!");this.config.set(t);const{initLocale:e="",translations:s}=t;s&&this.addTranslations(s),await this.loadTranslations(e)},this.addTranslations=(t,s)=>{if(!t)return;const i=Object.keys(e(t));this.privateTranslations.update((s=>i.reduce(((s,i)=>Object.assign(Object.assign({},s),{[i]:Object.assign(Object.assign({},e(s[i])),c(t[i]))})),s))),i.forEach((i=>{let a=Object.keys(t[i]).map((t=>`${t}`.split(".")[0]));s&&(a=s[i]),this.loadedKeys[i]=Array.from(new Set([...e(this.loadedKeys[i],[]),...e(a,[])]))}))},this.getTranslationProps=async(s=this.locale.get(),i=get(this.currentRoute))=>{const a=get(this.config);if(!a||!s)return[];const o=this.translations.get(),{loaders:r,fallbackLocale:n=""}=e(a),l=`${s}`.toLowerCase(),u=n&&`${n}`.toLowerCase(),h=o[l],d=o[u],g=e(r,[]).map((e=>{var{locale:s}=e,i=t(e,["locale"]);return Object.assign(Object.assign({},i),{locale:`${s}`.toLowerCase()})})).filter((({routes:t})=>{return!t||e(t,[]).some((s=i,t=>{try{if("string"==typeof t)return t===s;if("object"==typeof t)return t.test(s)}catch(t){throw new Error("Invalid route config!")}return!1}));var s})).filter((({key:t,locale:s})=>s===l&&(!h||!e(this.loadedKeys[l],[]).includes(t))||n&&s===u&&(!d||!e(this.loadedKeys[u],[]).includes(t))));if(g.length){this.isLoading.set(!0);const s=await(async s=>{try{return(await Promise.all(s.map((e=>{var{loader:s}=e,i=t(e,["loader"]);return new Promise((async t=>{let e;try{e=await s()}catch(t){console.error(`Failed to load translation. Verify your '${i.locale}' > '${i.key}' loader.`),console.error(t)}t(Object.assign(Object.assign({loader:s},i),{data:e}))}))})))).reduce(((t,{key:s,data:i,locale:a})=>i?Object.assign(Object.assign({},t),{[a]:c(Object.assign(Object.assign({},e(t[a])),{[s]:i}))}):t),{})}catch(t){console.error(t)}return{}})(g);this.isLoading.set(!1);const i=Object.keys(s).reduce(((t,e)=>Object.assign(Object.assign({},t),{[e]:Object.keys(s[e])})),{}),a=g.filter((({key:t,locale:s})=>e(i[s],[]).some((e=>`${e}`.startsWith(t))))).reduce(((t,{key:e,locale:s})=>Object.assign(Object.assign({},t),{[s]:[...t[s]||[],e]})),{});return[s,a]}return[]},this.translationLoader=async t=>{this.promises.push(new Promise((async e=>{const s=await this.getTranslationProps(t);s.length&&this.addTranslations(...s),e()}))),await this.loading.toPromise()},this.loadTranslations=async(t,e=get(this.currentRoute)||"")=>{t&&(this.setRoute(e),this.setLocale(t),await this.loading.toPromise())},s&&this.loadConfig(s),this.loaderTrigger.subscribe((()=>this.translationLoader()))}}export{g as default}; | ||
***************************************************************************** */function t(t,e){var s={};for(var i in t)Object.prototype.hasOwnProperty.call(t,i)&&e.indexOf(i)<0&&(s[i]=t[i]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var a=0;for(i=Object.getOwnPropertySymbols(t);a<i.length;a++)e.indexOf(i[a])<0&&Object.prototype.propertyIsEnumerable.call(t,i[a])&&(s[i[a]]=t[i[a]])}return s}const e=(t,e={})=>t||e,s=(t,e,s)=>{var i;return(null===(i=t.find((t=>t.key===e)))||void 0===i?void 0:i.value)||s},i=(t,s)=>Object.keys(e(t)).reduce(((e,a)=>{const o=t[a],r=s?`${s}.${a}`:`${a}`;return o&&"object"==typeof o?Object.assign(Object.assign({},e),i(o,r)):Object.assign(Object.assign({},e),{[r]:o})}),{}),a=(t,s=[],i="")=>e(s.find((({key:e})=>`${e}`.toLowerCase()===`${t}`.toLowerCase()))).value||i,o=(t,s=[],i="")=>{const a=s.sort(((t,e)=>+t.key-+e.key));return e(a.find((({key:e})=>+t<+e))).value||i},r=(t,s=[],i="")=>{const a=s.sort(((t,e)=>+e.key-+t.key));return e(a.find((({key:e})=>+t>+e))).value||i},n=[{key:"second",multiplier:1e3},{key:"minute",multiplier:60},{key:"hour",multiplier:60},{key:"day",multiplier:24},{key:"week",multiplier:7},{key:"month",multiplier:13/3},{key:"year",multiplier:12}],l=t=>n.reduce((([t,e],{key:s,multiplier:i},a)=>{if(!e||a===(t=>n.indexOf(n.find((e=>e.key===t))))(e)+1){const a=Math.round(t/i);if(!e||Math.abs(a)>=1)return[a,s]}return[t,e]}),[t,""]);var c=Object.freeze({__proto__:null,eq:a,ne:(t,s=[],i="")=>e(s.find((({key:e})=>`${e}`.toLowerCase()!==`${t}`.toLowerCase()))).value||i,lt:o,gt:r,lte:(t,e=[],s="")=>a(t,e,o(t,e,s)),gte:(t,e=[],s="")=>a(t,e,r(t,e,s)),number:(t,e=[],i="",a="")=>a&&new Intl.NumberFormat(a,{maximumFractionDigits:s(e,"decimals",s(e,"maxDecimals","2")),minimumFractionDigits:s(e,"minDecimals")}).format(+t||+i),date:(t,e=[],i="",a="")=>a&&new Intl.DateTimeFormat(a,{dateStyle:s(e,"dateStyle","medium"),timeStyle:s(e,"timeStyle","short")}).format(+t||+i),ago:(t,e=[],i="",a="")=>{if(!a)return"";const o=s(e,"format","auto"),r=(+t||+i)-Date.now(),n="auto"===o?l(r):[r,o];return new Intl.RelativeTimeFormat(a,{numeric:s(e,"numeric","auto"),style:s(e,"style","long")}).format(...n)}});const u=t=>t.replace(/\\(?=:|;|{|})/g,""),h=(t,s={},i,a)=>{if(((t="")=>/{{(?:(?!{{|}}).)+}}/.test(`${t}`))(t)){const o=((t,s={},i={},a)=>t.replace(/{{\s*(?:(?!{{|}}).)+\s*}}/g,(t=>{const o=u(`${t.match(/(?!{|\s).+?(?!\\[:;]).(?=\s*(?:[:;]|}}$))/)}`),r=null==s?void 0:s[o],[,n=""]=e(t.match(/.+?(?!\\;).;\s*default\s*:\s*([^\s:;].+?(?:\\[:;]|[^;\s}])*)(?=\s*(?:;|}}$))/i),[]);let[,l=""]=e(t.match(/{{\s*(?:[^;]|(?:\\;))+\s*(?:(?!\\:).[:])\s*(?!\s)((?:\\;|[^;])+?)(?=\s*(?:[;]|}}$))/i),[]);if(void 0===r&&"ne"!==l)return n;const h=!!l,d=Object.assign(Object.assign({},c),e(i));l=Object.keys(d).includes(l)?l:"eq";const g=d[l],y=e(t.match(/[^\s:;{](?:[^;]|\\[;])+[^\s:;}]/gi),[]).reduce(((t,e,s)=>{if(s>0){const s=u(`${e.match(/(?:(?:\\:)|[^:])+/)}`.trim()),i=`${e.match(/(?:(?:\\:)|[^:])+$/)}`.trim();if(s&&"default"!==s&&i)return[...t,{key:s,value:i}]}return t}),[]);return h||y.length?g(r,y,n,a):`${r}`})))(t,s,i,a);return h(o,s,i,a)}return u(`${t}`)},d=({customModifiers:t={}})=>({parse:({translations:s={},key:i,payload:a,locale:o,fallbackLocale:r})=>{if(!i)throw new Error("No key provided to $t()");if(!o)throw new Error("No locale set!");let n=e(s[o])[i];return r&&void 0===n&&(n=e(s[r])[i]),(null==a?void 0:a.default)&&void 0===n&&(n=`${a.default}`),void 0===n&&(n=`${i}`),h(n,a,t,o)}});class g{constructor(s){this.loadedKeys={},this.currentRoute=writable(),this.config=writable({}),this.isLoading=writable(!1),this.promises=[],this.loading={subscribe:this.isLoading.subscribe,toPromise:()=>Promise.all(this.promises),get:()=>get(this.isLoading)},this.privateTranslations=writable({}),this.translations={subscribe:this.privateTranslations.subscribe,get:()=>get(this.translations)},this.locales=Object.assign(Object.assign({},derived([this.config,this.privateTranslations],(([t,e])=>{const{loaders:s=[]}=t,i=s.map((({locale:t})=>`${t}`.toLowerCase())),a=Object.keys(e).map((t=>`${t}`.toLowerCase()));return[...new Set([...i,...a])]}),[])),{get:()=>get(this.locales)}),this.internalLocale=writable(),this.locale=Object.assign(Object.assign({set:this.internalLocale.set,update:this.internalLocale.update},derived(this.internalLocale,((t,e)=>{const s=t&&`${t}`.toLowerCase(),i=this.getLocale(s);i&&i!==this.locale.get()&&e(i)}))),{get:()=>get(this.locale)}),this.loaderTrigger=derived([this.internalLocale,this.currentRoute],(([t,e],s)=>{void 0!==t&&void 0!==e&&s([t,e])}),[]),this.initialized=derived([this.locale,this.currentRoute,this.privateTranslations],(([t,e,s],i)=>{get(this.initialized)||i(void 0!==t&&void 0!==e&&!!Object.keys(s).length)})),this.translation=derived([this.privateTranslations,this.locale,this.isLoading],(([t,e,s],i)=>{const a=t[e];a&&Object.keys(a).length&&!s&&i(a)}),{}),this.t=Object.assign(Object.assign({},derived([this.config,this.translation],(([{customModifiers:t,fallbackLocale:s}])=>(i,a)=>d({customModifiers:e(t)}).parse({key:i,payload:a,translations:this.translations.get(),locale:this.locale.get(),fallbackLocale:s})))),{get:(...t)=>get(this.t)(...t)}),this.l=Object.assign(Object.assign({},derived([this.config,this.translations],(([{customModifiers:t,fallbackLocale:s},i])=>(a,o,r)=>d({customModifiers:e(t)}).parse({key:o,payload:r,translations:i,locale:a,fallbackLocale:s})))),{get:(...t)=>get(this.l)(...t)}),this.getLocale=t=>{if(!t)return"";return`${this.locales.get().find((e=>`${e}`.toLowerCase()===`${t}`.toLowerCase()))||""}`.toLowerCase()},this.setLocale=async t=>{t&&(this.internalLocale.set(t),await this.loading.toPromise())},this.setRoute=async t=>{t!==get(this.currentRoute)&&this.currentRoute.set(t),await this.loading.toPromise()},this.loadConfig=async t=>{if(!t)throw new Error("No config!");this.config.set(t);const{initLocale:e="",translations:s}=t;s&&this.addTranslations(s),await this.loadTranslations(e)},this.addTranslations=(t,s)=>{if(!t)return;const a=Object.keys(e(t));this.privateTranslations.update((s=>a.reduce(((s,a)=>Object.assign(Object.assign({},s),{[a]:Object.assign(Object.assign({},e(s[a])),i(t[a]))})),s))),a.forEach((i=>{let a=Object.keys(t[i]).map((t=>`${t}`.split(".")[0]));s&&(a=s[i]),this.loadedKeys[i]=Array.from(new Set([...e(this.loadedKeys[i],[]),...e(a,[])]))}))},this.getTranslationProps=async(s=this.locale.get(),a=get(this.currentRoute))=>{const o=get(this.config);if(!o||!s)return[];const r=this.translations.get(),{loaders:n,fallbackLocale:l=""}=e(o),c=`${s}`.toLowerCase(),u=l&&`${l}`.toLowerCase(),h=r[c],d=r[u],g=e(n,[]).map((e=>{var{locale:s}=e,i=t(e,["locale"]);return Object.assign(Object.assign({},i),{locale:`${s}`.toLowerCase()})})).filter((({routes:t})=>{return!t||e(t,[]).some((s=a,t=>{try{if("string"==typeof t)return t===s;if("object"==typeof t)return t.test(s)}catch(t){throw new Error("Invalid route config!")}return!1}));var s})).filter((({key:t,locale:s})=>s===c&&(!h||!e(this.loadedKeys[c],[]).includes(t))||l&&s===u&&(!d||!e(this.loadedKeys[u],[]).includes(t))));if(g.length){this.isLoading.set(!0);const s=await(async s=>{try{return(await Promise.all(s.map((e=>{var{loader:s}=e,i=t(e,["loader"]);return new Promise((async t=>{let e;try{e=await s()}catch(t){console.error(`Failed to load translation. Verify your '${i.locale}' > '${i.key}' loader.`),console.error(t)}t(Object.assign(Object.assign({loader:s},i),{data:e}))}))})))).reduce(((t,{key:s,data:a,locale:o})=>a?Object.assign(Object.assign({},t),{[o]:i(Object.assign(Object.assign({},e(t[o])),{[s]:a}))}):t),{})}catch(t){console.error(t)}return{}})(g);this.isLoading.set(!1);const a=Object.keys(s).reduce(((t,e)=>Object.assign(Object.assign({},t),{[e]:Object.keys(s[e])})),{}),o=g.filter((({key:t,locale:s})=>e(a[s],[]).some((e=>`${e}`.startsWith(t))))).reduce(((t,{key:e,locale:s})=>Object.assign(Object.assign({},t),{[s]:[...t[s]||[],e]})),{});return[s,o]}return[]},this.translationLoader=async t=>{this.promises.push(new Promise((async e=>{const s=await this.getTranslationProps(t);s.length&&this.addTranslations(...s),e()}))),await this.loading.toPromise()},this.loadTranslations=async(t,e=get(this.currentRoute)||"")=>{t&&(this.setRoute(e),this.setLocale(t),await this.loading.toPromise())},s&&this.loadConfig(s),this.loaderTrigger.subscribe((()=>this.translationLoader()))}}export{g as default}; |
{ | ||
"name": "sveltekit-i18n", | ||
"version": "1.5.3", | ||
"version": "1.5.4", | ||
"description": "Internationalization library for SvelteKit", | ||
@@ -5,0 +5,0 @@ "type": "module", |
import { Readable, Writable } from 'svelte/store'; | ||
type ModifierOption = Record<'key' | 'value', string>; | ||
type Modifier = (value: any, options:ModifierOption[], defaultValue?: string, locale?: string) => string; | ||
type CustomModifiers = Record<string, Modifier>; | ||
type LoadingStore = Readable<boolean> & { toPromise: () => Promise<void[]>, get: () => boolean }; | ||
@@ -34,8 +40,2 @@ | ||
type ModifierOption = Record<'key' | 'value', string>; | ||
type Modifier = (value: any, options:ModifierOption[], defaultValue?: string, locale?: string) => string; | ||
type CustomModifiers = Record<string, Modifier>; | ||
declare class export_default{ | ||
@@ -42,0 +42,0 @@ constructor(config?: Config); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16318