Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing languages

Status
Not open for further replies.

SBuzzT

Programmer
Aug 24, 2005
86
CA
I need to be able to access different images for a bilingual website. I have stored the same named images into separate folders (en and fr) in order to change them based on the language the visitor selects. The code below allows me to switch the value from the default en to fr and then back to en, however from that point on, the value of $lang remains en. Any way to make it so that the en and fr ($lang and $lang2) values can be switched back and forth as many times as desired?

Code:
if (isset($_GET["lang"]))
{
  $lang  = $_GET["lang"];
  $lang2 = "en";
}
else
{
  $lang  = "en";
  $lang2 = "fr";
}
 
You would technically want to store the selected language in a session so its available throughout the sire, not just when the form is submitted.

Look in php.net's online manual for Sessions.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Yes, I have tried the following, but that does not solve the problem of switching back and forth at will. I need to change the value of lang from the same link. In other words, when $lang=en the link value will be fr. When I click on the link, $lang will now be fr and the link value will be en.
Code:
	if(!$_SESSION['lang']) $_SESSION['lang']  = 'en';
	
	$lang  = $_SESSION['lang'];
 
Its exactly the same, you just check for the value of the session variable and display the link accordingly.

Code:
session_start();
if(!isset($_SESSION['lang'])){ [green]\\check if session variable exists if not create it[/green]
$_SESSION['lang']="en"; [green]\\Default value to session variable[/green]
$link="<a href='" . $_SERVER['PHP_SELF'] . "?lang=fr'>Change to French</a>"; [green]\\Create language changing link[/green]
}

else{ [green]\\If session var did exists, then look for a language change request[/green]
if(isset($_GET['lang'])){
$_SESSION['lang']=$_GET['lang']; [green]\\change session variable value to requested language[/green]
}
}
[green]\\Output links accordingly[/green]
if($_SESSION['lang']=="en"){[green]\\For english link[/green]
$link="<a href='" . $_SERVER['PHP_SELF'] . "?lang=fr'>Change to French</a>";
}

if($_SESSION['lang']=="fr"){
$link="<a href='" . $_SERVER['PHP_SELF'] . "?lang=en'>Change to English</a>";[green]\\for french link[/green]
}

you can then use $link variable to output the link wherever you want. and you can check the session variable so you know what language to output




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
This definitely works more reliable than what I was trying to do. A little bit messy when I have to add all the image/swap image code, but definitely much better than my attempt. Thanks a lot for breaking this down for me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top