pome-ui
Version:
Front-end MVC library
1 lines • 1.41 kB
JavaScript
function create(){var s=null;return{localesGroups:[],locales:{},texts:{},overrides:{},getCurrentLocale(){return s},getLocalesGroups(){return this.localesGroups},addLocale(e,t,l){if(!t){var r=e.lastIndexOf("/");if(r<0){t=e}else{t=e.substr(e,r+1)}t=t.split(".")[0]}this.localesGroups.push(t);if(typeof t=="string"){this.locales[t]=e}else if(t instanceof Array){for(var s=0;s<t.length;++s){this.locales[t[s]]=e}}if(l){this.locales["fallback"]=e}},setLocale(e){if(!e){e=!window.localStorage.locale?window.navigator.language:window.localStorage.locale}if(!this.locales[e]){e="fallback"}if(!this.locales[e]){throw"No available translations for the locale"}s=e;this.texts=require(this.locales[e]).texts;if(this.locales["fallback"]){if(this.locales[e]!=this.locales["fallback"]){var t=require(this.locales["fallback"]).texts;var l=Object.getOwnPropertyNames(t);var r=this;l.forEach(function(e){if(!r.texts[e]){r.texts[e]=t[e]}})}}},sr(){if(arguments.length==0){return null}var e=arguments[0];var t=e;var l=false;if(this.overrides[this.getCurrentLocale()]&&this.overrides[this.getCurrentLocale()][e]){t=this.overrides[this.getCurrentLocale()][e];l=true}if(!l&&this.texts[e]){t=this.texts[e]}for(var r=1;r<arguments.length;++r){t=t.replaceAll(`{${r-1}}`,arguments[r])}return t},setLocalizedString(e,t,l){var r=this;e.forEach(function(e){if(!r.overrides[e]){r.overrides[e]={}}r.overrides[e][t]=l})}}}exports.create=create;