r108230 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108229‎ | r108230 | r108231 >
Date:14:11, 6 January 2012
Author:catrope
Status:ok
Tags:i18nreview, todo 
Comment:
Fix up r108203: just loading mw.jqueryMsg in the bottom queue, then assuming its presence in mw.Message doesn't work, see CR comments.

* Moved message parsing (including $1 replacement) to Message.prototype.parser(), and let jqueryMsg override that when loaded
** Make the Message constructor public to make this possible
** Moved logic for skipping jqueryMsg when the message is simple from mw.Message to mw.jqueryMsg, where it belongs
* Remove mw.jqueryMsg from the default modules list in OutputPage. Modules that require PLURAL/GENDER should depend on mw.jqueryMsg
* TODOs
** The jqueryMsg parser is recreated for every mw.msg() call. It should probably be cached, but the only way I can think of is to add it as a member of the Map object, which is kind of weird
** Because jqueryMsg doesn't support a 'text' mode that expands PLURAL/GENDER but doesn't output HTML (leaves e.g. links alone), mw.Message.plain() and mw.Message.parse() currently behave identically. This is wrong and should be fixed, but that needs support in jqueryMsg too
Modified paths:
  • /trunk/phase3/includes/OutputPage.php (modified) (history)
  • /trunk/phase3/resources/mediawiki/mediawiki.jqueryMsg.js (modified) (history)
  • /trunk/phase3/resources/mediawiki/mediawiki.js (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/OutputPage.php
@@ -2446,7 +2446,6 @@
24472447 'mediawiki.util',
24482448 'mediawiki.page.startup',
24492449 'mediawiki.page.ready',
2450 - 'mediawiki.jqueryMsg',
24512450 ) );
24522451 if ( $wgIncludeLegacyJavaScript ){
24532452 $this->addModules( 'mediawiki.legacy.wikibits' );
Index: trunk/phase3/resources/mediawiki/mediawiki.jqueryMsg.js
@@ -661,5 +661,22 @@
662662 window.gM = mw.jqueryMsg.getMessageFunction();
663663
664664 $.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+ };
665682
666683 } )( mediaWiki, jQuery );
Index: trunk/phase3/resources/mediawiki/mediawiki.js
@@ -8,7 +8,6 @@
99 /* Private Members */
1010
1111 var hasOwn = Object.prototype.hasOwnProperty;
12 - var parser;
1312 /* Object constructors */
1413
1514 /**
@@ -125,13 +124,26 @@
126125 this.format = 'plain';
127126 this.map = map;
128127 this.key = key;
129 - parser = parser || mw.jqueryMsg.getMessageFunction( );
130128 this.parameters = parameters === undefined ? [] : $.makeArray( parameters );
131129 return this;
132130 }
133131
134132 Message.prototype = {
135133 /**
 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+ /**
136148 * Appends (does not replace) parameters for replacement to the .parameters property.
137149 *
138150 * @param parameters Array
@@ -159,29 +171,24 @@
160172 }
161173 return '<' + this.key + '>';
162174 }
163 - var text = this.map.get( this.key ),
164 - parameters = this.parameters;
165175
 176+ var text;
166177 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();
177184 }
178185
179186 if ( this.format === 'escaped' ) {
180 - text = parser( this.key, this.parameters );
 187+ text = this.parser();
181188 text = mw.html.escape( text );
182189 }
183190
184191 if ( this.format === 'parse' ) {
185 - text = parser( this.key, this.parameters );
 192+ text = this.parser();
186193 }
187194
188195 return text;
@@ -240,6 +247,11 @@
241248 * @var constructor Make the Map constructor publicly available.
242249 */
243250 Map: Map,
 251+
 252+ /**
 253+ * @var constructor Make the Message constructor publicly available.
 254+ */
 255+ Message: Message,
244256
245257 /**
246258 * List of configuration values

Follow-up revisions

RevisionCommit summaryAuthorDate
r108231Fix broken oldParser call in r108230catrope14:17, 6 January 2012
r108236Followup r108231, remove useless _thiscatrope15:14, 6 January 2012
r108259release-notes: remove notes for r108203; adding new ones for r108230krinkle17:35, 6 January 2012
r109585[mediawiki.js] edit comment...krinkle23:52, 19 January 2012

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r108203Use mw.jqueryMsg parser for message parsing to support PLURAL and GENDER...santhosh09:14, 6 January 2012

Status & tagging log