/* 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;
}
* @return string Message as a string in the current form or <key> if key does not exist.
*/
toString: function() {
- if ( !this.map.exists( this.key ) ) {
+ if ( !this.exists( ) ) {
// Use <key> as text if key does not exist
if ( this.format !== 'plain' ) {
// format 'escape' and 'parse' need to have the brackets and key html escaped
var text = this.map.get( this.key ),
parameters = this.parameters;
- text = text.replace( /\$(\d+)/g, function ( str, match ) {
- var index = parseInt( match, 10 ) - 1;
- return parameters[index] !== undefined ? parameters[index] : '$' + match;
- } );
-
if ( this.format === 'plain' ) {
- return text;
+ // 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 );
+ }
}
+
if ( this.format === 'escaped' ) {
- // According to Message.php this needs {{-transformation, which is
- // still todo
- return mw.html.escape( text );
+ text = parser( this.key, this.parameters );
+ text = mw.html.escape( text );
}
-
- /* This should be fixed up when we have a parser
- if ( this.format === 'parse' && 'language' in mw ) {
- text = mw.language.parse( text );
+
+ if ( this.format === 'parse' ) {
+ text = parser( this.key, this.parameters );
}
- */
+
return text;
},
});
test( 'mw.message & mw.messages', function() {
- expect(17);
+ expect(20);
ok( mw.messages, 'messages defined' );
ok( mw.messages instanceof mw.Map, 'mw.messages instance of mw.Map' );
equal( goodbye.plain(), '<goodbye>', 'Message.toString returns plain <key> if format is "plain" and key does not exist' );
// bug 30684
equal( goodbye.escaped(), '<goodbye>', 'Message.toString returns properly escaped <key> if format is "escaped" and key does not exist' );
+
+ ok( mw.messages.set( 'pluraltestmsg', 'There {{PLURAL:$1|is|are}} $1 {{PLURAL:$1|result|results}}' ), 'mw.messages.set: Register' );
+ var pluralMessage = mw.message( 'pluraltestmsg' , 6 );
+ equal( pluralMessage.plain(), 'There are 6 results', 'plural get resolved when format is plain' );
+ equal( pluralMessage.parse(), 'There are 6 results', 'plural get resolved when format is parse' );
+
});
test( 'mw.msg', function() {
- expect(3);
+ expect(11);
ok( mw.messages.set( 'hello', 'Hello <b>awesome</b> world' ), 'mw.messages.set: Register' );
-
equal( mw.msg( 'hello' ), 'Hello <b>awesome</b> world', 'Gets message with default options (existing message)' );
equal( mw.msg( 'goodbye' ), '<goodbye>', 'Gets message with default options (nonexistent message)' );
+
+ ok( mw.messages.set( 'plural-item' , 'Found $1 {{PLURAL:$1|item|items}}' ) );
+ equal( mw.msg( 'plural-item', 5 ), 'Found 5 items', 'Apply plural for count 5' );
+ equal( mw.msg( 'plural-item', 0 ), 'Found 0 items', 'Apply plural for count 0' );
+ equal( mw.msg( 'plural-item', 1 ), 'Found 1 item', 'Apply plural for count 1' );
+
+ ok( mw.messages.set('gender-plural-msg' , '{{GENDER:$1|he|she|they}} {{PLURAL:$2|is|are}} awesome' ) );
+ equal( mw.msg( 'gender-plural-msg', 'male', 1 ), 'he is awesome', 'Gender test for male, plural count 1' );
+ equal( mw.msg( 'gender-plural-msg', 'female', '1' ), 'she is awesome', 'Gender test for female, plural count 1' );
+ equal( mw.msg( 'gender-plural-msg', 'unknown', 10 ), 'they are awesome', 'Gender test for neutral, plural count 10' );
+
});
test( 'mw.loader', function() {