* @ingroup Language
*/
class FakeConverter {
-
/**
* @var 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" );
}
/**
* 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;
}
$forms = array_values( $forms );
- $pluralForm = $this->getPluralForm( $count );
+ $pluralForm = $this->getPluralRuleIndexNumber( $count );
$pluralForm = min( $pluralForm, count( $forms ) - 1 );
return $forms[$pluralForm];
}
}
}
+ /**
+ * Get the ordered list of fallback languages, ending with the fallback
+ * language chain for the site language.
+ *
+ * @since 1.21
+ * @param $code string Language code
+ * @return array
+ */
+ public static function getFallbacksIncludingSiteLanguage( $code ) {
+ global $wgLanguageCode;
+
+ // Usually, we will only store a tiny number of fallback chains, so we
+ // keep them in static memory.
+ static $fallbackLanguageCache = array();
+ $cacheKey = "{$code}-{$wgLanguageCode}";
+
+ if ( !array_key_exists( $cacheKey, $fallbackLanguageCache ) ) {
+ $fallbacks = self::getFallbacksFor( $code );
+
+ // Take the final 'en' off of the array before splicing
+ if ( end( $fallbacks ) === 'en' ) {
+ array_pop( $fallbacks );
+ }
+ // Append the site's fallback chain
+ $siteFallbacks = self::getFallbacksFor( $wgLanguageCode );
+
+ // Eliminate any languages already included in the chain
+ $siteFallbacks = array_intersect( array_diff( $siteFallbacks, $fallbacks ), $siteFallbacks );
+ if ( $siteFallbacks ) {
+ $fallbacks = array_merge( $fallbacks, $siteFallbacks );
+ }
+ if ( end( $fallbacks ) !== 'en' ) {
+ $fallbacks[] = 'en';
+ }
+ $fallbackLanguageCache[$cacheKey] = $fallbacks;
+ }
+ return $fallbackLanguageCache[$cacheKey];
+ }
+
/**
* Get all messages for a given language
* WARNING: this may take a long time. If you just need all message *keys*
/**
* 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' );
}
/**
- * 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
*/
- private function getPluralForm( $number ) {
+ 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
+ */
+ 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';
+ }
+ }
}