jump to navigation

PHP Browser-Language Detection and Redirecting to Relevant Language-Page April 8, 2012

Posted by Tournas Dimitrios in PHP.

I was looking for a simple way to automatically detect a users language using PHP and then redirect the user to the relevant language-page  , finally  came across an interesting snippet which does just that . It uses the PHP superglobal ‘$_SERVER[HTTP_ACCEPT_LANGUAGE]’ array  . This information is located into the header package which is send to the server during each browser request and has the following signature .

Cookie:cookname=root; cookid=f23cda2a099b52f6bd3eb6e2d3e5c1d4; cookrealid=1
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.151 Safari/535.19

My favorite browser (Chrome) , is configured to send my preferred languages in the following order :
en-US,en;q=0.8,nl;q=0.6,el;q=0.4,de;q=0.2″. It means “I prefer en-US , but will accept British English , Netherlands ,Greeks and German ” . Each language is given an associated quality value which represents an estimate of the user’s preference for the languages specified by that range. The quality value  “q=1” (default) , defines the most preferred language and en;q=0.8,nl;q=0.6 (second and third preferred language respectively ) .

On Chrome the preferred languages and it’s associated quality factor are configured by :
Click on the little wrench ->Settings ->Under-the-Hood->Languages-and-Spell-Settings .

This code simply detects what language header is set (if there is one) and redirects the user to the relevant subdomain . English speaking users get re-directed to en.domain-name.com etc. The associative array $sites contains all language-codes with it’s relevant sub-domain links :

$sites = array(
    "en" => "http://en.mysite.com/",
    "nl" => "http://nl.mysite.com/",
    "el" => "http://el.mysite.com/",
	"de" => "http://de.mysite.com/",

// Get 2 char lang code
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
// Set default language if a '$lang' version of site is not available
if (!isset($sites[$lang])) {
$lang = ‘en’;
// Finally redirect to desired location
header('Location: ' . $sites[$lang]);
exit ;

An more sophisticated snippet that extract the language relative the “q” quality factor is as follows :

function getPreferredLanguage() {
$langs = array();
// break up string into pieces (languages and q factors)
		$_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
if (count($lang_parse[1])) {
	// create a list like "en" => 0.8
	$langs = array_combine($lang_parse[1], $lang_parse[4]);
	// set default to 1 for any without q factor
	foreach ($langs as $lang => $val) {
		if ($val === '') $langs[$lang] = 1;
	// sort list based on value
	arsort($langs, SORT_NUMERIC);
//extract most important (first)
foreach ($langs as $lang => $val) { break; }
//if complex language simplify it
if (stristr($lang,"-")) {$tmp = explode("-",$lang); $lang = $tmp[0]; }
return $lang;

//This returns the most preferred langauage "q=1"
 echo getPreferredLanguage();

Links :


1. toyoa 86 - April 9, 2012

Seriously, my close friend sent me to your post. I actually could not agree more with just what you are stating!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s