Index: trunk/extensions/LanguageSelector/LanguageSelector.php |
— | — | @@ -259,62 +259,55 @@ |
260 | 260 | return true; |
261 | 261 | } |
262 | 262 | |
263 | | -function wfLanguageSelectorDetectLanguage($mode) { |
| 263 | +function wfLanguageSelectorDetectLanguage( $mode ) { |
264 | 264 | global $wgContLang, $wgLanguageSelectorLanguages; |
265 | 265 | |
266 | | - $contLang = $wgContLang->getCode(); |
267 | | - |
268 | | - if (!$mode || $mode == LANGUAGE_SELECTOR_USE_CONTENT_LANG) { |
| 266 | + if ( !$mode || $mode == LANGUAGE_SELECTOR_USE_CONTENT_LANG ) { |
269 | 267 | return $contLang; |
270 | 268 | } |
271 | 269 | |
| 270 | + $contLang = $wgContLang->getCode(); |
| 271 | + |
272 | 272 | /** |
273 | 273 | * get accepted languages from Accept-Languages |
274 | 274 | * HTTP header. |
275 | 275 | */ |
276 | | - $l= @$_SERVER["HTTP_ACCEPT_LANGUAGE"]; |
| 276 | + $accept = @$_SERVER["HTTP_ACCEPT_LANGUAGE"]; |
| 277 | + |
| 278 | + if ( empty( $accept ) ) |
| 279 | + return $contLang; |
277 | 280 | |
278 | | - if (empty($l)) return $contLang; |
| 281 | + $accept = split( ',', $accept ); |
279 | 282 | |
280 | | - $l= split(',',$l); |
281 | | - |
282 | 283 | /** |
283 | 284 | * normalize accepted languages |
284 | 285 | */ |
285 | | - $languages= array(); |
286 | | - foreach ($l as $lan) { |
287 | | - $lan= trim($lan); |
288 | | - |
289 | | - $idx= strpos($lan,';'); |
290 | | - if ($idx !== false) { |
291 | | - #FIXME: qualifiers are ignored, order is relevant! |
292 | | - $lan= substr($lan,0,$idx); |
293 | | - $lan= trim($lan); |
| 286 | + $languages = array(); |
| 287 | + foreach ( $accept as $lan ) { |
| 288 | + @list( $value, $qpart ) = explode( ';', trim( $lan ) ); |
| 289 | + $match = array(); |
| 290 | + if( !isset( $qpart ) ) { |
| 291 | + $languages[$value] = 1.0; |
| 292 | + } elseif( preg_match( '/q\s*=\s*(\d*\.\d+)/', $qpart, $match ) ) { |
| 293 | + $languages[$value] = floatval( $match[1] ); |
294 | 294 | } |
295 | | - |
296 | | - $languages[]= $lan; |
297 | | - |
298 | | - $idx= strpos($lan,'-'); |
299 | | - if ($idx !== false) { |
300 | | - $lan= substr($lan,0,$idx); |
301 | | - $languages[]= $lan; |
302 | | - } |
303 | 295 | } |
304 | 296 | |
305 | 297 | /** |
306 | 298 | * see if the content language is accepted by the |
307 | 299 | * client. |
308 | 300 | */ |
309 | | - if ( ($mode == LANGUAGE_SELECTOR_PREFER_CONTENT_LANG) |
310 | | - && in_array($contLang,$languages) ) { |
| 301 | + if ( $mode == LANGUAGE_SELECTOR_PREFER_CONTENT_LANG && array_key_exist( $contLang, $languages ) ) { |
311 | 302 | return $contLang; |
312 | 303 | } |
313 | 304 | |
| 305 | + arsort( $languages, SORT_NUMERIC ); |
| 306 | + |
314 | 307 | /** |
315 | 308 | * look for a language that is acceptable to the client |
316 | 309 | * and known to the wiki. |
317 | 310 | */ |
318 | | - foreach($wgLanguageSelectorLanguages as $code) { |
| 311 | + foreach( $languages as $code => $q ) { |
319 | 312 | /** |
320 | 313 | * TODO: only accept languages for which an implementation exists. |
321 | 314 | * this is disabled, because it's slow. Note that this code is |
— | — | @@ -328,7 +321,7 @@ |
329 | 322 | } |
330 | 323 | */ |
331 | 324 | |
332 | | - if (in_array($code,$languages)) { |
| 325 | + if ( in_array( $code, $wgLanguageSelectorLanguages ) ) { |
333 | 326 | return $code; |
334 | 327 | } |
335 | 328 | } |