Jump to content

Maintaining the selected Language?


chrisguk

Recommended Posts

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!

Link to comment
Share on other sites

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 :confused:

 

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.

Link to comment
Share on other sites

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...?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.