Index: trunk/phase3/languages/LanguageConverter.php |
— | — | @@ -51,21 +51,14 @@ |
52 | 52 | $variantfallbacks = array(), |
53 | 53 | $flags = array(), |
54 | 54 | $manualLevel = array() ) { |
| 55 | + global $wgDisabledVariants, $wgLanguageNames; |
55 | 56 | $this->mLangObj = $langobj; |
56 | 57 | $this->mMainLanguageCode = $maincode; |
57 | | - |
58 | | - global $wgDisabledVariants; |
59 | | - $this->mVariants = array(); |
60 | | - foreach ( $variants as $variant ) { |
61 | | - if ( !in_array( $variant, $wgDisabledVariants ) ) { |
62 | | - $this->mVariants[] = $variant; |
63 | | - } |
64 | | - } |
| 58 | + $this->mVariants = array_diff( $variants, $wgDisabledVariants ); |
65 | 59 | $this->mVariantFallbacks = $variantfallbacks; |
66 | | - global $wgLanguageNames; |
67 | 60 | $this->mVariantNames = $wgLanguageNames; |
68 | 61 | $this->mCacheKey = wfMemcKey( 'conversiontables', $maincode ); |
69 | | - $f = array( |
| 62 | + $defaultflags = array( |
70 | 63 | // 'S' show converted text |
71 | 64 | // '+' add rules for alltext |
72 | 65 | // 'E' the gave flags is error |
— | — | @@ -79,7 +72,7 @@ |
80 | 73 | // (but no display in placed code ) |
81 | 74 | 'N' => 'N' // current variant name |
82 | 75 | ); |
83 | | - $this->mFlags = array_merge( $f, $flags ); |
| 76 | + $this->mFlags = array_merge( $defaultflags, $flags ); |
84 | 77 | foreach ( $this->mVariants as $v ) { |
85 | 78 | if ( array_key_exists( $v, $manualLevel ) ) { |
86 | 79 | $this->mManualLevel[$v] = $manualLevel[$v]; |
— | — | @@ -91,9 +84,12 @@ |
92 | 85 | } |
93 | 86 | |
94 | 87 | /** |
95 | | - * @public |
| 88 | + * Get all valid variants. |
| 89 | + * Call this instead of using $this->mVariants directly. |
| 90 | + * |
| 91 | + * @return Array: contains all valid variants |
96 | 92 | */ |
97 | | - function getVariants() { |
| 93 | + public function getVariants() { |
98 | 94 | return $this->mVariants; |
99 | 95 | } |
100 | 96 | |
— | — | @@ -104,27 +100,27 @@ |
105 | 101 | * when zh-sg is preferred but not defined, we will pick zh-hans |
106 | 102 | * in this case. Right now this is only used by zh. |
107 | 103 | * |
108 | | - * @param $v String: the language code of the variant |
109 | | - * @return string array The code of the fallback language or false if there |
110 | | - * is no fallback |
| 104 | + * @param $variant String: the language code of the variant |
| 105 | + * @return String: The code of the fallback language or the |
| 106 | + * main code if there is no fallback |
111 | 107 | */ |
112 | | - public function getVariantFallbacks( $v ) { |
113 | | - if ( isset( $this->mVariantFallbacks[$v] ) ) { |
114 | | - return $this->mVariantFallbacks[$v]; |
| 108 | + public function getVariantFallbacks( $variant ) { |
| 109 | + if ( isset( $this->mVariantFallbacks[$variant] ) ) { |
| 110 | + return $this->mVariantFallbacks[$variant]; |
115 | 111 | } |
116 | 112 | return $this->mMainLanguageCode; |
117 | 113 | } |
118 | 114 | |
119 | 115 | /** |
120 | 116 | * Get the title produced by the conversion rule. |
121 | | - * @returns String |
| 117 | + * @return String: The converted title text |
122 | 118 | */ |
123 | | - function getConvRuleTitle() { |
| 119 | + public function getConvRuleTitle() { |
124 | 120 | return $this->mConvRuleTitle; |
125 | 121 | } |
126 | 122 | |
127 | 123 | /** |
128 | | - * Get preferred language variants. |
| 124 | + * Get preferred language variant. |
129 | 125 | * @param $fromUser Boolean: get it from $wgUser's preferences |
130 | 126 | * @param $fromHeader Boolean: get it from Accept-Language |
131 | 127 | * @return String: the preferred language code |
— | — | @@ -158,12 +154,13 @@ |
159 | 155 | |
160 | 156 | /** |
161 | 157 | * Validate the variant |
162 | | - * @param $v String: the variant to validate |
| 158 | + * @param $variant String: the variant to validate |
163 | 159 | * @return Mixed: returns the variant if it is valid, null otherwise |
164 | 160 | */ |
165 | | - function validateVariant( $v = null ) { |
166 | | - if ( $v !== null && in_array( $v, $this->mVariants ) ) { |
167 | | - return $v; |
| 161 | + protected function validateVariant( $variant = null ) { |
| 162 | + if ( $variant !== null && |
| 163 | + in_array( $variant, $this->mVariants ) ) { |
| 164 | + return $variant; |
168 | 165 | } |
169 | 166 | return null; |
170 | 167 | } |
— | — | @@ -173,7 +170,7 @@ |
174 | 171 | * |
175 | 172 | * @return Mixed: variant if one found, false otherwise. |
176 | 173 | */ |
177 | | - function getURLVariant() { |
| 174 | + protected function getURLVariant() { |
178 | 175 | global $wgRequest; |
179 | 176 | $ret = null; |
180 | 177 | |
— | — | @@ -196,7 +193,7 @@ |
197 | 194 | * |
198 | 195 | * @return Mixed: variant if one found, false otherwise. |
199 | 196 | */ |
200 | | - function getUserVariant() { |
| 197 | + protected function getUserVariant() { |
201 | 198 | global $wgUser; |
202 | 199 | $ret = null; |
203 | 200 | |
— | — | @@ -226,7 +223,7 @@ |
227 | 224 | * |
228 | 225 | * @return Mixed: variant if one found, false otherwise. |
229 | 226 | */ |
230 | | - function getHeaderVariant() { |
| 227 | + protected function getHeaderVariant() { |
231 | 228 | global $wgRequest; |
232 | 229 | $ret = null; |
233 | 230 | |
— | — | @@ -236,7 +233,6 @@ |
237 | 234 | |
238 | 235 | // see if some supported language variant is set in the |
239 | 236 | // http header. |
240 | | - |
241 | 237 | $acceptLanguage = $wgRequest->getHeader( 'Accept-Language' ); |
242 | 238 | if ( !$acceptLanguage ) { |
243 | 239 | return null; |
— | — | @@ -299,9 +295,8 @@ |
300 | 296 | * or '<span title="text" ... ' |
301 | 297 | * |
302 | 298 | * @return String like ' alt="yyyy"' or ' title="yyyy"' |
303 | | - * @private |
304 | 299 | */ |
305 | | - function captionConvert( $matches ) { |
| 300 | + protected function captionConvert( $matches ) { |
306 | 301 | $toVariant = $this->getPreferredVariant(); |
307 | 302 | $title = $matches[1]; |
308 | 303 | $text = $matches[2]; |
— | — | @@ -314,13 +309,15 @@ |
315 | 310 | |
316 | 311 | /** |
317 | 312 | * Dictionary-based conversion. |
| 313 | + * This function would not parse the conversion rules. |
| 314 | + * If you want to parse rules, try to use convert() or |
| 315 | + * convertTo(). |
318 | 316 | * |
319 | 317 | * @param $text String: the text to be converted |
320 | 318 | * @param $toVariant String: the target language code |
321 | 319 | * @return String: the converted text |
322 | | - * @private |
323 | 320 | */ |
324 | | - function autoConvert( $text, $toVariant = false ) { |
| 321 | + public function autoConvert( $text, $toVariant = false ) { |
325 | 322 | $fname = 'LanguageConverter::autoConvert'; |
326 | 323 | |
327 | 324 | wfProfileIn( $fname ); |
— | — | @@ -399,15 +396,14 @@ |
400 | 397 | |
401 | 398 | /** |
402 | 399 | * Translate a string to a variant. |
403 | | - * Doesn't process markup or do any of that other stuff, for that use |
404 | | - * convert(). |
| 400 | + * Doesn't parse rules or do any of that other stuff, for that use |
| 401 | + * convert() or convertTo(). |
405 | 402 | * |
406 | 403 | * @param $text String: text to convert |
407 | 404 | * @param $variant String: variant language code |
408 | 405 | * @return String: translated text |
409 | | - * @private |
410 | 406 | */ |
411 | | - function translate( $text, $variant ) { |
| 407 | + protected function translate( $text, $variant ) { |
412 | 408 | wfProfileIn( __METHOD__ ); |
413 | 409 | // If $text is empty or only includes spaces, do nothing |
414 | 410 | // Otherwise translate it |
— | — | @@ -422,10 +418,10 @@ |
423 | 419 | } |
424 | 420 | |
425 | 421 | /** |
426 | | - * Convert text to all supported variants. |
| 422 | + * Call translate() to convert text to all valid variants. |
427 | 423 | * |
428 | 424 | * @param $text String: the text to be converted |
429 | | - * @return Array of string |
| 425 | + * @return Array: variant => converted text |
430 | 426 | */ |
431 | 427 | public function autoConvertToAllVariants( $text ) { |
432 | 428 | $fname = 'LanguageConverter::autoConvertToAllVariants'; |
— | — | @@ -447,7 +443,7 @@ |
448 | 444 | * Convert link text to all supported variants. |
449 | 445 | * |
450 | 446 | * @param $text String: the text to be converted |
451 | | - * @return Array of string |
| 447 | + * @return Array: variant => converted text |
452 | 448 | */ |
453 | 449 | public function convertLinkToAllVariants( $text ) { |
454 | 450 | if ( !$this->mTablesLoaded ) { |