intl-messageformat
Advanced tools
Comparing version 3.1.1 to 3.1.2
@@ -1640,3 +1640,5 @@ (function (global, factory) { | ||
return Intl.NumberFormat.supportedLocalesOf(locales, { | ||
localeMatcher: 'lookup' | ||
// IE11 localeMatcher `lookup` seems to convert `en` -> `en-US` | ||
// but not other browsers, | ||
localeMatcher: 'best fit' | ||
})[0]; | ||
@@ -1643,0 +1645,0 @@ } |
@@ -1,2 +0,2 @@ | ||
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(t=t||self).IntlMessageFormat=r()}(this,function(){"use strict";var n,t=(n=function(t,r){return(n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,r){t.__proto__=r}||function(t,r){for(var e in r)r.hasOwnProperty(e)&&(t[e]=r[e])})(t,r)},function(t,r){function e(){this.constructor=t}n(t,r),t.prototype=null===r?Object.create(r):(e.prototype=r.prototype,new e)}),a=(r.prototype.compile=function(t){return this.pluralStack=[],this.currentPlural=null,this.pluralNumberFormat=null,this.compileMessage(t)},r.prototype.compileMessage=function(t){var r=this;if(!t||"messageFormatPattern"!==t.type)throw new Error('Message AST is not of type: "messageFormatPattern"');var e=t.elements,n=e.filter(function(t){return"messageTextElement"===t.type||"argumentElement"===t.type}).map(function(t){return"messageTextElement"===t.type?r.compileMessageText(t):r.compileArgument(t)});if(n.length!==e.length)throw new Error("Message element does not have a valid type");return n},r.prototype.compileMessageText=function(t){return this.currentPlural&&/(^|[^\\])#/g.test(t.value)?(this.pluralNumberFormat||(this.pluralNumberFormat=new Intl.NumberFormat(this.locales)),new f(this.currentPlural.id,this.currentPlural.format.offset,this.pluralNumberFormat,t.value)):t.value.replace(/\\#/g,"#")},r.prototype.compileArgument=function(t){var r=t.format,e=t.id;if(!r)return new u(e);var n=this.formats,o=this.locales;switch(r.type){case"numberFormat":return{id:e,format:new Intl.NumberFormat(o,n.number[r.style]).format};case"dateFormat":return{id:e,format:new Intl.DateTimeFormat(o,n.date[r.style]).format};case"timeFormat":return{id:e,format:new Intl.DateTimeFormat(o,n.time[r.style]).format};case"pluralFormat":return new s(e,r.ordinal,r.offset,this.compileOptions(t),o);case"selectFormat":return new h(e,this.compileOptions(t));default:throw new Error("Message element does not have a valid format type")}},r.prototype.compileOptions=function(t){var e=this,r=t.format,n=r.options;this.pluralStack.push(this.currentPlural),this.currentPlural="pluralFormat"===r.type?t:null;var o=n.reduce(function(t,r){return t[r.selector]=e.compileMessage(r.value),t},{});return this.currentPlural=this.pluralStack.pop(),o},r);function r(t,r){this.locales=[],this.formats={number:{},date:{},time:{}},this.pluralNumberFormat=null,this.currentPlural=null,this.pluralStack=[],this.locales=t,this.formats=r}function e(t){this.id=t}var o,u=(t(i,o=e),i.prototype.format=function(t){return t||"number"==typeof t?"string"==typeof t?t:String(t):""},i);function i(){return null!==o&&o.apply(this,arguments)||this}var s=(c.prototype.getOption=function(t){var r=this.options;return r["="+t]||r[this.pluralRules.select(t-this.offset)]||r.other},c);function c(t,r,e,n,o){this.id=t,this.offset=e,this.options=n,this.pluralRules=new Intl.PluralRules(o,{type:r?"ordinal":"cardinal"})}var l,f=(t(p,l=e),p.prototype.format=function(t){var r=this.numberFormat.format(t-this.offset);return this.string.replace(/(^|[^\\])#/g,"$1"+r).replace(/\\#/g,"#")},p);function p(t,r,e,n){var o=l.call(this,t)||this;return o.offset=r,o.numberFormat=e,o.string=n,o}var h=(m.prototype.getOption=function(t){var r=this.options;return r[t]||r.other},m);function m(t,r){this.id=t,this.options=r}var d,g,y=(d=Gt,g=Error,v.prototype=g.prototype,d.prototype=new v,Gt.buildMessage=function(t,r){var e,u={literal:function(t){return'"'+o(t.text)+'"'},class:function(t){var r,e="";for(r=0;r<t.parts.length;r++)e+=t.parts[r]instanceof Array?a(t.parts[r][0])+"-"+a(t.parts[r][1]):a(t.parts[r]);return"["+(t.inverted?"^":"")+e+"]"},any:function(t){return"any character"},end:function(t){return"end of input"},other:function(t){return t.description}};function n(t){return t.charCodeAt(0).toString(16).toUpperCase()}function o(t){return t.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/\0/g,"\\0").replace(/\t/g,"\\t").replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/[\x00-\x0F]/g,function(t){return"\\x0"+n(t)}).replace(/[\x10-\x1F\x7F-\x9F]/g,function(t){return"\\x"+n(t)})}function a(t){return t.replace(/\\/g,"\\\\").replace(/\]/g,"\\]").replace(/\^/g,"\\^").replace(/-/g,"\\-").replace(/\0/g,"\\0").replace(/\t/g,"\\t").replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/[\x00-\x0F]/g,function(t){return"\\x0"+n(t)}).replace(/[\x10-\x1F\x7F-\x9F]/g,function(t){return"\\x"+n(t)})}return"Expected "+function(t){var r,e,n,o=new Array(t.length);for(r=0;r<t.length;r++)o[r]=(n=t[r],u[n.type](n));if(o.sort(),0<o.length){for(e=r=1;r<o.length;r++)o[r-1]!==o[r]&&(o[e]=o[r],e++);o.length=e}switch(o.length){case 1:return o[0];case 2:return o[0]+" or "+o[1];default:return o.slice(0,-1).join(", ")+", or "+o[o.length-1]}}(t)+" but "+((e=r)?'"'+o(e)+'"':"end of input")+" found."},{SyntaxError:Gt,parse:function(s,t){t=void 0!==t?t:{};var r,e,n,o,c={},u={start:Nt},a=Nt,i=function(t){return{type:"messageFormatPattern",elements:t,location:Pt()}},l=function(t){return t.reduce(function(t,r){return t.concat(r)},[]).join("")},f=function(t){return{type:"messageTextElement",value:t,location:Pt()}},p=/^[^ \t\n\r,.+={}#]/,h=St([" ","\t","\n","\r",",",".","+","=","{","}","#"],!0,!1),m="{",d=Et("{",!1),g=",",y=Et(",",!1),v="}",b=Et("}",!1),A=function(t,r){return{type:"argumentElement",id:t,format:r&&r[2],location:Pt()}},w="number",F=Et("number",!1),x="date",_=Et("date",!1),O="time",C=Et("time",!1),P=function(t,r){return{type:t+"Format",style:r&&r[2],location:Pt()}},E="plural",S=Et("plural",!1),T=function(t){return{type:t.type,ordinal:!1,offset:t.offset||0,options:t.options,location:Pt()}},I="selectordinal",M=Et("selectordinal",!1),j=function(t){return{type:t.type,ordinal:!0,offset:t.offset||0,options:t.options,location:Pt()}},N="select",k=Et("select",!1),R=function(t){return{type:"selectFormat",options:t,location:Pt()}},L="=",D=Et("=",!1),Z=function(t,r){return{type:"optionalFormatPattern",selector:t,value:r,location:Pt()}},U="offset:",W=Et("offset:",!1),$=function(t){return t},q=function(t,r){return{type:"pluralFormat",offset:t,options:r,location:Pt()}},z=Tt("whitespace"),B=/^[ \t\n\r]/,G=St([" ","\t","\n","\r"],!1,!1),H=Tt("optionalWhitespace"),J=/^[0-9]/,K=St([["0","9"]],!1,!1),Q=/^[0-9a-f]/i,V=St([["0","9"],["a","f"]],!1,!0),X="0",Y=Et("0",!1),tt=/^[1-9]/,rt=St([["1","9"]],!1,!1),et=function(t){return parseInt(t,10)},nt=/^[^{}\\\0-\x1F\x7F \t\n\r]/,ot=St(["{","}","\\",["\0",""],""," ","\t","\n","\r"],!0,!1),ut="\\\\",at=Et("\\\\",!1),it=function(){return"\\"},st="\\#",ct=Et("\\#",!1),lt=function(){return"\\#"},ft="\\{",pt=Et("\\{",!1),ht=function(){return"{"},mt="\\}",dt=Et("\\}",!1),gt=function(){return"}"},yt="\\u",vt=Et("\\u",!1),bt=function(t){return String.fromCharCode(parseInt(t,16))},At=function(t){return t.join("")},wt=0,Ft=0,xt=[{line:1,column:1}],_t=0,Ot=[],Ct=0;if("startRule"in t){if(!(t.startRule in u))throw new Error("Can't start parsing from rule \""+t.startRule+'".');a=u[t.startRule]}function Pt(){return Mt(Ft,wt)}function Et(t,r){return{type:"literal",text:t,ignoreCase:r}}function St(t,r,e){return{type:"class",parts:t,inverted:r,ignoreCase:e}}function Tt(t){return{type:"other",description:t}}function It(t){var r,e=xt[t];if(e)return e;for(r=t-1;!xt[r];)r--;for(e={line:(e=xt[r]).line,column:e.column};r<t;)10===s.charCodeAt(r)?(e.line++,e.column=1):e.column++,r++;return xt[t]=e}function Mt(t,r){var e=It(t),n=It(r);return{start:{offset:t,line:e.line,column:e.column},end:{offset:r,line:n.line,column:n.column}}}function jt(t){wt<_t||(_t<wt&&(_t=wt,Ot=[]),Ot.push(t))}function Nt(){return kt()}function kt(){var t,r,e;for(t=wt,r=[],e=Rt();e!==c;)r.push(e),e=Rt();return r!==c&&(Ft=t,r=i(r)),t=r}function Rt(){var t;return(t=function(){var t,r;return t=wt,(r=function(){var t,r,e,n,o,u;if(r=[],e=t=wt,(e=(n=Ut())!==c?(o=Bt())!==c&&(u=Ut())!==c?n=[n,o,u]:(wt=e,c):(wt=e,c))!==c)for(;e!==c;)r.push(e),e=wt,n=Ut(),e=n!==c&&(o=Bt())!==c&&(u=Ut())!==c?n=[n,o,u]:(wt=e,c);else r=c;return r!==c&&(Ft=t,r=l(r)),(t=r)===c&&(t=wt,r=Zt(),t=r!==c?s.substring(t,wt):r),t}())!==c&&(Ft=t,r=f(r)),t=r}())===c&&(t=function(){var t,r,e,n,o,u,a;return t=wt,123===s.charCodeAt(wt)?(r=m,wt++):(r=c,0===Ct&&jt(d)),t=r!==c?Ut()!==c&&(e=function(){var t,r,e;if((t=qt())===c){if(t=wt,r=[],p.test(s.charAt(wt))?(e=s.charAt(wt),wt++):(e=c,0===Ct&&jt(h)),e!==c)for(;e!==c;)r.push(e),p.test(s.charAt(wt))?(e=s.charAt(wt),wt++):(e=c,0===Ct&&jt(h));else r=c;t=r!==c?s.substring(t,wt):r}return t}())!==c&&Ut()!==c?(n=wt,44===s.charCodeAt(wt)?(o=g,wt++):(o=c,0===Ct&&jt(y)),(n=o!==c&&(u=Ut())!==c&&(a=function(){var t;return(t=function(){var t,r,e,n,o,u;return t=wt,s.substr(wt,6)===w?(r=w,wt+=6):(r=c,0===Ct&&jt(F)),r===c&&(s.substr(wt,4)===x?(r=x,wt+=4):(r=c,0===Ct&&jt(_)),r===c&&(s.substr(wt,4)===O?(r=O,wt+=4):(r=c,0===Ct&&jt(C)))),t=r!==c?Ut()!==c?(e=wt,44===s.charCodeAt(wt)?(n=g,wt++):(n=c,0===Ct&&jt(y)),(e=n!==c&&(o=Ut())!==c&&(u=Bt())!==c?n=[n,o,u]:(wt=e,c))===c&&(e=null),e!==c?(Ft=t,r=P(r,e)):(wt=t,c)):(wt=t,c):(wt=t,c)}())===c&&(t=function(){var t,r,e,n;return t=wt,s.substr(wt,6)===E?(r=E,wt+=6):(r=c,0===Ct&&jt(S)),t=r!==c?Ut()!==c?(44===s.charCodeAt(wt)?(e=g,wt++):(e=c,0===Ct&&jt(y)),e!==c&&Ut()!==c&&(n=Dt())!==c?(Ft=t,r=T(n)):(wt=t,c)):(wt=t,c):(wt=t,c)}())===c&&(t=function(){var t,r,e,n;return t=wt,s.substr(wt,13)===I?(r=I,wt+=13):(r=c,0===Ct&&jt(M)),t=r!==c?Ut()!==c?(44===s.charCodeAt(wt)?(e=g,wt++):(e=c,0===Ct&&jt(y)),e!==c&&Ut()!==c&&(n=Dt())!==c?(Ft=t,r=j(n)):(wt=t,c)):(wt=t,c):(wt=t,c)}())===c&&(t=function(){var t,r,e,n,o;if(t=wt,s.substr(wt,6)===N?(r=N,wt+=6):(r=c,0===Ct&&jt(k)),r!==c)if(Ut()!==c)if(44===s.charCodeAt(wt)?(e=g,wt++):(e=c,0===Ct&&jt(y)),e!==c)if(Ut()!==c){if(n=[],(o=Lt())!==c)for(;o!==c;)n.push(o),o=Lt();else n=c;t=n!==c?(Ft=t,r=R(n)):(wt=t,c)}else wt=t,t=c;else wt=t,t=c;else wt=t,t=c;else wt=t,t=c;return t}()),t}())!==c?o=[o,u,a]:(wt=n,c))===c&&(n=null),n!==c&&(o=Ut())!==c?(125===s.charCodeAt(wt)?(u=v,wt++):(u=c,0===Ct&&jt(b)),u!==c?(Ft=t,r=A(e,n)):(wt=t,c)):(wt=t,c)):(wt=t,c):(wt=t,c)}()),t}function Lt(){var t,r,e,n,o;return t=wt,t=Ut()!==c&&(r=function(){var t,r,e,n;return r=t=wt,61===s.charCodeAt(wt)?(e=L,wt++):(e=c,0===Ct&&jt(D)),(t=(r=e!==c&&(n=qt())!==c?e=[e,n]:(wt=r,c))!==c?s.substring(t,wt):r)===c&&(t=Bt()),t}())!==c&&Ut()!==c?(123===s.charCodeAt(wt)?(e=m,wt++):(e=c,0===Ct&&jt(d)),e!==c&&(n=kt())!==c?(125===s.charCodeAt(wt)?(o=v,wt++):(o=c,0===Ct&&jt(b)),o!==c?(Ft=t,Z(r,n)):(wt=t,c)):(wt=t,c)):(wt=t,c)}function Dt(){var t,r,e,n;if(t=wt,(r=function(){var t,r,e;return t=wt,s.substr(wt,7)===U?(r=U,wt+=7):(r=c,0===Ct&&jt(W)),t=r!==c&&Ut()!==c&&(e=qt())!==c?(Ft=t,r=$(e)):(wt=t,c)}())===c&&(r=null),r!==c)if(Ut()!==c){if(e=[],(n=Lt())!==c)for(;n!==c;)e.push(n),n=Lt();else e=c;t=e!==c?(Ft=t,r=q(r,e)):(wt=t,c)}else wt=t,t=c;else wt=t,t=c;return t}function Zt(){var t,r;if(Ct++,t=[],B.test(s.charAt(wt))?(r=s.charAt(wt),wt++):(r=c,0===Ct&&jt(G)),r!==c)for(;r!==c;)t.push(r),B.test(s.charAt(wt))?(r=s.charAt(wt),wt++):(r=c,0===Ct&&jt(G));else t=c;return Ct--,t===c&&(r=c,0===Ct&&jt(z)),t}function Ut(){var t,r,e;for(Ct++,t=wt,r=[],e=Zt();e!==c;)r.push(e),e=Zt();return t=r!==c?s.substring(t,wt):r,Ct--,t===c&&(r=c,0===Ct&&jt(H)),t}function Wt(){var t;return J.test(s.charAt(wt))?(t=s.charAt(wt),wt++):(t=c,0===Ct&&jt(K)),t}function $t(){var t;return Q.test(s.charAt(wt))?(t=s.charAt(wt),wt++):(t=c,0===Ct&&jt(V)),t}function qt(){var t,r,e,n,o,u;if(t=wt,48===s.charCodeAt(wt)?(r=X,wt++):(r=c,0===Ct&&jt(Y)),r===c){if(e=r=wt,tt.test(s.charAt(wt))?(n=s.charAt(wt),wt++):(n=c,0===Ct&&jt(rt)),n!==c){for(o=[],u=Wt();u!==c;)o.push(u),u=Wt();e=o!==c?n=[n,o]:(wt=e,c)}else wt=e,e=c;r=e!==c?s.substring(r,wt):e}return r!==c&&(Ft=t,r=et(r)),t=r}function zt(){var t,r,e,n,o,u,a,i;return nt.test(s.charAt(wt))?(t=s.charAt(wt),wt++):(t=c,0===Ct&&jt(ot)),t===c&&(t=wt,s.substr(wt,2)===ut?(r=ut,wt+=2):(r=c,0===Ct&&jt(at)),r!==c&&(Ft=t,r=it()),(t=r)===c&&(t=wt,s.substr(wt,2)===st?(r=st,wt+=2):(r=c,0===Ct&&jt(ct)),r!==c&&(Ft=t,r=lt()),(t=r)===c&&(t=wt,s.substr(wt,2)===ft?(r=ft,wt+=2):(r=c,0===Ct&&jt(pt)),r!==c&&(Ft=t,r=ht()),(t=r)===c&&(t=wt,s.substr(wt,2)===mt?(r=mt,wt+=2):(r=c,0===Ct&&jt(dt)),r!==c&&(Ft=t,r=gt()),(t=r)===c&&(t=wt,s.substr(wt,2)===yt?(r=yt,wt+=2):(r=c,0===Ct&&jt(vt)),t=r!==c?(n=e=wt,(e=(n=(o=$t())!==c&&(u=$t())!==c&&(a=$t())!==c&&(i=$t())!==c?o=[o,u,a,i]:(wt=n,c))!==c?s.substring(e,wt):n)!==c?(Ft=t,r=bt(e)):(wt=t,c)):(wt=t,c)))))),t}function Bt(){var t,r,e;if(t=wt,r=[],(e=zt())!==c)for(;e!==c;)r.push(e),e=zt();else r=c;return r!==c&&(Ft=t,r=At(r)),t=r}if((r=a())!==c&&wt===s.length)return r;throw r!==c&&wt<s.length&&jt({type:"end"}),e=Ot,n=_t<s.length?s.charAt(_t):null,o=_t<s.length?Mt(_t,_t+1):Mt(_t,_t),new Gt(Gt.buildMessage(e,n),e,n,o)}});function Gt(t,r,e,n){this.message=t,this.expected=r,this.found=e,this.location=n,this.name="SyntaxError","function"==typeof Error.captureStackTrace&&Error.captureStackTrace(this,Gt)}function v(){this.constructor=d}var b,A=(b=function(t,r){return(b=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,r){t.__proto__=r}||function(t,r){for(var e in r)r.hasOwnProperty(e)&&(t[e]=r[e])})(t,r)},function(t,r){function e(){this.constructor=t}b(t,r),t.prototype=null===r?Object.create(r):(e.prototype=r.prototype,new e)}),w=function(){return(w=Object.assign||function(t){for(var r,e=1,n=arguments.length;e<n;e++)for(var o in r=arguments[e])Object.prototype.hasOwnProperty.call(r,o)&&(t[o]=r[o]);return t}).apply(this,arguments)};function F(t,r){return r?w({},t,{date:function(e,n){return n?w({},e||{},n||{},Object.keys(e).reduce(function(t,r){return t[r]=w({},e[r],n[r]||{}),t},{})):e}(t.date,r.date)}):t}var x,_=(x=Error,A(O,x),O);function O(t,r){var e=x.call(this,t)||this;return e.variableId=r,e}var C=(P.prototype.resolvedOptions=function(){return{locale:this._locale}},P.defaultLocale="en",P.formats={number:{currency:{style:"currency"},percent:{style:"percent"}},date:{short:{month:"numeric",day:"numeric",year:"2-digit"},medium:{month:"short",day:"numeric",year:"numeric"},long:{month:"long",day:"numeric",year:"numeric"},full:{weekday:"long",month:"long",day:"numeric",year:"numeric"}},time:{short:{hour:"numeric",minute:"numeric"},medium:{hour:"numeric",minute:"numeric",second:"numeric"},long:{hour:"numeric",minute:"numeric",second:"numeric",timeZoneName:"short"},full:{hour:"numeric",minute:"numeric",second:"numeric",timeZoneName:"short"}}},P.__parse=y.parse,P);function P(t,r,e){var n=this;void 0===r&&(r=P.defaultLocale),this.format=function(t){try{return function t(r,e){for(var n="",o=0,u=r;o<u.length;o++){var a=u[o];if("string"!=typeof a){var i=a.id;if(!(e&&i in e))throw new _("A value must be provided for: "+i,i);var s=e[i];a.options?n+=t(a.getOption(s),e):n+=a.format(s)}else n+=a}return n}(n.pattern,t)}catch(t){throw t.variableId?new Error("The intl string context variable '"+t.variableId+"' was not provided to the string '"+n.message+"'"):t}};var o="string"==typeof t?P.__parse(t):t;if(!o||"messageFormatPattern"!==o.type)throw new TypeError("A message must be provided as a String or AST.");var u=F(P.formats,e);this._locale=function(t){"string"==typeof t&&(t=[t]);try{return Intl.NumberFormat.supportedLocalesOf(t,{localeMatcher:"lookup"})[0]}catch(t){return C.defaultLocale}}(r||[]),this.pattern=new a(r,u).compile(o),this.message=t}return C}); | ||
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(t=t||self).IntlMessageFormat=r()}(this,function(){"use strict";var n,t=(n=function(t,r){return(n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,r){t.__proto__=r}||function(t,r){for(var e in r)r.hasOwnProperty(e)&&(t[e]=r[e])})(t,r)},function(t,r){function e(){this.constructor=t}n(t,r),t.prototype=null===r?Object.create(r):(e.prototype=r.prototype,new e)}),a=(r.prototype.compile=function(t){return this.pluralStack=[],this.currentPlural=null,this.pluralNumberFormat=null,this.compileMessage(t)},r.prototype.compileMessage=function(t){var r=this;if(!t||"messageFormatPattern"!==t.type)throw new Error('Message AST is not of type: "messageFormatPattern"');var e=t.elements,n=e.filter(function(t){return"messageTextElement"===t.type||"argumentElement"===t.type}).map(function(t){return"messageTextElement"===t.type?r.compileMessageText(t):r.compileArgument(t)});if(n.length!==e.length)throw new Error("Message element does not have a valid type");return n},r.prototype.compileMessageText=function(t){return this.currentPlural&&/(^|[^\\])#/g.test(t.value)?(this.pluralNumberFormat||(this.pluralNumberFormat=new Intl.NumberFormat(this.locales)),new f(this.currentPlural.id,this.currentPlural.format.offset,this.pluralNumberFormat,t.value)):t.value.replace(/\\#/g,"#")},r.prototype.compileArgument=function(t){var r=t.format,e=t.id;if(!r)return new u(e);var n=this.formats,o=this.locales;switch(r.type){case"numberFormat":return{id:e,format:new Intl.NumberFormat(o,n.number[r.style]).format};case"dateFormat":return{id:e,format:new Intl.DateTimeFormat(o,n.date[r.style]).format};case"timeFormat":return{id:e,format:new Intl.DateTimeFormat(o,n.time[r.style]).format};case"pluralFormat":return new s(e,r.ordinal,r.offset,this.compileOptions(t),o);case"selectFormat":return new h(e,this.compileOptions(t));default:throw new Error("Message element does not have a valid format type")}},r.prototype.compileOptions=function(t){var e=this,r=t.format,n=r.options;this.pluralStack.push(this.currentPlural),this.currentPlural="pluralFormat"===r.type?t:null;var o=n.reduce(function(t,r){return t[r.selector]=e.compileMessage(r.value),t},{});return this.currentPlural=this.pluralStack.pop(),o},r);function r(t,r){this.locales=[],this.formats={number:{},date:{},time:{}},this.pluralNumberFormat=null,this.currentPlural=null,this.pluralStack=[],this.locales=t,this.formats=r}function e(t){this.id=t}var o,u=(t(i,o=e),i.prototype.format=function(t){return t||"number"==typeof t?"string"==typeof t?t:String(t):""},i);function i(){return null!==o&&o.apply(this,arguments)||this}var s=(c.prototype.getOption=function(t){var r=this.options;return r["="+t]||r[this.pluralRules.select(t-this.offset)]||r.other},c);function c(t,r,e,n,o){this.id=t,this.offset=e,this.options=n,this.pluralRules=new Intl.PluralRules(o,{type:r?"ordinal":"cardinal"})}var l,f=(t(p,l=e),p.prototype.format=function(t){var r=this.numberFormat.format(t-this.offset);return this.string.replace(/(^|[^\\])#/g,"$1"+r).replace(/\\#/g,"#")},p);function p(t,r,e,n){var o=l.call(this,t)||this;return o.offset=r,o.numberFormat=e,o.string=n,o}var h=(m.prototype.getOption=function(t){var r=this.options;return r[t]||r.other},m);function m(t,r){this.id=t,this.options=r}var d,g,y=(d=Gt,g=Error,v.prototype=g.prototype,d.prototype=new v,Gt.buildMessage=function(t,r){var e,u={literal:function(t){return'"'+o(t.text)+'"'},class:function(t){var r,e="";for(r=0;r<t.parts.length;r++)e+=t.parts[r]instanceof Array?a(t.parts[r][0])+"-"+a(t.parts[r][1]):a(t.parts[r]);return"["+(t.inverted?"^":"")+e+"]"},any:function(t){return"any character"},end:function(t){return"end of input"},other:function(t){return t.description}};function n(t){return t.charCodeAt(0).toString(16).toUpperCase()}function o(t){return t.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/\0/g,"\\0").replace(/\t/g,"\\t").replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/[\x00-\x0F]/g,function(t){return"\\x0"+n(t)}).replace(/[\x10-\x1F\x7F-\x9F]/g,function(t){return"\\x"+n(t)})}function a(t){return t.replace(/\\/g,"\\\\").replace(/\]/g,"\\]").replace(/\^/g,"\\^").replace(/-/g,"\\-").replace(/\0/g,"\\0").replace(/\t/g,"\\t").replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/[\x00-\x0F]/g,function(t){return"\\x0"+n(t)}).replace(/[\x10-\x1F\x7F-\x9F]/g,function(t){return"\\x"+n(t)})}return"Expected "+function(t){var r,e,n,o=new Array(t.length);for(r=0;r<t.length;r++)o[r]=(n=t[r],u[n.type](n));if(o.sort(),0<o.length){for(e=r=1;r<o.length;r++)o[r-1]!==o[r]&&(o[e]=o[r],e++);o.length=e}switch(o.length){case 1:return o[0];case 2:return o[0]+" or "+o[1];default:return o.slice(0,-1).join(", ")+", or "+o[o.length-1]}}(t)+" but "+((e=r)?'"'+o(e)+'"':"end of input")+" found."},{SyntaxError:Gt,parse:function(s,t){t=void 0!==t?t:{};var r,e,n,o,c={},u={start:Nt},a=Nt,i=function(t){return{type:"messageFormatPattern",elements:t,location:Pt()}},l=function(t){return t.reduce(function(t,r){return t.concat(r)},[]).join("")},f=function(t){return{type:"messageTextElement",value:t,location:Pt()}},p=/^[^ \t\n\r,.+={}#]/,h=St([" ","\t","\n","\r",",",".","+","=","{","}","#"],!0,!1),m="{",d=Et("{",!1),g=",",y=Et(",",!1),v="}",b=Et("}",!1),A=function(t,r){return{type:"argumentElement",id:t,format:r&&r[2],location:Pt()}},w="number",F=Et("number",!1),x="date",_=Et("date",!1),O="time",C=Et("time",!1),P=function(t,r){return{type:t+"Format",style:r&&r[2],location:Pt()}},E="plural",S=Et("plural",!1),T=function(t){return{type:t.type,ordinal:!1,offset:t.offset||0,options:t.options,location:Pt()}},I="selectordinal",M=Et("selectordinal",!1),j=function(t){return{type:t.type,ordinal:!0,offset:t.offset||0,options:t.options,location:Pt()}},N="select",k=Et("select",!1),R=function(t){return{type:"selectFormat",options:t,location:Pt()}},L="=",D=Et("=",!1),Z=function(t,r){return{type:"optionalFormatPattern",selector:t,value:r,location:Pt()}},U="offset:",W=Et("offset:",!1),$=function(t){return t},q=function(t,r){return{type:"pluralFormat",offset:t,options:r,location:Pt()}},z=Tt("whitespace"),B=/^[ \t\n\r]/,G=St([" ","\t","\n","\r"],!1,!1),H=Tt("optionalWhitespace"),J=/^[0-9]/,K=St([["0","9"]],!1,!1),Q=/^[0-9a-f]/i,V=St([["0","9"],["a","f"]],!1,!0),X="0",Y=Et("0",!1),tt=/^[1-9]/,rt=St([["1","9"]],!1,!1),et=function(t){return parseInt(t,10)},nt=/^[^{}\\\0-\x1F\x7F \t\n\r]/,ot=St(["{","}","\\",["\0",""],""," ","\t","\n","\r"],!0,!1),ut="\\\\",at=Et("\\\\",!1),it=function(){return"\\"},st="\\#",ct=Et("\\#",!1),lt=function(){return"\\#"},ft="\\{",pt=Et("\\{",!1),ht=function(){return"{"},mt="\\}",dt=Et("\\}",!1),gt=function(){return"}"},yt="\\u",vt=Et("\\u",!1),bt=function(t){return String.fromCharCode(parseInt(t,16))},At=function(t){return t.join("")},wt=0,Ft=0,xt=[{line:1,column:1}],_t=0,Ot=[],Ct=0;if("startRule"in t){if(!(t.startRule in u))throw new Error("Can't start parsing from rule \""+t.startRule+'".');a=u[t.startRule]}function Pt(){return Mt(Ft,wt)}function Et(t,r){return{type:"literal",text:t,ignoreCase:r}}function St(t,r,e){return{type:"class",parts:t,inverted:r,ignoreCase:e}}function Tt(t){return{type:"other",description:t}}function It(t){var r,e=xt[t];if(e)return e;for(r=t-1;!xt[r];)r--;for(e={line:(e=xt[r]).line,column:e.column};r<t;)10===s.charCodeAt(r)?(e.line++,e.column=1):e.column++,r++;return xt[t]=e}function Mt(t,r){var e=It(t),n=It(r);return{start:{offset:t,line:e.line,column:e.column},end:{offset:r,line:n.line,column:n.column}}}function jt(t){wt<_t||(_t<wt&&(_t=wt,Ot=[]),Ot.push(t))}function Nt(){return kt()}function kt(){var t,r,e;for(t=wt,r=[],e=Rt();e!==c;)r.push(e),e=Rt();return r!==c&&(Ft=t,r=i(r)),t=r}function Rt(){var t;return(t=function(){var t,r;return t=wt,(r=function(){var t,r,e,n,o,u;if(r=[],e=t=wt,(e=(n=Ut())!==c?(o=Bt())!==c&&(u=Ut())!==c?n=[n,o,u]:(wt=e,c):(wt=e,c))!==c)for(;e!==c;)r.push(e),e=wt,n=Ut(),e=n!==c&&(o=Bt())!==c&&(u=Ut())!==c?n=[n,o,u]:(wt=e,c);else r=c;return r!==c&&(Ft=t,r=l(r)),(t=r)===c&&(t=wt,r=Zt(),t=r!==c?s.substring(t,wt):r),t}())!==c&&(Ft=t,r=f(r)),t=r}())===c&&(t=function(){var t,r,e,n,o,u,a;return t=wt,123===s.charCodeAt(wt)?(r=m,wt++):(r=c,0===Ct&&jt(d)),t=r!==c?Ut()!==c&&(e=function(){var t,r,e;if((t=qt())===c){if(t=wt,r=[],p.test(s.charAt(wt))?(e=s.charAt(wt),wt++):(e=c,0===Ct&&jt(h)),e!==c)for(;e!==c;)r.push(e),p.test(s.charAt(wt))?(e=s.charAt(wt),wt++):(e=c,0===Ct&&jt(h));else r=c;t=r!==c?s.substring(t,wt):r}return t}())!==c&&Ut()!==c?(n=wt,44===s.charCodeAt(wt)?(o=g,wt++):(o=c,0===Ct&&jt(y)),(n=o!==c&&(u=Ut())!==c&&(a=function(){var t;return(t=function(){var t,r,e,n,o,u;return t=wt,s.substr(wt,6)===w?(r=w,wt+=6):(r=c,0===Ct&&jt(F)),r===c&&(s.substr(wt,4)===x?(r=x,wt+=4):(r=c,0===Ct&&jt(_)),r===c&&(s.substr(wt,4)===O?(r=O,wt+=4):(r=c,0===Ct&&jt(C)))),t=r!==c?Ut()!==c?(e=wt,44===s.charCodeAt(wt)?(n=g,wt++):(n=c,0===Ct&&jt(y)),(e=n!==c&&(o=Ut())!==c&&(u=Bt())!==c?n=[n,o,u]:(wt=e,c))===c&&(e=null),e!==c?(Ft=t,r=P(r,e)):(wt=t,c)):(wt=t,c):(wt=t,c)}())===c&&(t=function(){var t,r,e,n;return t=wt,s.substr(wt,6)===E?(r=E,wt+=6):(r=c,0===Ct&&jt(S)),t=r!==c?Ut()!==c?(44===s.charCodeAt(wt)?(e=g,wt++):(e=c,0===Ct&&jt(y)),e!==c&&Ut()!==c&&(n=Dt())!==c?(Ft=t,r=T(n)):(wt=t,c)):(wt=t,c):(wt=t,c)}())===c&&(t=function(){var t,r,e,n;return t=wt,s.substr(wt,13)===I?(r=I,wt+=13):(r=c,0===Ct&&jt(M)),t=r!==c?Ut()!==c?(44===s.charCodeAt(wt)?(e=g,wt++):(e=c,0===Ct&&jt(y)),e!==c&&Ut()!==c&&(n=Dt())!==c?(Ft=t,r=j(n)):(wt=t,c)):(wt=t,c):(wt=t,c)}())===c&&(t=function(){var t,r,e,n,o;if(t=wt,s.substr(wt,6)===N?(r=N,wt+=6):(r=c,0===Ct&&jt(k)),r!==c)if(Ut()!==c)if(44===s.charCodeAt(wt)?(e=g,wt++):(e=c,0===Ct&&jt(y)),e!==c)if(Ut()!==c){if(n=[],(o=Lt())!==c)for(;o!==c;)n.push(o),o=Lt();else n=c;t=n!==c?(Ft=t,r=R(n)):(wt=t,c)}else wt=t,t=c;else wt=t,t=c;else wt=t,t=c;else wt=t,t=c;return t}()),t}())!==c?o=[o,u,a]:(wt=n,c))===c&&(n=null),n!==c&&(o=Ut())!==c?(125===s.charCodeAt(wt)?(u=v,wt++):(u=c,0===Ct&&jt(b)),u!==c?(Ft=t,r=A(e,n)):(wt=t,c)):(wt=t,c)):(wt=t,c):(wt=t,c)}()),t}function Lt(){var t,r,e,n,o;return t=wt,t=Ut()!==c&&(r=function(){var t,r,e,n;return r=t=wt,61===s.charCodeAt(wt)?(e=L,wt++):(e=c,0===Ct&&jt(D)),(t=(r=e!==c&&(n=qt())!==c?e=[e,n]:(wt=r,c))!==c?s.substring(t,wt):r)===c&&(t=Bt()),t}())!==c&&Ut()!==c?(123===s.charCodeAt(wt)?(e=m,wt++):(e=c,0===Ct&&jt(d)),e!==c&&(n=kt())!==c?(125===s.charCodeAt(wt)?(o=v,wt++):(o=c,0===Ct&&jt(b)),o!==c?(Ft=t,Z(r,n)):(wt=t,c)):(wt=t,c)):(wt=t,c)}function Dt(){var t,r,e,n;if(t=wt,(r=function(){var t,r,e;return t=wt,s.substr(wt,7)===U?(r=U,wt+=7):(r=c,0===Ct&&jt(W)),t=r!==c&&Ut()!==c&&(e=qt())!==c?(Ft=t,r=$(e)):(wt=t,c)}())===c&&(r=null),r!==c)if(Ut()!==c){if(e=[],(n=Lt())!==c)for(;n!==c;)e.push(n),n=Lt();else e=c;t=e!==c?(Ft=t,r=q(r,e)):(wt=t,c)}else wt=t,t=c;else wt=t,t=c;return t}function Zt(){var t,r;if(Ct++,t=[],B.test(s.charAt(wt))?(r=s.charAt(wt),wt++):(r=c,0===Ct&&jt(G)),r!==c)for(;r!==c;)t.push(r),B.test(s.charAt(wt))?(r=s.charAt(wt),wt++):(r=c,0===Ct&&jt(G));else t=c;return Ct--,t===c&&(r=c,0===Ct&&jt(z)),t}function Ut(){var t,r,e;for(Ct++,t=wt,r=[],e=Zt();e!==c;)r.push(e),e=Zt();return t=r!==c?s.substring(t,wt):r,Ct--,t===c&&(r=c,0===Ct&&jt(H)),t}function Wt(){var t;return J.test(s.charAt(wt))?(t=s.charAt(wt),wt++):(t=c,0===Ct&&jt(K)),t}function $t(){var t;return Q.test(s.charAt(wt))?(t=s.charAt(wt),wt++):(t=c,0===Ct&&jt(V)),t}function qt(){var t,r,e,n,o,u;if(t=wt,48===s.charCodeAt(wt)?(r=X,wt++):(r=c,0===Ct&&jt(Y)),r===c){if(e=r=wt,tt.test(s.charAt(wt))?(n=s.charAt(wt),wt++):(n=c,0===Ct&&jt(rt)),n!==c){for(o=[],u=Wt();u!==c;)o.push(u),u=Wt();e=o!==c?n=[n,o]:(wt=e,c)}else wt=e,e=c;r=e!==c?s.substring(r,wt):e}return r!==c&&(Ft=t,r=et(r)),t=r}function zt(){var t,r,e,n,o,u,a,i;return nt.test(s.charAt(wt))?(t=s.charAt(wt),wt++):(t=c,0===Ct&&jt(ot)),t===c&&(t=wt,s.substr(wt,2)===ut?(r=ut,wt+=2):(r=c,0===Ct&&jt(at)),r!==c&&(Ft=t,r=it()),(t=r)===c&&(t=wt,s.substr(wt,2)===st?(r=st,wt+=2):(r=c,0===Ct&&jt(ct)),r!==c&&(Ft=t,r=lt()),(t=r)===c&&(t=wt,s.substr(wt,2)===ft?(r=ft,wt+=2):(r=c,0===Ct&&jt(pt)),r!==c&&(Ft=t,r=ht()),(t=r)===c&&(t=wt,s.substr(wt,2)===mt?(r=mt,wt+=2):(r=c,0===Ct&&jt(dt)),r!==c&&(Ft=t,r=gt()),(t=r)===c&&(t=wt,s.substr(wt,2)===yt?(r=yt,wt+=2):(r=c,0===Ct&&jt(vt)),t=r!==c?(n=e=wt,(e=(n=(o=$t())!==c&&(u=$t())!==c&&(a=$t())!==c&&(i=$t())!==c?o=[o,u,a,i]:(wt=n,c))!==c?s.substring(e,wt):n)!==c?(Ft=t,r=bt(e)):(wt=t,c)):(wt=t,c)))))),t}function Bt(){var t,r,e;if(t=wt,r=[],(e=zt())!==c)for(;e!==c;)r.push(e),e=zt();else r=c;return r!==c&&(Ft=t,r=At(r)),t=r}if((r=a())!==c&&wt===s.length)return r;throw r!==c&&wt<s.length&&jt({type:"end"}),e=Ot,n=_t<s.length?s.charAt(_t):null,o=_t<s.length?Mt(_t,_t+1):Mt(_t,_t),new Gt(Gt.buildMessage(e,n),e,n,o)}});function Gt(t,r,e,n){this.message=t,this.expected=r,this.found=e,this.location=n,this.name="SyntaxError","function"==typeof Error.captureStackTrace&&Error.captureStackTrace(this,Gt)}function v(){this.constructor=d}var b,A=(b=function(t,r){return(b=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,r){t.__proto__=r}||function(t,r){for(var e in r)r.hasOwnProperty(e)&&(t[e]=r[e])})(t,r)},function(t,r){function e(){this.constructor=t}b(t,r),t.prototype=null===r?Object.create(r):(e.prototype=r.prototype,new e)}),w=function(){return(w=Object.assign||function(t){for(var r,e=1,n=arguments.length;e<n;e++)for(var o in r=arguments[e])Object.prototype.hasOwnProperty.call(r,o)&&(t[o]=r[o]);return t}).apply(this,arguments)};function F(t,r){return r?w({},t,{date:function(e,n){return n?w({},e||{},n||{},Object.keys(e).reduce(function(t,r){return t[r]=w({},e[r],n[r]||{}),t},{})):e}(t.date,r.date)}):t}var x,_=(x=Error,A(O,x),O);function O(t,r){var e=x.call(this,t)||this;return e.variableId=r,e}var C=(P.prototype.resolvedOptions=function(){return{locale:this._locale}},P.defaultLocale="en",P.formats={number:{currency:{style:"currency"},percent:{style:"percent"}},date:{short:{month:"numeric",day:"numeric",year:"2-digit"},medium:{month:"short",day:"numeric",year:"numeric"},long:{month:"long",day:"numeric",year:"numeric"},full:{weekday:"long",month:"long",day:"numeric",year:"numeric"}},time:{short:{hour:"numeric",minute:"numeric"},medium:{hour:"numeric",minute:"numeric",second:"numeric"},long:{hour:"numeric",minute:"numeric",second:"numeric",timeZoneName:"short"},full:{hour:"numeric",minute:"numeric",second:"numeric",timeZoneName:"short"}}},P.__parse=y.parse,P);function P(t,r,e){var n=this;void 0===r&&(r=P.defaultLocale),this.format=function(t){try{return function t(r,e){for(var n="",o=0,u=r;o<u.length;o++){var a=u[o];if("string"!=typeof a){var i=a.id;if(!(e&&i in e))throw new _("A value must be provided for: "+i,i);var s=e[i];a.options?n+=t(a.getOption(s),e):n+=a.format(s)}else n+=a}return n}(n.pattern,t)}catch(t){throw t.variableId?new Error("The intl string context variable '"+t.variableId+"' was not provided to the string '"+n.message+"'"):t}};var o="string"==typeof t?P.__parse(t):t;if(!o||"messageFormatPattern"!==o.type)throw new TypeError("A message must be provided as a String or AST.");var u=F(P.formats,e);this._locale=function(t){"string"==typeof t&&(t=[t]);try{return Intl.NumberFormat.supportedLocalesOf(t,{localeMatcher:"best fit"})[0]}catch(t){return C.defaultLocale}}(r||[]),this.pattern=new a(r,u).compile(o),this.message=t}return C}); | ||
//# sourceMappingURL=intl-messageformat.min.js.map |
@@ -1,5 +0,3 @@ | ||
/* jshint node:true */ | ||
'use strict'; | ||
require('intl-pluralrules'); | ||
var IntlMessageFormat = require('./dist/intl-messageformat'); | ||
@@ -6,0 +4,0 @@ |
@@ -1,2 +0,20 @@ | ||
import IntlMessageFormat from './core'; | ||
export default IntlMessageFormat; | ||
import { Formats } from './compiler'; | ||
import { MessageFormatPattern } from 'intl-messageformat-parser'; | ||
export interface LocaleData { | ||
locale: string; | ||
parentLocale?: string; | ||
[k: string]: any; | ||
} | ||
export default class MessageFormat { | ||
static defaultLocale: string; | ||
static readonly formats: Formats; | ||
private _locale; | ||
private pattern; | ||
private message; | ||
constructor(message: string | MessageFormatPattern, locales?: string | string[], overrideFormats?: Partial<Formats>); | ||
static __parse: (msg: string) => MessageFormatPattern; | ||
format: (values?: Record<string, string | number | boolean | null | undefined> | undefined) => string; | ||
resolvedOptions(): { | ||
locale: string; | ||
}; | ||
} |
202
lib/index.js
@@ -1,3 +0,201 @@ | ||
import IntlMessageFormat from './core'; | ||
export default IntlMessageFormat; | ||
/* | ||
Copyright (c) 2014, Yahoo! Inc. All rights reserved. | ||
Copyrights licensed under the New BSD License. | ||
See the accompanying LICENSE file for terms. | ||
*/ | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
/* jslint esnext: true */ | ||
import Compiler, { isSelectOrPluralFormat } from './compiler'; | ||
import parser from 'intl-messageformat-parser'; | ||
function resolveLocale(locales) { | ||
if (typeof locales === 'string') { | ||
locales = [locales]; | ||
} | ||
try { | ||
return Intl.NumberFormat.supportedLocalesOf(locales, { | ||
// IE11 localeMatcher `lookup` seems to convert `en` -> `en-US` | ||
// but not other browsers, | ||
localeMatcher: 'best fit' | ||
})[0]; | ||
} | ||
catch (e) { | ||
return MessageFormat.defaultLocale; | ||
} | ||
} | ||
function formatPatterns(pattern, values) { | ||
var result = ''; | ||
for (var _i = 0, pattern_1 = pattern; _i < pattern_1.length; _i++) { | ||
var part = pattern_1[_i]; | ||
// Exist early for string parts. | ||
if (typeof part === 'string') { | ||
result += part; | ||
continue; | ||
} | ||
var id = part.id; | ||
// Enforce that all required values are provided by the caller. | ||
if (!(values && id in values)) { | ||
throw new FormatError("A value must be provided for: " + id, id); | ||
} | ||
var value = values[id]; | ||
// Recursively format plural and select parts' option — which can be a | ||
// nested pattern structure. The choosing of the option to use is | ||
// abstracted-by and delegated-to the part helper object. | ||
if (isSelectOrPluralFormat(part)) { | ||
result += formatPatterns(part.getOption(value), values); | ||
} | ||
else { | ||
result += part.format(value); | ||
} | ||
} | ||
return result; | ||
} | ||
function mergeConfig(c1, c2) { | ||
if (!c2) { | ||
return c1; | ||
} | ||
return __assign({}, (c1 || {}), (c2 || {}), Object.keys(c1).reduce(function (all, k) { | ||
all[k] = __assign({}, c1[k], (c2[k] || {})); | ||
return all; | ||
}, {})); | ||
} | ||
function mergeConfigs(defaultConfig, configs) { | ||
if (!configs) { | ||
return defaultConfig; | ||
} | ||
return __assign({}, defaultConfig, { date: mergeConfig(defaultConfig.date, configs.date) }); | ||
} | ||
var FormatError = /** @class */ (function (_super) { | ||
__extends(FormatError, _super); | ||
function FormatError(msg, variableId) { | ||
var _this = _super.call(this, msg) || this; | ||
_this.variableId = variableId; | ||
return _this; | ||
} | ||
return FormatError; | ||
}(Error)); | ||
var MessageFormat = /** @class */ (function () { | ||
function MessageFormat(message, locales, overrideFormats) { | ||
var _this = this; | ||
if (locales === void 0) { locales = MessageFormat.defaultLocale; } | ||
// "Bind" `format()` method to `this` so it can be passed by reference like | ||
// the other `Intl` APIs. | ||
this.format = function (values) { | ||
try { | ||
return formatPatterns(_this.pattern, values); | ||
} | ||
catch (e) { | ||
if (e.variableId) { | ||
throw new Error("The intl string context variable '" + e.variableId + "' was not provided to the string '" + _this.message + "'"); | ||
} | ||
else { | ||
throw e; | ||
} | ||
} | ||
}; | ||
// Parse string messages into an AST. | ||
var ast = typeof message === 'string' ? MessageFormat.__parse(message) : message; | ||
if (!(ast && ast.type === 'messageFormatPattern')) { | ||
throw new TypeError('A message must be provided as a String or AST.'); | ||
} | ||
// Creates a new object with the specified `formats` merged with the default | ||
// formats. | ||
var formats = mergeConfigs(MessageFormat.formats, overrideFormats); | ||
// Defined first because it's used to build the format pattern. | ||
this._locale = resolveLocale(locales || []); | ||
// Compile the `ast` to a pattern that is highly optimized for repeated | ||
// `format()` invocations. **Note:** This passes the `locales` set provided | ||
// to the constructor instead of just the resolved locale. | ||
this.pattern = new Compiler(locales, formats).compile(ast); | ||
this.message = message; | ||
} | ||
MessageFormat.prototype.resolvedOptions = function () { | ||
return { locale: this._locale }; | ||
}; | ||
MessageFormat.defaultLocale = 'en'; | ||
// Default format options used as the prototype of the `formats` provided to the | ||
// constructor. These are used when constructing the internal Intl.NumberFormat | ||
// and Intl.DateTimeFormat instances. | ||
MessageFormat.formats = { | ||
number: { | ||
currency: { | ||
style: 'currency' | ||
}, | ||
percent: { | ||
style: 'percent' | ||
} | ||
}, | ||
date: { | ||
short: { | ||
month: 'numeric', | ||
day: 'numeric', | ||
year: '2-digit' | ||
}, | ||
medium: { | ||
month: 'short', | ||
day: 'numeric', | ||
year: 'numeric' | ||
}, | ||
long: { | ||
month: 'long', | ||
day: 'numeric', | ||
year: 'numeric' | ||
}, | ||
full: { | ||
weekday: 'long', | ||
month: 'long', | ||
day: 'numeric', | ||
year: 'numeric' | ||
} | ||
}, | ||
time: { | ||
short: { | ||
hour: 'numeric', | ||
minute: 'numeric' | ||
}, | ||
medium: { | ||
hour: 'numeric', | ||
minute: 'numeric', | ||
second: 'numeric' | ||
}, | ||
long: { | ||
hour: 'numeric', | ||
minute: 'numeric', | ||
second: 'numeric', | ||
timeZoneName: 'short' | ||
}, | ||
full: { | ||
hour: 'numeric', | ||
minute: 'numeric', | ||
second: 'numeric', | ||
timeZoneName: 'short' | ||
} | ||
} | ||
}; | ||
MessageFormat.__parse = parser.parse; | ||
return MessageFormat; | ||
}()); | ||
export default MessageFormat; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "intl-messageformat", | ||
"version": "3.1.1", | ||
"version": "3.1.2", | ||
"description": "Formats ICU Message strings with number, date, plural, and select placeholders to create localized messages.", | ||
@@ -34,9 +34,10 @@ "keywords": [ | ||
"dependencies": { | ||
"intl-messageformat-parser": "1.6.2" | ||
"intl-messageformat-parser": "1.6.2", | ||
"intl-pluralrules": "^1.0.1" | ||
}, | ||
"scripts": { | ||
"benchmark": "ts-node --project tsconfig.all.json tests/benchmark", | ||
"build": "npm run clean && tsc && npm run prettier && npm run rollup", | ||
"benchmark": "ts-node --project tests/tsconfig.json tests/benchmark", | ||
"build": "npm run clean && tsc -p src && npm run prettier && npm run rollup", | ||
"rollup": "rollup -c rollup.config.js", | ||
"clean": "rimraf dist lib", | ||
"clean": "rimraf dist lib tests/*.js tests/*.d.ts", | ||
"cover": "nyc npm t", | ||
@@ -46,3 +47,4 @@ "prepublishOnly": "npm run build", | ||
"test": "TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\"}' mocha -r ts-node/register -r source-map-support/register -r chai/register-expect tests/index.ts --reporter spec", | ||
"travis": "npm run build && npm run cover" | ||
"karma:local": "karma start karma.conf.js", | ||
"karma:ci": "karma start karma.conf-ci.js" | ||
}, | ||
@@ -58,3 +60,3 @@ "homepage": "https://github.com/formatjs/intl-messageformat", | ||
}, | ||
"gitHead": "318ed7c11b9d4d14a9df38cc9ee10c1ad55a2838" | ||
"gitHead": "45cf9119c119c26a0d0b939560d3fd9af6b4ef37" | ||
} |
@@ -7,9 +7,6 @@ Intl MessageFormat | ||
[![npm Version][npm-badge]][npm] | ||
[![Build Status][travis-badge]][travis] | ||
[![Dependency Status][david-badge]][david] | ||
![gzip size](https://img.badgesize.io/formatjs/intl-messageformat/master/dist/intl-messageformat.min.js?compression=gzip&label=core+gzip+size) | ||
[![Sauce Test Status][sauce-badge]][sauce] | ||
[![Sauce Test Status](https://saucelabs.com/browser-matrix/formatjsproject.svg)](https://saucelabs.com/u/formatjsproject) | ||
@@ -221,10 +218,4 @@ Overview | ||
[npm-badge]: https://img.shields.io/npm/v/intl-messageformat.svg?style=flat-square | ||
[david]: https://david-dm.org/formatjs/intl-messageformat | ||
[david-badge]: https://img.shields.io/david/formatjs/intl-messageformat.svg?style=flat-square | ||
[travis]: https://travis-ci.org/formatjs/intl-messageformat | ||
[travis-badge]: https://img.shields.io/travis/formatjs/intl-messageformat/master.svg?style=flat-square | ||
[sauce]: https://saucelabs.com/u/intl-messageformat | ||
[sauce-badge]: https://saucelabs.com/browser-matrix/intl-messageformat.svg | ||
[strawman]: http://wiki.ecmascript.org/doku.php?id=globalization:messageformatting | ||
[parser]: https://github.com/formatjs/intl-messageformat-parser | ||
[parser]: https://github.com/formatjs/formatjs/blob/master/packages/intl-messageformat-parser | ||
[ICU]: http://userguide.icu-project.org/formatparse/messages | ||
@@ -239,3 +230,3 @@ [CLDR]: http://cldr.unicode.org/ | ||
[semver]: http://semver.org/ | ||
[LICENSE]: https://github.com/formatjs/intl-messageformat/blob/master/LICENSE | ||
[intl-pluralrules]: https://github.com/eemeli/intl-pluralrules | ||
[LICENSE]: https://github.com/formatjs/formatjs/blob/master/LICENSE | ||
[intl-pluralrules]: https://github.com/eemeli/intl-pluralrules |
234
src/index.ts
@@ -1,2 +0,232 @@ | ||
import IntlMessageFormat from './core'; | ||
export default IntlMessageFormat; | ||
/* | ||
Copyright (c) 2014, Yahoo! Inc. All rights reserved. | ||
Copyrights licensed under the New BSD License. | ||
See the accompanying LICENSE file for terms. | ||
*/ | ||
/* jslint esnext: true */ | ||
import Compiler, { Formats, isSelectOrPluralFormat, Pattern } from './compiler'; | ||
import parser, { MessageFormatPattern } from 'intl-messageformat-parser'; | ||
// -- MessageFormat -------------------------------------------------------- | ||
export interface LocaleData { | ||
locale: string; | ||
parentLocale?: string; | ||
[k: string]: any; | ||
} | ||
function resolveLocale(locales: string | string[]): string { | ||
if (typeof locales === 'string') { | ||
locales = [locales]; | ||
} | ||
try { | ||
return Intl.NumberFormat.supportedLocalesOf(locales, { | ||
// IE11 localeMatcher `lookup` seems to convert `en` -> `en-US` | ||
// but not other browsers, | ||
localeMatcher: 'best fit' | ||
})[0]; | ||
} catch (e) { | ||
return MessageFormat.defaultLocale; | ||
} | ||
} | ||
function formatPatterns( | ||
pattern: Pattern[], | ||
values?: Record<string, string | number | boolean | null | undefined> | ||
) { | ||
let result = ''; | ||
for (const part of pattern) { | ||
// Exist early for string parts. | ||
if (typeof part === 'string') { | ||
result += part; | ||
continue; | ||
} | ||
const { id } = part; | ||
// Enforce that all required values are provided by the caller. | ||
if (!(values && id in values)) { | ||
throw new FormatError(`A value must be provided for: ${id}`, id); | ||
} | ||
const value = values[id]; | ||
// Recursively format plural and select parts' option — which can be a | ||
// nested pattern structure. The choosing of the option to use is | ||
// abstracted-by and delegated-to the part helper object. | ||
if (isSelectOrPluralFormat(part)) { | ||
result += formatPatterns(part.getOption(value as any), values); | ||
} else { | ||
result += part.format(value as any); | ||
} | ||
} | ||
return result; | ||
} | ||
function mergeConfig(c1: Record<string, object>, c2?: Record<string, object>) { | ||
if (!c2) { | ||
return c1; | ||
} | ||
return { | ||
...(c1 || {}), | ||
...(c2 || {}), | ||
...Object.keys(c1).reduce((all: Record<string, object>, k) => { | ||
all[k] = { | ||
...c1[k], | ||
...(c2[k] || {}) | ||
}; | ||
return all; | ||
}, {}) | ||
}; | ||
} | ||
function mergeConfigs( | ||
defaultConfig: Formats, | ||
configs?: Partial<Formats> | ||
): Formats { | ||
if (!configs) { | ||
return defaultConfig; | ||
} | ||
return { | ||
...defaultConfig, | ||
date: mergeConfig(defaultConfig.date, configs.date) | ||
}; | ||
} | ||
class FormatError extends Error { | ||
public readonly variableId?: string; | ||
constructor(msg?: string, variableId?: string) { | ||
super(msg); | ||
this.variableId = variableId; | ||
} | ||
} | ||
export default class MessageFormat { | ||
public static defaultLocale: string = 'en'; | ||
// Default format options used as the prototype of the `formats` provided to the | ||
// constructor. These are used when constructing the internal Intl.NumberFormat | ||
// and Intl.DateTimeFormat instances. | ||
public static readonly formats: Formats = { | ||
number: { | ||
currency: { | ||
style: 'currency' | ||
}, | ||
percent: { | ||
style: 'percent' | ||
} | ||
}, | ||
date: { | ||
short: { | ||
month: 'numeric', | ||
day: 'numeric', | ||
year: '2-digit' | ||
}, | ||
medium: { | ||
month: 'short', | ||
day: 'numeric', | ||
year: 'numeric' | ||
}, | ||
long: { | ||
month: 'long', | ||
day: 'numeric', | ||
year: 'numeric' | ||
}, | ||
full: { | ||
weekday: 'long', | ||
month: 'long', | ||
day: 'numeric', | ||
year: 'numeric' | ||
} | ||
}, | ||
time: { | ||
short: { | ||
hour: 'numeric', | ||
minute: 'numeric' | ||
}, | ||
medium: { | ||
hour: 'numeric', | ||
minute: 'numeric', | ||
second: 'numeric' | ||
}, | ||
long: { | ||
hour: 'numeric', | ||
minute: 'numeric', | ||
second: 'numeric', | ||
timeZoneName: 'short' | ||
}, | ||
full: { | ||
hour: 'numeric', | ||
minute: 'numeric', | ||
second: 'numeric', | ||
timeZoneName: 'short' | ||
} | ||
} | ||
}; | ||
private _locale: string; | ||
private pattern: Pattern[]; | ||
private message: string | MessageFormatPattern; | ||
constructor( | ||
message: string | MessageFormatPattern, | ||
locales: string | string[] = MessageFormat.defaultLocale, | ||
overrideFormats?: Partial<Formats> | ||
) { | ||
// Parse string messages into an AST. | ||
const ast = | ||
typeof message === 'string' ? MessageFormat.__parse(message) : message; | ||
if (!(ast && ast.type === 'messageFormatPattern')) { | ||
throw new TypeError('A message must be provided as a String or AST.'); | ||
} | ||
// Creates a new object with the specified `formats` merged with the default | ||
// formats. | ||
const formats = mergeConfigs(MessageFormat.formats, overrideFormats); | ||
// Defined first because it's used to build the format pattern. | ||
this._locale = resolveLocale(locales || []); | ||
// Compile the `ast` to a pattern that is highly optimized for repeated | ||
// `format()` invocations. **Note:** This passes the `locales` set provided | ||
// to the constructor instead of just the resolved locale. | ||
this.pattern = new Compiler(locales, formats).compile(ast); | ||
this.message = message; | ||
} | ||
public static __parse = parser.parse; | ||
// "Bind" `format()` method to `this` so it can be passed by reference like | ||
// the other `Intl` APIs. | ||
format = ( | ||
values?: Record<string, string | number | boolean | null | undefined> | ||
) => { | ||
try { | ||
return formatPatterns(this.pattern, values); | ||
} catch (e) { | ||
if (e.variableId) { | ||
throw new Error( | ||
`The intl string context variable '${ | ||
e.variableId | ||
}' was not provided to the string '${this.message}'` | ||
); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
}; | ||
resolvedOptions() { | ||
return { locale: this._locale }; | ||
} | ||
} |
@@ -7,8 +7,6 @@ { | ||
"lib": ["es6", "dom", "es2018.intl"], | ||
"outDir": "lib", | ||
"declaration": true, | ||
"strict": true, | ||
"sourceMap": true | ||
}, | ||
"include": ["src/*.ts"] | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
411500
2777
2
22
230
+ Addedintl-pluralrules@^1.0.1
+ Addedintl-pluralrules@1.3.1(transitive)