Merge "Export mw.Message's string formatter as mw.format"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 8 Jan 2015 23:37:53 +0000 (23:37 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 8 Jan 2015 23:37:53 +0000 (23:37 +0000)
1  2 
resources/src/mediawiki/mediawiki.js

                }
        }
  
+       // String format helper. Replaces $1, $2 .. $N placeholders with positional
+       // args. Used by Message.prototype.parser() and exported as mw.format().
+       function format( formatString ) {
+               var parameters = slice.call( arguments, 1 );
+               return formatString.replace( /\$(\d+)/g, function ( str, match ) {
+                       var index = parseInt( match, 10 ) - 1;
+                       return parameters[index] !== undefined ? parameters[index] : '$' + match;
+               } );
+       }
        /* Object constructors */
  
        /**
                 * This function will not be called for nonexistent messages.
                 */
                parser: function () {
-                       var parameters = this.parameters;
-                       return this.map.get( this.key ).replace( /\$(\d+)/g, function ( str, match ) {
-                               var index = parseInt( match, 10 ) - 1;
-                               return parameters[index] !== undefined ? parameters[index] : '$' + match;
-                       } );
+                       return format.apply( null, [ this.map.get( this.key ) ].concat( this.parameters ) );
                },
  
                /**
                                function () { return +new Date(); };
                }() ),
  
+               /**
+                * Format a string. Replace $1, $2 ... $N with positional arguments.
+                *
+                * @method
+                * @since 1.25
+                * @param {string} fmt Format string
+                * @param {Mixed...} parameters Substitutions for $N placeholders.
+                * @return {string} Formatted string
+                */
+               format: format,
                /**
                 * Track an analytic event.
                 *
                 *
                 * @property {mw.Map} config
                 */
 -              // Dummy placeholder. Re-assigned in ResourceLoaderStartupModule to an instance of `mw.Map`.
 +              // Dummy placeholder. Re-assigned in ResourceLoaderStartUpModule to an instance of `mw.Map`.
                config: null,
  
                /**