Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto detect language and redirect user

I am doing my own website and I managed to write some code that makes directs user to the language version according to the browser's language. Here is the script:

<?php
  if ($_SERVER["HTTP_ACCEPT_LANGUAGE"] == "sv")
    header("location: index.php");
  if ($_SERVER["HTTP_ACCEPT_LANGUAGE"] == "pt")
    header("location: pt/index.php");
  else 
    header("location: en/index.html");
?>

I have put this in the index.php before the . It seems to be working because I am not in an English speaking country but my browser is in English and I am being redirected to the English version.

Is this correct? Is there a better/cleaner way to do this?

like image 516
viriato Avatar asked Jul 23 '13 07:07

viriato


2 Answers

PHP 5.3.0+ comes with locale_accept_from_http() which gets the preferred language from the Accept-Language header.

You should always prefer this method to a self-written method as the header field is more complicated than one might think. (It's a list of weighted preferences.)

You should retrieve the language like this:

$lang = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);

But even then, you won't just have en for every English user and es for Spanish ones. It can become much more difficult than that, and things like es-ES and es-US are standard.

This means you should iterate over a list of regular expressions that you try and determine the page language that way. See PHP-I18N for an example.

like image 77
caw Avatar answered Oct 19 '22 19:10

caw


Well, I came across some problems with my code which is no surprise due to I am not a PHP expert. I kept therefore on searching for a possible solution and I found the following code on another website:

<?php
    // Initialize the language code variable
$lc = ""; 
    // Check to see that the global language server variable isset()
    // If it is set, we cut the first two characters from that string
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
    $lc = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);

    // Now we simply evaluate that variable to detect specific languages
if($lc == "fr"){
    header("location: index_french.php");
    exit();
} else if($lc == "de"){
    header("location: index_german.php");
    exit();
}
else{ // don't forget the default case if $lc is empty
    header("location: index_english.php");
    exit();
}
?>

This did the job perfectly! I only had a problem left. There was no way to change language, even with direct links into another language because as soon as the page was loading, the php block would redirect me to the borwser's language. This can be a problem if you are living in another country and have for instance Swedish as a mother language but you have your browser in English because you bought your computer in the UK.

So my solution for this issue was to create folders with a duplicate version for every language (even the one for the main language) without this php code on the index.html (and therefore not index.php). So now my website is auto detecting the language and the user also has the option to change it manually in case of they want!

Hope it will help out someone else with the same problem!

like image 28
viriato Avatar answered Oct 19 '22 19:10

viriato