| Index: trunk/phase3/includes/OutputPage.php |
| — | — | @@ -2446,7 +2446,6 @@ |
| 2447 | 2447 | 'mediawiki.util', |
| 2448 | 2448 | 'mediawiki.page.startup', |
| 2449 | 2449 | 'mediawiki.page.ready', |
| 2450 | | - 'mediawiki.jqueryMsg', |
| 2451 | 2450 | ) ); |
| 2452 | 2451 | if ( $wgIncludeLegacyJavaScript ){ |
| 2453 | 2452 | $this->addModules( 'mediawiki.legacy.wikibits' ); |
| Index: trunk/phase3/resources/mediawiki/mediawiki.jqueryMsg.js |
| — | — | @@ -661,5 +661,22 @@ |
| 662 | 662 | window.gM = mw.jqueryMsg.getMessageFunction(); |
| 663 | 663 | |
| 664 | 664 | $.fn.msg = mw.jqueryMsg.getPlugin(); |
| | 665 | + |
| | 666 | + // Replace the default message parser with jqueryMsg |
| | 667 | + var oldParser = mw.Message.prototype.parser; |
| | 668 | + mw.Message.prototype.parser = function() { |
| | 669 | + // TODO: should we cache the message function so we don't create a new one every time? Benchmark this maybe? |
| | 670 | + // Caching is somewhat problematic, because we do need different message functions for different maps, so |
| | 671 | + // we'd have to cache the parser as a member of this.map, which sounds a bit ugly. |
| | 672 | + |
| | 673 | + // Do not use mw.jqueryMsg unless required |
| | 674 | + if ( this.map.get( this.key ).indexOf( '{{' ) < 0 ) { |
| | 675 | + // Fall back to mw.msg's simple parser |
| | 676 | + return oldParser( this.key, this.parameters ); |
| | 677 | + } |
| | 678 | + |
| | 679 | + var messageFunction = mw.jqueryMsg.getMessageFunction( { 'messages': this.map } ); |
| | 680 | + return messageFunction( this.key, this.parameters ); |
| | 681 | + }; |
| 665 | 682 | |
| 666 | 683 | } )( mediaWiki, jQuery ); |
| Index: trunk/phase3/resources/mediawiki/mediawiki.js |
| — | — | @@ -8,7 +8,6 @@ |
| 9 | 9 | /* Private Members */ |
| 10 | 10 | |
| 11 | 11 | var hasOwn = Object.prototype.hasOwnProperty; |
| 12 | | - var parser; |
| 13 | 12 | /* Object constructors */ |
| 14 | 13 | |
| 15 | 14 | /** |
| — | — | @@ -125,13 +124,26 @@ |
| 126 | 125 | this.format = 'plain'; |
| 127 | 126 | this.map = map; |
| 128 | 127 | this.key = key; |
| 129 | | - parser = parser || mw.jqueryMsg.getMessageFunction( ); |
| 130 | 128 | this.parameters = parameters === undefined ? [] : $.makeArray( parameters ); |
| 131 | 129 | return this; |
| 132 | 130 | } |
| 133 | 131 | |
| 134 | 132 | Message.prototype = { |
| 135 | 133 | /** |
| | 134 | + * Simple message parser, does $N replacement and nothing else. |
| | 135 | + * This may be overridden to provide a more complex message parser. |
| | 136 | + * |
| | 137 | + * This function will not be called for nonexistent messages. |
| | 138 | + */ |
| | 139 | + parser: function() { |
| | 140 | + var parameters = this.parameters; |
| | 141 | + return this.map.get( this.key ).replace( /\$(\d+)/g, function ( str, match ) { |
| | 142 | + var index = parseInt( match, 10 ) - 1; |
| | 143 | + return parameters[index] !== undefined ? parameters[index] : '$' + match; |
| | 144 | + } ); |
| | 145 | + }, |
| | 146 | + |
| | 147 | + /** |
| 136 | 148 | * Appends (does not replace) parameters for replacement to the .parameters property. |
| 137 | 149 | * |
| 138 | 150 | * @param parameters Array |
| — | — | @@ -159,29 +171,24 @@ |
| 160 | 172 | } |
| 161 | 173 | return '<' + this.key + '>'; |
| 162 | 174 | } |
| 163 | | - var text = this.map.get( this.key ), |
| 164 | | - parameters = this.parameters; |
| 165 | 175 | |
| | 176 | + var text; |
| 166 | 177 | if ( this.format === 'plain' ) { |
| 167 | | - // Do not use parser unless required. |
| 168 | | - if ( text.indexOf( '{{' ) < 0 ) { |
| 169 | | - text = text.replace( /\$(\d+)/g, function ( str, match ) { |
| 170 | | - var index = parseInt( match, 10 ) - 1; |
| 171 | | - return parameters[index] !== undefined ? parameters[index] : '$' + match; |
| 172 | | - } ); |
| 173 | | - } |
| 174 | | - else{ |
| 175 | | - text = parser( this.key, this.parameters ); |
| 176 | | - } |
| | 178 | + // FIXME this is wrong. There should be a way |
| | 179 | + // to tell parser() whether we're looking for |
| | 180 | + // plain text or HTML, but I don't know jQueryMsg |
| | 181 | + // well enough to implement this. |
| | 182 | + // Currently it always outputs HTML |
| | 183 | + text = this.parser(); |
| 177 | 184 | } |
| 178 | 185 | |
| 179 | 186 | if ( this.format === 'escaped' ) { |
| 180 | | - text = parser( this.key, this.parameters ); |
| | 187 | + text = this.parser(); |
| 181 | 188 | text = mw.html.escape( text ); |
| 182 | 189 | } |
| 183 | 190 | |
| 184 | 191 | if ( this.format === 'parse' ) { |
| 185 | | - text = parser( this.key, this.parameters ); |
| | 192 | + text = this.parser(); |
| 186 | 193 | } |
| 187 | 194 | |
| 188 | 195 | return text; |
| — | — | @@ -240,6 +247,11 @@ |
| 241 | 248 | * @var constructor Make the Map constructor publicly available. |
| 242 | 249 | */ |
| 243 | 250 | Map: Map, |
| | 251 | + |
| | 252 | + /** |
| | 253 | + * @var constructor Make the Message constructor publicly available. |
| | 254 | + */ |
| | 255 | + Message: Message, |
| 244 | 256 | |
| 245 | 257 | /** |
| 246 | 258 | * List of configuration values |