Preferred Languages Are Not Detected As Expected

Using Yii 1.1.14, I encounter a problem with preferred languages detection in Android’s built-in browser (not Chrome). Here is the code of the method which I have been using before I moved to 1.1.14, and this implementation works as expected, so I patched 1.1.14 for myself. I did not dig into the code to pinpoint where is the bug in Yii’s implementation. Just want to let you know that the code below (it is borrowed from some Yii commits in the past) is free from the bug.


	public function getPreferredLanguages()

	{

		if($this->_preferredLanguages===null)

		{

			$sortedLanguages = array();

			if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && ($n=preg_match_all('/([\w\-_]+)\s*(;\s*q\s*=\s*(\d*\.\d*))?/',$_SERVER['HTTP_ACCEPT_LANGUAGE'],$matches))>0)

			{

				$languages=array();

				for($i=0;$i<$n;++$i) {

					$prefVal=empty($matches[3][$i]) ? 1.0 : floatval($matches[3][$i]);

					if (!isset($languages[$matches[1][$i]]) || $languages[$matches[1][$i]][0] < $prefVal) {

						$languages[$matches[1][$i]][0] = $prefVal;

						$languages[$matches[1][$i]][1] = $i;

						$languages[$matches[1][$i]][2] = $matches[1][$i];

					}

				}

				usort($languages, create_function('$a, $b', 'if ($a[0] < $b[0]) { return 1; } else if ($a[0] > $b[0]) { return -1; } else if ($a[1] < $b[1]) { return -1; } else if($a[1] > $b[1]) { return 1; } else { return 0; }'));

				for($i=0;$i<count($languages);$i++) {

					$sortedLanguages[] = $languages[$i][2];

				}

			}

			$this->_preferredLanguages = $sortedLanguages;

		}

		return $this->_preferredLanguages;

	}



Sorry if I sound too rude, but you post a piece of code that fix something you don’t describe and you don’t know how? That surely is useful for everybody.

I think I provide enough information. If not, here is some details. If you have a site with a couple of supported locales, say English and Russian, and access the site from Android’s WebView with Russian enabled on the system level, the site serves english pages. With the patch applied it serves in russian.

One can compare current implementation of this method and look though its history in repository to find the code I posted. Unfortunately I’m not in a position of debugging this code. I did this once for 1.1.12, and posted here the question about the patch - no one responded that time.