0
var $language = 'es_ES';
// en_US
// es_ES

how can i change language through HTML with "links"

through HTML this works, but when i go to another webpage it disappears &lang=es_ES

http://exemplewebpage.com/?action=clients&lang=es_ES
http://exemplewebpage.com/?action=clients&lang=en_US
Fabio
  • 23,183
  • 12
  • 55
  • 64
LostLife
  • 152
  • 8
  • You might want to use a session and store the language there, each page will use language from the session – Fabio Nov 04 '14 at 08:16
  • 1
    [This](http://stackoverflow.com/questions/19249159/best-practice-multi-language-website) could help you. – Davide Pastore Nov 04 '14 at 08:17

4 Answers4

0

In order to solve this, you have to use session variables.

Of course, you will have to start the session by using the php function session_start() and when the language is selected (i.e &lang=es_ES) you will have to initialize the session variable like this:

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

from here on, the session will remain set with that value on whatever page you go trough...

Hope this helps! :D

Ares Draguna
  • 1,641
  • 2
  • 16
  • 32
0

The way i understand it you should try used

$lang = $_GET['lang'];
if($lang == 'es_ES'){
var $language = 'es_ES';
} else if($lang == 'en_US'){
var $language = 'en_US';
}

Maybe this is what you need

Jesper Kaae
  • 118
  • 1
  • 9
0

You may say

<a href="homepage.php?lang='es_ES'">
<a href="homepage.php?lang='fr_FR'">

and then

if ( !empty($_GET['lang']) ) 
    $_COOKIE['lang'] = $_GET['lang'];
 else 
    $_COOKIE['language'] = 'some_default_language';

setcookie('language', $_COOKIE['lang']);

Or you might use $_SESSION if you don't want it to persist for the user.

argentum47
  • 2,385
  • 1
  • 18
  • 20
0

Given that:

$language = $_GET['lang'];

Pass the lang='.$language.' in every href="...&lang=' as well as hidden fields in forms when making GET request

<hidden name="lang" value="<?=$language?>"/>

and when making POST request).

<form method="POST" action="...&lang=<?=$language?>">

But it might be better to use a session.

At the beginning of your document:

session_start();

if(!isset($_SESSION['language'])) {
    //Pick a default language
    $_SESSION['language'] = "es_ES";
}
//check if there is an override 
if(isset($_GET['lang']) && $_GET['lang'] != "") {
    $_SESSION['language'] = $_GET['lang'];
}

Now in your business logic you can refer to it as:

if($_SESSION['language'] == "es_ES") {
    //do whatever
} else if ($_SESSION['language'] == "en_US") {
    //HAHA I'm speaking english
}

Good luck!

Depending on your requirements you may wish to use cookies rather then sessions.

Dawid O
  • 6,091
  • 7
  • 28
  • 36
  • but how can i set lang through home.php or how can client set lang of session? – LostLife Nov 04 '14 at 08:57
  • This is why you have a default language. If the session isn't set the default language is "es_ES". (This is what the first if statement does). If you want to change the session, all you have to do is have on your website a button/link with the following: href=home.php?lang=fr_FR or whatever language you want. This is what the second if statement does. It checks whether $_GET['lang'] is present in your URL if it is we are going to use it – Dawid O Nov 04 '14 at 09:00
  • Thanks +1 dude, i did it with Session and it worked :) – LostLife Nov 04 '14 at 09:15
  • if(!isset($_SESSION['language'])) { $_SESSION['language'] = "ca_ES"; } if(isset($this->url->lang) && $this->url->lang != "") { $_SESSION['language'] = $this->url->lang; } $this->language=$_SESSION['language']; – LostLife Nov 04 '14 at 09:16