chrisguk Posted April 27, 2012 Share Posted April 27, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/261700-maintaining-the-selected-language/ Share on other sites More sharing options...
titan21 Posted April 27, 2012 Share Posted April 27, 2012 Have a look at incorporating session variables into the application. You'll be able to mainatin state across a visit then: session_start(); $language = $_SESSION['language']; //handle code based on language... Quote Link to comment https://forums.phpfreaks.com/topic/261700-maintaining-the-selected-language/#findComment-1341033 Share on other sites More sharing options...
chrisguk Posted April 27, 2012 Author Share Posted April 27, 2012 Have a look at incorporating session variables into the application. You'll be able to mainatin state across a visit then: session_start(); $language = $_SESSION['language']; //handle code based on language... I get that, I think But how can I be sure that it will use the .mo file in the locale folder. What I am attempting to do is have a backend panel that I can choose the preferred language from a drop down and then hit the save button. The that changes the site wide language. Quote Link to comment https://forums.phpfreaks.com/topic/261700-maintaining-the-selected-language/#findComment-1341065 Share on other sites More sharing options...
titan21 Posted April 27, 2012 Share Posted April 27, 2012 Sorry - I totally rushed into answering that! I haven't actually used this particular functionality before but had a look at the documentation. and I think that if you select the correct language then bindtextdomain will select the correct gettext file (I think??) If you just put $linkchoice in a session then you can call this at the top of each page and it will generate the correct text. Again - I haven;t used this before but it seems like a sensible plan...? Quote Link to comment https://forums.phpfreaks.com/topic/261700-maintaining-the-selected-language/#findComment-1341076 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.