petite-vue-i18n-lite
Version:
A super lightweight and minimal plugin that introduces internationalization into your [Petite Vue](https://github.com/vuejs/petite-vue) app with a simple API.
7 lines (6 loc) • 1.47 kB
JavaScript
/*!
* petite-vue-i18n-lite v1.0.2
* (c) 2021 Erik Pham & niu tech
* @license MIT
*/
var VueI18nLite=function(e){"use strict";const t=["__proto__","prototype","constructor"];function n(e){const n=e.split("."),r=[];for(let e=0;e<n.length;e++){let t=n[e];for(;"\\"===t[t.length-1]&&void 0!==n[e+1];)t=t.slice(0,-1)+".",t+=n[++e];r.push(t)}return r.some((e=>-1!==t.indexOf(e)))?[]:r}function r(e,t){if(!function(e){const t=typeof e;return null!==e&&("object"===t||"function"===t)}(e))return"";const r=n(t);if(0===r.length)return"";let o=Object.assign({},e);for(let e=0;e<r.length;e++){if("string"==typeof o[r[e]])return o[r[e]];if(o=o[r[e]],null==o){if(e!==r.length-1)return"";break}}return""}return e.createI18n=function(e){const t=Object.assign({locale:"en",fallbackLocale:"en",messages:{}},e),n=t.messages;return{t(e,o){if(!e)return"";let c=r(n["string"==typeof o&&o?o:this.current],e)||r(n[t.fallbackLocale],e);if(o&&"string"!=typeof o){const e=function(e,t){if(Array.isArray(t)){const n={},r=[...e.matchAll(/{(.+?)}/g)];return r&&r.forEach(((e,r)=>{t[r]&&(n[e[1]]=t[r])})),n}return t}(c,o);s=e,(Array.isArray(s)?s.length:Object.keys(s).length)&&(c=function(e,t){for(const n in t)e=e.replace(`{${n}}`,String(t[n]));return e}(c,e))}var s;return c||e},current:t.locale,options:e||{},changeLocale(e){this.current=e},setLocaleMessage(e,t){n[e]=Object.assign({},n[e]||{},t)},getLocaleMessage:e=>n[e]||{}}},Object.defineProperty(e,"__esModule",{value:!0}),e}({});