php menu setting for language settings

3.4k views Asked by At

I am going to implement the multi-language support for the website using php

The default language is chinese

I have selected en as british English one.

the webpage link is set as www.abc.com/index.php?lang=en

When it comes to selecting the other menu items,

the query string ?lang=en is gone and resume to chinese language

Would you please tell me how to pass the 'en' and ?lang= to other menu so as to maintain the selection of language desired through the browsing ?

The below is my code

<html>
<body>...    
        <div id="languages">
        <?php $string = basename(__FILE__); ?>
        <?php echo "<a href=".$string.'?lang=en >';  $_SESSION['lang']= 'en'; ?><img src="images/en.png" /></a>
        <?php echo "<a href=".$string.'?lang=hk >';  $_SESSION['lang']= 'hk'; ?><img src="images/hk.png" /></a>
        <?php echo "<a href=".$string.'?lang=cn >';  $_SESSION['lang']= 'cn'; ?><img src="images/cn.png" /></a>
        </div>

    ...


      <div class="navigation">
                <div id="smoothmenu1" class="ddsmoothmenu">
                    <ul>
                        <li><a href="index.php"><?php echo $lang['MENU_HOME']; ?></a></li>
                        <!--<li><a href="#">About Club</a></li>-->
                        <li><a href="news.php"><?php echo $lang['MENU_NEWS']; ?> </a></li>
                        <li><a href="gallery.php"><?php echo $lang['MENU_GALLERY']; ?></a></li>
                        <!--<li><a href="reservation.php">Make Reservation</a></li>-->
                        <li><a href="contact.php"><?php echo $lang['MENU_CONTACTS']; ?></a></li>
                        <!--<li class="dropdown"><a href="#">Pages</a>
                            <ul>       
                                <li><a href="index.php">Home</a> </li>
                                <li><a  href="news.php">News</a></li>
                                <li><a href="blog.php">Blog</a></li>
                                <li><a href="blogpost.php">Blog Post</a></li>
                                <li><a  href="gallery.php">Gallery</a></li>
                                <li><a  href="reservation.php">Reservation</a></li>
                                <li class="nbbdr"><a href="contact.php">Contact Us </a></li>
                            </ul>
                       </li>-->                   
                   </ul>
                </div>
            </div>
2

There are 2 answers

4
Michał Prajsnar On BEST ANSWER

This fragment of code:

<?php $string = basename(__FILE__); ?>
<?php echo "<a href=".$string.'?lang=en >';  $_SESSION['lang']= 'en'; ?><img src="images/en.png" /></a>
<?php echo "<a href=".$string.'?lang=hk >';  $_SESSION['lang']= 'hk'; ?><img src="images/hk.png" /></a>
<?php echo "<a href=".$string.'?lang=cn >';  $_SESSION['lang']= 'cn'; ?><img src="images/cn.png" /></a>

Will always set your $_SESSION['lang'] to cn value, not only when you click certain anchor. Instead you should change it to:

<?php $string = basename(__FILE__); ?>
<a href="<?php echo $string; ?>?lang=en"><img src="images/en.png" /></a>
<a href="<?php echo $string; ?>?lang=hk"><img src="images/hk.png" /></a>
<a href="<?php echo $string; ?>?lang=cn"><img src="images/cn.png" /></a>

And also at the beginning of the file you should add:

<?php
if(!isset($_SESSION['lang']))
    $_SESSION['lang'] = 'cn'; // Sets default language to 'cn'

if(isset($_GET['lang']) && in_array($_GET['lang'], array('en', 'hk', 'cn')))
    $_SESSION['lang'] = $_GET['lang']; // Sets language based on URL
?>

Don't forget to add session_start() at the beginning, before previously mentioned block of code aswell, if you don't have it yet.

From now on, if you won't have ?lang=... in your URL, you will have your last lang set in $_SESSION['lang'].

To change links in the other menu (<li><a href="index.php"><?php echo $lang['MENU_HOME']; ?></a></li> and so on), you should change their href's from:

<li><a href="filename.php"><?php echo $lang['MENU_HOME']; ?></a></li>

To:

<li><a href="filename.php?lang=<?php echo $_SESSION['lang']; ?>"><?php echo $lang['MENU_HOME']; ?></a></li>
0
Dejv On

Best was to keep desired language selected is using $_SESSION variable.

Clicking on your menu link with ?lang=en should redirrect to any script, where you catch $_GET['lang'] variable and insert it into $_SESSION['lang'] variable.

Then you can simply look into $_SESSION['lang'] variable to get current language and this variable is global so you can access it from anywhere.

session_start();
if($_GET && $_GET['lang']){
  $_SESSION['lang'] = $_GET['lang'];
}