Yii Framework Forum: Preferred Languages Are Not Detected As Expected - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Preferred Languages Are Not Detected As Expected Rate Topic: -----

#1 User is offline   _Stan_ 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 50
  • Joined: 18-January 13

Posted 29 September 2013 - 02:21 PM

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;
	}

0

#2 User is offline   nineinchnick 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 622
  • Joined: 12-September 11
  • Location:Bialystok, Poland

Posted 29 September 2013 - 04:06 PM

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.
Don't be a dick.
0

#3 User is offline   _Stan_ 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 50
  • Joined: 18-January 13

Posted 30 September 2013 - 09:02 AM

View Postnineinchnick, on 29 September 2013 - 04:06 PM, said:

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users