Re: _SERVER["HTTP_ACCEPT_LANGUAGE"] en-us

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 17 Oct 2006 08:18:47 +0100, Stut wrote:

> John Taylor-Johnston wrote:
>>
>> This is what http_accept_language gives me depending on which browser. 
>> Depending on the visitor in my region, it will either be French or 
>> English.
>>
>> _SERVER["HTTP_ACCEPT_LANGUAGE"] en-us,en;q=0.8,fr;q=0.5,fr-ca;q=0.3
>> _SERVER["HTTP_ACCEPT_LANGUAGE"] fr-ca,en-us;q=0.5
>>
>>
>> Is this a reasonable approach?
>>
>> if(stristr($_SERVER["HTTP_HOST"],"fr"))
>> { include("french.htm");}else{ include("english.htm");}
> 
> No it's not unless you want to ignore the users preferred language. The 
> order is important in that header - the first language is the preferred 
> language. So in the first example you should serve english.htm and 
> french.htm in the second. If you only have english and french, your best 
> bet is to do the following...
> 
> if (substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) == 'fr')
> {
>     include('french.htm');
> }
> else
> {
>     include('english.htm');
> }

Not to annoy you, but this will fail if a user has the preferred
languages in the order Dutch, French, English (which would be very
likely for a Belgian user). If you really want to deal with it
while totally respecting the user's wishes, you'd want to explode() the
string using ',', and analyze the given array in the right order:

// Available languages & user preferences.
$aLang = array('fr' => 'french.htm', 'en' => 'english.htm');
$aSett = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);

// Make sure that if no languages match, English is picked.
$aSett[] = 'en'; 

// Loop the user preference.
foreach ($aSett as $sLang) {
    $sLang = substr($sLang, 0, 2);
    if (array_key_exists($sLang, $aLang)) {
        // Language found.
        include $aLang[$sLang];
        break;
    }
}

HTH,

Ivo

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux