如何使用PHP触发网站语言更改?
我正在创建一个站点,只使用
PHP作为后端(没有框架),它应该有3种语言版本.站点结构(表/ div)可能不太常见.它倾向于模拟Ajax有点行为.我在正文中包含一个文件(selector.php),它通过导航链接传递的_GET()来处理它的内容.这意味着,该网站实际上永远不会离开index.php.语言是链接(标志).
现在,我确实设法成功实现了3种语言(数组/会话/ cookie),它确实有效.但是,我需要的是能够使语言选择保持/刷新它所在的页面.这里变得棘手,因为我只使用一个页面,每个包含/后续包含(30)都是从index.php调用的,所以使用标题(‘Location:…’)似乎不是一个选项. 注1:使用mod_rewrite我重定向了我的语言查询,所以现在代替mysite.com/index.php?lang=en,它是mysite.com/en/index.php的形式,所以我可以使用更友好的链接来调用身体内容. 提前致谢. 所以语言选择器看起来像这样: 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; 那么选择器…… 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'); } 我用以下内容启动index.php: <?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> 然后左侧包含标志: <tr> <td width="30"> </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"> </td> </tr> 还有菜单,这是一堆列表,如: <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> 内容简单,文字和图像.所以主要代码是……
经过1万亿次试验/错误,我终于成功了!也许它不是最优雅的解决方案(即斯巴达),但它确实有效.问题是要读取当前URL,将其展开以获取带有参数的当前页面,然后将其放入标志的href中.
所以在index.php的开头我放了: <?php $url = $_SERVER['REQUEST_URI']; $pieces = explode("/",$url); ?> 然后我的旗帜的链接变成了: <a href="<?=WEBROOT . 'en/' . $pieces[3]?>" title="English"> 其中WEBROOT是我的基本网址和文件夹,而$pieces [3]总是我当前的页面(即.index.php?cont = history.这样我总是落在当前页面上,语言会相应地改变. 注意:在我的情况下.第3部分包含当前页面.如果其他人使用此技术,请务必检查.此外,我一直只使用1个参数,所以请记住(这实际上并不重要). 如果其他人知道更好的解决方案,我会心胸开阔.谢谢大家. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |