tags:

views:

268

answers:

3

Hi! Im a hobbycoder that is trying to build his first multilangual site

I use this piece of code to detect users language. If you havent chosen a language it will include your language file based on HTTP_ACCEPT_LANGUAGE i dont know where it gets it from tho..

session_start();

if (!isset($_SESSION['lang'])) {
   $_SESSION['lang'] = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
}

elseif (isset($_GET['setLang']) && $_GET['setLang'] == 'en') $_SESSION['lang'] = "en";
elseif (isset($_GET['setLang']) && $_GET['setLang'] == 'sv') $_SESSION['lang'] = "sv";
elseif (isset($_GET['setLang']) && $_GET['setLang'] == 'pl') $_SESSION['lang'] = "pl";
elseif (isset($_GET['setLang']) && $_GET['setLang'] == 'fr') $_SESSION['lang'] = "fr";

include('languages/'.$_SESSION['lang'].'.php');

It works for me and includes the polish lang file. But is this code accurate? Or is there another way?? How do you think YouTube does this for example?

Would be great if some english, french, swedish or polish folks could visit my site and see if it includes the right file! So i know that it doesnt just work for me :) http://jorm.homeftp.org/

Anyway you think I could improve my code? it it will looks messy as i add more languages with all those elseif!

Thanks

+8  A: 

The browser generally sends a HTTP header, name Accept-Language, that indicates which languages the user is willing to get.

For instance, this header can be :

Accept-Language: en-us,en;q=0.5

There is notion of priority in it, btw ;-)

In PHP, you can get this in the $_SERVER super global :

var_dump($_SERVER['HTTP_ACCEPT_LANGUAGE']);

will get me :

string 'en-us,en;q=0.5' (length=14)

Now, you have to parse that ;-)


If I edit my preferences in the browser's option to say "I want french, and if you don't can't serve me french, get me english from the US ; and if you can't get me that either, just get me english), the header will be :

Accept-Language: fr-fr,en-us;q=0.7,en;q=0.3

And, from PHP :

string 'fr-fr,en-us;q=0.7,en;q=0.3' (length=26)


For more informations, you can take a look at section 14.4 of the HTTP RFC.

And you probably can find lots of code example in PHP to parse that header ; for instance : Parse Accept-Language to detect a user's language

Have fun !

Pascal MARTIN
awesome answer thank you!
You're welcome :-)
Pascal MARTIN
A: 

Here's the script I used for a bi-lingual site. It is to be used as index.php of mysite.com. Based on the user's browser's language preference, it would redirect to desired language version of the site or the default language site if the site in user's preferred langauge was not available.

<?php
// List of available localized versions as 'lang code' => 'url' map
$sites = array(
    "en" => "http://en.mysite.com/",
    "bn" => "http://bn.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 (!in_array($lang, array_keys($sites)))
    $lang = 'en';

// Finally redirect to desired location
header('Location: ' . $sites[$lang]);
?>
Imran
+1  A: 

Your code looks just fine. You might want to add a final else default choice if the visitor asks for a language you aren't providing. Also, if the visitor himself selects a language you should save that choice in a persistent cookie and check its value, giving it precedence over HTTP_ACCEPT_LANGUAGE.

As far as I can tell Youtube does use HTTP_ACCEPT_LANGUAGE, but at the same time uses IP geolocation to suggest a change in language if the langauge of the visitor's country doesn't match that. Definitely annoying.

Just nitpicking: if you're gonna add languages to the list a switch() statement might be more readable.

djn