Localisation updates from http://translatewiki.net.
[lhc/web/wiklou.git] / languages / Language.php
index c73ccd1..e5cab05 100644 (file)
@@ -81,7 +81,7 @@ class Language {
 
        public $mVariants, $mCode, $mLoaded = false;
        public $mMagicExtensions = array(), $mMagicHookDone = false;
-       private $mHtmlCode = null;
+       private $mHtmlCode = null, $mParentLanguage = false;
 
        public $dateFormatStrings = array();
        public $mExtendedSpecialPageAliases;
@@ -170,6 +170,14 @@ class Language {
                'seconds' => 1,
        );
 
+       /**
+        * Cache for language fallbacks.
+        * @see Language::getFallbacksIncludingSiteLanguage
+        * @since 1.21
+        * @var array
+        */
+       static private $fallbackLanguageCache = array();
+
        /**
         * Get a cached or new language object for a given language code
         * @param $code String
@@ -377,6 +385,12 @@ class Language {
        public static function isKnownLanguageTag( $tag ) {
                static $coreLanguageNames;
 
+               // Quick escape for invalid input to avoid exceptions down the line
+               // when code tries to process tags which are not valid at all.
+               if ( !self::isValidBuiltInCode( $tag ) ) {
+                       return false;
+               }
+
                if ( $coreLanguageNames === null ) {
                        include MWInit::compiledPath( 'languages/Names.php' );
                }
@@ -2237,7 +2251,7 @@ class Language {
         * @param MWTimestamp $relativeTo Base timestamp
         * @param User $user User preferences to use
         * @return string Human timestamp
-        * @since 1.21
+        * @since 1.22
         */
        public function getHumanTimestamp( MWTimestamp $ts, MWTimestamp $relativeTo, User $user ) {
                $diff = $ts->diff( $relativeTo );
@@ -2945,12 +2959,16 @@ class Language {
        }
 
        /**
+        * Get all magic words from cache.
         * @return array
         */
        function getMagicWords() {
                return self::$dataCache->getItem( $this->mCode, 'magicWords' );
        }
 
+       /**
+        * Run the LanguageGetMagic hook once.
+        */
        protected function doMagicHook() {
                if ( $this->mMagicHookDone ) {
                        return;
@@ -2967,17 +2985,16 @@ class Language {
         * @param $mw
         */
        function getMagic( $mw ) {
-               $this->doMagicHook();
+               // Saves a function call
+               if ( ! $this->mMagicHookDone ) {
+                       $this->doMagicHook();
+               }
 
                if ( isset( $this->mMagicExtensions[$mw->mId] ) ) {
                        $rawEntry = $this->mMagicExtensions[$mw->mId];
                } else {
-                       $magicWords = $this->getMagicWords();
-                       if ( isset( $magicWords[$mw->mId] ) ) {
-                               $rawEntry = $magicWords[$mw->mId];
-                       } else {
-                               $rawEntry = false;
-                       }
+                       $rawEntry = self::$dataCache->getSubitem(
+                               $this->mCode, 'magicWords', $mw->mId );
                }
 
                if ( !is_array( $rawEntry ) ) {
@@ -3597,10 +3614,6 @@ class Language {
         * @return string Correct form of plural for $count in this language
         */
        function convertPlural( $count, $forms ) {
-               if ( !count( $forms ) ) {
-                       return '';
-               }
-
                // Handle explicit n=pluralform cases
                foreach ( $forms as $index => $form ) {
                        if ( preg_match( '/\d+=/i', $form ) ) {
@@ -3611,7 +3624,11 @@ class Language {
                                unset( $forms[$index] );
                        }
                }
+
                $forms = array_values( $forms );
+               if ( !count( $forms ) ) {
+                       return '';
+               }
 
                $pluralForm = $this->getPluralRuleIndexNumber( $count );
                $pluralForm = min( $pluralForm, count( $forms ) - 1 );
@@ -3922,6 +3939,34 @@ class Language {
                return $this;
        }
 
+       /**
+        * Get the "parent" language which has a converter to convert a "compatible" language
+        * (in another variant) to this language (eg. zh for zh-cn, but not en for en-gb).
+        *
+        * @return Language|null
+        * @since 1.22
+        */
+       public function getParentLanguage() {
+               if ( $this->mParentLanguage !== false ) {
+                       return $this->mParentLanguage;
+               }
+
+               $pieces = explode( '-', $this->getCode() );
+               $code = $pieces[0];
+               if ( !in_array( $code, LanguageConverter::$languagesWithVariants ) ) {
+                       $this->mParentLanguage = null;
+                       return null;
+               }
+               $lang = Language::factory( $code );
+               if ( !$lang->hasVariant( $this->getCode() ) ) {
+                       $this->mParentLanguage = null;
+                       return null;
+               }
+
+               $this->mParentLanguage = $lang;
+               return $lang;
+       }
+
        /**
         * Get the RFC 3066 code for this language object
         *
@@ -3956,8 +4001,9 @@ class Language {
         */
        public function setCode( $code ) {
                $this->mCode = $code;
-               // Ensure we don't leave an incorrect html code lying around
+               // Ensure we don't leave incorrect cached data lying around
                $this->mHtmlCode = null;
+               $this->mParentLanguage = false;
        }
 
        /**
@@ -4053,6 +4099,36 @@ class Language {
                }
        }
 
+       /**
+        * Get the ordered list of fallback languages, ending with the fallback
+        * language chain for the site language.
+        *
+        * @since 1.22
+        * @param string $code Language code
+        * @return array array( fallbacks, site fallbacks )
+        */
+       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.
+               $cacheKey = "{$code}-{$wgLanguageCode}";
+
+               if ( !array_key_exists( $cacheKey, self::$fallbackLanguageCache ) ) {
+                       $fallbacks = self::getFallbacksFor( $code );
+
+                       // Append the site's fallback chain, including the site language itself
+                       $siteFallbacks = self::getFallbacksFor( $wgLanguageCode );
+                       array_unshift( $siteFallbacks, $wgLanguageCode );
+
+                       // Eliminate any languages already included in the chain
+                       $siteFallbacks = array_diff( $siteFallbacks, $fallbacks );
+
+                       self::$fallbackLanguageCache[$cacheKey] = array( $fallbacks, $siteFallbacks );
+               }
+               return self::$fallbackLanguageCache[$cacheKey];
+       }
+
        /**
         * Get all messages for a given language
         * WARNING: this may take a long time. If you just need all message *keys*
@@ -4457,7 +4533,7 @@ class Language {
 
        /**
         * Get the plural rule types for the language
-        * @since 1.21
+        * @since 1.22
         * @return array Associative array with plural form number and plural rule type as key-value pairs
         */
        public function getPluralRuleTypes() {
@@ -4488,7 +4564,7 @@ class Language {
         * 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
+        * @since 1.22
         * @return string The name of the plural rule type, e.g. one, two, few, many
         */
        public function getPluralRuleType( $number ) {