Hi
I need to know what is the best way to record the session language (session variable) and use the variable value to choose the language to show.
My doubt: session programming.
Printable View
Hi
I need to know what is the best way to record the session language (session variable) and use the variable value to choose the language to show.
My doubt: session programming.
You can usually find the users language in the browsers USER_AGENT string. e.g:
You can store this in the sessions using the following:Code:Mozilla/5.0 (Windows; U; Windows NT 5.0; en-GB; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1
PHP Code:session_start();
if (! isset($_SESSION['language'])) // check whether the language has been set
if (preg_match('[a-z]{2}\-[A-Z]{2}', $_SERVER['HTTP_USER_AGENT'], $matches)) {
$lang = $matches[0];
} else {
// language is not in the UA String - dispaly apage asking for language here
exit;
}
$_SESSION['language'] = $lang;
}
OK. But i want the user to choose from 2-3 pre-defined languages...
This will allow you to select the language you want and proccess it. But this can be incorperated with visualAd's script so that the default language will be the language of the users computer.PHP Code:<?
session_start();
if(isset($_GET['lang']))
{
$_SESSION['language'] = $_GET['lang'];
}
switch($_SESSION['language'])
{
case "en":
//do english stuff
print "English";
break;
case "fr":
//do french stuff
print "French";
break;
default:
//select your default language
print "Engish";
break;
}
?>
<a href="lang.php?lang=en">En</a> <a href="lang.php?lang=fr">fr</a>