Home » Php » How to trigger site language change using php?

How to trigger site language change using php?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am making a site, using only PHP as back-end (no framework), which should be available in 3 languages. The site structure (tables/divs) is maybe less common. It tends to simulate an Ajax kinda behavior. I include a file (selector.php) in the body, which takes care of it’s content, via _GET(), passed from the navigation links. This means, that the site actually never leaves index.php. The languages are links (flags).

Now, I did managed to implement successfully the 3 languages (arrays/session/cookie), and it does work. However, what I need is to be able to make the language selection to stay/refresh the same page from where it was called. Here it becomes tricky, since I am using only one page, every include/subsequent includes (30+) are called from the index.php, so using the header ('Location: ...') seems not to be an option.
I know how to retrieve my current address, including the GET parameter, so I could use it. But how? Reading it at the beginning of the code, then re-read in the language module does match at any given point. There is a session, so I could also use session variables, but making 35+ variables for the content (so url changes) and match them all the time, does not seem to be the right method. Where, and more precisely how to catch the language change, and how to refresh/redirect to the “same page” for the changes to take effect? It could be a design flow.

Note 1: Using mod_rewrite I redirected my language query, so now instead of mysite.com/index.php?lang=en, it’s in form of mysite.com/en/index.php so I can use more friendly links to call the body content.
Note 2: I passed the last week reading/searching here/anywhere Google leads, trying countless times different approaches, none of them worked as expected (maybe concept flow?!).
Note 3: Since this is for a client, who explicitly asked not to use Java at all, please don’t give Ajax/jQuery/Java solutions (with those, I would know how to do).
Note 4: The whole code is rather large to paste here, so if you need to see different parts, name them, and I’ll edit my question.
Note 5: I still retain myself as a beginner, so please bear with me.

Thanks in advance.

So the language selector looks like that:

    session_start();
    header('Cache-control: private'); // IE 6 FIX
    if(isSet($_GET['lang'])) {
        $lang = $_GET['lang'];
        $_SESSION['lang'] = $lang;
        setcookie("lang", $lang, time() + (3600 * 24 * 30)); }
    else if(isSet($_SESSION['lang'])) {
        $lang = $_SESSION['lang']; }
    else if(isSet($_COOKIE['lang'])) {
        $lang = $_COOKIE['lang']; }
    else {
        $lang = 'ro'; }

    switch ($lang) {
    case 'en': $lang_file = 'lang.en.php'; break;
    case 'it': $lang_file = 'lang.it.php'; break;
    case 'ro': $lang_file = 'lang.ro.php'; break;
    default: $lang_file = 'lang.ro.php'; break; }

    include_once 'languages/'.$lang_file; 

Then the selector…

    function isValidPage($cont) {
     $validpages = array("history","mission","etc");
     if(in_array($cont,$validpages) && file_exists(ltrim($cont,'/') . '.php')) {
          return TRUE; } 
    else {
          return FALSE; }
    }

    if(isset($_GET['cont']) && isValidPage($_GET['cont'])) {
        @include($_GET['cont'] . '.php'); } 
    else {
        @include('sub_index.php'); }  

I start my index.php with:

      <?php include_once 'lang_sel.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>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>mysite</title>
    <link rel="stylesheet" href="style/css.css" type="text/css" />   

</head>
<body>
    <div class="parent">
        <div class="left"><?php include("main/left_main.php"); ?></div>
        <div class="right">
            <div class="topmain"><?php include("main/top_main.php"); ?></div>
            <div class="dummyx"></div> <!-- dummy div for adjusting starting position -->
            <div class="rightall">
                <?php include("main/top_nav.php"); ?>
                <?php /*include("main/top_rotator.php"); */?>
                <table width="780" border="0" cellpadding="10px" cellspacing="0" bgcolor="transparent" id="middle"><?php include ('selector.php'); ?></table> <!-- container for the main content -->
            </div>       
            <div class="footer">
                <?php include("main/bottom.php"); ?>
                <?php include("main/footer.php"); ?>
            </div>
        </div>
        <div class="clr"></div> <!-- clearing div attributes -->
    </div>  

Then the left side contains the flags:

<tr>
    <td width="30">&nbsp;  </td>
    <td width="63" align="center" valign="middle"><span class="lang"><a href="<?=WEBROOT?>ro/index.php" title="Romana"><img src="images/flags_ro.jpg" alt="ro" name="Romana" width="35" height="40" /></a></span></td>
    <td width="63" align="center" valign="middle"><span class="lang"><a href="<?=WEBROOT?>en/index.php" title="English"><img src="images/flags_en.jpg" alt="en" name="English" width="35" height="40" /></a></span></td>
    <td width="63" align="center" valign="middle"><span class="lang"><a href="<?=WEBROOT?>it/index.php" title="Italiano"><img src="images/flags_it.jpg" alt="it" name="Italiano" width="35" height="40" /></a></span></td>
    <td width="30">&nbsp;  </td>
</tr>

Also the menu, which is a bunch of lists like:

        <ul class="menu">   
            <li><a href="index.php?cont=history" style="background:none"><?php echo $lang['MENU_HISTORY']; ?></a><!--<div><span>if you want to add 'sub-content' put it here</span></div>!--></li>
            <li><a href="index.php?cont=mission" style="background:none"><?php echo $lang['MENU_MISSION']; ?></a></li>
            <li><a href="index.php?cont=p_systems" style="background:none"><?php echo $lang['MENU_P_SYSTEM']; ?></a></li>
            <li><a href="index.php?cont=s_dist" style="background:none"><?php echo $lang['MENU_S_DIST']; ?></a></li>
        </ul>

The content is simple, text and images. So the main code is above…

How to&Answers:

After 1 trillion trials/errors, I finally made it! Maybe it’s not the most elegant solution (ie. spartan), but it works. The catch was to read the current URL, explode it to get the current page with params, then put it in the href’s of the flags.

So at the beginning of index.php I’ve put:

<?php 
    $url = $_SERVER['REQUEST_URI']; 
    $pieces = explode("/", $url); 
?>

Then my flag’s links became:

<a href="<?=WEBROOT . 'en/' . $pieces[3]?>" title="English">

where WEBROOT is my base url and folder, and $pieces[3] is always my current page (ie. index.php?cont=history. This way I always land on the current page, and the language is changed accordingly.

Note: In my case. the 3rd piece contains the current page. If someone else use this technique, be sure to check that. Also, I’m using only 1 parameter all the time, so keep that in mind (tho it doesn’t actually matter).

If someone else knows a better solution, I’m open minded. Thanks for everyone tho.

Answer:

See if this makes sense for your needs. http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_8910-A-Polyglot-Web-Site-in-PHP.html