Forum Moderators: coopster
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]
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
}
?>