I'm using this script:

$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang){
    case "nl":
        //echo "PAGE FR";
        include("talen/nl.php");//include check session FR
        break;
    case "es":
        //echo "PAGE IT";
        include("talen/es.php");
        break;
    case "en":
        //echo "PAGE EN";
        include("talen/en.php");
        break;        
    default:
        //echo "PAGE EN - Setting Default";
        include("talen/en.php");//include EN in all other cases of different lang detection
        break;
}

But when I set browser language to "af" it doesnt get the default english language...

How can I make that happen?

EDIT:

Because I have no case "af" it doesnt show anything...

When there is no case like "af" I want the default to be "en" but that doesnt work

Edit2:

I'm using this now:

<?
$accept_lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$accept_lang = str_replace(' ', '', $accept_lang);

$arr = explode(',', $accept_lang);

$lang = "en";
$q = 0;

if (count($arr) > 0) {
    foreach ($arr as $a) {
    $l = explode(';', $a);

    if (count($l) == 1) {
        $l[1] = 'q=1';  
    }

    $l[1] = str_replace('q=', '', $l[1]);

    if ($l[1] > $q) {
        $q = $l[1];
    $lang = $l[0];  
    }
}
}

$lang = preg_replace('/-(.*)/', '', $lang);

switch ($lang){
    case "nl":
        //echo "PAGE FR";
        include("talen/nl.php");//include check session FR
        break;
    case "es":
        //echo "PAGE IT";
        include("talen/es.php");
        break;
    case "en":
        //echo "PAGE EN";
        include("talen/en.php");
        break;        
    default:
        //echo "PAGE EN - Setting Default";
        include("talen/en.php");//include EN in all other cases of different lang detection
}
?>

But my $lang is still set to "af" and not the default "en"

EDIT 3:

Thanks for your help:

When I try use this:

print $_SERVER['HTTP_ACCEPT_LANGUAGE'];
switch ($lang){
    case "nl":
        //echo "PAGE FR";
        print("nl");//include check session FR
        break;
    case "es":
        //echo "PAGE IT";
        print("es");
        break;
    case "en":
        //echo "PAGE EN";
        print("en");
        break;        
    default:
        //echo "PAGE EN - Setting Default";
        print("en");//include EN in all other cases of different lang detection
        break; 
}

It print out this:

af,nl;q=0.8,en;q=0.5,en-us;q=0.3en

But I still got "af".. how

Just for you to know... I got my browser language set to "af" to test it.. just that you know... But it should be load the default with your sript...

有帮助吗?

解决方案

You should use something like this script I wrote some time ago, which checks for the language with the best quantity value:

$accept_lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$accept_lang = str_replace(' ', '', $accept_lang);

$arr = explode(',', $accept_lang);

$lang = 'en';
$q = 0;

if (count($arr) > 0) {
    foreach ($arr as $a) {
    $l = explode(';', $a);

    if (count($l) == 1) {
        $l[1] = 'q=1';  
    }

    $l[1] = str_replace('q=', '', $l[1]);

    if ($l[1] > $q) {
        $q = $l[1];
    $lang = $l[0];  
    }
}
}

$lang = preg_replace('/-(.*)/', '', $lang);

Then you can switch() over $lang like you did.

Omit the break; on the default: case, though.

Edit:

I changed $lang = 'de'; to $lang = 'en';. This is the default language that is taken if HTTP_ACCEPT_LANGUAGE is empty.

其他提示

Just try to do one think. remove break; from default case.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top