Permalink
Browse files

Correctly parse som accept language edge cases

Some search engine bot came with these accept languages:

                   [0] => zh-CN
                   [1] => zh-Hans;0.95
                   [2] => zh-SG;0.95
                   [3] => zh-HK;0.85
                   [4] => zh-TW;0.85
                   [5] => zh-MO;0.85
                   [6] => zh-Hant;0.85
                   [7] => zh;0.8
                   [8] => en-us;0.55
                   [9] => en-gb;0.55
                   [10] => en;q=0.5
                   [11] => *;q=0.3

I decided to ignore the values entirely for these mal-formatted values (defaulting to 1).

Closes #138 as fixed.

This change may be merged back to the 1.0 branch
  • Loading branch information...
sabberworm committed Oct 15, 2012
1 parent 8a42f95 commit dc2e0ab00310ed016cbc7b88550fd107e78f7e6d
Showing with 7 additions and 2 deletions.
  1. +2 −1 lib/classes/LocaleUtil.php
  2. +5 −1 lib/tests/UtilDateFunctionTests.php
@@ -33,7 +33,7 @@ public static function acceptLocales($sLanguageId = false) {
$aLocaleId = explode('-', $aLocaleIdWithQ[0]);
$aQ = isset($aLocaleIdWithQ[1]) ? explode('=', $aLocaleIdWithQ[1]) : null;
$oResult = new StdClass();
$oResult->q = $aQ ? (float) $aQ[1] : 1.0;
$oResult->q = $aQ && isset($aQ[1]) ? (float) $aQ[1] : 1.0;
$oResult->q_slanted = $oResult->q + $fQSlant;
$fQSlant -= $fQSlantAmount;
$oResult->language_id = strtolower($aLocaleId[0]);
@@ -43,6 +43,7 @@ public static function acceptLocales($sLanguageId = false) {
$oResult->country_code = strtoupper(isset($aLocaleId[1]) ? $aLocaleId[1] : $aLocaleId[0]);
$aResult[] = $oResult;
}
// FIXME: This sort algorithm isn’t stable so we’re using $fQSlant to give higher priority to languages that come first
usort($aResult, function($a, $b) {
if($a->q_slanted > $b->q_slanted) { return -1; }
if($a->q_slanted == $b->q_slanted) { return 0; }
@@ -29,13 +29,17 @@ public function testDateParserNegativeENGB() {
$this->assertSame("26/11/1931", LocaleUtil::localizeDate($iDate, "en"));
}
public function testGetLocaleIdEN() {
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = "en-US,de-DE,de-AT";
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = "en-US,de-DE,de-AT,en_GB";
$this->assertSame("en_US", LocaleUtil::getLocaleId("en"));
}
public function testGetLocaleIdENq() {
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = "de-DE ; q = 0.3,en-US;q=0.8,de-AT";
$this->assertSame("de_AT", LocaleUtil::getLocaleId("de"));
}
public function testGetLocaleIdENqSearchEngine() {
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = "de-CH;,de-DE;q = 0.3,en-US;q=0.8,de-AT;,de-DE;0.4";
$this->assertSame("de_CH", LocaleUtil::getLocaleId("de"));
}
public function testGetLocaleIdDE() {
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = "en-US,de-AT";
$this->assertSame("de_AT", LocaleUtil::getLocaleId("de"));

0 comments on commit dc2e0ab

Please sign in to comment.