sveltekit-i18n
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -1,1 +0,1 @@ | ||
import{writable,derived,get}from"svelte/store";const t=(t,e=[],s="")=>r(e.find((({key:e})=>`${e}`.toLowerCase()===`${t}`.toLowerCase()))).value||s,e=(t,e=[],s="")=>{const i=e.sort(((t,e)=>+t.key-+e.key));return r(i.find((({key:e})=>+t<+e))).value||s},s=(t,e=[],s="")=>{const i=e.sort(((t,e)=>+e.key-+t.key));return r(i.find((({key:e})=>+t>+e))).value||s},i=[{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}],a=t=>i.reduce((([t,e],{key:s,multiplier:a},o)=>{if(!e||o===(t=>i.indexOf(i.find((e=>e.key===t))))(e)+1){const i=t/a;if(!e||Math.abs(i)>=1)return[i,s]}return[t,e]}),[t,""]);var o=Object.freeze({__proto__:null,eq:t,lt:e,gt:s,lte:(s,i=[],a="")=>t(s,i)||e(s,i,a),gte:(e,i=[],a="")=>t(e,i)||s(e,i,a),date:(t,e=[],s="",i="")=>i&&new Intl.DateTimeFormat(i,{dateStyle:n(e,"dateStyle","medium"),timeStyle:n(e,"timeStyle","short")}).format(+t||+s),ago:(t,e=[],s="",i="")=>{if(!i)return"";const o=n(e,"format","auto"),r=(+t||+s)-Date.now(),l="auto"===o?a(r):[r,o];return new Intl.RelativeTimeFormat(i,{numeric:n(e,"numeric","auto"),style:n(e,"style","long")}).format(...l)}});const r=(t,e={})=>t||e,n=(t,e,s)=>{var i;return(null===(i=t.find((t=>t.key===e)))||void 0===i?void 0:i.value)||s},l=(t,e)=>Object.keys(r(t)).reduce(((s,i)=>{const a=t[i],o=e?`${e}.${i}`:`${i}`;return a&&"object"==typeof a?Object.assign(Object.assign({},s),l(a,o)):Object.assign(Object.assign({},s),{[o]:a})}),{}),c=t=>t.replace(/\\(?=:|;|{|})/g,""),h=(t,e={},s,i)=>{if(((t="")=>/{{(?:.(?<!{{|}}))+}}/.test(`${t}`))(t)){const a=((t,e={},s={},i)=>t.replace(/{{\s*(?:.(?<!{{|}}))+\s*}}/g,(t=>{const a=c(`${t.match(/(?<={{\s*)(?!\s|;)(?:.(?<!{{|}}))+?(?=\s*(?:[;:](?<!\\[;:])|}}$))/)}`),n=e[a],l=`${t.match(/(?<={{.*;(?<!\\;)\s*default\s*:\s*)(?!\s|;).+?(?=\s*(?:;(?<!\\;)|}}$))/i)||""}`;if(void 0===n)return l;let h=`${t.match(/(?<={{\s*(?:.(?<!;(?<!\\;)))+\s*:(?<!\\:)\s*)(?!\s|;).+?(?=\s*;(?<!\\;))/)||""}`;const u=!!h,d=Object.assign(Object.assign({},o),r(s));h=Object.keys(d).includes(h)?h:"eq";const y=d[h],f=r(t.match(/(?<={{.+?;(?<!\\;)\s*)(?!\s)(?:.(?<!\s*default\s*:\s*))+?(?=\s*(?:;(?<!\\;)|}}$))/gi),[]).reduce(((t,e)=>{const s=c(`${e.match(/.+?(?=\s*:(?<!\\:)\s*)/)}`),i=`${e.match(/(?<=.+\s*:(?<!\\:)\s*)(?!\s).+/)}`;return s&&i?[...t,{key:s,value:i}]:t}),[]);return u||f.length?y(n,f,l,i):`${n}`})))(t,e,s,i);return h(a,e,s,i)}return c(`${t}`)},u=(t,e,s={},i={},a="")=>{if(!e)throw new Error("no key provided to $t()");const o=`${r(r(t)[e],e)}`;return h(o,s,i,a)};class d{constructor(t){this.loadedKeys={},this.currentRoute=writable(""),this.config=writable({}),this.isLoading=writable(!1),this.loading={subscribe:this.isLoading.subscribe},this.privateTranslations=writable({}),this.translations={subscribe:this.privateTranslations.subscribe},this.locales=derived([this.config,this.privateTranslations],(([t,e])=>{const{loaders:s=[]}=t,i=s.map((({locale:t})=>t)),a=Object.keys(e).map((t=>`${t}`.toLowerCase()));return[...new Set([...i,...a])]}),[]),this.locale=writable(""),this.initialized=derived(this.privateTranslations,(t=>!!get(this.initialized)||!!Object.keys(t).length),!1),this.translation=derived([this.privateTranslations,this.locale,this.isLoading],(([t,e,s])=>{const i=t[e];return i&&Object.keys(i).length&&!s?i:get(this.translation)}),{}),this.t=derived([this.translation,this.config],(([t,{customModifiers:e}])=>(s,i)=>u(t,s,i,r(e),get(this.locale)))),this.l=derived([this.translations,this.config],(([t,{customModifiers:e}])=>(s,i,a)=>u(t[s],i,a,r(e),get(this.locale)))),this.getLocale=t=>`${get(this.locales).find((e=>`${e}`.toLowerCase()===`${t}`.toLowerCase()))||""}`.toLowerCase(),this.loadConfig=async t=>{if(!t)throw new Error("No config!");this.config.set(t);const{initLocale:e=""}=t;await this.loadTranslations(e)},this.addTranslations=(t,e)=>{const s=Object.keys(r(t));this.privateTranslations.update((e=>s.reduce(((e,s)=>Object.assign(Object.assign({},e),{[s]:Object.assign(Object.assign({},r(e[s])),l(t[s]))})),e))),s.forEach((s=>{let i=Object.keys(t[s]).map((t=>`${t}`.split(".")[0]));e&&(i=e[s]),this.loadedKeys[s]=[...r(this.loadedKeys[s],[]),...r(i,[])]})),this.locale.update((t=>{if(!t){const{initLocale:t}=get(this.config);return this.getLocale(t)}return t}))},this.loadTranslations=async(t,e=get(this.currentRoute))=>{const s=get(this.config),i=this.getLocale(t);if(!s||!i)return;let a=get(this.locale);a||(this.locale.set(i),a=i),e&&this.currentRoute.set(e);const o=get(this.privateTranslations)[a],{loaders:n}=r(s),c=r(n,[]).filter((({locale:t})=>`${t}`.toLowerCase()===`${a}`.toLowerCase())).filter((({key:t})=>!o||!r(this.loadedKeys[a],[]).includes(t))).filter((({routes:t})=>!t||r(t,[]).some((t=>e=>{try{if("string"==typeof e)return e===t;if("object"==typeof e)return e.test(t)}catch(t){throw new Error("Invalid route config!")}return!1})(e))));if(c.length){this.isLoading.set(!0);const t=await(async t=>{try{const e=(await Promise.all(t.map((async({key:t,loader:e})=>({[t]:await e()}))))).reduce(((t,e)=>Object.assign(Object.assign({},t),e)),{});return l(e)}catch(t){throw console.error(t),new Error("Failed to load translation. Verify the loader function.")}})(c);this.addTranslations({[a]:t},{[a]:c.map((({key:t})=>t))}),this.isLoading.set(!1)}},t&&this.loadConfig(t),this.locale.subscribe(this.loadTranslations)}}export{d as default}; | ||
import{writable,derived,get}from"svelte/store";const t=(t,e={})=>t||e,e=(t,e,s)=>{var i;return(null===(i=t.find((t=>t.key===e)))||void 0===i?void 0:i.value)||s},s=(e,s=[],i="")=>t(s.find((({key:t})=>`${t}`.toLowerCase()===`${e}`.toLowerCase()))).value||i,i=(e,s=[],i="")=>{const a=s.sort(((t,e)=>+t.key-+e.key));return t(a.find((({key:t})=>+e<+t))).value||i},a=(e,s=[],i="")=>{const a=s.sort(((t,e)=>+e.key-+t.key));return t(a.find((({key:t})=>+e>+t))).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}],o=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 n=Object.freeze({__proto__:null,eq:s,ne:(e,s=[],i="")=>t(s.find((({key:t})=>`${t}`.toLowerCase()!==`${e}`.toLowerCase()))).value||i,lt:i,gt:a,lte:(t,e=[],a="")=>s(t,e,i(t,e,a)),gte:(t,e=[],i="")=>s(t,e,a(t,e,i)),number:(t,s=[],i="",a="")=>a&&new Intl.NumberFormat(a,{maximumFractionDigits:e(s,"decimals",e(s,"maxDecimals","2")),minimumFractionDigits:e(s,"minDecimals")}).format(+t||+i),date:(t,s=[],i="",a="")=>a&&new Intl.DateTimeFormat(a,{dateStyle:e(s,"dateStyle","medium"),timeStyle:e(s,"timeStyle","short")}).format(+t||+i),ago:(t,s=[],i="",a="")=>{if(!a)return"";const r=e(s,"format","auto"),n=(+t||+i)-Date.now(),l="auto"===r?o(n):[n,r];return new Intl.RelativeTimeFormat(a,{numeric:e(s,"numeric","auto"),style:e(s,"style","long")}).format(...l)}});const l=(e,s)=>Object.keys(t(e)).reduce(((t,i)=>{const a=e[i],r=s?`${s}.${i}`:`${i}`;return a&&"object"==typeof a?Object.assign(Object.assign({},t),l(a,r)):Object.assign(Object.assign({},t),{[r]:a})}),{}),c=t=>t.replace(/\\(?=:|;|{|})/g,""),u=(e,s={},i,a)=>{if(((t="")=>/{{(?:(?!{{|}}).)+}}/.test(`${t}`))(e)){const r=((e,s={},i={},a)=>e.replace(/{{\s*(?:(?!{{|}}).)+\s*}}/g,(e=>{const r=c(`${e.match(/(?!{|\s).+?(?!\\[:;]).(?=\s*(?:[:;]|}}$))/)}`),o=s[r],[,l=""]=t(e.match(/.+?(?!\\;).;\s*default\s*:\s*([^\s:;].+?(?:\\[:;]|[^;\s}])*)(?=\s*(?:;|}}$))/i),[]);let[,u=""]=t(e.match(/{{\s*(?:[^;]|(?:\\;))+\s*(?:(?!\\:).[:])\s*(?!\s)((?:\\;|[^;])+?)(?=\s*(?:[;]|}}$))/i),[]);if(void 0===o&&"ne"!==u)return l;const h=!!u,d=Object.assign(Object.assign({},n),t(i));u=Object.keys(d).includes(u)?u:"eq";const m=d[u],f=t(e.match(/[^\s:;{](?:[^;]|\\[;])+[^\s:;}]/gi),[]).reduce(((t,e,s)=>{if(s>0){const s=c(`${e.match(/(?:(?:\\:)|[^:])+/)}`.trim()),i=`${e.match(/(?:(?:\\:)|[^:])+$/)}`.trim();if(s&&"default"!==s&&i)return[...t,{key:s,value:i}]}return t}),[]);return h||f.length?m(o,f,l,a):`${o}`})))(e,s,i,a);return u(r,s,i,a)}return c(`${e}`)},h=(e,s,i={},a={},r="")=>{if(!s)throw new Error("no key provided to $t()");const o=`${t(t(e)[s],s)}`;return u(o,i,a,r)};class d{constructor(e){this.loadedKeys={},this.currentRoute=writable(""),this.config=writable({}),this.isLoading=writable(!1),this.loading={subscribe:this.isLoading.subscribe},this.privateTranslations=writable({}),this.translations={subscribe:this.privateTranslations.subscribe},this.locales=derived([this.config,this.privateTranslations],(([t,e])=>{const{loaders:s=[]}=t,i=s.map((({locale:t})=>t)),a=Object.keys(e).map((t=>`${t}`.toLowerCase()));return[...new Set([...i,...a])]}),[]),this.locale=writable(""),this.initialized=derived(this.privateTranslations,(t=>!!get(this.initialized)||!!Object.keys(t).length),!1),this.translation=derived([this.privateTranslations,this.locale,this.isLoading],(([t,e,s])=>{const i=t[e];return i&&Object.keys(i).length&&!s?i:get(this.translation)}),{}),this.t=derived([this.translation,this.config],(([e,{customModifiers:s}])=>(i,a)=>h(e,i,a,t(s),get(this.locale)))),this.l=derived([this.translations,this.config],(([e,{customModifiers:s}])=>(i,a,r)=>h(e[i],a,r,t(s),get(this.locale)))),this.getLocale=t=>`${get(this.locales).find((e=>`${e}`.toLowerCase()===`${t}`.toLowerCase()))||""}`.toLowerCase(),this.loadConfig=async t=>{if(!t)throw new Error("No config!");this.config.set(t);const{initLocale:e=""}=t;await this.loadTranslations(e)},this.addTranslations=(e,s)=>{const i=Object.keys(t(e));this.privateTranslations.update((s=>i.reduce(((s,i)=>Object.assign(Object.assign({},s),{[i]:Object.assign(Object.assign({},t(s[i])),l(e[i]))})),s))),i.forEach((i=>{let a=Object.keys(e[i]).map((t=>`${t}`.split(".")[0]));s&&(a=s[i]),this.loadedKeys[i]=[...t(this.loadedKeys[i],[]),...t(a,[])]})),this.locale.update((t=>{if(!t){const{initLocale:t}=get(this.config);return this.getLocale(t)}return t}))},this.loadTranslations=async(e,s=get(this.currentRoute))=>{const i=get(this.config),a=this.getLocale(e);if(!i||!a)return;let r=get(this.locale);r||(this.locale.set(a),r=a),s&&this.currentRoute.set(s);const o=get(this.privateTranslations)[r],{loaders:n}=t(i),c=t(n,[]).filter((({locale:t})=>`${t}`.toLowerCase()===`${r}`.toLowerCase())).filter((({key:e})=>!o||!t(this.loadedKeys[r],[]).includes(e))).filter((({routes:e})=>!e||t(e,[]).some((t=>e=>{try{if("string"==typeof e)return e===t;if("object"==typeof e)return e.test(t)}catch(t){throw new Error("Invalid route config!")}return!1})(s))));if(c.length){this.isLoading.set(!0);const t=await(async t=>{try{const e=(await Promise.all(t.map((async({key:t,loader:e})=>({[t]:await e()}))))).reduce(((t,e)=>Object.assign(Object.assign({},t),e)),{});return l(e)}catch(t){throw console.error(t),new Error("Failed to load translation. Verify the loader function.")}})(c);this.addTranslations({[r]:t},{[r]:c.map((({key:t})=>t))}),this.isLoading.set(!1)}},e&&this.loadConfig(e),this.locale.subscribe(this.loadTranslations)}}export{d as default}; |
{ | ||
"name": "sveltekit-i18n", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Internationalization library for SvelteKit", | ||
@@ -13,7 +13,6 @@ "type": "module", | ||
"dev": "rollup -c -w", | ||
"test": "npx jest", | ||
"test": "jest", | ||
"build:lib": "rm -rf lib types && rollup -c", | ||
"build:precompile": "rm -rf precompiled types && tsc", | ||
"build": "npm run build:precompile && npm run build:lib && rm -rf precompiled", | ||
"version:patch": "npm version patch", | ||
"prepublishOnly": "npm run build", | ||
@@ -20,0 +19,0 @@ "lint": "eslint --fix --ext .ts,.js --ignore-path .gitignore ." |
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
11857
62