@@ -119,7 +119,7 @@ var tokenRegex = /%\{(.*?)\}/g;
119119//
120120// You should pass in a third argument, the locale, to specify the correct plural type.
121121// It defaults to `'en'` with 2 plural forms.
122- function transformPhrase ( phrase , substitutions , locale ) {
122+ function transformPhrase ( phrase , substitutions , locale , numberFormat ) {
123123 if ( typeof phrase !== 'string' ) {
124124 throw new TypeError ( 'Polyglot.transformPhrase expects argument #1 to be string' ) ;
125125 }
@@ -144,8 +144,14 @@ function transformPhrase(phrase, substitutions, locale) {
144144 // Interpolate: Creates a `RegExp` object for each interpolation placeholder.
145145 result = result . replace ( tokenRegex , function ( expression , argument ) {
146146 if ( ! has ( options , argument ) ) { return '' ; }
147+
148+ var replacement = options [ argument ] ;
149+ if ( typeof replacement === 'number' ) {
150+ replacement = numberFormat . format ( replacement ) ;
151+ }
152+
147153 // Ensure replacement value is escaped to prevent special $-prefixed regex replace tokens.
148- return replace . call ( options [ argument ] , dollarRegex , dollarBillsYall ) ;
154+ return replace . call ( replacement , dollarRegex , dollarBillsYall ) ;
149155 } ) ;
150156
151157 return result ;
@@ -157,6 +163,12 @@ function Polyglot(options) {
157163 this . phrases = { } ;
158164 this . extend ( opts . phrases || { } ) ;
159165 this . currentLocale = opts . locale || 'en' ;
166+ if ( typeof Intl === 'object' ) {
167+ this . numberFormat = new Intl . NumberFormat ( this . currentLocale ) ;
168+ } else {
169+ // Fallback for IE<11
170+ this . numberFormat = { format : function ( n ) { return String ( n ) ; } } ;
171+ }
160172 this . allowMissing = ! ! opts . allowMissing ;
161173 this . warn = opts . warn || warn ;
162174}
@@ -165,7 +177,15 @@ function Polyglot(options) {
165177//
166178// Get or set locale. Internally, Polyglot only uses locale for pluralization.
167179Polyglot . prototype . locale = function ( newLocale ) {
168- if ( newLocale ) this . currentLocale = newLocale ;
180+ if ( newLocale ) {
181+ this . currentLocale = newLocale ;
182+ if ( typeof Intl === 'object' ) {
183+ this . numberFormat = new Intl . NumberFormat ( this . currentLocale ) ;
184+ } else {
185+ // Fallback for IE<11
186+ this . numberFormat = { format : function ( n ) { return String ( n ) ; } } ;
187+ }
188+ }
169189 return this . currentLocale ;
170190} ;
171191
@@ -314,7 +334,7 @@ Polyglot.prototype.t = function (key, options) {
314334 result = key ;
315335 }
316336 if ( typeof phrase === 'string' ) {
317- result = transformPhrase ( phrase , opts , this . currentLocale ) ;
337+ result = transformPhrase ( phrase , opts , this . currentLocale , this . numberFormat ) ;
318338 }
319339 return result ;
320340} ;
0 commit comments