sveltekit-i18n
Advanced tools
Comparing version 1.0.0-rc.2 to 1.0.0
@@ -1,1 +0,1 @@ | ||
import{writable,derived,get}from"svelte/store";const t=(t,s={})=>t||s,s=(e,i)=>Object.keys(t(e)).reduce(((t,a)=>{const o=e[a],n=i?`${i}.${a}`:`${a}`;return o&&"object"==typeof o?Object.assign(Object.assign({},t),s(o,n)):Object.assign(Object.assign({},t),{[n]:o})}),{}),e=(s,e=[],i="")=>t(e.find((({key:t})=>`${t}`.toLowerCase()===`${s}`.toLowerCase()))).value||i,i=(s,e=[],i="")=>{const a=e.sort(((t,s)=>+t.key-+s.key));return t(a.find((({key:t})=>+s<+t))).value||i},a=(s,e=[],i="")=>{const a=e.sort(((t,s)=>+s.key-+t.key));return t(a.find((({key:t})=>+s>+t))).value||i},o={lt:i,lte:(t,s=[],a="")=>e(t,s)||i(t,s,a),eq:e,gte:(t,s=[],i="")=>e(t,s)||a(t,s,i),gt:a},n=t=>t.replace(/\\(?=;|{|})/g,""),r=(s,e={},i,a)=>{if(((t="")=>/{{(?:.(?<!{{|}}))+}}/.test(`${t}`))(s)){const c=((s,e={},i={},a)=>s.replace(/{{\s*(?:.(?<!{{|}}))+\s*}}/g,(s=>{const r=n(`${s.match(/(?<={{\s*)(?!\s|;)(?:.(?<!{{|}}))+?(?=\s*(?:[;:](?<!\\[;:])|}}$))/)}`),c=e[r],l=`${s.match(/(?<={{.*;(?<!\\;)\s*default\s*:\s*)(?!\s|;).+?(?=\s*(?:;(?<!\\;)|}}$))/i)||""}`;if(void 0===c)return l;let h=`${s.match(/(?<={{\s*(?:.(?<!;(?<!\\;)))+\s*:(?<!\\:)\s*)(?!\s|;).+?(?=\s*;(?<!\\;))/)||""}`;const d=!!h,u=Object.assign(Object.assign({},o),t(i));h=Object.keys(u).includes(h)?h:"eq";const g=u[h],f=t(s.match(/(?<={{.+?;(?<!\\;)\s*)(?!\s)(?:.(?<!\s*default\s*:\s*))+?(?=\s*(?:;(?<!\\;)|}}$))/gi),[]).reduce(((t,s)=>{const e=n(`${s.match(/.+?(?=\s*:(?<!\\:)\s*)/)}`),i=`${s.match(/(?<=.+\s*:(?<!\\:)\s*)(?!\s).+/)}`;return e&&i?[...t,{key:e,value:i}]:t}),[]);return d||f.length?g(c,f,l,a):`${c}`})))(s,e,i,a);return r(c,e,i,a)}return n(`${s}`)},c=(s,e,i={},a={},o="")=>{if(!e)throw new Error("no key provided to $t()");const n=`${t(t(s)[e],e)}`;return r(n,i,a,o)};class l{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,s])=>{const{loaders:e=[]}=t,i=e.map((({locale:t})=>t)),a=Object.keys(s).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,s,e])=>{const i=t[s];return i&&Object.keys(i).length&&!e?i:get(this.translation)}),{}),this.t=derived([this.translation,this.config],(([s,{customModifiers:e}])=>(i,a)=>c(s,i,a,t(e),get(this.locale)))),this.l=derived([this.translations,this.config],(([s,{customModifiers:e}])=>(i,a,o)=>c(s[i],a,o,t(e),get(this.locale)))),this.getLocale=t=>`${get(this.locales).find((s=>`${s}`.toLowerCase()===`${t}`.toLowerCase()))||""}`.toLowerCase(),this.loadConfig=async t=>{if(!t)throw new Error("No config!");this.config.set(t);const{initLocale:s=""}=t;await this.loadTranslations(s)},this.addTranslations=(e,i)=>{const a=Object.keys(t(e));this.privateTranslations.update((i=>a.reduce(((i,a)=>Object.assign(Object.assign({},i),{[a]:Object.assign(Object.assign({},t(i[a])),s(e[a]))})),i))),a.forEach((s=>{let a=Object.keys(e[s]).map((t=>`${t}`.split(".")[0]));i&&(a=i[s]),this.loadedKeys[s]=[...t(this.loadedKeys[s],[]),...t(a,[])]})),this.locale.update((t=>{if(!t){const{initLocale:t}=get(this.config);return this.getLocale(t)}return t}))},this.loadTranslations=async(e,i=get(this.currentRoute))=>{const a=get(this.config),o=this.getLocale(e);if(!a||!o)return;let n=get(this.locale);n||(this.locale.set(o),n=o),i&&this.currentRoute.set(i);const r=get(this.privateTranslations)[n],{loaders:c}=t(a),l=t(c,[]).filter((({locale:t})=>`${t}`.toLowerCase()===`${n}`.toLowerCase())).filter((({key:s})=>!r||!t(this.loadedKeys[n],[]).includes(s))).filter((({routes:s})=>!s||t(s,[]).some((t=>s=>{try{if("string"==typeof s)return s===t;if("object"==typeof s)return s.test(t)}catch(t){throw new Error("Invalid route config!")}return!1})(i))));if(l.length){this.isLoading.set(!0);const t=await(async t=>{try{const e=(await Promise.all(t.map((async({key:t,loader:s})=>({[t]:await s()}))))).reduce(((t,s)=>Object.assign(Object.assign({},t),s)),{});return s(e)}catch(t){throw console.error(t),new Error("Failed to load translation. Verify the loader function.")}})(l);this.addTranslations({[n]:t},{[n]:l.map((({key:t})=>t))}),this.isLoading.set(!1)}},e&&this.loadConfig(e),this.locale.subscribe(this.loadTranslations)}}export{l as default}; | ||
import{writable,derived,get}from"svelte/store";const t=(t,s={})=>t||s,s=(e,i)=>Object.keys(t(e)).reduce(((t,a)=>{const o=e[a],n=i?`${i}.${a}`:`${a}`;return o&&"object"==typeof o?Object.assign(Object.assign({},t),s(o,n)):Object.assign(Object.assign({},t),{[n]:o})}),{}),e=(s,e=[],i="")=>t(e.find((({key:t})=>`${t}`.toLowerCase()===`${s}`.toLowerCase()))).value||i,i=(s,e=[],i="")=>{const a=e.sort(((t,s)=>+t.key-+s.key));return t(a.find((({key:t})=>+s<+t))).value||i},a=(s,e=[],i="")=>{const a=e.sort(((t,s)=>+s.key-+t.key));return t(a.find((({key:t})=>+s>+t))).value||i},o={lt:i,lte:(t,s=[],a="")=>e(t,s)||i(t,s,a),eq:e,gte:(t,s=[],i="")=>e(t,s)||a(t,s,i),gt:a},n=t=>t.replace(/\\(?=:|;|{|})/g,""),r=(s,e={},i,a)=>{if(((t="")=>/{{(?:.(?<!{{|}}))+}}/.test(`${t}`))(s)){const c=((s,e={},i={},a)=>s.replace(/{{\s*(?:.(?<!{{|}}))+\s*}}/g,(s=>{const r=n(`${s.match(/(?<={{\s*)(?!\s|;)(?:.(?<!{{|}}))+?(?=\s*(?:[;:](?<!\\[;:])|}}$))/)}`),c=e[r],l=`${s.match(/(?<={{.*;(?<!\\;)\s*default\s*:\s*)(?!\s|;).+?(?=\s*(?:;(?<!\\;)|}}$))/i)||""}`;if(void 0===c)return l;let h=`${s.match(/(?<={{\s*(?:.(?<!;(?<!\\;)))+\s*:(?<!\\:)\s*)(?!\s|;).+?(?=\s*;(?<!\\;))/)||""}`;const d=!!h,u=Object.assign(Object.assign({},o),t(i));h=Object.keys(u).includes(h)?h:"eq";const g=u[h],f=t(s.match(/(?<={{.+?;(?<!\\;)\s*)(?!\s)(?:.(?<!\s*default\s*:\s*))+?(?=\s*(?:;(?<!\\;)|}}$))/gi),[]).reduce(((t,s)=>{const e=n(`${s.match(/.+?(?=\s*:(?<!\\:)\s*)/)}`),i=`${s.match(/(?<=.+\s*:(?<!\\:)\s*)(?!\s).+/)}`;return e&&i?[...t,{key:e,value:i}]:t}),[]);return d||f.length?g(c,f,l,a):`${c}`})))(s,e,i,a);return r(c,e,i,a)}return n(`${s}`)},c=(s,e,i={},a={},o="")=>{if(!e)throw new Error("no key provided to $t()");const n=`${t(t(s)[e],e)}`;return r(n,i,a,o)};class l{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,s])=>{const{loaders:e=[]}=t,i=e.map((({locale:t})=>t)),a=Object.keys(s).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,s,e])=>{const i=t[s];return i&&Object.keys(i).length&&!e?i:get(this.translation)}),{}),this.t=derived([this.translation,this.config],(([s,{customModifiers:e}])=>(i,a)=>c(s,i,a,t(e),get(this.locale)))),this.l=derived([this.translations,this.config],(([s,{customModifiers:e}])=>(i,a,o)=>c(s[i],a,o,t(e),get(this.locale)))),this.getLocale=t=>`${get(this.locales).find((s=>`${s}`.toLowerCase()===`${t}`.toLowerCase()))||""}`.toLowerCase(),this.loadConfig=async t=>{if(!t)throw new Error("No config!");this.config.set(t);const{initLocale:s=""}=t;await this.loadTranslations(s)},this.addTranslations=(e,i)=>{const a=Object.keys(t(e));this.privateTranslations.update((i=>a.reduce(((i,a)=>Object.assign(Object.assign({},i),{[a]:Object.assign(Object.assign({},t(i[a])),s(e[a]))})),i))),a.forEach((s=>{let a=Object.keys(e[s]).map((t=>`${t}`.split(".")[0]));i&&(a=i[s]),this.loadedKeys[s]=[...t(this.loadedKeys[s],[]),...t(a,[])]})),this.locale.update((t=>{if(!t){const{initLocale:t}=get(this.config);return this.getLocale(t)}return t}))},this.loadTranslations=async(e,i=get(this.currentRoute))=>{const a=get(this.config),o=this.getLocale(e);if(!a||!o)return;let n=get(this.locale);n||(this.locale.set(o),n=o),i&&this.currentRoute.set(i);const r=get(this.privateTranslations)[n],{loaders:c}=t(a),l=t(c,[]).filter((({locale:t})=>`${t}`.toLowerCase()===`${n}`.toLowerCase())).filter((({key:s})=>!r||!t(this.loadedKeys[n],[]).includes(s))).filter((({routes:s})=>!s||t(s,[]).some((t=>s=>{try{if("string"==typeof s)return s===t;if("object"==typeof s)return s.test(t)}catch(t){throw new Error("Invalid route config!")}return!1})(i))));if(l.length){this.isLoading.set(!0);const t=await(async t=>{try{const e=(await Promise.all(t.map((async({key:t,loader:s})=>({[t]:await s()}))))).reduce(((t,s)=>Object.assign(Object.assign({},t),s)),{});return s(e)}catch(t){throw console.error(t),new Error("Failed to load translation. Verify the loader function.")}})(l);this.addTranslations({[n]:t},{[n]:l.map((({key:t})=>t))}),this.isLoading.set(!1)}},e&&this.loadConfig(e),this.locale.subscribe(this.loadTranslations)}}export{l as default}; |
{ | ||
"name": "sveltekit-i18n", | ||
"version": "1.0.0-rc.2", | ||
"version": "1.0.0", | ||
"description": "Internationalization library for SvelteKit", | ||
@@ -13,8 +13,7 @@ "type": "module", | ||
"dev": "rollup -c -w", | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"test": "npx 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:rc": "npm version prerelease --preid rc", | ||
"prepublishOnly": "npm run version:rc && npm run build", | ||
"version:patch": "npm version patch", | ||
"lint": "eslint --fix --ext .ts,.js --ignore-path .gitignore ." | ||
@@ -50,2 +49,3 @@ }, | ||
"@rollup/plugin-typescript": "^8.3.0", | ||
"@types/jest": "^27.0.3", | ||
"@typescript-eslint/eslint-plugin": "^5.7.0", | ||
@@ -56,2 +56,3 @@ "@typescript-eslint/parser": "^5.7.0", | ||
"eslint-plugin-import": "^2.25.3", | ||
"jest": "^27.4.5", | ||
"rollup": "^2.61.1", | ||
@@ -62,4 +63,5 @@ "rollup-plugin-auto-external": "^2.0.0", | ||
"svelte": "^3.44.3", | ||
"ts-jest": "^27.1.2", | ||
"typescript": "^4.5.4" | ||
} | ||
} | ||
} |
@@ -6,4 +6,2 @@ [![npm version](https://badge.fury.io/js/sveltekit-i18n.svg)](https://badge.fury.io/js/sveltekit-i18n) ![](https://github.com/jarda-svoboda/sveltekit-i18n/workflows/Tests/badge.svg) | ||
___NOTE: This project is currently in RC! 1.0.0 will be released as soon as tests are completed...___ | ||
## Key features | ||
@@ -19,5 +17,2 @@ | ||
## TODO | ||
- tests | ||
## Usage | ||
@@ -24,0 +19,0 @@ |
@@ -19,3 +19,3 @@ import { Readable, Writable } from 'svelte/store'; | ||
declare type ModifierOption = Record<'key' | 'value', string>; | ||
declare type Modifier = (value: string, options: ModifierOption[], defaultValue?: string, locale?: string) => string; | ||
declare type Modifier = (value: any, options: ModifierOption[], defaultValue?: string, locale?: string) => string; | ||
declare type CustomModifiers = Record<string, Modifier>; | ||
@@ -22,0 +22,0 @@ |
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
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
10646
15
112