window.gM = mw.jqueryMsg.getMessageFunction();
$.fn.msg = mw.jqueryMsg.getPlugin();
+
+ // Replace the default message parser with jqueryMsg
+ var oldParser = mw.Message.prototype.parser;
+ mw.Message.prototype.parser = function() {
+ // TODO: should we cache the message function so we don't create a new one every time? Benchmark this maybe?
+ // Caching is somewhat problematic, because we do need different message functions for different maps, so
+ // we'd have to cache the parser as a member of this.map, which sounds a bit ugly.
+
+ // Do not use mw.jqueryMsg unless required
+ if ( this.map.get( this.key ).indexOf( '{{' ) < 0 ) {
+ // Fall back to mw.msg's simple parser
+ return oldParser( this.key, this.parameters );
+ }
+
+ var messageFunction = mw.jqueryMsg.getMessageFunction( { 'messages': this.map } );
+ return messageFunction( this.key, this.parameters );
+ };
} )( mediaWiki, jQuery );
/* Private Members */
var hasOwn = Object.prototype.hasOwnProperty;
- var parser;
/* Object constructors */
/**
this.format = 'plain';
this.map = map;
this.key = key;
- parser = parser || mw.jqueryMsg.getMessageFunction( );
this.parameters = parameters === undefined ? [] : $.makeArray( parameters );
return this;
}
Message.prototype = {
+ /**
+ * Simple message parser, does $N replacement and nothing else.
+ * This may be overridden to provide a more complex message parser.
+ *
+ * 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;
+ } );
+ },
+
/**
* Appends (does not replace) parameters for replacement to the .parameters property.
*
}
return '<' + this.key + '>';
}
- var text = this.map.get( this.key ),
- parameters = this.parameters;
+ var text;
if ( this.format === 'plain' ) {
- // Do not use parser unless required.
- if ( text.indexOf( '{{' ) < 0 ) {
- text = text.replace( /\$(\d+)/g, function ( str, match ) {
- var index = parseInt( match, 10 ) - 1;
- return parameters[index] !== undefined ? parameters[index] : '$' + match;
- } );
- }
- else{
- text = parser( this.key, this.parameters );
- }
+ // FIXME this is wrong. There should be a way
+ // to tell parser() whether we're looking for
+ // plain text or HTML, but I don't know jQueryMsg
+ // well enough to implement this.
+ // Currently it always outputs HTML
+ text = this.parser();
}
if ( this.format === 'escaped' ) {
- text = parser( this.key, this.parameters );
+ text = this.parser();
text = mw.html.escape( text );
}
if ( this.format === 'parse' ) {
- text = parser( this.key, this.parameters );
+ text = this.parser();
}
return text;
* @var constructor Make the Map constructor publicly available.
*/
Map: Map,
+
+ /**
+ * @var constructor Make the Message constructor publicly available.
+ */
+ Message: Message,
/**
* List of configuration values