Welcome to WebmasterWorld Guest from 54.144.243.34

Forum Moderators: coopster & jatar k

Maintaining the selected Language?

   
10:29 am on Apr 27, 2012 (gmt 0)

5+ Year Member



Hi,

I have managed to get the code below working with a few tweaks here and there. However, you can see that I have written a small function in the file name local.php to allow the user to select "English" or "German" in the link on the test.php page.

This works fine but it will only work on that particular webpage. As soon as I move away from that page the locale will return back to "en_US"(English). Is there a way I could implement lets say an option dropdown and save the locale across the whole site.

Just in case its relevant I have a MySQL DB available for use if thats appropriate.

local.php
<?php

function english(){

$directory = dirname(__FILE__).'/locale';
$domain = 'messages';
$locale ="en_UK.utf8";
setlocale( LC_MESSAGES, $locale);
bindtextdomain($domain, $directory);
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');

}

function german(){

$directory = dirname(__FILE__).'/locale';
$domain = 'messages';
$locale ="de_DE.utf8";
setlocale( LC_MESSAGES, $locale);
bindtextdomain($domain, $directory);
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');

}

//Pagination and normal view switch
if (isset($_GET['run'])) $linkchoice=$_GET['run'];
else $linkchoice='';

switch($linkchoice){

case 'English' :
english();
break;

case 'German' :
german();
break;

default :
english();
break;

}

?>


test.php
<?php include("local.php"); ?>
<html><head></head>
<body>
<?php echo "<p><a href='?run=English'>English</a> | <a href='?run=German'>German</a> </p>"; ?>
<br />
<?php echo gettext("Welcome to My PHP Application");
echo "<br />";
// Or use the alias _() for gettext()
echo _("Have a nice day"); ?>
</body>
</html>


Many thanks in advance!
5:15 pm on Apr 27, 2012 (gmt 0)

WebmasterWorld Administrator incredibill is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Start a session and maintain any sticky settings you need in the session.
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month