diff --git a/packages/main/dist/cdn.js b/packages/main/dist/cdn.js index eeb3f71..e845c8d 100755 --- a/packages/main/dist/cdn.js +++ b/packages/main/dist/cdn.js @@ -3,7 +3,7 @@ var localeChange = new Event("alpine-i18n:locale-change"); var i18nReady = new Event("alpine-i18n:ready"); var AlpineI18n = { - version: "2.1.1", + version: "2.2.0", set locale(name) { this.checkLocale(name); this.currentLocale = name; @@ -26,16 +26,15 @@ } }, t(name, vars) { - let message = name; + let message = ""; try { message = name.split(".").reduce((o, i) => o[i], this.messages[this.locale]); - } catch (error) { - console.warn("AlpineI18n: key " + name + " not found. Using fallbackLocale."); - if (this.fallbackLocale.length) { - message = name.split(".").reduce((o, i) => o[i], this.messages[this.fallbackLocale]); - } else { - return name; - } + } catch { + } + if (!message && this.fallbackLocale.length) { + message = name.split(".").reduce((o, i) => o[i], this.messages[this.fallbackLocale]); + } else if (!message) { + return name; } for (const key in vars) { if (Object.prototype.hasOwnProperty.call(vars, key)) { diff --git a/packages/main/dist/cdn.min.js b/packages/main/dist/cdn.min.js index 5808c5f..d4758b0 100755 --- a/packages/main/dist/cdn.min.js +++ b/packages/main/dist/cdn.min.js @@ -1 +1 @@ -(()=>{var a=new Event("alpine-i18n:locale-change"),r=new Event("alpine-i18n:ready"),o={version:"2.1.1",set locale(e){this.checkLocale(e),this.currentLocale=e,document.dispatchEvent(a)},get locale(){return this.currentLocale},currentLocale:"",messages:{},fallbackLocale:"",create(e,i){this.messages=i,this.checkLocale(e),this.locale=e},checkLocale(e){if(!Object.keys(this.messages).includes(e))throw new Error(`Alpine I18n: The locale ${this.locale} does not exist.`)},t(e,i){let n=e;try{n=e.split(".").reduce((t,l)=>t[l],this.messages[this.locale])}catch(t){if(console.warn("AlpineI18n: key "+e+" not found. Using fallbackLocale."),this.fallbackLocale.length)n=e.split(".").reduce((l,c)=>l[c],this.messages[this.fallbackLocale]);else return e}for(let t in i)if(Object.prototype.hasOwnProperty.call(i,t)){let l=i[t],c=new RegExp("{s*("+t+")s*}","g");n=n.replaceAll(c,l)}return n}};function s(e){window.AlpineI18n=e.reactive(o),document.dispatchEvent(r),e.magic("locale",i=>n=>{if(!n)return window.AlpineI18n.locale;window.AlpineI18n.locale=n}),e.magic("t",i=>(n,t)=>window.AlpineI18n.t(n,t))}document.addEventListener("alpine:initializing",()=>{s(window.Alpine)});})(); +(()=>{var a=new Event("alpine-i18n:locale-change"),r=new Event("alpine-i18n:ready"),o={version:"2.2.0",set locale(e){this.checkLocale(e),this.currentLocale=e,document.dispatchEvent(a)},get locale(){return this.currentLocale},currentLocale:"",messages:{},fallbackLocale:"",create(e,i){this.messages=i,this.checkLocale(e),this.locale=e},checkLocale(e){if(!Object.keys(this.messages).includes(e))throw new Error(`Alpine I18n: The locale ${this.locale} does not exist.`)},t(e,i){let n="";try{n=e.split(".").reduce((t,l)=>t[l],this.messages[this.locale])}catch{}if(!n&&this.fallbackLocale.length)n=e.split(".").reduce((t,l)=>t[l],this.messages[this.fallbackLocale]);else if(!n)return e;for(let t in i)if(Object.prototype.hasOwnProperty.call(i,t)){let l=i[t],c=new RegExp("{s*("+t+")s*}","g");n=n.replaceAll(c,l)}return n}};function s(e){window.AlpineI18n=e.reactive(o),document.dispatchEvent(r),e.magic("locale",i=>n=>{if(!n)return window.AlpineI18n.locale;window.AlpineI18n.locale=n}),e.magic("t",i=>(n,t)=>window.AlpineI18n.t(n,t))}document.addEventListener("alpine:initializing",()=>{s(window.Alpine)});})(); diff --git a/packages/main/dist/module.cjs.js b/packages/main/dist/module.cjs.js index c905f78..2b5ef4c 100755 --- a/packages/main/dist/module.cjs.js +++ b/packages/main/dist/module.cjs.js @@ -15,7 +15,7 @@ __export(exports, { var localeChange = new Event("alpine-i18n:locale-change"); var i18nReady = new Event("alpine-i18n:ready"); var AlpineI18n = { - version: "2.1.1", + version: "2.2.0", set locale(name) { this.checkLocale(name); this.currentLocale = name; @@ -38,16 +38,15 @@ var AlpineI18n = { } }, t(name, vars) { - let message = name; + let message = ""; try { message = name.split(".").reduce((o, i) => o[i], this.messages[this.locale]); - } catch (error) { - console.warn("AlpineI18n: key " + name + " not found. Using fallbackLocale."); - if (this.fallbackLocale.length) { - message = name.split(".").reduce((o, i) => o[i], this.messages[this.fallbackLocale]); - } else { - return name; - } + } catch { + } + if (!message && this.fallbackLocale.length) { + message = name.split(".").reduce((o, i) => o[i], this.messages[this.fallbackLocale]); + } else if (!message) { + return name; } for (const key in vars) { if (Object.prototype.hasOwnProperty.call(vars, key)) { diff --git a/packages/main/dist/module.esm.js b/packages/main/dist/module.esm.js index 5f9dc71..36f6674 100755 --- a/packages/main/dist/module.esm.js +++ b/packages/main/dist/module.esm.js @@ -2,7 +2,7 @@ var localeChange = new Event("alpine-i18n:locale-change"); var i18nReady = new Event("alpine-i18n:ready"); var AlpineI18n = { - version: "2.1.1", + version: "2.2.0", set locale(name) { this.checkLocale(name); this.currentLocale = name; @@ -25,16 +25,15 @@ var AlpineI18n = { } }, t(name, vars) { - let message = name; + let message = ""; try { message = name.split(".").reduce((o, i) => o[i], this.messages[this.locale]); - } catch (error) { - console.warn("AlpineI18n: key " + name + " not found. Using fallbackLocale."); - if (this.fallbackLocale.length) { - message = name.split(".").reduce((o, i) => o[i], this.messages[this.fallbackLocale]); - } else { - return name; - } + } catch { + } + if (!message && this.fallbackLocale.length) { + message = name.split(".").reduce((o, i) => o[i], this.messages[this.fallbackLocale]); + } else if (!message) { + return name; } for (const key in vars) { if (Object.prototype.hasOwnProperty.call(vars, key)) {