multi-lingual language selection- can you build it better?

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
itp
Forum Commoner
Posts: 67
Joined: Fri Jun 15, 2007 6:50 am

multi-lingual language selection- can you build it better?

Post by itp »

Here is what I am trying to do: I am developing a multi-lingual site containing several forms. User must be able to click one of several "language selection" links (eg: ENG,FR,SP) on top right of page at any time which passes a $_GET variable back to program which will load appropriate language variables from language file and present page in appropriate language. I would then load the language preference as a $_SESSION variable for future reference.

Here is my first crack at it. It works but is clunky. Can anyone proposed a better or more idiomatic approach? or any improvements (I would like to avoid an Ajax or mod rewrite type approach here)

Code: Select all

 
<?php
session_start();
error_reporting(E_ALL);
 
$number = isset($_REQUEST['number']) ? $_REQUEST['number'] : '';
$language = isset($_REQUEST['language']) ? $_REQUEST['language'] : '';
 
// on "normal" navigation (no language changes) capture all variables and store values as session
if (strlen($language) == 0) 
{
    foreach($_REQUEST AS $key => $value) 
    {   
        $_SESSION[$key] = $value;
        echo $key . " = " .$_REQUEST[$key] . "<br>";                
    }
}
else
{
    // first set new language requested in Session variable
    $_SESSION['language'] = $language;
 
    // then bring back previouly stored variables
    foreach($_SESSION as $key=>$value) 
    {     
        $_REQUEST[$key] = $value;
        echo $key . " = " .$_REQUEST[$key] . "<br>";        
    }
 
    // make sure than we have not lost value of number
    $number = isset($_REQUEST['number']) ? $_REQUEST['number'] : '';
    echo "<br> Number is still " .$number;
}
 
$language = isset($_SESSION['language']) ? $_SESSION['language'] : '';
echo "<br> Language is " .$language;
 
to test:
http://myhost/myPgm.php?number=22
http://myhost/myPgm.php?language=E
Post Reply