For languages without plural forms, use them from fallback language
authorSanthosh Thottingal <santhosh.thottingal@gmail.com>
Sat, 15 Sep 2012 19:49:44 +0000 (12:49 -0700)
committerSanthosh Thottingal <santhosh.thottingal@gmail.com>
Sat, 15 Sep 2012 19:49:44 +0000 (12:49 -0700)
* Fixes Bug 40251 and this is alternate for I403a29e2
* This brings back the old mediawiki behavior for languages without
  defined plural rules
* Add a test for hu, which had issue as per Bug 40251

Change-Id: I345c305134a62d43c9dfedc5243981d0e77e326d

languages/Language.php
tests/phpunit/languages/LanguageHuTest.php [new file with mode: 0644]

index 1edca80..40d1f36 100644 (file)
@@ -3005,7 +3005,7 @@ class Language {
        function listToText( array $l ) {
                $s = '';
                $m = count( $l ) - 1;
-               
+
                if ( $m === 0 ) {
                        return $l[0];
                } elseif ( $m === 1 ) {
@@ -4187,7 +4187,17 @@ class Language {
         * @return array Associative array with plural form, and plural rule as key-value pairs
         */
        public function getCompiledPluralRules() {
-               return self::$dataCache->getItem( strtolower( $this->mCode ), 'compiledPluralRules' );
+               $pluralRules = self::$dataCache->getItem( strtolower( $this->mCode ), 'compiledPluralRules' );
+               $fallbacks = Language::getFallbacksFor( $this->mCode );
+               if ( !$pluralRules ) {
+                       foreach ( $fallbacks as $fallbackCode ) {
+                               $pluralRules = self::$dataCache->getItem( strtolower( $fallbackCode ), 'compiledPluralRules' );
+                               if ( $pluralRules ) {
+                                       break;
+                               }
+                       }
+               }
+               return $pluralRules;
        }
 
        /**
@@ -4196,7 +4206,17 @@ class Language {
         * @return array Associative array with plural form, and plural rule as key-value pairs
         */
        public function getPluralRules() {
-               return self::$dataCache->getItem( strtolower( $this->mCode ), 'pluralRules' );
+               $pluralRules = self::$dataCache->getItem( strtolower( $this->mCode ), 'pluralRules' );
+               $fallbacks = Language::getFallbacksFor( $this->mCode );
+               if ( !$pluralRules ) {
+                       foreach ( $fallbacks as $fallbackCode ) {
+                               $pluralRules = self::$dataCache->getItem( strtolower( $fallbackCode ), 'pluralRules' );
+                               if ( $pluralRules ) {
+                                       break;
+                               }
+                       }
+               }
+               return $pluralRules;
        }
 
        /**
diff --git a/tests/phpunit/languages/LanguageHuTest.php b/tests/phpunit/languages/LanguageHuTest.php
new file mode 100644 (file)
index 0000000..adbd37e
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+/**
+ * @author Santhosh Thottingal
+ * @copyright Copyright © 2012, Santhosh Thottingal
+ * @file
+ */
+
+/** Tests for MediaWiki languages/LanguageHu.php */
+class LanguageHuTest extends MediaWikiTestCase {
+       private $lang;
+
+       function setUp() {
+               $this->lang = Language::factory( 'Hu' );
+       }
+       function tearDown() {
+               unset( $this->lang );
+       }
+
+       /** @dataProvider providePlural */
+       function testPlural( $result, $value ) {
+               $forms = array( 'one', 'other' );
+               $this->assertEquals( $result, $this->lang->convertPlural( $value, $forms ) );
+       }
+
+       function providePlural() {
+               return array (
+                       array( 'other', 0 ),
+                       array( 'one', 1 ),
+                       array( 'other', 2 ),
+                       array( 'other', 200 ),
+               );
+       }
+
+}