Page 1 of 1
Changing Languages
Posted: Sat Apr 15, 2006 2:34 pm
by shiznatix
I am trying to come up with a good way to change between languagues without the website changing. Like... the user is viewing a certain thing on the site and wants to change languages. Navigation is done through GET so I made this quick function
Code: Select all
function lang_links()
{
$return = '';
if (count($_GET) != 0)
{
foreach ($_GET as $key => $val)
{
if ($key != 'lang')
$return .= $key.'='.$val.'&';
}
}
return $return;
}
Then I just make the link to change the language like this:
Code: Select all
<a href="?<?= $lang_links ?>lang=eng">eng</a>
But what happens if the user changes from english to estonian to finnish then tries to hit the back button? Problems thats what! Basically I need a non-javascript way to change the language the user is browsing in (stored in a session) without affecting their browsing expierence.
Any ideas?
Posted: Sat Apr 15, 2006 2:39 pm
by feyd
What are the problems that you are speaking of? Hitting the back button should take them back to estonian in your example.
Posted: Sat Apr 15, 2006 2:44 pm
by alex.barylski
Whats wrong with people hitting the back button?
Is it because they'd go from Finnish back to English again?
I guess that is a small problem, but I emphasize small...
Your going to need to refresh the page in order to make language changes so the back button will always be a problem.
Unless you use AJAX to change the language or if there is some way to tell the browser not to cache a refresh in it's history???
Look into HTTP headers, perhaps there is something in there you could use, to at least expire previous pages so when you hit back you get an error instead of English???
Maybe there is a Javascript function which lets you change URL's and prevent caching?
Or you could use a landing/splash page which makes people pick a language before even browsing the site?
Posted: Sat Apr 15, 2006 2:54 pm
by John Cartwright
I use an intermediate page which will accept the url paramter of which language to use, and then redirect.. so if they do for some reason hit the back button they are simply redirected back to the page (although this time no processing is done).
Posted: Sat Apr 15, 2006 2:54 pm
by feyd
Hockey wrote:Maybe there is a Javascript function which lets you change URL's and prevent caching?
location.replace(
url);
Posted: Sat Apr 15, 2006 2:57 pm
by shiznatix
Ok let me be a little more clear:
let assume im browsing in english. I am looking at someones profile and everythings in english of course. Now I want to view that page in Estonian, then in Finnish. I am now viewing in Finnish but I want to go back to the profile I was viewing before this one I am currently on. Now I go back one and I am viewing in estonian, same profile (this should go back to the other profile instead of this one!). I go back again and finally I am back to the other profile but in Estonian (Should be in finnish since that was the last language I chose).
Understand? I don't know very many headers so if someone knows of one that might be able to help me here that would be fantastic unless someone has another idea (please no javascript)
EDIT:
Jcart wrote:I use an intermediate page which will accept the url paramter of which language to use, and then redirect.. so if they do for some reason hit the back button they are simply redirected back to the page (although this time no processing is done).
How do you get the entire current URL that the user is on? I may be one index.php, view.php, ... How would I make it find out which page to redirect to?
Posted: Sat Apr 15, 2006 3:12 pm
by John Cartwright
In my framework, I track the users much like phpBB does.
This is useful for multiple modules, so the little overhead involved isn't much of an issue..
basically, it boils down to simply tracking their last page in a session. A more sophisticated approach that I use in zend framework is something like
Code: Select all
$url = $this->getController() .'/';
$url .= $this->getAction();
$params = $this->getParams();
if (count($params)
{
foreach ($params as $paramName => $value)
{
// etc etc
}
}
$_SESSION['LAST_URI'] = $url;
Posted: Sat Apr 15, 2006 3:15 pm
by alex.barylski
feyd wrote:Hockey wrote:Maybe there is a Javascript function which lets you change URL's and prevent caching?
location.replace(
url);
Thats the one I was thinking of

Posted: Sat Apr 15, 2006 4:28 pm
by shiznatix
ok so heres what I am trying for right now:
Code: Select all
<?php
if (!empty($_GET['lang']))
{
$langs = array('eng', 'est', 'fin', 'rus');
$language = strtolower($_GET['lang']);
$language = trim($language);
if (in_array($language, $langs))
{
$_SESSION['lang'] = $language;
header("Location: ".$_SESSION['last_page']);
die();
}
}
if (!empty($_SERVER['HTTP_REFERER']))
$_SESSION['last_page'] = $_SERVER['HTTP_REFERER'];
else
$_SESSION['last_page'] = $GLOBALS['sroot'];
?>
and for some odd reason when I change the $_SESSION['lang'] it does not save as it goes to another page. I dump($_SESSION) after setting it but before the header and I get:
[lang] => est
and after the header I do dump($_SESSION) and get:
[lang] => eng
...yaaaaaaaaaa. PHP 5.0.5-2ubuntu1.2...