Index: trunk/phase3/languages/classes/LanguageZh.php |
— | — | @@ -33,7 +33,7 @@ |
34 | 34 | 'zh-sg' => '新加坡', |
35 | 35 | 'zh-my' => '大马', |
36 | 36 | ); |
37 | | - $this->mVariantNames = array_merge($this->mVariantNames,$names); |
| 37 | + $this->mLanguageNames = array_merge($this->mLanguageNames,$names); |
38 | 38 | } |
39 | 39 | |
40 | 40 | function loadDefaultTables() { |
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, $mVariantNames; |
| 22 | + var $mVariants, $mVariantFallbacks, $mLanguageNames; |
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->mVariantNames = $wgLanguageNames; |
| 62 | + $this->mLanguageNames = $wgLanguageNames; |
63 | 63 | $this->mCacheKey = wfMemcKey( 'conversiontables', $maincode ); |
64 | 64 | $m = array( |
65 | 65 | 'begin'=>'-{', |
— | — | @@ -117,85 +117,9 @@ |
118 | 118 | return $this->mMainLanguageCode; |
119 | 119 | } |
120 | 120 | |
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 | | - */ |
| 121 | + // this method body in Language class |
127 | 122 | function getPreferredVariant( $fromUser = true ) { |
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 | | - |
| 123 | + return $this->mLangObj->getPreferredVariant( $fromUser ); |
200 | 124 | } |
201 | 125 | |
202 | 126 | /** |
— | — | @@ -208,10 +132,10 @@ |
209 | 133 | * @private |
210 | 134 | */ |
211 | 135 | function captionConvert( $matches ) { |
212 | | - // we convert captions except URL |
213 | 136 | $toVariant = $this->getPreferredVariant(); |
214 | 137 | $title = $matches[1]; |
215 | 138 | $text = $matches[2]; |
| 139 | + // we convert captions except URL |
216 | 140 | if( !strpos( $text, '://' ) ) |
217 | 141 | $text = $this->translate($text, $toVariant); |
218 | 142 | return " $title=\"$text\""; |
— | — | @@ -978,7 +902,7 @@ |
979 | 903 | $unidtable[$v][$from]=$to; |
980 | 904 | } |
981 | 905 | // syntax error, pass |
982 | | - if (!array_key_exists($v,$this->mConverter->mVariantNames)){ |
| 906 | + if (!array_key_exists($v,$this->mConverter->mLanguageNames)){ |
983 | 907 | $bidtable = array(); |
984 | 908 | $unidtable = array(); |
985 | 909 | break; |
— | — | @@ -996,10 +920,10 @@ |
997 | 921 | $varsep = $this->mConverter->mDescVarSep; |
998 | 922 | $text=''; |
999 | 923 | foreach($this->mBidtable as $k => $v) |
1000 | | - $text .= $this->mConverter->mVariantNames[$k]."$codesep$v$varsep"; |
| 924 | + $text .= $this->mConverter->mLanguageNames[$k]."$codesep$v$varsep"; |
1001 | 925 | foreach($this->mUnidtable as $k => $a) |
1002 | 926 | foreach($a as $from=>$to) |
1003 | | - $text.=$from.'⇒'.$this->mConverter->mVariantNames[$k]."$codesep$to$varsep"; |
| 927 | + $text.="$from⇒".$this->mConverter->mLanguageNames[$k]."$codesep$to$varsep"; |
1004 | 928 | return $text; |
1005 | 929 | } |
1006 | 930 | |
— | — | @@ -1125,7 +1049,7 @@ |
1126 | 1050 | $this->mRuleDisplay = $rules; |
1127 | 1051 | } elseif ( in_array('N',$flags) ){ |
1128 | 1052 | // proces N flag: output current variant name |
1129 | | - $this->mRuleDisplay = $this->mConverter->mVariantNames[trim($rules)]; |
| 1053 | + $this->mRuleDisplay = $this->mConverter->mLanguageNames[trim($rules)]; |
1130 | 1054 | } elseif ( in_array('D',$flags) ){ |
1131 | 1055 | // proces D flag: output rules description |
1132 | 1056 | $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, $mVariants, $mCode, $mLoaded = false; |
| 57 | + var $mConverter, $mVariant, $mVariants, $mCode, $mLoaded = false; |
58 | 58 | var $mMagicExtensions = array(), $mMagicHookDone = false; |
59 | 59 | |
60 | 60 | static public $mLocalisationKeys = array( |
— | — | @@ -2210,9 +2210,86 @@ |
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; |
2214 | 2223 | |
2215 | | - function getPreferredVariant( $fromUser = true ) { |
2216 | | - return $this->mConverter->getPreferredVariant( $fromUser ); |
| 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 | + |
2217 | 2294 | } |
2218 | 2295 | |
2219 | 2296 | /** |