Language Adaptive Website Demo

Your browser language preferences: en-us,en;q=0.5

I will use $_SERVER[HTTP_ACCEPT_LANGUAGE] to decide which language to return. Lets use the following rules:

  1. This website can serve pages in Chinese[zh], Japanese[ja] and English[en]
  2. If most preferred language is one of above then serve page in that language
  3. If most preferred language is not Chinese or Japanese then serve English page ie English is the default fallback language
  4. I will assume that the browser sends the languages in priority order and that the first language has no qvalue
Array ( [0] => en-us [1] => en;q=0.5 )

I do not have a page in your language en-us so here it is in English!

Source

<?php
header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN">
<html>
<head>
<style type="text/css">
h3{background-color:#77ff77;}
</style>
<title>Language Adaptive</title>
</head>
<body>
<h1> Language Adaptive Website Demo</h1>
<?php
echo '<p> Your browser language preferences: '.$_SERVER['HTTP_ACCEPT_LANGUAGE'].'</p>';
?>
<p>
I will use $_SERVER[HTTP_ACCEPT_LANGUAGE] to decide which language to return. Lets use the following rules:
</p>
<ol>
<li>This website can serve pages in Chinese[zh], Japanese[ja] and English[en]</li>
<li>If most preferred language is one of above then serve page in that language</li>
<li>If most preferred language is not Chinese or Japanese then serve English page ie English is the default fallback language</li>
<li>I will assume that the browser sends the languages in priority order and that the first language has no qvalue</li>
</ol>

<?php
$languages=explode(',',$_SERVER[HTTP_ACCEPT_LANGUAGE]);
print_r($languages);
$most_preferred=$languages[0];
switch($most_preferred){
  case 'zh':
    echo '<h3>As requested, here is the page in Chinese!</h3>';
    break;
  case 'ja':
    echo '<h3>As requested, here is the page in Japanese!</h3>';
    break;
  case 'en':
    echo '<h3>As requested, here is the page in English!</h3>';
    break;
  default:
    echo '<h3>I do not have a page in your language '.$most_preferred.' so here it is in English!';
    break;
}
?>

</body>
</html>