Restore r52293 zh l10n fix for bug 18403 which was mistakenly reverted in r52303.
[lhc/web/wiklou.git] / languages / Language.php
index dae3952..e0fb273 100644 (file)
@@ -35,11 +35,12 @@ if( function_exists( 'mb_strtoupper' ) ) {
 class FakeConverter {
        var $mLang;
        function FakeConverter($langobj) {$this->mLang = $langobj;}
+       function autoConvertToAllVariants($text) {return $text;}
        function convert($t, $i) {return $t;}
        function parserConvert($t, $p) {return $t;}
        function getVariants() { return array( $this->mLang->getCode() ); }
        function getPreferredVariant() {return $this->mLang->getCode(); }
-       function findVariantLink(&$l, &$n, $forTemplate = false) {}
+       function findVariantLink(&$l, &$n, $ignoreOtherCond = false) {}
        function getExtraHashOptions() {return '';}
        function getParsedTitle() {return '';}
        function markNoConversion($text, $noParse=false) {return $text;}
@@ -55,11 +56,10 @@ class FakeConverter {
 class Language {
        var $mConverter, $mVariants, $mCode, $mLoaded = false;
        var $mMagicExtensions = array(), $mMagicHookDone = false;
-       var $mLocalizedLanguagesNames = null;
 
        static public $mLocalisationKeys = array(
                'fallback', 'namespaceNames', 'mathNames', 'bookstoreList',
-               'magicWords', 'messages', 'rtl', 'digitTransformTable',
+               'magicWords', 'messages', 'rtl', 'capitalizeAllNouns', 'digitTransformTable',
                'separatorTransformTable', 'fallback8bitEncoding', 'linkPrefixExtension',
                'defaultUserOptionOverrides', 'linkTrail', 'namespaceAliases',
                'dateFormats', 'datePreferences', 'datePreferenceMigrationMap',
@@ -410,19 +410,6 @@ class Language {
                return $names;
        }
 
-       /**
-        * Get localized language names
-        *
-        * @return array
-        */
-       function getLocalizedLanguageNames() {
-               if( !is_array( $this->mLocalizedLanguagesNames ) ) {
-                       $this->mLocalizedLanguagesNames = array();
-                       wfRunHooks( 'LanguageGetLocalizedLanguageNames', array( &$this->mLocalizedLanguagesNames, $this->getCode() ) );
-               }
-               return $this->mLocalizedLanguagesNames;
-       }
-
        /**
         * Get a message from the MediaWiki namespace.
         *
@@ -433,27 +420,12 @@ class Language {
                return wfMsgExt( $msg, array( 'parsemag', 'language' => $this ) );
        }
 
-       /**
-        * Get a language name
-        *
-        * @param $code String language code
-        * @return $localized boolean gets the localized language name
-        */
-       function getLanguageName( $code, $localized = false ) {
+       function getLanguageName( $code ) {
                $names = self::getLanguageNames();
                if ( !array_key_exists( $code, $names ) ) {
                        return '';
                }
-               if( $localized ) {
-                       $languageNames = $this->getLocalizedLanguageNames();
-                       return isset( $languageNames[$code] ) ? $languageNames[$code] : $names[$code];
-               } else {
-                       return $names[$code];
-               }
-       }
-
-       function getLanguageNameLocalized( $code ) {
-               return self::getLanguageName( $code, true );
+               return $names[$code];
        }
 
        function getMonthName( $key ) {
@@ -595,6 +567,11 @@ class Language {
         *
         *    xkY  Y (full year) in Thai solar calendar. Months and days are
         *                       identical to the Gregorian calendar
+        *    xoY  Y (full year) in Minguo calendar or Juche year.
+        *                       Months and days are identical to the
+        *                       Gregorian calendar
+        *    xtY  Y (full year) in Japanese nengo. Months and days are
+        *                       identical to the Gregorian calendar
         *
         * Characters enclosed in double quotes will be considered literal (with
         * the quotes themselves removed). Unmatched quotes will be considered
@@ -626,6 +603,8 @@ class Language {
                $hebrew = false;
                $hijri = false;
                $thai = false;
+               $minguo = false;
+               $tenno = false;
                for ( $p = 0; $p < strlen( $format ); $p++ ) {
                        $num = false;
                        $code = $format[$p];
@@ -633,7 +612,7 @@ class Language {
                                $code .= $format[++$p];
                        }
 
-                       if ( ( $code === 'xi' || $code == 'xj' || $code == 'xk' || $code == 'xm' ) && $p < strlen( $format ) - 1 ) {
+                       if ( ( $code === 'xi' || $code == 'xj' || $code == 'xk' || $code == 'xm' || $code == 'xo' || $code == 'xt' ) && $p < strlen( $format ) - 1 ) {
                                $code .= $format[++$p];
                        }
 
@@ -777,9 +756,17 @@ class Language {
                                        $num = $hebrew[0];
                                        break;
                                case 'xkY':
-                                       if ( !$thai ) $thai = self::tsToThai( $ts );
+                                       if ( !$thai ) $thai = self::tsToYear( $ts, 'thai' );
                                        $num = $thai[0];
                                        break;
+                               case 'xoY':
+                                       if ( !$minguo ) $minguo = self::tsToYear( $ts, 'minguo' );
+                                       $num = $minguo[0];
+                                       break;
+                               case 'xtY':
+                                       if ( !$tenno ) $tenno = self::tsToYear( $ts, 'tenno' );
+                                       $num = $tenno[0];
+                                       break;
                                case 'y':
                                        $num = substr( $ts, 2, 2 );
                                        break;
@@ -1141,26 +1128,72 @@ class Language {
        }
 
        /**
-        * Algorithm to convert Gregorian dates to Thai solar dates.
+        * Algorithm to convert Gregorian dates to Thai solar dates,
+        * Minguo dates or Minguo dates.
         *
         * Link: http://en.wikipedia.org/wiki/Thai_solar_calendar
+        *       http://en.wikipedia.org/wiki/Minguo_calendar
+        *       http://en.wikipedia.org/wiki/Japanese_era_name
         *
-        * @param $ts String: 14-character timestamp
+        * @param $ts String: 14-character timestamp, calender name
         * @return array converted year, month, day
         */
-       private static function tsToThai( $ts ) {
+       private static function tsToYear( $ts, $cName ) {
                $gy = substr( $ts, 0, 4 );
                $gm = substr( $ts, 4, 2 );
                $gd = substr( $ts, 6, 2 );
 
-               # Add 543 years to the Gregorian calendar
-               # Months and days are identical
-               $gy_thai = $gy + 543;
+               if (!strcmp($cName,'thai')) {
+                       # Thai solar dates
+                       # Add 543 years to the Gregorian calendar
+                       # Months and days are identical
+                       $gy_offset = $gy + 543;
+               } else if ((!strcmp($cName,'minguo')) || !strcmp($cName,'juche')) {
+                       # Minguo dates
+                       # Deduct 1911 years from the Gregorian calendar
+                       # Months and days are identical
+                       $gy_offset = $gy - 1911;
+               } else if (!strcmp($cName,'tenno')) {
+                       # Nengō dates up to Meiji period
+                       # Deduct years from the Gregorian calendar
+                       # depending on the nengo periods
+                       # Months and days are identical
+                       if (($gy < 1912) || (($gy == 1912) && ($gm < 7)) || (($gy == 1912) && ($gm == 7) && ($gd < 31))) {
+                               # Meiji period
+                               $gy_gannen = $gy - 1868 + 1;
+                               $gy_offset = $gy_gannen;
+                               if ($gy_gannen == 1)
+                                       $gy_offset = '元';
+                               $gy_offset = '明治'.$gy_offset;
+                       } else if ((($gy == 1912) && ($gm == 7) && ($gd == 31)) || (($gy == 1912) && ($gm >= 8)) || (($gy > 1912) && ($gy < 1926)) || (($gy == 1926) && ($gm < 12)) || (($gy == 1926) && ($gm == 12) && ($gd < 26))) {
+                               # Taishō period
+                               $gy_gannen = $gy - 1912 + 1;
+                               $gy_offset = $gy_gannen;
+                               if ($gy_gannen == 1)
+                                       $gy_offset = '元';
+                               $gy_offset = '大正'.$gy_offset;
+                       } else if ((($gy == 1926) && ($gm == 12) && ($gd >= 26)) || (($gy > 1926) && ($gy < 1989)) || (($gy == 1989) && ($gm == 1) && ($gd < 8))) {
+                               # Shōwa period
+                               $gy_gannen = $gy - 1926 + 1;
+                               $gy_offset = $gy_gannen;
+                               if ($gy_gannen == 1)
+                                       $gy_offset = '元';
+                               $gy_offset = '昭和'.$gy_offset;
+                       } else {
+                               # Heisei period
+                               $gy_gannen = $gy - 1989 + 1;
+                               $gy_offset = $gy_gannen;
+                               if ($gy_gannen == 1)
+                                       $gy_offset = '元';
+                               $gy_offset = '平成'.$gy_offset;
+                       }
+               } else {
+                       $gy_offset = $gy;
+               }
 
-               return array( $gy_thai, $gm, $gd );
+               return array( $gy_offset, $gm, $gd );
        }
 
-
        /**
         * Roman number formatting up to 3000
         */
@@ -1593,7 +1626,7 @@ class Language {
                        $n = $minLength-1;
                        $out = preg_replace(
                                "/\b(\w{1,$n})\b/",
-                               "$1U800",
+                               "$1u800",
                                $out );
                }
                
@@ -1605,7 +1638,7 @@ class Language {
                // "example.wikipedia.com" and "192.168.83.1" as well.
                $out = preg_replace(
                        "/(\w)\.(\w|\*)/u",
-                       "$1U82e$2",
+                       "$1u82e$2",
                        $out );
                
                wfProfileOut( __METHOD__ );
@@ -1618,7 +1651,7 @@ class Language {
         * settings or anything else of the sort.
         */
        protected function stripForSearchCallback( $matches ) {
-               return 'U8' . bin2hex( $matches[1] );
+               return 'u8' . bin2hex( $matches[1] );
        }
        
        /**
@@ -1759,6 +1792,11 @@ class Language {
                return $this->isRTL() ? "\xE2\x80\x8F" : "\xE2\x80\x8E";
        }
 
+       function capitalizeAllNouns() {
+               $this->load();
+               return $this->capitalizeAllNouns;
+       }
+
        /**
         * An arrow, depending on the language direction
         *
@@ -2016,9 +2054,21 @@ class Language {
        function commaList( $list ) {
                return implode(
                        $list,
-                       wfMsgExt( 'comma-separator', array( 'escapenoentities', 'language' => $this ) ) );
+                       wfMsgExt( 'comma-separator', array( 'parsemag', 'escapenoentities', 'language' => $this ) ) );
        }
-       
+
+       /**
+        * Take a list of strings and build a locale-friendly semicolon-separated
+        * list, using the local semicolon-separator message.
+        * @param $list array of strings to put in a semicolon list
+        * @return string
+        */
+       function semicolonList( $list ) {
+               return implode(
+                       $list,
+                       wfMsgExt( 'semicolon-separator', array( 'parsemag', 'escapenoentities', 'language' => $this ) ) );
+       }
+
        /**
         * Same as commaList, but separate it with the pipe instead.
         * @param $list array of strings to put in a pipe list
@@ -2045,7 +2095,12 @@ class Language {
         * @param $ellipsis String to append to the truncated text
         * @return string
         */
-       function truncate( $string, $length, $ellipsis = "" ) {
+       function truncate( $string, $length, $ellipsis = '...' ) {
+               # Use the localized ellipsis character
+               if( $ellipsis == '...' ) {
+                       $ellipsis = wfMsgExt( 'ellipsis', array( 'escapenoentities', 'language' => $this ) );
+               }
+
                if( $length == 0 ) {
                        return $ellipsis;
                }
@@ -2062,7 +2117,7 @@ class Language {
                        } elseif( $char >= 0x80 &&
                                  preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
                                              '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) ) {
-                           # We chopped in the middle of a character; remove it
+                               # We chopped in the middle of a character; remove it
                                $string = $m[1];
                        }
                        return $string . $ellipsis;
@@ -2093,6 +2148,22 @@ class Language {
                return $word;
        }
 
+       /**
+        * Provides an alternative text depending on specified gender.
+        * Usage {{gender:username|masculine|feminine|neutral}}.
+        * username is optional, in which case the gender of current user is used,
+        * but only in (some) interface messages; otherwise default gender is used.
+        * If second or third parameter are not specified, masculine is used.
+        * These details may be overriden per language.
+        */
+       function gender( $gender, $forms ) {
+               if ( !count($forms) ) { return ''; }
+               $forms = $this->preConvertPlural( $forms, 2 );
+               if ( $gender === 'male' ) return $forms[0];
+               if ( $gender === 'female' ) return $forms[1];
+               return isset($forms[2]) ? $forms[2] : $forms[0];
+       }
+
        /**
         * Plural form transformations, needed for some languages.
         * For example, there are 3 form of plural in Russian and Polish,
@@ -2177,6 +2248,11 @@ class Language {
                return $text;
        }
 
+       # convert text to all supported variants
+       function autoConvertToAllVariants($text) {
+               return $this->mConverter->autoConvertToAllVariants($text);
+       }
+
        # convert text to different variants of a language.
        function convert( $text, $isTitle = false) {
                return $this->mConverter->convert($text, $isTitle);
@@ -2236,10 +2312,12 @@ class Language {
         *
         * @param $link String: the name of the link
         * @param $nt Mixed: the title object of the link
+        * @param boolean $ignoreOtherCond: to disable other conditions when
+        *      we need to transclude a template or update a category's link
         * @return null the input parameters may be modified upon return
         */
-       function findVariantLink( &$link, &$nt, $forTemplate = false ) {
-               $this->mConverter->findVariantLink($link, $nt, $forTemplate );
+       function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
+               $this->mConverter->findVariantLink( $link, $nt, $ignoreOtherCond );
        }
 
        /**
@@ -2247,7 +2325,6 @@ class Language {
         * into an array of all possible variants of the text:
         *  'variant' => text in that variant
         */
-
        function convertLinkToAllVariants($text){
                return $this->mConverter->convertLinkToAllVariants($text);
        }
@@ -2366,6 +2443,8 @@ class Language {
                                                return self::$mLocalisationCache[$code]['deps'];
                                        }
                                }
+                       } else {
+                               $cache = false;
                        }
 
                        # Try the global cache
@@ -2409,6 +2488,12 @@ class Language {
                        wfDebug( "Language::loadLocalisation(): got localisation for $code from source\n" );
                }
 
+               # Load magic word source file
+               global $IP;
+               $filename = "$IP/includes/MagicWord.php";
+               $newDeps = array( $filename => filemtime( $filename ) );
+               $deps = array_merge( $deps, $newDeps );
+
                if ( !empty( $fallback ) ) {
                        # Load the fallback localisation, with a circular reference guard
                        if ( isset( $recursionGuard[$code] ) ) {
@@ -2484,6 +2569,10 @@ class Language {
                        self::loadLocalisation( $cache );
                        $cache = self::$mLocalisationCache[$cache];
                }
+               // At least one language file and the MagicWord file needed
+               if( count($cache['deps']) < 2 ) {
+                       return true;
+               }
                $expired = false;
                foreach ( $cache['deps'] as $file => $mtime ) {
                        if ( !file_exists( $file ) || filemtime( $file ) > $mtime ) {
@@ -2578,16 +2667,8 @@ class Language {
                        $this->namespaceNames[NS_PROJECT_TALK] = $wgMetaNamespaceTalk;
                } else {
                        $talk = $this->namespaceNames[NS_PROJECT_TALK];
-                       $talk = str_replace( '$1', $wgMetaNamespace, $talk );
-
-                       # Allow grammar transformations
-                       # Allowing full message-style parsing would make simple requests 
-                       # such as action=raw much more expensive than they need to be. 
-                       # This will hopefully cover most cases.
-                       $talk = preg_replace_callback( '/{{grammar:(.*?)\|(.*?)}}/i', 
-                               array( &$this, 'replaceGrammarInNamespace' ), $talk );
-                       $talk = str_replace( ' ', '_', $talk );
-                       $this->namespaceNames[NS_PROJECT_TALK] = $talk;
+                       $this->namespaceNames[NS_PROJECT_TALK] =
+                               $this->fixVariableInNamespace( $talk );
                }
                
                # The above mixing may leave namespaces out of canonical order.
@@ -2604,6 +2685,11 @@ class Language {
                }
                if ( $this->namespaceAliases ) {
                        foreach ( $this->namespaceAliases as $name => $index ) {
+                               if ( $index === NS_PROJECT_TALK ) {
+                                       unset( $this->namespaceAliases[$name] );
+                                       $name = $this->fixVariableInNamespace( $name );
+                                       $this->namespaceAliases[$name] = $index;
+                               }
                                $this->mNamespaceIds[$this->lc($name)] = $index;
                        }
                }
@@ -2619,6 +2705,21 @@ class Language {
                wfProfileOut( __METHOD__ );
        }
 
+       function fixVariableInNamespace( $talk ) {
+               if ( strpos( $talk, '$1' ) === false ) return $talk;
+
+               global $wgMetaNamespace;
+               $talk = str_replace( '$1', $wgMetaNamespace, $talk );
+
+               # Allow grammar transformations
+               # Allowing full message-style parsing would make simple requests 
+               # such as action=raw much more expensive than they need to be. 
+               # This will hopefully cover most cases.
+               $talk = preg_replace_callback( '/{{grammar:(.*?)\|(.*?)}}/i', 
+                       array( &$this, 'replaceGrammarInNamespace' ), $talk );
+               return str_replace( ' ', '_', $talk );
+       }
+
        function replaceGrammarInNamespace( $m ) {
                return $this->convertGrammar( trim( $m[2] ), trim( $m[1] ) );
        }