From 7a071abfb987c2ddb8d3776e362cbb894261757d Mon Sep 17 00:00:00 2001 From: Santhosh Thottingal Date: Fri, 6 Jan 2012 09:14:45 +0000 Subject: [PATCH] Use mw.jqueryMsg parser for message parsing to support PLURAL and GENDER Follow up r107556 and based on the discussions on wikitech-l about this. mediawiki.jqueryMsg is now loaded always. mw.msg uses the parser if required. Add qunit test cases. --- RELEASE-NOTES-1.19 | 1 + includes/OutputPage.php | 1 + resources/mediawiki/mediawiki.js | 36 ++++++++++--------- .../resources/mediawiki/mediawiki.test.js | 22 ++++++++++-- 4 files changed, 41 insertions(+), 19 deletions(-) diff --git a/RELEASE-NOTES-1.19 b/RELEASE-NOTES-1.19 index e073ce5f15..3db8153a70 100644 --- a/RELEASE-NOTES-1.19 +++ b/RELEASE-NOTES-1.19 @@ -118,6 +118,7 @@ production. loaded in some contexts. * (bug 33456) Show $wgQueryCacheLimit on cached query pages. * (bug 10574) Add an option to allow all pages to be exported by Special:Export. +* Use mw.jqueryMsg parser for message parsing to support PLURAL and GENDER === Bug fixes in 1.19 === * $wgUploadNavigationUrl should be used for file redlinks if. diff --git a/includes/OutputPage.php b/includes/OutputPage.php index ee1ee4f8ef..b57284014e 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -2446,6 +2446,7 @@ $templates 'mediawiki.util', 'mediawiki.page.startup', 'mediawiki.page.ready', + 'mediawiki.jqueryMsg', ) ); if ( $wgIncludeLegacyJavaScript ){ $this->addModules( 'mediawiki.legacy.wikibits' ); diff --git a/resources/mediawiki/mediawiki.js b/resources/mediawiki/mediawiki.js index 5eed8b8eec..a3faf92725 100644 --- a/resources/mediawiki/mediawiki.js +++ b/resources/mediawiki/mediawiki.js @@ -8,7 +8,7 @@ var mw = ( function ( $, undefined ) { /* Private Members */ var hasOwn = Object.prototype.hasOwnProperty; - + var parser; /* Object constructors */ /** @@ -125,6 +125,7 @@ var mw = ( function ( $, undefined ) { this.format = 'plain'; this.map = map; this.key = key; + parser = parser || mw.jqueryMsg.getMessageFunction( ); this.parameters = parameters === undefined ? [] : $.makeArray( parameters ); return this; } @@ -150,7 +151,7 @@ var mw = ( function ( $, undefined ) { * @return string Message as a string in the current form or if key does not exist. */ toString: function() { - if ( !this.map.exists( this.key ) ) { + if ( !this.exists( ) ) { // Use as text if key does not exist if ( this.format !== 'plain' ) { // format 'escape' and 'parse' need to have the brackets and key html escaped @@ -161,25 +162,28 @@ var mw = ( function ( $, undefined ) { 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; }, diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.test.js index 13f19852b6..e6934eda08 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.test.js @@ -80,7 +80,7 @@ test( 'mw.config', function() { }); 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' ); @@ -116,15 +116,31 @@ test( 'mw.message & mw.messages', function() { equal( goodbye.plain(), '', 'Message.toString returns plain 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 awesome world' ), 'mw.messages.set: Register' ); - equal( mw.msg( 'hello' ), 'Hello awesome world', 'Gets message with default options (existing message)' ); equal( mw.msg( '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() { -- 2.20.1