Hello,
I have made a script so the user can change the language of the site.
But of some reason the user has to click twice on the flag before it changes.
On page load he doesn't see the language (sets it to the default).
This is my code:
Index.php:
And in the function:
Thanks in advance!
I have made a script so the user can change the language of the site.
But of some reason the user has to click twice on the flag before it changes.
On page load he doesn't see the language (sets it to the default).
This is my code:
Index.php:
Code:
<?
include( "inc/functions.php" );
$lang = language();
include( "lang/" . $lang . ".php" );
include( "inc/config.php" );
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" media="screen" href="template/css/index.css" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?=$title ?></title>
</head>
<body>
<div id="container">
<div id="banner_container1"></div><!-- banner1 -->
<div id="centre_content">
<div id="header">
<div id="language"><?
if(!empty( $_COOKIE['language'] )){
if( $_COOKIE['language'] == "dutch" ){
echo '<a href="' . $_SERVER['PHP_SELF'] . '?language=english"><img src="template/images/us.gif" border=0 /></a>';
}else{
echo '<a href="' . $_SERVER['PHP_SELF'] . '?language=dutch"><img src="template/images/nl.gif" border="0" /></a>';
}
}
?></div><!-- language -->
</div><!-- header -->
</div>
</div>
Code:
function language()
{
# function returns longhand form of the language.
# function expects longhand form of the language in the
# $_GET variable to switch languages (?language=english)
// Set variables
$default_language = 'english';
$supported_languages = array(
'en' => 'english',
'nl' => 'dutch'
);
// Client is setting language with query string '?language=english
if(isset( $_GET['language'] ) && in_array( $_GET['language'], $supported_languages ))
{
setcookie ('language', $_GET['language'], time()+100000000, '/', '', 0);
return $_GET['language'];
}else{
setcookie('language', $default_language, time()+100000000, '/', '', 0);
}
// Check for preset language in $_COOKIE['language']
if(empty( $_COOKIE['language']) && in_array( $_COOKIE['language'], $supported_languages ))
{
return $_COOKIE['language'];
}
// If none of the above the script finds the first
// matching browser language or returns the language
// in the $default_language variable on no match
if(empty($_SERVER['HTTP_ACCEPT_LANGUAGE']))
{
return $default_language;
}
$bar = ''; # to use in regex pattern
$pattern = '';
foreach($supported_languages as $k => $v){
$pattern .= $bar . $k;
$bar = '|';
}
if(preg_match('/' . $pattern . '/', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches))
{
return $supported_languages[$matches['0']];
}
return $default_language;
}
Comment