Results 1 to 4 of 4

Thread: session variable for language

Hybrid View

  1. #1

    Thread Starter
    Member
    Join Date
    May 2001
    Posts
    39

    session variable for language

    Hi

    I need to know what is the best way to record the session language (session variable) and use the variable value to choose the language to show.
    My doubt: session programming.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: session variable for language

    You can usually find the users language in the browsers USER_AGENT string. e.g:
    Code:
    Mozilla/5.0 (Windows; U; Windows NT 5.0; en-GB; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1
    You can store this in the sessions using the following:
    PHP Code:
    session_start();

    if (! isset(
    $_SESSION['language'])) // check whether the language has been set
        
    if (preg_match('[a-z]{2}\-[A-Z]{2}'$_SERVER['HTTP_USER_AGENT'], $matches)) {
            
    $lang $matches[0];
        } else {
            
    // language is not in the UA String - dispaly apage asking for language here
            
    exit;
        }

        
    $_SESSION['language'] = $lang;

    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Member
    Join Date
    May 2001
    Posts
    39

    Re: session variable for language

    OK. But i want the user to choose from 2-3 pre-defined languages...

  4. #4
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: session variable for language

    PHP Code:
    <?
    session_start(); 
    if(isset($_GET['lang']))
    {
        $_SESSION['language'] = $_GET['lang'];
    }

    switch($_SESSION['language'])
    {
        case "en":
            //do english stuff
            print "English";
        break;
        case "fr":
            //do french stuff
            print "French";
        break;
        default:
            //select your default language
            print "Engish";
        break;
    }
    ?>
    <a href="lang.php?lang=en">En</a> <a href="lang.php?lang=fr">fr</a>
    This will allow you to select the language you want and proccess it. But this can be incorperated with visualAd's script so that the default language will be the language of the users computer.

Posting Permissions

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



Click Here to Expand Forum to Full Width