Jump to content

A language switcher between English and French


janggu

Recommended Posts

Hi there,

 

I need to create a language swticher script between English and French content. I am kind of stuck how I can approach this. Is using PHP cookie the best way - I need to triger the switcher by clicking English/French link? Has anyone done this type of work?

 

Thanks a lot for your help in advance!

This is what have so far. Basically, I have two links of English/French with a query string called "lang". When one of these links is clicked, my script is supposed to create or update the session variables. Howerver, this is not working... PLEASE HELP!!!

 

<?

session_start();

$lg = $_GET['lang'];

 

if (!isset($_SESSION["lang"]))

{

$_SESSION['lang'] = "en";

}

else

{

if ($_SESSION["lang"] == "en" && $lg == "fr")

{

session_destroy();

$_SESSION["lang"] = "fr";

}

if ($_SESSION["lang"] == "fr" && $lg == "en")

{

session_destroy();

$_SESSION["lang"] = "en";

}

}

?>

 

<?php
session_start();
$langs = array("ENG","FRA"):
if(in_array($_GET['lang'],$langs)){
   $_SESSION['Lang'] = $_GET['lang'];
}
if(in_array($_SESSION['Lang'],$langs)){
    $_SESSION['Lang'] = $langs[0];
}
?>

 

 

That will do it it will verify a get version is legit if the session isn't set or is invalid it resets it to the default first value.  You can add more languages very easily with this.

 

Make sure all this appears pre any headers being sent so the page is reflective of the new header data.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.