Use mw.jqueryMsg parser for message parsing to support PLURAL and GENDER
authorSanthosh Thottingal <santhosh@users.mediawiki.org>
Fri, 6 Jan 2012 09:14:45 +0000 (09:14 +0000)
committerSanthosh Thottingal <santhosh@users.mediawiki.org>
Fri, 6 Jan 2012 09:14:45 +0000 (09:14 +0000)
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
includes/OutputPage.php
resources/mediawiki/mediawiki.js
tests/qunit/suites/resources/mediawiki/mediawiki.test.js

index e073ce5..3db8153 100644 (file)
@@ -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.
index ee1ee4f..b572840 100644 (file)
@@ -2446,6 +2446,7 @@ $templates
                        'mediawiki.util',
                        'mediawiki.page.startup',
                        'mediawiki.page.ready',
+                       'mediawiki.jqueryMsg',
                ) );
                if ( $wgIncludeLegacyJavaScript ){
                        $this->addModules( 'mediawiki.legacy.wikibits' );
index 5eed8b8..a3faf92 100644 (file)
@@ -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 <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
@@ -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;
                },
 
index 13f1985..e6934ed 100644 (file)
@@ -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(), '<goodbye>', 'Message.toString returns plain <key> if format is "plain" and key does not exist' );
        // bug 30684
        equal( goodbye.escaped(), '&lt;goodbye&gt;', 'Message.toString returns properly escaped &lt;key&gt; 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() {