dave
dave

Reputation: 203

Change Language without leaving current page

I am trying to change languages but stay on the current page: i.e:

www.myurl.com/english/aboutus.php

www.myurl.com/german/aboutus.php

www.myurl.com/french/aboutus.php

So only the language directory changes. I have the following but can't get it to work:

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

I get the url to change from '/french/aboutus.php' to '/english' but it doesn't remember the page name 'aboutus.php' and returns to the index page.

Could any one please help?

Upvotes: 2

Views: 1175

Answers (2)

Ignas
Ignas

Reputation: 1965

Try this instead. It will generate links for all the languages except the current selected one :)

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

Upvotes: 0

Antony
Antony

Reputation: 15106

Use basename($_SERVER['PHP_SELF']) for the current page.

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

See PHP documentation on $_SERVER.

Upvotes: 2

Related Questions