Attached is a quick-n-dirty PHP script which should parse the Accept-language header correctly and pick the most preferred known language. Ie; if the first language is not in our known list, we keep going to the next ones until we find one or run out.
-- brion vibber (brion @ pobox.com)
<?
$knownlangs = array( "de", "en", "eo", "fr" ); #etc $lang = "en"; # Last-ditch fallback, it's the most developed wiki $lastquality = 0.0;
# Note HTTP reference RFC 2616 - ftp://ftp.isi.edu/in-notes/rfc2616.txt
$langtag = '((?:[a-zA-Z]{1,8})(?:-[a-zA-Z]{1,8})*)'; $qvalue ='(0(?:.[0-9]{1,3})?|1(?:.0{1,3}))'; $eachbit = '^' . $langtag . '(?:;q=' . $qvalue . ')?(?:,\s*)?(.*)$';
$alh = trim( $HTTP_SERVER_VARS["HTTP_ACCEPT_LANGUAGE"] );
echo "$alh<br>\n\n";
while(strlen($alh)) { if( preg_match( "/$eachbit/", $alh, $m ) ) { $tag = $m[1]; $quality = $m[2]; if(strlen($quality) == 0) $quality = 1; $alh = $m[3]; #echo "language '$tag' quality '$quality'\n"; if(in_array($tag, $knownlangs) and $quality > $lastquality) { $lang = $tag; $lastquality = $quality; } } else { break; } }
echo "Using language $lang with preference $lastquality";
?>
wikitech-l@lists.wikimedia.org