Merge "IcuCollation::$tailoringFirstLetters: 'sv', 'vi' verified"
[lhc/web/wiklou.git] / languages / Language.php
index 01751db..42883b5 100644 (file)
@@ -44,7 +44,6 @@ if ( function_exists( 'mb_strtoupper' ) ) {
  * @ingroup Language
  */
 class FakeConverter {
-
        /**
         * @var Language
         */
@@ -349,12 +348,12 @@ class Language {
        public static function isValidBuiltInCode( $code ) {
 
                if ( !is_string( $code ) ) {
-                       $type = gettype( $code );
-                       if ( $type === 'object' ) {
+                       if ( is_object( $code ) ) {
                                $addmsg = " of class " . get_class( $code );
                        } else {
                                $addmsg = '';
                        }
+                       $type = gettype( $code );
                        throw new MWException( __METHOD__ . " must be passed a string, $type given$addmsg" );
                }
 
@@ -531,7 +530,7 @@ class Language {
        /**
         * Resets all of the namespace caches. Mainly used for testing
         */
-       public function resetNamespaces( ) {
+       public function resetNamespaces() {
                $this->namespaceNames = null;
                $this->mNamespaceIds = null;
                $this->namespaceAliases = null;
@@ -3544,7 +3543,7 @@ class Language {
                }
                $forms = array_values( $forms );
 
-               $pluralForm = $this->getPluralForm( $count );
+               $pluralForm = $this->getPluralRuleIndexNumber( $count );
                $pluralForm = min( $pluralForm, count( $forms ) - 1 );
                return $forms[$pluralForm];
        }
@@ -4410,7 +4409,7 @@ class Language {
        /**
         * Get the plural rules for the language
         * @since 1.20
-        * @return array Associative array with plural form, and plural rule as key-value pairs
+        * @return array Associative array with plural form number and plural rule as key-value pairs
         */
        public function getPluralRules() {
                $pluralRules = self::$dataCache->getItem( strtolower( $this->mCode ), 'pluralRules' );
@@ -4427,13 +4426,48 @@ class Language {
        }
 
        /**
-        * Find the plural form matching to the given number
-        * It return the form index.
-        * @return int The index of the plural form
+        * Get the plural rule types for the language
+        * @since 1.21
+        * @return array Associative array with plural form number and plural rule type as key-value pairs
+        */
+       public function getPluralRuleTypes() {
+               $pluralRuleTypes = self::$dataCache->getItem( strtolower( $this->mCode ), 'pluralRuleTypes' );
+               $fallbacks = Language::getFallbacksFor( $this->mCode );
+               if ( !$pluralRuleTypes ) {
+                       foreach ( $fallbacks as $fallbackCode ) {
+                               $pluralRuleTypes = self::$dataCache->getItem( strtolower( $fallbackCode ), 'pluralRuleTypes' );
+                               if ( $pluralRuleTypes ) {
+                                       break;
+                               }
+                       }
+               }
+               return $pluralRuleTypes;
+       }
+
+       /**
+        * Find the index number of the plural rule appropriate for the given number
+        * @return int The index number of the plural rule
         */
-       private function getPluralForm( $number ) {
+       public function getPluralRuleIndexNumber( $number ) {
                $pluralRules = $this->getCompiledPluralRules();
                $form = CLDRPluralRuleEvaluator::evaluateCompiled( $number, $pluralRules );
                return $form;
        }
+
+       /**
+        * Find the plural rule type appropriate for the given number
+        * For example, if the language is set to Arabic, getPluralType(5) should
+        * return 'few'.
+        * @since 1.21
+        * @return string The name of the plural rule type, e.g. one, two, few, many
+        */
+       public function getPluralRuleType( $number ) {
+               $index = $this->getPluralRuleIndexNumber( $number );
+               $pluralRuleTypes = $this->getPluralRuleTypes();
+               if ( isset( $pluralRuleTypes[$index] ) ) {
+                       return $pluralRuleTypes[$index];
+               } else {
+                       return 'other';
+               }
+       }
 }