r70356 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r70355‎ | r70356 | r70357 >
Date:20:16, 2 August 2010
Author:maxsem
Status:resolved
Tags:
Comment:
Follow-up to r64587:
* Use WebRequest::getAcceptLang() instead of home-brewed code (and in LanguageConverter too).
* Refactored that function to use getHeader()
Modified paths:
  • /trunk/phase3/includes/WebRequest.php (modified) (history)
  • /trunk/phase3/includes/installer/WebInstaller.php (modified) (history)
  • /trunk/phase3/languages/LanguageConverter.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/installer/WebInstaller.php
@@ -444,28 +444,14 @@
445445 * @return string
446446 */
447447 public function getAcceptLanguage() {
448 - global $wgLanguageCode;
 448+ global $wgLanguageCode, $wgRequest;
449449
450450 $mwLanguages = Language::getLanguageNames();
451 - $langs = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
 451+ $headerLanguages = array_keys( $wgRequest->getAcceptLang() );
452452
453 - foreach ( explode( ';', $langs ) as $splitted ) {
454 - foreach ( explode( ',', $splitted ) as $lang ) {
455 - $lang = trim( strtolower( $lang ) );
456 -
457 - if ( $lang == '' || $lang[0] == 'q' ) {
458 - continue;
459 - }
460 -
461 - if ( isset( $mwLanguages[$lang] ) ) {
462 - return $lang;
463 - }
464 -
465 - $lang = preg_replace( '/^(.*?)(?=-[^-]*)$/', '\\1', $lang );
466 -
467 - if ( $lang != '' && isset( $mwLanguages[$lang] ) ) {
468 - return $lang;
469 - }
 453+ foreach ( $headerLanguages as $lang ) {
 454+ if ( isset( $mwLanguages[$lang] ) ) {
 455+ return $lang;
470456 }
471457 }
472458
Index: trunk/phase3/includes/WebRequest.php
@@ -733,14 +733,15 @@
734734 */
735735 public function getAcceptLang() {
736736 // Modified version of code found at http://www.thefutureoftheweb.com/blog/use-accept-language-header
737 - if ( !isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) {
 737+ $acceptLang = $this->getHeader( 'Accept-Language' );
 738+ if ( !$acceptLang ) {
738739 return array();
739740 }
740741
741742 // Break up string into pieces (languages and q factors)
742743 $lang_parse = null;
743744 preg_match_all( '/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0(\.[0-9]+))?)?/i',
744 - $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse );
 745+ $acceptLang, $lang_parse );
745746
746747 if ( !count( $lang_parse[1] ) ) {
747748 return array();
Index: trunk/phase3/languages/LanguageConverter.php
@@ -233,29 +233,13 @@
234234
235235 // see if some supported language variant is set in the
236236 // http header.
237 - $acceptLanguage = $wgRequest->getHeader( 'Accept-Language' );
238 - if ( !$acceptLanguage ) {
 237+ $languages = array_keys( $wgRequest->getAcceptLang() );
 238+ if ( empty( $languages ) ) {
239239 return null;
240240 }
241241
242 - // explode by comma
243 - $result = StringUtils::explode( ',', strtolower( $acceptLanguage ) );
244 - $languages = array();
245 -
246 - foreach ( $result as $elem ) {
247 - // if $elem likes 'zh-cn;q=0.9'
248 - if ( ( $posi = strpos( $elem, ';' ) ) !== false ) {
249 - // get the real language code likes 'zh-cn'
250 - $languages[] = substr( $elem, 0, $posi );
251 - } else {
252 - $languages[] = $elem;
253 - }
254 - }
255 -
256242 $fallback_languages = array();
257243 foreach ( $languages as $language ) {
258 - // strip whitespace
259 - $language = trim( $language );
260244 $this->mHeaderVariant = $this->validateVariant( $language );
261245 if ( $this->mHeaderVariant ) {
262246 break;

Follow-up revisions

RevisionCommit summaryAuthorDate
r70383Follow up r70356....platonides13:23, 3 August 2010

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r64587new-installer: added language sniffingmaxsem06:36, 4 April 2010

Status & tagging log