Changing Languages

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Changing Languages

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What are the problems that you are speaking of? Hitting the back button should take them back to estonian in your example.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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).
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Hockey wrote:Maybe there is a Javascript function which lets you change URL's and prevent caching?
location.replace(url);
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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;
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 ;)
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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...
Post Reply