Index: trunk/phase3/languages/LanguageConverter.php |
— | — | @@ -18,7 +18,7 @@ |
19 | 19 | class LanguageConverter { |
20 | 20 | var $mPreferredVariant=''; |
21 | 21 | var $mMainLanguageCode; |
22 | | - var $mVariants, $mVariantFallbacks, $mLanguageNames; |
| 22 | + var $mVariants, $mVariantFallbacks, $mVariantNames; |
23 | 23 | var $mTablesLoaded = false; |
24 | 24 | var $mTables; |
25 | 25 | var $mManualAddTables; |
— | — | @@ -58,7 +58,7 @@ |
59 | 59 | $this->mVariants = $variants; |
60 | 60 | $this->mVariantFallbacks = $variantfallbacks; |
61 | 61 | global $wgLanguageNames; |
62 | | - $this->mLanguageNames = $wgLanguageNames; |
| 62 | + $this->mVariantNames = $wgLanguageNames; |
63 | 63 | $this->mCacheKey = wfMemcKey( 'conversiontables', $maincode ); |
64 | 64 | $m = array( |
65 | 65 | 'begin'=>'-{', |
— | — | @@ -117,9 +117,85 @@ |
118 | 118 | return $this->mMainLanguageCode; |
119 | 119 | } |
120 | 120 | |
121 | | - // this method body in Language class |
| 121 | + /** |
| 122 | + * get preferred language variants. |
| 123 | + * @param boolean $fromUser Get it from $wgUser's preferences |
| 124 | + * @return string the preferred language code |
| 125 | + * @public |
| 126 | + */ |
122 | 127 | function getPreferredVariant( $fromUser = true ) { |
123 | | - return $this->mLangObj->getPreferredVariant( $fromUser ); |
| 128 | + global $wgUser, $wgRequest, $wgVariantArticlePath, $wgDefaultLanguageVariant; |
| 129 | + |
| 130 | + if($this->mPreferredVariant) |
| 131 | + return $this->mPreferredVariant; |
| 132 | + |
| 133 | + // figure out user lang without constructing wgLang to avoid infinite recursion |
| 134 | + if( $fromUser ) |
| 135 | + $defaultUserLang = $wgUser->getOption( 'language' ); |
| 136 | + else |
| 137 | + $defaultUserLang = $this->mMainLanguageCode; |
| 138 | + $userLang = $wgRequest->getVal( 'uselang', $defaultUserLang ); |
| 139 | + // see if interface language is same as content, if not, prevent conversion |
| 140 | + if( ! in_array( $userLang, $this->mVariants ) ){ |
| 141 | + $this->mPreferredVariant = $this->mMainLanguageCode; // no conversion |
| 142 | + return $this->mPreferredVariant; |
| 143 | + } |
| 144 | + |
| 145 | + // see if the preference is set in the request |
| 146 | + $req = $wgRequest->getText( 'variant' ); |
| 147 | + if( in_array( $req, $this->mVariants ) ) { |
| 148 | + $this->mPreferredVariant = $req; |
| 149 | + return $req; |
| 150 | + } |
| 151 | + |
| 152 | + // check the syntax /code/ArticleTitle |
| 153 | + if($wgVariantArticlePath!=false && isset($_SERVER['SCRIPT_NAME'])){ |
| 154 | + // Note: SCRIPT_NAME probably won't hold the correct value if PHP is run as CGI |
| 155 | + // (it will hold path to php.cgi binary), and might not exist on some very old PHP installations |
| 156 | + $scriptBase = basename( $_SERVER['SCRIPT_NAME'] ); |
| 157 | + if(in_array($scriptBase,$this->mVariants)){ |
| 158 | + $this->mPreferredVariant = $scriptBase; |
| 159 | + return $this->mPreferredVariant; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + // get language variant preference from logged in users |
| 164 | + // Don't call this on stub objects because that causes infinite |
| 165 | + // recursion during initialisation |
| 166 | + if( $fromUser && $wgUser->isLoggedIn() ) { |
| 167 | + $this->mPreferredVariant = $wgUser->getOption('variant'); |
| 168 | + return $this->mPreferredVariant; |
| 169 | + } |
| 170 | + |
| 171 | + // see if default variant is globaly set |
| 172 | + if($wgDefaultLanguageVariant != false && in_array( $wgDefaultLanguageVariant, $this->mVariants )){ |
| 173 | + $this->mPreferredVariant = $wgDefaultLanguageVariant; |
| 174 | + return $this->mPreferredVariant; |
| 175 | + } |
| 176 | + |
| 177 | + # FIXME rewrite code for parsing http header. The current code |
| 178 | + # is written specific for detecting zh- variants |
| 179 | + if( !$this->mPreferredVariant ) { |
| 180 | + // see if some supported language variant is set in the |
| 181 | + // http header, but we don't set the mPreferredVariant |
| 182 | + // variable in case this is called before the user's |
| 183 | + // preference is loaded |
| 184 | + $pv=$this->mMainLanguageCode; |
| 185 | + if(array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER)) { |
| 186 | + $header = str_replace( '_', '-', strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"])); |
| 187 | + $zh = strstr($header, $pv.'-'); |
| 188 | + if($zh) { |
| 189 | + $ary = split("[,;]",$zh); |
| 190 | + $pv = $ary[0]; |
| 191 | + } |
| 192 | + } |
| 193 | + // don't try to return bad variant |
| 194 | + if(in_array( $pv, $this->mVariants )) |
| 195 | + return $pv; |
| 196 | + } |
| 197 | + |
| 198 | + return $this->mMainLanguageCode; |
| 199 | + |
124 | 200 | } |
125 | 201 | |
126 | 202 | /** |
— | — | @@ -132,10 +208,10 @@ |
133 | 209 | * @private |
134 | 210 | */ |
135 | 211 | function captionConvert( $matches ) { |
| 212 | + // we convert captions except URL |
136 | 213 | $toVariant = $this->getPreferredVariant(); |
137 | 214 | $title = $matches[1]; |
138 | 215 | $text = $matches[2]; |
139 | | - // we convert captions except URL |
140 | 216 | if( !strpos( $text, '://' ) ) |
141 | 217 | $text = $this->translate($text, $toVariant); |
142 | 218 | return " $title=\"$text\""; |
— | — | @@ -296,19 +372,12 @@ |
297 | 373 | if( !in_array($v,$this->mVariants) )continue; |
298 | 374 | if( $action=="add" ) { |
299 | 375 | foreach($t as $from=>$to) { |
300 | | - // to ensure that $from and $to not be left blank |
301 | | - // so $this->translate() could always return a string |
302 | | - if ($from || $to) |
303 | | - // more efficient than array_merge(), about 2.5 times. |
304 | | - $this->mManualAddTables[$v][$from] = $to; |
| 376 | + // more efficient than array_merge(), about 2.5 times. |
| 377 | + $this->mManualAddTables[$v][$from] = $to; |
305 | 378 | } |
306 | 379 | } |
307 | | - elseif ( $action=="remove" ) { |
308 | | - foreach($t as $from=>$to) { |
309 | | - if ($from || $to) |
310 | | - $this->mManualRemoveTables[$v][$from] = $to; |
311 | | - } |
312 | | - } |
| 380 | + elseif ( $action=="remove" ) |
| 381 | + $this->mManualRemoveTables[$v] = array_merge($this->mManualRemoveTables[$v], $t); |
313 | 382 | } |
314 | 383 | } |
315 | 384 | |
— | — | @@ -909,7 +978,7 @@ |
910 | 979 | $unidtable[$v][$from]=$to; |
911 | 980 | } |
912 | 981 | // syntax error, pass |
913 | | - if (!array_key_exists($v,$this->mConverter->mLanguageNames)){ |
| 982 | + if (!array_key_exists($v,$this->mConverter->mVariantNames)){ |
914 | 983 | $bidtable = array(); |
915 | 984 | $unidtable = array(); |
916 | 985 | break; |
— | — | @@ -927,10 +996,10 @@ |
928 | 997 | $varsep = $this->mConverter->mDescVarSep; |
929 | 998 | $text=''; |
930 | 999 | foreach($this->mBidtable as $k => $v) |
931 | | - $text .= $this->mConverter->mLanguageNames[$k]."$codesep$v$varsep"; |
| 1000 | + $text .= $this->mConverter->mVariantNames[$k]."$codesep$v$varsep"; |
932 | 1001 | foreach($this->mUnidtable as $k => $a) |
933 | 1002 | foreach($a as $from=>$to) |
934 | | - $text.="$from⇒".$this->mConverter->mLanguageNames[$k]."$codesep$to$varsep"; |
| 1003 | + $text.=$from.'⇒'.$this->mConverter->mVariantNames[$k]."$codesep$to$varsep"; |
935 | 1004 | return $text; |
936 | 1005 | } |
937 | 1006 | |
— | — | @@ -1056,7 +1125,7 @@ |
1057 | 1126 | $this->mRuleDisplay = $rules; |
1058 | 1127 | } elseif ( in_array('N',$flags) ){ |
1059 | 1128 | // proces N flag: output current variant name |
1060 | | - $this->mRuleDisplay = $this->mConverter->mLanguageNames[trim($rules)]; |
| 1129 | + $this->mRuleDisplay = $this->mConverter->mVariantNames[trim($rules)]; |
1061 | 1130 | } elseif ( in_array('D',$flags) ){ |
1062 | 1131 | // proces D flag: output rules description |
1063 | 1132 | $this->mRuleDisplay = $this->getRulesDesc(); |
Index: trunk/phase3/languages/Language.php |
— | — | @@ -53,7 +53,7 @@ |
54 | 54 | * @ingroup Language |
55 | 55 | */ |
56 | 56 | class Language { |
57 | | - var $mConverter, $mVariant, $mVariants, $mCode, $mLoaded = false; |
| 57 | + var $mConverter, $mVariants, $mCode, $mLoaded = false; |
58 | 58 | var $mMagicExtensions = array(), $mMagicHookDone = false; |
59 | 59 | |
60 | 60 | static public $mLocalisationKeys = array( |
— | — | @@ -2210,86 +2210,9 @@ |
2211 | 2211 | return $this->mConverter->getVariants(); |
2212 | 2212 | } |
2213 | 2213 | |
2214 | | - /** |
2215 | | - * get preferred language variants. |
2216 | | - * moved from LanguageConverter class |
2217 | | - * @param boolean $fromUser Get it from $wgUser's preferences |
2218 | | - * @return string the preferred language code |
2219 | | - * @public |
2220 | | - */ |
2221 | | - function getPreferredVariant( $fromUser = true ) { |
2222 | | - global $wgUser, $wgRequest, $wgVariantArticlePath, $wgDefaultLanguageVariant; |
2223 | 2214 | |
2224 | | - if($this->mVariant) |
2225 | | - return $this->mVariant; |
2226 | | - |
2227 | | - // figure out user lang without constructing wgLang to avoid infinite recursion |
2228 | | - if( $fromUser ) |
2229 | | - $defaultUserLang = $wgUser->getOption( 'language' ); |
2230 | | - else |
2231 | | - $defaultUserLang = $this->mConverter->mMainLanguageCode; |
2232 | | - $userLang = $wgRequest->getVal( 'uselang', $defaultUserLang ); |
2233 | | - // see if interface language is same as content, if not, prevent conversion |
2234 | | - if( ! in_array( $userLang, $this->mConverter->mVariants ) ){ |
2235 | | - $this->mVariant = $this->mConverter->mMainLanguageCode; // no conversion |
2236 | | - return $this->mVariant; |
2237 | | - } |
2238 | | - |
2239 | | - // see if the preference is set in the request |
2240 | | - $req = $wgRequest->getText( 'variant' ); |
2241 | | - if( in_array( $req, $this->mConverter->mVariants ) ) { |
2242 | | - $this->mVariant = $req; |
2243 | | - return $req; |
2244 | | - } |
2245 | | - |
2246 | | - // check the syntax /code/ArticleTitle |
2247 | | - if($wgVariantArticlePath!=false && isset($_SERVER['SCRIPT_NAME'])){ |
2248 | | - // Note: SCRIPT_NAME probably won't hold the correct value if PHP is run as CGI |
2249 | | - // (it will hold path to php.cgi binary), and might not exist on some very old PHP installations |
2250 | | - $scriptBase = basename( $_SERVER['SCRIPT_NAME'] ); |
2251 | | - if(in_array($scriptBase,$this->mConverter->mVariants)){ |
2252 | | - $this->mVariant = $scriptBase; |
2253 | | - return $this->mPreferredVariant; |
2254 | | - } |
2255 | | - } |
2256 | | - |
2257 | | - // get language variant preference from logged in users |
2258 | | - // Don't call this on stub objects because that causes infinite |
2259 | | - // recursion during initialisation |
2260 | | - if( $fromUser && $wgUser->isLoggedIn() ) { |
2261 | | - $this->mVariant = $wgUser->getOption('variant'); |
2262 | | - return $this->mVariant; |
2263 | | - } |
2264 | | - |
2265 | | - // see if default variant is globaly set |
2266 | | - if($wgDefaultLanguageVariant != false && in_array( $wgDefaultLanguageVariant, $this->mVariants )){ |
2267 | | - $this->mVariant = $wgDefaultLanguageVariant; |
2268 | | - return $this->mVariant; |
2269 | | - } |
2270 | | - |
2271 | | - # FIXME rewrite code for parsing http header. The current code |
2272 | | - # is written specific for detecting zh- variants |
2273 | | - if( !$this->mVariant ) { |
2274 | | - // see if some supported language variant is set in the |
2275 | | - // http header, but we don't set the mPreferredVariant |
2276 | | - // variable in case this is called before the user's |
2277 | | - // preference is loaded |
2278 | | - $pv=$this->mConverter->mMainLanguageCode; |
2279 | | - if(array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER)) { |
2280 | | - $header = str_replace( '_', '-', strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"])); |
2281 | | - $zh = strstr($header, $pv.'-'); |
2282 | | - if($zh) { |
2283 | | - $ary = split("[,;]",$zh); |
2284 | | - $pv = $ary[0]; |
2285 | | - } |
2286 | | - } |
2287 | | - // don't try to return bad variant |
2288 | | - if(in_array( $pv, $this->mConverter->mVariants )) |
2289 | | - return $pv; |
2290 | | - } |
2291 | | - |
2292 | | - return $this->mConverter->mMainLanguageCode; |
2293 | | - |
| 2215 | + function getPreferredVariant( $fromUser = true ) { |
| 2216 | + return $this->mConverter->getPreferredVariant( $fromUser ); |
2294 | 2217 | } |
2295 | 2218 | |
2296 | 2219 | /** |
Index: trunk/phase3/languages/classes/LanguageZh.php |
— | — | @@ -33,7 +33,7 @@ |
34 | 34 | 'zh-sg' => '新加坡', |
35 | 35 | 'zh-my' => '大马', |
36 | 36 | ); |
37 | | - $this->mLanguageNames = array_merge($this->mLanguageNames,$names); |
| 37 | + $this->mVariantNames = array_merge($this->mVariantNames,$names); |
38 | 38 | } |
39 | 39 | |
40 | 40 | function loadDefaultTables() { |