Change language - click 2 times...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • djpaul
    New Member
    • Oct 2006
    • 137

    #1

    Change language - click 2 times...

    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:
    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>
    And in the function:
    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;
    }
    Thanks in advance!
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    On lines #25 and #27 on your first script you are checking $_COOKIE['language'], where you should be checking the $lang variable.

    The $_COOKIE doesn't change immediately after you set it using setcookie(). It only changes the following request, once the cookie has been sent to the browser and the browser returned it to PHP.

    So, even though your language function seems to work fine, your main code is using the old language, rather than the new language.

    Comment

    • djpaul
      New Member
      • Oct 2006
      • 137

      #3
      Damn, so simple....
      Thanks, it works great now!!

      Case closed :)

      Comment

      Working...