0

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>
4

2 回答 2

2

这段代码:

<?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>

将始终设置您$_SESSION['lang']cn价值,不仅在您单击某个锚点时。相反,您应该将其更改为:

<?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>

并且还应该在文件的开头添加:

<?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
?>

如果您还没有,请不要忘记session_start()在前面提到的代码块之前添加。

从现在开始,如果您?lang=...的 URL 中没有,您将在$_SESSION['lang'].

要更改其他菜单中的链接(<li><a href="index.php"><?php echo $lang['MENU_HOME']; ?></a></li>等等),您应该更改它们的href's来源:

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

至:

<li><a href="filename.php?lang=<?php echo $_SESSION['lang']; ?>"><?php echo $lang['MENU_HOME']; ?></a></li>
于 2013-08-27T14:27:34.500 回答
0

最好的方法是使用$_SESSION变量来选择所需的语言。

单击您的菜单链接?lang=en应该重定向到任何脚本,您可以在其中捕获 $_GET['lang'] 变量并将其插入到 $_SESSION['lang'] 变量中。

然后你可以简单地查看 $_SESSION['lang'] 变量来获取当前语言,这个变量是全局的,所以你可以从任何地方访问它。

session_start();
if($_GET && $_GET['lang']){
  $_SESSION['lang'] = $_GET['lang'];
}
于 2013-08-27T14:23:53.233 回答