【问题标题】:Change Language without leaving current page在不离开当前页面的情况下更改语言
【发布时间】:2013-03-14 16:00:21
【问题描述】:

我正在尝试更改语言但停留在当前页面: 即:

www.myurl.com/english/aboutus.php

www.myurl.com/german/aboutus.php

www.myurl.com/french/aboutus.php

所以只有语言目录会改变。 我有以下内容,但无法正常工作:

<?php 
$full_url = $_SERVER['FULL_URL'] ;  

$temparray = explode(".com/",$full_url,2); 

$englishtemp = $temparray[0] . ".com/english/" . $temparray[1]; 
$englishlink = "<a href=$englishtemp><img src=../images/english-flag.jpg></a>"; 

echo $englishlink; 
?>

我将网址从“/french/aboutus.php”更改为“/english”,但它不记得页面名称“aboutus.php”并返回到索引页面。

有人可以帮忙吗?

【问题讨论】:

  • 你试过什么?你研究过ajax吗?即便如此,也许您最好还是刷新页面。这有什么问题?
  • 编辑了 Q. 使其更加清晰。

标签: php


【解决方案1】:

当前页面使用basename($_SERVER['PHP_SELF'])

$englishtemp = "/english/" . basename($_SERVER['PHP_SELF']); 
$englishlink = "<a href=$englishtemp><img src=../images/english-flag.jpg></a>";
echo $englishlink;

PHP documentation on $_SERVER

【讨论】:

  • 感谢安东尼,它成功了! - 只需删除 '$_SERVER['SERVER_NAME'] 。'第一个链接的位,因为它重复了 URL - 再次感谢!
  • @dave 已编辑答案以删除 $_SERVER['SERVER_NAME']
【解决方案2】:

试试这个。它将为除当前选择的语言之外的所有语言生成链接:)

<?php
$full_url = $_SERVER['REQUEST_URI']; //select full url without the domain name
$languages = array('english', 'french', 'german'); //array of all supported languages

$url_bits = explode('/', $full_url); //divide url into bits by /
$current_language = $url_bits[1]; //current language is held in the second item of the array

//find current language in the array and remove it so we wouldn't have to display it
unset($languages[array_search($current_language, $languages)]);

$links = '';

foreach ($languages as $language) {
    //generate links with images for the rest of the languages
    $links .= '<a href="/' . $language . '/' . $url_bits[2] . '"><img src="../images/' . $language . '-flag.jpg" title="'.ucfirst($language).'" /></a>';
}

echo $links;
?>

【讨论】:

  • 在我个人看来,我不喜欢使用带有域名的 URL,最好只使用绝对路径(url 以 / 开头),但这取决于你 :) 看起来更干净 :)
  • 感谢 Ignas - 感谢您抽出宝贵的时间来做这件事,Antonys 的解决方案在这个场合对我有用,但您的看起来也不错!再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-13
  • 2011-03-20
  • 2011-07-15
  • 1970-01-01
  • 2013-08-09
  • 1970-01-01
相关资源
最近更新 更多