While you can handle the request dynamically, you need to provide another method, and here's why. According to Google if you care anything about SEO, you'll want to Make sure the page language is obvious. You can do the hat trick on the fly and switch the language via language preferences, but you also need to provide a link to a page that is always in the other language if you expect to get any traffic in that language from search engines.
If this page dynamically detects the language, then whatever language Google sees when it crawls is the language it will make the page as.
http://www.example.com/
If the site contains a query string flag for another language the site is likely to be crawled in both languages IF there is a link to the second language somewhere on your site. These are good examples (a separate domain is the best option) for showing Google where a Spanish page would reside.
http://es.example.com - best for SEO
http://www.example.com/?lang=es
http://www.example.com/es/
In your PHP code you're only catching the initial 2 digit code for the preferred language of the browser. Some people actually read in more than one language (basically anyone who's not American). They might have those other languages in their preferred languages, so while someone from France may have fr
as their preferred language, they may also understand English (en
), so their browser accepted string might look something like this in Chrome:
fr,en-US;q=0.8,en;q=0.6
or this in IE:
fr-FR,en-US;q=0.5
This code will check to see if they've set the $_GET
flag for the language and override the automatic translation. I found this code, then modified it to work for my purposes because the site where it's being used is European where most people speak multiple languages. For this code I've modified it here, so if their preferred language isn't Spanish it will default to English. This would go into a file like lang.php
which would get included on every page.
<?php
$langs = array();
if (!empty($_GET['lang'])){
$tempLang = $_GET['lang'];
//Setup the switch for all of the possible languages.
switch($tempLang){
case 'es':
$pref='es';
break;
default:
//english
$pref='en';
}
} elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
// break up string into pieces (languages and q factors)
preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_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 === '') $val = 1;
$lParse = strtolower(preg_replace('!\-.*$!','',$lang));
if(!isset($newLangs[$lParse])){
$newLangs[$lParse] = $val;
}
}
// sort list based on value
arsort($newLangs, SORT_NUMERIC);
}
//Set the preferred languages for the website (available translations)
$preferred = array('en','es');
//Check for overlap keeping the preference order
$intersect = array_values(array_intersect(array_keys($newLangs), $preferred));
//print_r($newLangs);
if(isset($intersect[0])){
//Set the first preference
$pref = $intersect[0];
if(preg_match('!^es!',$pref)){
$pref='es';
}
} else {
//default to english
$pref = 'en';
}
} else {
//default to english
$pref = 'en';
}
?>
On the page including lang.php
you would need to check the $pref
variable to show the proper translation for the language.
Javascript
You can do a redirect with Javascript to make the page load the other language using this same code, however search engines typically only look at Javascript for exploits and won't follow the links in the Javascript. According to Google Webmaster's Guidelines:
Use a text browser such as Lynx to examine your site, because most
search engine spiders see your site much as Lynx would. If fancy
features such as JavaScript, cookies, session IDs, frames, DHTML, or
Flash keep you from seeing all of your site in a text browser, then
search engine spiders may have trouble crawling your site.
You can do the change with a click using AJAX, however the URL where AJAX would listen would need to be passed the parameter and these are usually passed via $_GET
variables.
IP Look-up
Another method some people have tried is to check the IP address and reverse look-up the location to see where the country of origin is, then they'll pick the national language. The problems with this are:
- People travel
- Some IP addresses are registered to owners in other countries (think corporate blocks) rather than the country where the user is browser.
- Some of the IP address listings are highly inaccurate.
Cookies
Not all users accept cookies and some corporate firewalls may even strip the cookies.