r45751 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r45750‎ | r45751 | r45752 >
Date:21:30, 14 January 2009
Author:ialex
Status:deferred (Comments)
Tags:
Comment:
Fix user's language detection: sort languages with the "; q=..." part passed by the client rather finding any language accepted by the client
Modified paths:
  • /trunk/extensions/LanguageSelector/LanguageSelector.php (modified) (history)

Diff [purge]

Index: trunk/extensions/LanguageSelector/LanguageSelector.php
@@ -259,62 +259,55 @@
260260 return true;
261261 }
262262
263 -function wfLanguageSelectorDetectLanguage($mode) {
 263+function wfLanguageSelectorDetectLanguage( $mode ) {
264264 global $wgContLang, $wgLanguageSelectorLanguages;
265265
266 - $contLang = $wgContLang->getCode();
267 -
268 - if (!$mode || $mode == LANGUAGE_SELECTOR_USE_CONTENT_LANG) {
 266+ if ( !$mode || $mode == LANGUAGE_SELECTOR_USE_CONTENT_LANG ) {
269267 return $contLang;
270268 }
271269
 270+ $contLang = $wgContLang->getCode();
 271+
272272 /**
273273 * get accepted languages from Accept-Languages
274274 * HTTP header.
275275 */
276 - $l= @$_SERVER["HTTP_ACCEPT_LANGUAGE"];
 276+ $accept = @$_SERVER["HTTP_ACCEPT_LANGUAGE"];
 277+
 278+ if ( empty( $accept ) )
 279+ return $contLang;
277280
278 - if (empty($l)) return $contLang;
 281+ $accept = split( ',', $accept );
279282
280 - $l= split(',',$l);
281 -
282283 /**
283284 * normalize accepted languages
284285 */
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] );
294294 }
295 -
296 - $languages[]= $lan;
297 -
298 - $idx= strpos($lan,'-');
299 - if ($idx !== false) {
300 - $lan= substr($lan,0,$idx);
301 - $languages[]= $lan;
302 - }
303295 }
304296
305297 /**
306298 * see if the content language is accepted by the
307299 * client.
308300 */
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 ) ) {
311302 return $contLang;
312303 }
313304
 305+ arsort( $languages, SORT_NUMERIC );
 306+
314307 /**
315308 * look for a language that is acceptable to the client
316309 * and known to the wiki.
317310 */
318 - foreach($wgLanguageSelectorLanguages as $code) {
 311+ foreach( $languages as $code => $q ) {
319312 /**
320313 * TODO: only accept languages for which an implementation exists.
321314 * this is disabled, because it's slow. Note that this code is
@@ -328,7 +321,7 @@
329322 }
330323 */
331324
332 - if (in_array($code,$languages)) {
 325+ if ( in_array( $code, $wgLanguageSelectorLanguages ) ) {
333326 return $code;
334327 }
335328 }

Comments

#Comment by Raymond (talk | contribs)   13:34, 15 January 2009

Typo in line 300 fixed with r45768.

Status & tagging log