Language specific response

Knowing the languages selected in the browser of our visitors may be a very important data we may used to redirect the user to a specific page in the corresponding language, or to display specific information directed to the user (as for example the most suitable ad).

The basic command we will use to know the languages selected in the browser is this one:

$_SERVER["HTTP_ACCEPT_LANGUAGE"]


In the table bellow we have used an script to identify the languages selected in your browser.  You may get the complete code here.

- not identified

In the table bellow are displayed some values for $_SERVER["HTTP_ACCEPT_LANGUAGE"] obtained from visitors to my home page.

en-us 
en-gb
de
nl
en-us,zh;q=0.5
es
en
nl-be
it
eu,en,es
en-za
en-us,el;q=0.5
en-ie
pt-br
fr-ca
es-mx
en-us,hi;q=0.5
zu;q=0.3
th
sv
sl
ro
pl
in
he
fr-ch
fr
en-ca
en-au
en;q=0.7,
en,fr-CA
de-ch

Redirecting visitors to lenguage specific pages

Let suppose we have our information in English and Spanish, and we will also suppose spanish is not the more used language by our visitors (our default language will be English)

By using  $_SERVER["HTTP_ACCEPT_LANGUAGE"] we may know whether Spanish is one of the languages selected in the browser of our visitor. If it is so, we may consider to situations:

  • Spanish is the first language (p.e: Request.ServerVariables("HTTP_ACCEPT_LANGUAGE") value is "es,en")
  • Spanish is not the first langauge (p.e: Request.ServerVariables("HTTP_ACCEPT_LANGUAGE") value is "en,es")
We may consider also two other situations:
  • If Spanish is in the list, the user knows Spanish
  • The user knows Spanish only when Spanish is the first langauge in the list
Option 1 (If Spanish is in the list, the user knows Spanish, so we will redirect the user to the page in Spanish)
 
defauldpage.php
<?
if (strpos(" ". $_SERVER["HTTP_ACCEPT_LANGUAGE"], "es")>0){
    include("spanishpage.html");
}else{
    include("englishpage.html");
?>

In line 2 we will check whether substring "es" is included in the variable $_SERVER["HTTP_ACCEPT_LANGUAGE"]. 

If comparation is true, a Spanish file is included (line 3).
If the comparation is false, a English file is included (line 5).
 
 

Option 2 (Spanish is in the list, but the user only knows Spanish when this language is the first one in the list. If it is so, we will show the page in Spanish)
 

defauldpage.php
<?
if (substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2) == "es"){
    include("spanishpage.html");
}else{
    include("englishpage.html");
?>

In line 2 we will get the two first positions from $_SERVER["HTTP_ACCEPT_LANGUAGE"] by using function substr, and we will check whether its value is "es".

Showing lenguage specific ads or text

This example will work basically in the same way shown for previous example.

In this case we will assume that  if Spanish is in the list of lenguages selected in the browser, the user knows Spanish, so we will display an ad or text in Spanish. In the script bellow we will show ads:
 

languagespecificad.php
<html> 
<title>My page</title> 
<body> 

<? if (substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2) == "es"){ ?>
   <a href=http://www.link1.com><img scr=spanishad.gif></a>
<? }else{ ?>
    <a href=http://www.link2.com><img scr=englishad.gif></a>
<? } ?>

 My info

 </body> 
 </html>

Spanish ad is the red code (if comparation is true) and English one is in magenta (if comparation is false).
In order to display text instead of ads or figures, we just need to substitute the corresponding code.