在不离开当前页面的情况下更改语言


Change Language without leaving current page

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

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

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

有谁能帮帮忙吗?

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

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

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

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