es2015-i18n-tag
Version:
ES2015 template literal tag for i18n and l10n translation and localization
2 lines • 9.23 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["es2015-i18n-tag"]=t():e["es2015-i18n-tag"]=t()}("undefined"!=typeof self?self:this,function(){return function(e){function t(r){if(n[r])return n[r].exports;var i=n[r]={i:r,l:!1,exports:{}};return e[r].call(i.exports,i,i.exports,t),i.l=!0,i.exports}var n={};return t.m=e,t.c=n,t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,n){"use strict";function r(e){if(Array.isArray(e)){for(var t=0,n=Array(e.length);t<e.length;t++)n[t]=e[t];return n}return Array.from(e)}function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},a=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),u=/^:([a-z])(\(([^)]+)\))?/,s="string",c="number",l="date",d="decimal",f="currency",y="percent",m={weekday:void 0,era:void 0,year:"numeric",month:"numeric",day:"numeric",hour:void 0,minute:void 0,second:void 0,timeZoneName:void 0},v={weekday:"long",era:void 0,year:"numeric",month:"long",day:"numeric",hour:void 0,minute:void 0,second:void 0,timeZoneName:void 0},h={weekday:"long",era:void 0,year:"numeric",month:"long",day:"numeric",hour:"numeric",minute:"2-digit",second:void 0,timeZoneName:void 0},p={weekday:"long",era:void 0,year:"numeric",month:"long",day:"numeric",hour:"numeric",minute:"2-digit",second:"2-digit",timeZoneName:void 0},g={weekday:void 0,era:void 0,year:"numeric",month:"numeric",day:"numeric",hour:"numeric",minute:"2-digit",second:void 0,timeZoneName:void 0},b={weekday:void 0,era:void 0,year:"numeric",month:"numeric",day:"numeric",hour:"numeric",minute:"2-digit",second:"2-digit",timeZoneName:void 0},_={weekday:void 0,era:void 0,year:void 0,month:"long",day:"numeric",hour:void 0,minute:void 0,second:void 0,timeZoneName:void 0},w={weekday:void 0,era:void 0,year:void 0,month:void 0,day:void 0,hour:"numeric",minute:"2-digit",second:void 0,timeZoneName:void 0},C={weekday:void 0,era:void 0,year:void 0,month:void 0,day:void 0,hour:"numeric",minute:"2-digit",second:"2-digit",timeZoneName:void 0},k={weekday:void 0,era:void 0,year:"numeric",month:"long",day:void 0,hour:void 0,minute:void 0,second:void 0,timeZoneName:void 0},j={d:m,D:v,f:h,F:p,g:g,G:b,m:_,M:_,t:w,T:C,y:k,Y:k},O=function(){function e(){var t=this;i(this,e),this.defaultConfig={locales:void 0,translations:{},number:{currency:"USD"},date:{},string:{}},this.configs={"":this.defaultConfig},this.translationCache={},this.keyCache={},this.typeInfoCache={},this._localizers={s:function(e,n,r){var i=void 0;return r&&null!==(i=t._runCustomFormatters(e,s,r,n))?i:n?n.toLocaleString(e.locales):String(n)},n:function(e,n,r){if("number"!=typeof n)throw Error("value is not a number. type: "+(void 0===n?"undefined":o(n)));if(r){var i=parseInt(r);if(!isNaN(i))return n.toLocaleString(e.locales,Object.assign({},e.number,{style:d,minimumFractionDigits:i,maximumFractionDigits:i}));var a=void 0;if(null!==(a=t._runCustomFormatters(e,c,r,n)))return a}return n.toLocaleString(e.locales,Object.assign({},e.number,{style:d,minimumFractionDigits:0,maximumFractionDigits:3}))},t:function(e,n,r){if(!(n instanceof Date))throw Error("value is not a Date. type: "+n.constructor.name);if(r){switch(r.toUpperCase()){case"R":return n.toUTCString();case"O":return n.toISOString()}var i=j[r];if(i)return n.toLocaleString(e.locales,Object.assign({},e.date,i));var o=t._runCustomFormatters(e,l,r,n);if(null!==o)return o}return n.toLocaleString(e.locales,Object.assign({},e.date))},c:function(e,t,n){if("number"!=typeof t)throw Error("value is not a number. type: "+(void 0===t?"undefined":o(t)));return t.toLocaleString(e.locales,n?Object.assign({},e.number,{style:f,currency:n}):Object.assign({},e.number,{style:f}))},p:function(e,t,n){if("number"!=typeof t)throw Error("value is not a number. type: "+(void 0===t?"undefined":o(t)));return t.toLocaleString(e.locales,n?Object.assign({},e.number,{style:y,minimumFractionDigits:n}):Object.assign({},e.number,{style:y}))}},this.i18n=this.i18n.bind(this),this.translate=this.translate.bind(this),this.i18nConfig=this.i18nConfig.bind(this),this._localize=this._localize.bind(this),this._extractTypeInfo=this._extractTypeInfo.bind(this)}return a(e,[{key:"i18nConfig",value:function(e){var t=e.locales,n=e.translations,r=e.group,i=e.number,o=e.date,a=e.standardFormatters;this.translationCache={};var u=this.configs[r||""]||this.defaultConfig;this.configs[r||""]=Object.assign({},u,{locales:t||u.locales,translations:n||u.translations,number:i||u.number,date:o||u.date,standardFormatters:a||u.standardFormatters})}},{key:"i18n",value:function(e,t,n){for(var i=this,o=this._buildKey(n),a=this._getCachedTranslation(e,t,o),u=a.configGroup,s=a.translatedKey,c=n.slice(1).map(this._extractTypeInfo),l=arguments.length,d=Array(l>3?l-3:0),f=3;f<l;f++)d[f-3]=arguments[f];var y=d.map(function(e,t){return i._localize(u,e,c[t])});return this._buildMessage.apply(this,[s].concat(r(y)))}},{key:"translate",value:function(e,t,n){var i=this;void 0===n||null===n?n="":"string"!=typeof n&&(n=String(n));for(var o=this._getCachedTranslation(e,t,n),a=o.configGroup,u=o.translatedKey,s=arguments.length,c=Array(s>3?s-3:0),l=3;l<s;l++)c[l-3]=arguments[l];var d=c.map(function(e){return e instanceof Object&&e.constructor===Object?i._localize(a,e.value||"",{type:e.formatter||"s",options:e.format}):i._localize(a,e,{type:"s",options:""})});return this._buildMessage.apply(this,[u].concat(r(d)))}},{key:"_getCachedTranslation",value:function(e,t,n){var r=[e||"",t||"",n].join(),i=this.translationCache[r],o=this.configs[t||""]||this.defaultConfig;if(i)return{configGroup:o,translatedKey:i};var a=this._getTranslation(e,o,n);return this.translationCache[r]=a,{configGroup:o,translatedKey:a}}},{key:"_getTranslation",value:function(e,t,n){var r=t.translations,i=void 0,a=void 0;return(void 0===e?"undefined":o(e))===s&&(a=e),a&&(i=r[a])instanceof Object&&(i=i[n]),i||(i="string"==typeof r[n]&&r[n]||n),i}},{key:"_runCustomFormatters",value:function(e,t,n,r){var i=null;if(e.standardFormatters){var o=e.standardFormatters[t];if(o){var a=o[n];a&&(i=a(e.locales,e[t],r))}}return i}},{key:"_extractTypeInfo",value:function(e){var t=this.typeInfoCache[e];if(t)return t;var n=u.exec(e);return t=n?{type:n[1],options:n[3]}:{type:"s",options:""},this.typeInfoCache[e]=t,t}},{key:"_localize",value:function(e,t,n){var r=n.type,i=n.options,o=this._localizers[r];if(o)return o(e,t,i);throw new Error("Type '"+r+"' is not supported. Supported types are: "+Object.keys(this._localizers).join())}},{key:"_buildKey",value:function(e){var t=e.join(),n=this.keyCache[t];if(n)return n;var r=function(e){return e.replace(u,"")},i=r(e[e.length-1]),o=function(e,t,n){return r(t)+"${"+n+"}"+e},a=e.slice(0,-1).reduceRight(o,i).replace(/\r\n/g,"\n");return this.keyCache[t]=a,a}},{key:"_buildMessage",value:function(e){for(var t=arguments.length,n=Array(t>1?t-1:0),r=1;r<t;r++)n[r-1]=arguments[r];return e.replace(/\${(\d)}/g,function(e,t){return n[Number(t)]})}}]),e}(),S=new O,x=S.i18nConfig,A=function(e){for(var t=arguments.length,n=Array(t>1?t-1:0),r=1;r<t;r++)n[r-1]=arguments[r];if("string"==typeof e){if(n.length&&"string"==typeof n[0]){var i=function(t){for(var r=arguments.length,i=Array(r>1?r-1:0),o=1;o<r;o++)i[o-1]=arguments[o];return S.i18n.apply(S,[e,n[0],t].concat(i))};return i.translate=function(t){for(var r=arguments.length,i=Array(r>1?r-1:0),o=1;o<r;o++)i[o-1]=arguments[o];return S.translate.apply(S,[e,n[0],t].concat(i))},i}var o=function(t){for(var n=arguments.length,r=Array(n>1?n-1:0),i=1;i<n;i++)r[i-1]=arguments[i];return S.i18n.apply(S,[e,null,t].concat(r))};return o.translate=function(t){for(var n=arguments.length,r=Array(n>1?n-1:0),i=1;i<n;i++)r[i-1]=arguments[i];return S.translate.apply(S,[e,null,t].concat(r))},o}return S.i18n.apply(S,[null,null,e].concat(n))};A.translate=function(e){for(var t=arguments.length,n=Array(t>1?t-1:0),r=1;r<t;r++)n[r-1]=arguments[r];return S.translate.apply(S,[null,null,e].concat(n))};var F=function(e,t){return function(n){return n.prototype.i18n=function(n){for(var r=arguments.length,i=Array(r>1?r-1:0),o=1;o<r;o++)i[o-1]=arguments[o];return A(e,t).apply(void 0,[n].concat(i))},n.prototype.i18n.translate=function(n){for(var r=arguments.length,i=Array(r>1?r-1:0),o=1;o<r;o++)i[o-1]=arguments[o];return S.translate.apply(S,[e,t,n].concat(i))},n}};"undefined"!=typeof window&&(window.i18n=A,window.i18nConfig=x,window.i18nGroup=F),t.default=A,t.i18nConfig=x,t.i18nGroup=F}])});
//# sourceMappingURL=index.umd.min.js.map