0

I'm building multi-language support for PHP page. Let's say I have a page url:

http://mypage.php?user=eric&city=newyork

What is the common solution to switch between get parameters lang=en and lang=es while keeping the get parameters already in the url?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
luca
  • 36,606
  • 27
  • 86
  • 125

4 Answers4

3

A useful script that returns the relative URL of the current page with the current "GET data".

<?php 
function getCurrentURL() { 
    $currentURL = basename($_SERVER["PHP_SELF"]); 
    $i = 0; 
    foreach($_GET as $key => $value) { 
        $i++; 
        if($i == 1) { $currentURL .= "?"; } 
        else { $currentURL .= "&amp;"; } 
        $currentURL .= $key."=".$value; 
    } 
    return $currentURL; 
} 
?> 

modify it and concatenate your language parameters accordingly

Syed Absar
  • 2,274
  • 1
  • 26
  • 45
  • if your going to use this method then before you call the function you can do `isset($_GET['lang']) ? $_GET['lang'] : 'en'` which will auto append it to the url when you call the function. – RobertPitt Mar 30 '11 at 08:45
  • This is a great function that really helped me a lot. I added the following before the return so I could use it without having to worry if I had get parameters or not: if($i!=0){$currentURL.='&';} else{$currentURL.='?';} – Elaine Marley Jul 08 '11 at 10:37
2

You would not keep the GET Params in the url at all times, only when your setting the language, you would use sessions to remember what language that current client is using.

create the following function in your php

function getCurrentLanguage()
{
    if(isset($_REQUEST['lang']))
    {
         //Validate that $_REQUEST['lang'] is valid
         return $_SESSION['lang'] = $_REQUEST['lang'];
    }

    if(isset($_SESSION['lang']))
    {
        return $_SESSION['lang'];
    }

    return 'en'; //Default
}

now when the user comes to the site the default language would be English, but if he navigates to index.php?lang=es then the language would be set to es, so when he navigates around the site without the ?lang=es then it would show the Spanish version.

RobertPitt
  • 56,863
  • 21
  • 114
  • 161
  • thanks robert what if im in the middle of a search (i have a search bar in the page that uses GET parameters) and i want to switch language but still keeping the search creterea in the url??=) – luca Mar 29 '11 at 10:05
  • Then you will place a drop down box within the form for the language, such as `` and then the language will be sent with the search, if the above function is called before you produce any content it would be fine. – RobertPitt Mar 29 '11 at 10:09
  • `getCurrentLanguage()` will always return the last known set language for that user, if the user has never selected a language it would default to `en`, once you passed 'lang=xx' you do not need it to be passed again, it will remember while that user is on the site. alternativly you can use a cookie and set the expiration period to 1 year. – RobertPitt Mar 29 '11 at 10:11
0

It's better to use cookie or session/id data to store this kind of information. I’ll have less things to carry around.

pcmind
  • 990
  • 10
  • 12
0

If I understand the question correctly, you want a way to add a parameter to a URL or replace its value with a new value if it already exists.

Here's a function that does so safely in all manner of URLs:

function url_with_parameter($url, $name, $value) {
    if(strpos($url, '?') === false) {
        if (strpos($url, '#') === false) {
            // No query and no fragment -- append a new query to the url
            $newurl = $url.'?'.$name.'='.$value;
        }
        else {
            // No query with fragment -- insert query before existing fragment
            $newurl = str_replace('#', '?'.$name.'='.$value.'#', $url);
        }
    }
    else {
        if (strpos($url, $name.'=') === false) {
            // With query, param does not exist -- insert at beginning of query
            $newurl = str_replace('?', '?'.$name.'='.$value.'&', $url);
        }
        else {
            // With query, param exists -- replace it
            $parsed = parse_url($url, PHP_URL_QUERY);
            parse_str($parsed, $vars);
            $newurl = str_replace($name.'='.$vars[$name], $name.'='.$value, $url);
        }
    }

    return $newurl;
}

Remember to call urlencode before using the output of this function to make a link.

Jon
  • 428,835
  • 81
  • 738
  • 806