www.webdeveloper.com
Results 1 to 4 of 4

Thread: Maintaining the selected Language?

Hybrid View

  1. #1
    Join Date
    Apr 2012
    Posts
    23

    Maintaining the selected Language?

    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 Code:
    <?php

    function english(){
        
    $directory dirname(__FILE__).'/locale';
    $domain 'messages';    
    $locale ="en_UK.utf8";
    setlocaleLC_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";
    setlocaleLC_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 Code:
    <?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!

  2. #2
    Join Date
    Jun 2008
    Posts
    118
    Set up a session with a session variable $language

  3. #3
    Join Date
    Apr 2012
    Posts
    23
    Quote Originally Posted by Nightcat View Post
    Set up a session with a session variable $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.

  4. #4
    Join Date
    Jun 2008
    Posts
    118
    PHP Code:

    function language($locale){
        
    $directory dirname(__FILE__).'/locale';
    $domain 'messages';   
    setlocaleLC_MESSAGES$locale);
    bindtextdomain($domain$directory);
    textdomain($domain);
    bind_textdomain_codeset($domain'UTF-8');


    That's your function


    PHP Code:

    language
    ($session['language']); 
    and this is the way you call it in your code.

    read up a bit about "session" and "setting cookies"

    Good luck

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles