sveltekit-i18n
Advanced tools
Comparing version 1.3.4 to 1.4.0
@@ -1,1 +0,15 @@ | ||
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 r=s.sort(((t,e)=>+t.key-+e.key));return t(r.find((({key:t})=>+e<+t))).value||i},r=(e,s=[],i="")=>{const r=s.sort(((t,e)=>+e.key-+t.key));return t(r.find((({key:t})=>+e>+t))).value||i},a=[{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=>a.reduce((([t,e],{key:s,multiplier:i},r)=>{if(!e||r===(t=>a.indexOf(a.find((e=>e.key===t))))(e)+1){const r=Math.round(t/i);if(!e||Math.abs(r)>=1)return[r,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:r,lte:(t,e=[],r="")=>s(t,e,i(t,e,r)),gte:(t,e=[],i="")=>s(t,e,r(t,e,i)),number:(t,s=[],i="",r="")=>r&&new Intl.NumberFormat(r,{maximumFractionDigits:e(s,"decimals",e(s,"maxDecimals","2")),minimumFractionDigits:e(s,"minDecimals")}).format(+t||+i),date:(t,s=[],i="",r="")=>r&&new Intl.DateTimeFormat(r,{dateStyle:e(s,"dateStyle","medium"),timeStyle:e(s,"timeStyle","short")}).format(+t||+i),ago:(t,s=[],i="",r="")=>{if(!r)return"";const a=e(s,"format","auto"),n=(+t||+i)-Date.now(),l="auto"===a?o(n):[n,a];return new Intl.RelativeTimeFormat(r,{numeric:e(s,"numeric","auto"),style:e(s,"style","long")}).format(...l)}});const l=(e,s)=>Object.keys(t(e)).reduce(((t,i)=>{const r=e[i],a=s?`${s}.${i}`:`${i}`;return r&&"object"==typeof r?Object.assign(Object.assign({},t),l(r,a)):Object.assign(Object.assign({},t),{[a]:r})}),{}),c=t=>t.replace(/\\(?=:|;|{|})/g,""),h=(e,s={},i,r)=>{if(((t="")=>/{{(?:(?!{{|}}).)+}}/.test(`${t}`))(e)){const a=((e,s={},i={},r)=>e.replace(/{{\s*(?:(?!{{|}}).)+\s*}}/g,(e=>{const a=c(`${e.match(/(?!{|\s).+?(?!\\[:;]).(?=\s*(?:[:;]|}}$))/)}`),o=s[a],[,l=""]=t(e.match(/.+?(?!\\;).;\s*default\s*:\s*([^\s:;].+?(?:\\[:;]|[^;\s}])*)(?=\s*(?:;|}}$))/i),[]);let[,h=""]=t(e.match(/{{\s*(?:[^;]|(?:\\;))+\s*(?:(?!\\:).[:])\s*(?!\s)((?:\\;|[^;])+?)(?=\s*(?:[;]|}}$))/i),[]);if(void 0===o&&"ne"!==h)return l;const u=!!h,d=Object.assign(Object.assign({},n),t(i));h=Object.keys(d).includes(h)?h:"eq";const m=d[h],g=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 u||g.length?m(o,g,l,r):`${o}`})))(e,s,i,r);return h(a,s,i,r)}return c(`${e}`)},u=(e,s,i={},r={},a="")=>{if(!s)throw new Error("no key provided to $t()");const o=`${t(t(e)[s],s)}`;return h(o,i,r,a)};class d{constructor(e){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)},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)),r=Object.keys(e).map((t=>`${t}`.toLowerCase()));return[...new Set([...i,...r])]}),[]),this.locale=writable(),this.loaderTrigger=derived([this.locale,this.currentRoute],(([t,e],s)=>{void 0!==t&&void 0!==e&&s([t,e])}),[]),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=Object.assign(Object.assign({},derived([this.translation,this.config],(([e,{customModifiers:s}])=>(i,r)=>u(e,i,r,t(s),get(this.locale))))),{get:(...t)=>get(this.t)(...t)}),this.l=Object.assign(Object.assign({},derived([this.translations,this.config],(([e,{customModifiers:s}])=>(i,r,a)=>u(e[i],r,a,t(s),get(this.locale))))),{get:(...t)=>get(this.l)(...t)}),this.getLocale=t=>`${get(this.locales).find((e=>`${e}`.toLowerCase()===`${t}`.toLowerCase()))||""}`.toLowerCase(),this.initLocale=async t=>{if(!t)return;const e=this.getLocale(t);if(!e)return;get(this.locale)||this.locale.set(e),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=""}=t;await this.loadTranslations(e)},this.addTranslations=(e,s)=>{if(!e)return;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 r=Object.keys(e[i]).map((t=>`${t}`.split(".")[0]));s&&(r=s[i]),this.loadedKeys[i]=Array.from(new Set([...t(this.loadedKeys[i],[]),...t(r,[])]))}))},this.getTranslationProps=async(e=get(this.locale),s=get(this.currentRoute))=>{const i=get(this.config);if(!i||!e)return[];const r=get(this.privateTranslations)[e],{loaders:a}=t(i),o=t(a,[]).filter((({key:s})=>!r||!t(this.loadedKeys[e],[]).includes(s))).filter((({locale:t})=>`${t}`.toLowerCase()===`${e}`.toLowerCase())).filter((({routes:e})=>{return!e||t(e,[]).some((i=s,t=>{try{if("string"==typeof t)return t===i;if("object"==typeof t)return t.test(i)}catch(t){throw new Error("Invalid route config!")}return!1}));var i}));if(o.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.")}})(o);return this.isLoading.set(!1),[{[e]:t},{[e]:o.map((({key:t})=>t))}]}return[]},this.translationLoader=async()=>{this.promises.push(new Promise((async t=>{const e=await this.getTranslationProps();e.length&&this.addTranslations(...e),t()}))),await this.loading.toPromise()},this.loadTranslations=async(t,e=get(this.currentRoute)||"")=>{t&&(this.setRoute(e),this.initLocale(t),await this.loading.toPromise())},e&&this.loadConfig(e),this.loaderTrigger.subscribe(this.translationLoader)}}export{d as default}; | ||
import{writable,derived,get}from"svelte/store"; | ||
/*! ***************************************************************************** | ||
Copyright (c) Microsoft Corporation. | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
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})}),{}),h=t=>t.replace(/\\(?=:|;|{|})/g,""),u=(t,s={},i,a)=>{if(((t="")=>/{{(?:(?!{{|}}).)+}}/.test(`${t}`))(t)){const o=((t,s={},i={},a)=>t.replace(/{{\s*(?:(?!{{|}}).)+\s*}}/g,(t=>{const o=h(`${t.match(/(?!{|\s).+?(?!\\[:;]).(?=\s*(?:[:;]|}}$))/)}`),r=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 u=!!c,d=Object.assign(Object.assign({},l),e(i));c=Object.keys(d).includes(c)?c:"eq";const m=d[c],g=e(t.match(/[^\s:;{](?:[^;]|\\[;])+[^\s:;}]/gi),[]).reduce(((t,e,s)=>{if(s>0){const s=h(`${e.match(/(?:(?:\\:)|[^:])+/)}`.trim()),i=`${e.match(/(?:(?:\\:)|[^:])+$/)}`.trim();if(s&&"default"!==s&&i)return[...t,{key:s,value:i}]}return t}),[]);return u||g.length?m(r,g,n,a):`${r}`})))(t,s,i,a);return u(o,s,i,a)}return h(`${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]),void 0===l&&(l=`${i}`),u(l,a,o,r)};class m{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)},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}`.toLowerCase())),a=Object.keys(e).map((t=>`${t}`.toLowerCase()));return[...new Set([...i,...a])]}),[]),this.internalLocale=writable(),this.locale=Object.assign(Object.assign({},this.internalLocale),derived(this.internalLocale,((t,e)=>{const s=t&&`${t}`.toLowerCase();s!==get(this.locale)&&e(s)}))),this.loaderTrigger=derived([this.locale,this.currentRoute],(([t,e],s)=>{void 0!==t&&void 0!==e&&s([t,e])}),[]),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],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:get(this.translations),key:a,vars:o,customModifiers:e(s),locale:get(this.locale),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:get(this.locale),fallbackLocale:i})))),{get:(...t)=>get(this.l)(...t)}),this.getLocale=t=>`${get(this.locales).find((e=>`${e}`.toLowerCase()===`${t}`.toLowerCase()))||""}`.toLowerCase(),this.initLocale=async t=>{if(!t)return;const e=this.getLocale(t);if(!e)return;get(this.locale)||this.locale.set(e),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=""}=t;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=get(this.locale),i=get(this.currentRoute))=>{const a=get(this.config);if(!a||!s)return[];const o=get(this.privateTranslations),{loaders:r,fallbackLocale:n=""}=e(a),l=`${s}`.toLowerCase(),h=n&&`${n}`.toLowerCase(),u=o[l],d=o[h],m=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&&(!u||!e(this.loadedKeys[l],[]).includes(t))||n&&s===h&&(!d||!e(this.loadedKeys[h],[]).includes(t))));if(m.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=>{const e=await s();t(Object.assign(Object.assign({loader:s},i),{data:e}))}))})))).reduce(((t,{key:s,data:i,locale:a})=>Object.assign(Object.assign({},t),{[a]:c(Object.assign(Object.assign({},e(t[a])),{[s]:i}))})),{})}catch(t){throw console.error(t),new Error("Failed to load translation. Verify the loader function.")}})(m);this.isLoading.set(!1);return[s,m.reduce(((t,{key:e,locale:s})=>Object.assign(Object.assign({},t),{[s]:[...t[s]||[],e]})),{})]}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.initLocale(t),await this.loading.toPromise())},s&&this.loadConfig(s),this.loaderTrigger.subscribe((()=>this.translationLoader()))}}export{m as default}; |
{ | ||
"name": "sveltekit-i18n", | ||
"version": "1.3.4", | ||
"version": "1.4.0", | ||
"description": "Internationalization library for SvelteKit", | ||
@@ -9,3 +9,4 @@ "type": "module", | ||
"exports": { | ||
"import": "./lib/index.js" | ||
".": "./lib/index.js", | ||
"./package.json": "./package.json" | ||
}, | ||
@@ -32,6 +33,9 @@ "scripts": { | ||
"keywords": [ | ||
"svelte", | ||
"sveltekit", | ||
"i18n", | ||
"l10n", | ||
"internationalization", | ||
"svelte", | ||
"sveltekit" | ||
"localization", | ||
"translation" | ||
], | ||
@@ -38,0 +42,0 @@ "author": "Jarda Svoboda", |
@@ -83,7 +83,7 @@ [![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) | ||
export const load = async ({ page }) => { | ||
const { path } = page; | ||
export const load = async ({ url }) => { | ||
const { pathname } = url; | ||
const locale = 'en'; // get from cookie or user session... | ||
await loadTranslations(locale, path); | ||
await loadTranslations(locale, pathname); | ||
@@ -113,2 +113,3 @@ return {}; | ||
[Docs](https://github.com/jarda-svoboda/sveltekit-i18n/tree/master/docs/README.md)\ | ||
[Examples](https://github.com/jarda-svoboda/sveltekit-i18n/tree/master/examples) | ||
[Examples](https://github.com/jarda-svoboda/sveltekit-i18n/tree/master/examples)\ | ||
[Changelog](https://github.com/jarda-svoboda/sveltekit-i18n/releases) |
import { Readable, Writable } from 'svelte/store'; | ||
type LoadingStore = Readable<boolean> & { toPromise: () => Promise<void[]> }; | ||
type Loader = () => Promise<Record<any, any>> | Record<any, any>; | ||
@@ -27,2 +29,3 @@ | ||
initLocale?: string; | ||
fallbackLocale?: string; | ||
customModifiers?: CustomModifiers; | ||
@@ -44,8 +47,7 @@ }; | ||
private promises; | ||
loading: Readable<boolean> & { | ||
toPromise: () => Promise<void[]>; | ||
}; | ||
loading: LoadingStore; | ||
private privateTranslations; | ||
translations: Readable<Translations>; | ||
locales: Readable<string[]>; | ||
private internalLocale; | ||
locale: Writable<string>; | ||
@@ -52,0 +54,0 @@ private loaderTrigger; |
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
15411
95
114