Page 1 of 1

PHP Localisation

Posted: Thu Mar 05, 2009 11:53 am
by Darkzaelus
Is there anything in the HTTP header that I can use to pick languages for php for a multilingual website?

Cheers,

Darkzaelus

Re: PHP Localisation

Posted: Thu Mar 05, 2009 4:22 pm
by VladSun
I use this:

Code: Select all

$languageFileMapping = Array(
    'en' => 'english',
    'bg' => 'bulgarian',
    'ru' => 'russian',
);
 
$defaultLanguage = 'en';
 
if (empty($_SESSION['language']))
{
    $parsedLanguages = explode(',', $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
    foreach ($parsedLanguages as $parsedLanguage)
    {
        $language = substr($parsedLanguage, 0, 2);
        if (array_key_exists($language, $languageFileMapping))
        {
            $_SESSION['language'] = $language;
            break;          
        }
    }
}
 
if (empty($_SESSION['language']))
    $_SESSION['language'] = $defaultLanguage;

Re: PHP Localisation

Posted: Thu Mar 05, 2009 4:25 pm
by Darkzaelus
Wonderful! Will keep this in mind when i start localising my code.

Cheers! Darkzaelus

Re: PHP Localisation

Posted: Thu Mar 05, 2009 4:35 pm
by VladSun
You're welcome :)
A little fix to apply:
change the IF line to:

Code: Select all

if (!empty($language) && array_key_exists($language, $languageFileMapping))
to avoid PHP warnings

Re: PHP Localisation

Posted: Thu Mar 05, 2009 4:50 pm
by Darkzaelus
Thanks once again!