Hi,

I'm getting a little paranoid with a dual language website I built.

What I want to happen:

If a user arrives with an EN browser or a browser with EN as the primary language setting then the language of the site is in English.

If any other language then the default language of the site is ES (Spanish).

In both cases a cookie is set to store the data.

In both cases the user has the option to change the language (and the cookie be updated).

I thought I had this working, but have noted erratic behaviour with languages in the last few days (language not being set, changing from page to page).

My first code was as follows:

//SET LANGUAGE
if(isset($setlang)) {
$lang = $setlang;
setcookie ("langset", "$setlang");
} else {
if(isset($langset)) {
$lang = $langset;
} else {
if (!isset($lang)) $lang=substr($HTTP_ACCEPT_LANGUAGE,0,2);
setcookie ("langset", "$lang");
}
}

One problem I quickly realsed with this version was that it did not take account of users with browsers that were not ES or EN. So a modified version was produced to set the language to ES where the ACCEPT_LANGUAGE request was neither ES or EN:

//SET LANGUAGE
if(isset($setlang)) {

// IF LANG IS NOT ES OR EN SET TO ES
// ADDED 03032003 TO FIX LANG ERRORS
if (($setlang != "en") && ($setlang != "es")) {
	$setlang = "es";
	}

$lang = $setlang; 
setcookie ("langset", "$setlang"); 

} else {
if(isset($langset)) {
$lang = $langset;
} else {
if (!isset($lang)) $lang=substr($HTTP_ACCEPT_LANGUAGE,0,2);
// IF LANG IS NOT ES OR EN SET TO ES
// ADDED 03032003 TO FIX LANG ERRORS
if (($lang != "en") && ($lang != "es")) {
$lang = "es";
}

	setcookie ("langset", "$lang"); 
} 

}

Now, after all is said and done I'm a little confused as the newer script still seems to give erratic behaviour and I've been looking at it too long to see clearly.

Can anyone offer advice or suggest a better way to do this?

The site is question (now back to version one of the script) is at http://www.circulodelarte.com/

Many thanks,

Gary Crighton

    Hi!

    I tried this, and it seems to work. My tip is that you try this out in a simple script first, then apply it to the site.
    Nice site, by the way 🙂

    <?PHP
    if(!isset($lang)) {
    $language = substr($HTTP_ACCEPT_LANGUAGE,0,2);
    if($language == "no") {
    $langset = "no";
    } else {
    $langset = "en";
    }
    setcookie ("lang", "$langset");
    header("Location:language.php");
    }
    
    if(isset($change_lang)) {
    $langset = $change_lang;
    setcookie ("lang", "$langset");
    header("Location:language.php");
    }
    ?>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    
    <html>
    <head>
    <title>My little language switch :-)</title>
    </head>
    
    <body>
    <?PHP
    
    
    if($lang == "no") {
    echo "<a href=\"language.php?change_lang=en\">Forandre til engelsk</a><br>";
    echo "Heisann montebello!";
    } else {
    echo "<a href=\"language.php?change_lang=no\">Change to norwegian</a><br>";
    echo "Hellu, Monty python!";
    }
    ?>
    
    
    </body>
    </html>
    

    (There's one or two lines of norwegian, but I guess you'll manage 😉 )

    knutm

      Write a Reply...