Changing language in my site ?

Discussion in 'PHP' started by Mackos, Jul 22, 2010.

  1. #1
    Hi!
    I want to make my site bilingual, so I made a begining of the script:
    <form action="jazyk.php" method="POST">
    <select name="lang">
    		<option>polski</option>
    		<option>angielski</option>
    	</select>
    	<input type="submit">
    </form>
    <?php if ($_POST[lang]==polski)
    { echo'Polski';
    }
    else
     echo'Angielski'
    
    ?>
    PHP:
    But can you help me with two things ?
    1. How to check web browser language ?
    2. How to save chosen language in cookies etc. ?

    /I'm a newbie in PHP so I want to make this script simple ;) /
     
    Mackos, Jul 22, 2010 IP
  2. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There are many ways to do this. One way would be to extract all of the text that needs to be in the local language and split it up into locale-specific include files. Then on your main PHP page, you would have a conditional that would check the browsers language (have a look at $_SERVER['HTTP_ACCEPT_LANGUAGE']) and load the appropriate include file.

    Main:
    
    <?php
        $language = getLanguage();
    
        switch ($language) {
            case 'en':
                include_once('lang/english.php');
                break;
            case 'fr':
                include_once('lang/french.php');
                break;
            case 'de':
                include_once('lang/german.php');
                break;
            case 'pl':
                include_once('lang/polish.php');
                break;
            default:  
                include_once('lang/english.php');
                break;
        }
    ?>
    
    <h1><?php echo WELCOME_MSG; ?></h1>
    
    PHP:
    and an example include would be something like:
    
    <?php
        define('WELCOME_MSG', 'Hello, there');
        define('GOODBYE_MSG', 'Goodbye!');
        define('THANKS', 'Thank you!');  
    ?>
    
    PHP:
     
    Deacalion, Jul 22, 2010 IP
  3. Zeh.

    Zeh. Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Some additions:
    $_SERVER['HTTP_ACCEPT_LANGUAGE'] will return a standard two-char language code, like 'en', 'es', 'fr', 'de', etc.
    You can simply name your files according these codes:
    <?php
    $lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
    if(file_exists("lang/{$lang}.php")) include("lang/{$lang}.php");
    else include("lang/en.php"); // A default;
    ?>
    PHP:
    Also you can let your users select a language, maybe with some links and $_GET:
    <?php
    $lang = (isset($_GET['lang'])) ? $_GET['lang'] : $_SERVER['HTTP_ACCEPT_LANGUAGE'];
    if(file_exists("lang/{$lang}.php")) include("lang/{$lang}.php");
    else include("lang/en.php"); // A default;
    ?>
    PHP:
    Finally, for website-wide config, you can try something with sessions, like replacing $lang with $_SESSION['lang'] or something.

    Regards.
     
    Zeh., Jul 22, 2010 IP
  4. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Both of these are vulnerable to attack. The HTTP_ACCEPT_LANGUAGE is a header that can be faked and $_GET['lang'] can be changed easily... you could make it include anything, for example "../../etc/passwd\0en".

    Try this above that code, with 'any_file.php' being any file you have in the same directory:
    
    $_GET['lang'] = "any_file.php\0en";    
    
    PHP:
    This is why I used getLanguage() - so you could code a safe and secure way to get their language and make it a function like so :).
     
    Deacalion, Jul 23, 2010 IP
  5. Zeh.

    Zeh. Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That was only an idea :p Maybe with:
    <?php
    $lang = (isset($_GET['lang'])) ? substr(basename($_GET['lang']), 0, 2) : substr(basename($_SERVER['HTTP_ACCEPT_LANGUAGE']), 0, 2);
    if(file_exists("lang/{$lang}.php")) include("lang/{$lang}.php");
    else include("lang/en.php"); // A default;
    ?>
    PHP:
    Maybe it's more secure :)

    Thanks ;)
    Bye!
     
    Zeh., Jul 23, 2010 IP
  6. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yeah :) it's surprising where you can find little holes sometimes, hehe.
     
    Deacalion, Jul 23, 2010 IP
  7. Mackos

    Mackos Well-Known Member

    Messages:
    364
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #7
    Well I decided to make it simplest way:
    Cause it's not a big site ;)
     
    Mackos, Jul 25, 2010 IP
  8. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #8
    That's always the best :). You should just preg_match() instead of ereg() though, since it's deprecated.
     
    Deacalion, Jul 25, 2010 IP
  9. Mackos

    Mackos Well-Known Member

    Messages:
    364
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #9
    Oh, good to know :) Changed.
    Thanks Deacalion and Zeh for help.
     
    Mackos, Jul 26, 2010 IP