alpinejs-i18n
Version:
Internationalization (i18n) support for Alpine.js
2 lines (1 loc) • 1.15 kB
JavaScript
var c=new Event("alpine-i18n:locale-change"),r=new Event("alpine-i18n:ready"),o={version:"2.5.3",set locale(e){this.checkLocale(e),this.currentLocale=e,document.dispatchEvent(c)},get locale(){return this.currentLocale},currentLocale:"",messages:{},fallbackLocale:"",options:{},create(e,i,n){this.messages=i,this.checkLocale(e),this.locale=e,this.options=n},checkLocale(e){if(!Object.keys(this.messages).includes(e))throw new Error(`Alpine I18n: The locale ${e} 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 this.options?.debug?`???${e}`:e;for(let t in i)if(n&&n.replaceAll&&Object.prototype.hasOwnProperty.call(i,t)){let l=i[t],a=new RegExp("{s*("+t+")s*}","g");n=n.replaceAll(a,l)}return this.options?.debug?`[${n}]`:n}},g=function(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))},s=g;var d=s;export{d as default};