homepage Welcome to WebmasterWorld Guest from 54.198.42.105
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Maintaining the selected Language?
chrisguk




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

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!

 

incrediBILL




msg:4446607
 5:15 pm on Apr 27, 2012 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved