Forum Moderators: coopster

Message Too Old, No Replies

HTTP_ACCEPT_LANGUAGE problems

HTTP_ACCEPT_LANGUAGE == 'en-gb' does not work in Firefox

         

ivacho

12:42 pm on Aug 1, 2005 (gmt 0)

10+ Year Member



In my PHP code of index.php i lookup to see the language the visitor has. I do that with HTTP_ACCEPT_LANGUAGE. Only Internet Explorer does it good. In Firefox most of the times the language is not recognize at all.

What do i do wrong or forget for Firefox to get the language correct?

Here's a part of my index.php:

<?php if (!isset($_SESSION['language']))
{
$_SESSION['language'] = $_SERVER["HTTP_ACCEPT_LANGUAGE"];
}
if (isset($_GET['language']))
{
$_SESSION['language'] = $_GET['language'];
}

if ($_SESSION['language'] == 'en-us'
OR $_SESSION['language'] == 'en'
OR $_SESSION['language'] == 'us'
OR $_SESSION['language'] == 'en-gb'
OR $_SESSION['language'] == 'en-za'
OR $_SESSION['language'] == 'en-ie'
OR $_SESSION['language'] == 'en-ca'
OR $_SESSION['language'] == 'en-au')
{
include("global/menu_text/en.php");
}
elseif ($_SESSION['language'] == 'de'
OR $_SESSION['language'] == 'de-at'
OR $_SESSION['language'] == 'de-lu'
OR $_SESSION['language'] == 'de-ch'
OR $_SESSION['language'] == 'de-li')
{
include("global/menu_text/de.php");
}
?>

[edited by: BlobFisk at 12:52 pm (utc) on Aug. 1, 2005]
[edit reason] No URLs please! See TOS [webmasterworld.com] [/edit]

encyclo

1:32 pm on Aug 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to the forums, ivacho.

I think the problem is the way you're handling the

HTTP_ACCEPT_LANGUAGE
with your script. If you echo the variable, what do you see? For my version of Firefox, I get this:

en-ca,en;q=0.8,fr-ca;q=0.5,fr;q=0.3

As you see,

HTTP_ACCEPT_LANGUAGE
can contain several different language variants, each with a quotient indicating their order of importance. The best way of parsing it is to create an array and check each value of the array in turn until you find one which is appropriate. If you take my string and split it at the commas:

en-ca
en;q=0.8
fr-ca;q=0.5
fr;q=0.3

As you only have one version of your page for each language, you can just parse the first two letters:

en
en
fr
fr

So in my case, I should get English first, or if that is not available, I should get French. The following is a good starter:

<?php
// PHP detect language script.

//check first to see if they've been nice and
//set the language
if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) {

//grab all the languages
$langs=explode(",",$_SERVER["HTTP_ACCEPT_LANGUAGE"]);

//start going through each one
foreach ($langs as $value) {

//select only the first two letters
$choice=substr($value,0,2);

//include the different language page
//based on their first chosen language
switch ($choice) {
case "en":
include("global/menu_text/en.php
break;
case "de":
include("global/menu_text/de.php
break;
default:
include("global/menu_text/en.php
}
}
}

//If the language is not set then use this
//as default
else {
include("global/menu_text/en.php
}
?>

ivacho

1:55 pm on Aug 1, 2005 (gmt 0)

10+ Year Member



Thanks, i write this piece of code:
$_SESSION['language'] = substr($_GET['language'],0,2);
now i have to test it with FF.

ivacho

2:00 pm on Aug 1, 2005 (gmt 0)

10+ Year Member



$_SESSION['language'] = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2);