X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=languages%2FLanguage.php;h=472871fc89be65c0b628b937d15627d2262c3a74;hb=a8f11c4001beb705bc56c0b7e127865af3afb3b4;hp=06f4073400b48da9c00cb2069fe6671aac323c42;hpb=22c6ef5ebbacdb2ed17d323944e34c00fc9cd331;p=lhc%2Fweb%2Fwiklou.git diff --git a/languages/Language.php b/languages/Language.php index 06f4073400..472871fc89 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -32,7 +32,7 @@ if ( !defined( 'MEDIAWIKI' ) ) { # Read language names global $wgLanguageNames; -require_once( dirname( __FILE__ ) . '/Names.php' ); +require_once( __DIR__ . '/Names.php' ); if ( function_exists( 'mb_strtoupper' ) ) { mb_internal_encoding( 'UTF-8' ); @@ -266,9 +266,9 @@ class Language { */ public static function isValidBuiltInCode( $code ) { - if( !is_string($code) ) { + if ( !is_string( $code ) ) { $type = gettype( $code ); - if( $type === 'object' ) { + if ( $type === 'object' ) { $addmsg = " of class " . get_class( $code ); } else { $addmsg = ''; @@ -419,6 +419,16 @@ class Language { */ public function setNamespaces( array $namespaces ) { $this->namespaceNames = $namespaces; + $this->mNamespaceIds = null; + } + + /** + * Resets all of the namespace caches. Mainly used for testing + */ + public function resetNamespaces( ) { + $this->namespaceNames = null; + $this->mNamespaceIds = null; + $this->namespaceAliases = null; } /** @@ -742,7 +752,7 @@ class Language { $names = array(); - if( $inLanguage ) { + if ( $inLanguage ) { # TODO: also include when $inLanguage is null, when this code is more efficient wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $inLanguage ) ); } @@ -762,11 +772,11 @@ class Language { $returnMw = array(); $coreCodes = array_keys( $mwNames ); - foreach( $coreCodes as $coreCode ) { + foreach ( $coreCodes as $coreCode ) { $returnMw[$coreCode] = $names[$coreCode]; } - if( $include === 'mwfile' ) { + if ( $include === 'mwfile' ) { $namesMwFile = array(); # We do this using a foreach over the codes instead of a directory # loop so that messages files in extensions will work correctly. @@ -3005,7 +3015,7 @@ class Language { function listToText( array $l ) { $s = ''; $m = count( $l ) - 1; - + if ( $m === 0 ) { return $l[0]; } elseif ( $m === 1 ) { @@ -3032,10 +3042,7 @@ class Language { */ function commaList( array $list ) { return implode( - wfMsgExt( - 'comma-separator', - array( 'parsemag', 'escapenoentities', 'language' => $this ) - ), + wfMessage( 'comma-separator' )->inLanguage( $this )->escaped(), $list ); } @@ -3048,10 +3055,7 @@ class Language { */ function semicolonList( array $list ) { return implode( - wfMsgExt( - 'semicolon-separator', - array( 'parsemag', 'escapenoentities', 'language' => $this ) - ), + wfMessage( 'semicolon-separator' )->inLanguage( $this )->escaped(), $list ); } @@ -3063,10 +3067,7 @@ class Language { */ function pipeList( array $list ) { return implode( - wfMsgExt( - 'pipe-separator', - array( 'escapenoentities', 'language' => $this ) - ), + wfMessage( 'pipe-separator' )->inLanguage( $this )->escaped(), $list ); } @@ -3091,7 +3092,7 @@ class Language { function truncate( $string, $length, $ellipsis = '...', $adjustLength = true ) { # Use the localized ellipsis character if ( $ellipsis == '...' ) { - $ellipsis = wfMsgExt( 'ellipsis', array( 'escapenoentities', 'language' => $this ) ); + $ellipsis = wfMessage( 'ellipsis' )->inLanguage( $this )->escaped(); } # Check if there is no need to truncate if ( $length == 0 ) { @@ -3189,7 +3190,7 @@ class Language { function truncateHtml( $text, $length, $ellipsis = '...' ) { # Use the localized ellipsis character if ( $ellipsis == '...' ) { - $ellipsis = wfMsgExt( 'ellipsis', array( 'escapenoentities', 'language' => $this ) ); + $ellipsis = wfMessage( 'ellipsis' )->inLanguage( $this )->escaped(); } # Check if there is clearly no need to truncate if ( $length <= 0 ) { @@ -3418,9 +3419,9 @@ class Language { if ( !count( $forms ) ) { return ''; } - $forms = $this->preConvertPlural( $forms, 2 ); - - return ( $count == 1 ) ? $forms[0] : $forms[1]; + $pluralForm = $this->getPluralForm( $count ); + $pluralForm = min( $pluralForm, count( $forms ) - 1 ); + return $forms[$pluralForm]; } /** @@ -4116,7 +4117,7 @@ class Language { $dirmark = ( $oppositedm ? $this->getDirMark( true ) : '' ) . $this->getDirMark(); $details = $details ? $dirmark . $this->getMessageFromDB( 'word-separator' ) . - wfMsgExt( 'parentheses', array( 'escape', 'replaceafter', 'language' => $this ), $details ) : ''; + wfMessage( 'parentheses' )->rawParams( $details )->inLanguage( $this )->escaped() : ''; return $page . $details; } @@ -4189,4 +4190,54 @@ class Language { public function getConvRuleTitle() { return $this->mConverter->getConvRuleTitle(); } + + /** + * Get the compiled plural rules for the language + * @since 1.20 + * @return array Associative array with plural form, and plural rule as key-value pairs + */ + public function getCompiledPluralRules() { + $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; + } + + /** + * Get the plural rules for the language + * @since 1.20 + * @return array Associative array with plural form, and plural rule as key-value pairs + */ + public function getPluralRules() { + $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; + } + + /** + * Find the plural form matching to the given number + * It return the form index. + * @return int The index of the plural form + */ + private function getPluralForm( $number ) { + $pluralRules = $this->getCompiledPluralRules(); + $form = CLDRPluralRuleEvaluator::evaluateCompiled( $number, $pluralRules ); + return $form; + } + }