Page 4 of 4

Re: Language detection?

Posted: Sun Dec 14, 2008 11:03 pm
by bongobongo
If need someone else to test something I can assist tomorrow.
Just send me an example of the languages as defined in Language prefs, and browser used and I can do some testing tomorrow.

Now it is 00:12 here in Norway..... getting to sleepy her now...

Regards

Re: Language detection?

Posted: Sun Dec 14, 2008 11:16 pm
by alby
bongobongo wrote: Now it is 00:12 here in Norway..... getting to sleepy her now...
Same TMZ for Italy

Alby

Re: Language detection?

Posted: Sun Dec 14, 2008 11:20 pm
by alby
bongobongo wrote: Is it q value related?
no, same q values are mixed and not in same order (strange the function for same index=1 return 0)
However this depend of NUMBER of items (languages)
For about second item and more items (about 7) first third item

Very strange????

Alby

Re: Language detection?

Posted: Mon Dec 15, 2008 8:57 am
by bongobongo
Hi....

Tested chrome this morning using 6 languages... and then failed.... as you said it would.

There is probably a more elegant solution but this seem to works:

1. Comment out the array_reverse line.

2. Replace the usort line with this code:

Code: Select all

echo '<hr>Before:<br />';
foreach($browser_langs as $lang)
	echo $lang[0] .'-'. $lang[1].'<br />'; 
	
   // Order the codes by quality
   $numBrowserLangs = count($browser_langs);
   if ($numBrowserLangs > 0){
	   $useUsort = 1;
	   for ($i=0; $i<$numBrowserLangs; $i++){
	   		for ($k=0; $k<$numBrowserLangs; $k++){
	   			if ($i <> $k){
	   				if ($browser_langs[$i][1] == $browser_langs[$k][1]){
	   					// if here then duplicate q value and RFC is broken, then use order of langs as in accept-language header
	   					$useUsort = 0;
	   					break 2;
	   				}
	   			}
	   		}
	   }
	   if ($useUsort == 1){
			echo "<br><br>Using usort<br><br>";
	   		usort($browser_langs, "language_accept_order");
	   }
   }
	
echo '<hr>After:<br />';
foreach($browser_langs as $lang)
	echo $lang[0] .'-'. $lang[1].'<br />'; 

Re: Language detection?

Posted: Mon Dec 15, 2008 12:37 pm
by bongobongo
Here is a solution with less code:

Changes in read, and no use of array_reverse:


function language_user_setting( $hls )
{


$browser_langs=array();
$t_hls = transform_language_arr($hls);
$useUsort = 1;
$qualityArr = array();


// Check if we have $_SERVER['HTTP_ACCEPT_LANGUAGE'] set and
// it no longer breaks if you only have one language set :)
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$browser_accept = explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']);

// Go through all language preference specs
for ($i = 0; $i < count($browser_accept); $i++) {
// The language part is either a code or a code with a quality
// We cannot do anything with a * code, so it is skipped
// If the quality is missing, it is assumed to be 1 according to the RFC
if (preg_match("!([a-z-]+)(;q=([0-9\\.]+))?!", trim($browser_accept[$i]), $found)) {
$quality = (isset($found[3]) ? (float) $found[3] : 1.0);
$browser_langs[] = array($found[1], $quality);

if ($useUsort == 1 && array_search($quality, $qualityArr) === false){
$qualityArr[] = $quality;
} else if ($useUsort == 1){
$useUsort = 0;
}

}
unset($found);
}
}

   // Order the codes by quality
   
  if ($useUsort == 1){
    usort($browser_langs, "language_accept_order");
   }

Re: Language detection?

Posted: Fri Jan 30, 2009 8:12 am
by alby
Ok, in current SVN and next 1.5.2b I change browser detection function of PHP site, it was broken for me
Now following user preferences and RFC

Alby