Page 1 of 1

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

Posted: Wed Apr 01, 2009 7:02 pm
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