From: Ori Livneh Date: Tue, 6 Jan 2015 18:23:41 +0000 (-0800) Subject: Export mw.Message's string formatter as mw.format X-Git-Tag: 1.31.0-rc.0~12745^2 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/banques/?a=commitdiff_plain;h=ec285f4420700fd47dfb8ef8b5b03694cb773c77;p=lhc%2Fweb%2Fwiklou.git Export mw.Message's string formatter as mw.format For very simple string formatting, make mw.Message's `$N` substring interpolation functionality available publicly as mw.format. Change-Id: I87f62ac478aab80994caf79d3c89e1215cf088d4 --- diff --git a/resources/src/mediawiki/mediawiki.js b/resources/src/mediawiki/mediawiki.js index 6bf93f49ac..3d5c19c5b7 100644 --- a/resources/src/mediawiki/mediawiki.js +++ b/resources/src/mediawiki/mediawiki.js @@ -43,6 +43,16 @@ } } + // 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 */ /** @@ -297,11 +307,7 @@ * 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 ) ); }, /** @@ -435,6 +441,17 @@ 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. *