0

So I'm building a small multilingual (French + English) website and there is a little bug. I would like to remove the "english default" language in the code, so if a user picks french on one page, any page he will select afterwards will be in french and not be "back" to english. Same if the user picks english in the first place. But since there will be more french users, I would like it to be the default language on the home page.

The content is in folder with prefixes: fr_language.php and en_language.php

The lang.php file here

Links for FR or EN

    <a href="?lang=fr">Français</a>
    <a href="?lang=en">English</a>

And the navigation

<ul>
    <li><a href="index.php"><?php echo $lang['home']; ?></a></li>
    <li><a href="services.php"><?php echo $lang['services']; ?></a></li>
    <li><a href="about.php"><?php echo $lang['aboutus']; ?></a></li>
    <li><a href="contact.php"><?php echo $lang['contact']; ?></a></li>
</ul>

Thanks for your help!

EDIT:

Ok great, with your help it's working! In my "nav.php" include, I wrote this. Perhaps it's possible to do a cleaner version, haha! Any ideas? Thanks again!

Sam
  • 471
  • 4
  • 8
  • Storing the default in a session variable might help. – rahulmishra Aug 18 '14 at 15:34
  • 1
    `if(isset($_GET['lang']) && $_GET['lang'] == "fr"){ // fait quelque chose }else { // do something else }` - However, you may be better off using a `case` method using a default. – Funk Forty Niner Aug 18 '14 at 15:37
  • Check out my answer. I have made some edits since my OP. – Nicolas Aug 18 '14 at 15:47
  • As per your Edit, I was [right](http://stackoverflow.com/questions/25366952/php-link-for-actual-language-id#comment39554661_25366952) ;) – Funk Forty Niner Aug 18 '14 at 16:35
  • Yes @Fred-ii- thanks for your help. Is there any way to make it cleaner though or you think it's fine like this? (I can't set your comment as the accepted answer, unfortunately!) – Sam Aug 18 '14 at 18:01
  • You're welcome. (I have made my comment as an answer). I think it looks pretty good. If you're happy with the way it works and its functionality; I don't see anything wrong with it. If you want to change it later, using the include method will save from editing a whole bunch of files. – Funk Forty Niner Aug 18 '14 at 18:32
  • Here are a few links you can look over, they might give you more ideas. These I keep on file http://stackoverflow.com/q/24163048/ - http://stackoverflow.com/q/16173614/ - http://stackoverflow.com/q/17070604/ - http://stackoverflow.com/a/17192194/ - http://stackoverflow.com/q/21386670/ - http://stackoverflow.com/q/21557994/ - I too deal with bilingual websites, but the method I use isn't PHP. Do watch for XSS also, see http://www.smashingmagazine.com/2011/01/11/keeping-web-users-safe-by-sanitizing-input-data/ – Funk Forty Niner Aug 18 '14 at 18:53

4 Answers4

0

you need to keep the state of the language somewhere.

you will either have to pass it around with each request by

  • url parameter: page.php?lang=en
  • path: yourpage.com/en/page.php

or use a cookie to store it. you will get the cookie value passed with each server request

you can read the cookie through the $_COOKIE var. you can set it either in javascript or php

dreamlab
  • 3,321
  • 20
  • 23
  • Apparently someone did not like this correct answer. – drmarvelous Aug 18 '14 at 15:36
  • @drmarvelous, if you check the question, you'll see that it's already being sent in via a URL parameter. OP is looking for a way for the language to persist, not ways to get the initial preference. This is an answer to a question that wasn't asked. – Tango Bravo Aug 18 '14 at 15:39
  • this answer provides three ways to persist it. two of them are to pass it around with every requests url – dreamlab Aug 18 '14 at 15:40
0

Personally, I recommend an on click for your language selector that will create a new PHP session and store the language.

The benefit of doing it this way is that you don't always have to have your language appended to the end of your URL. This will only happen each time a session has to be created, (so if you don't set a timeout then only when the computer restarts). Once the page redirects from the page that sets the language it doesn't have to $_GET anymore, but rather just check for an active session.

You will have to redirect the user to the page that has your PHP script and then set the language based on the what is sent in the URL. You could however, redirect after the script finishes back to the page the user was on (and load their new selected language).

HTML:

<a href="setLanguage.php?lang=fr">Français</a>
<a href="setLanguage.php?lang=en">English</a>

PHP:

// intitially set language by your selector
<?php
    session_start();
    $_SESSION['language'] = $_GET['lang'];
?>

Now you could place your getter code on any page that gets included on all your pages (like a header or base page):

// check for the language
<?php
    session_start();

    if (isset($_SESSION['language'])) {
        // now change the language of the page based on what it is
        if ($_SESSION['language'] == "en") {
            // change page language to english
        } else {
            // change page language to french
        }
    }
?>
Nicolas
  • 1,125
  • 1
  • 15
  • 31
0

It looks like you are using you are using a function called get_lang_id() to pull from the cookie and use that to load the language file.

The get_lang_id() function is currently defaulting to English:

function get_lang_id() 
{
    return ( isset( $_COOKIE['lang'] ) && 
             strlen( $_COOKIE['lang'] ) == 2 && 
             is_language_supported( $_COOKIE['lang'] ) ) ?
           htmlspecialchars($_COOKIE['lang']) : 'en';
}

If you change the 'en' at the end to 'fr', it will change the default language file to the French version. Clicking either link will set a cookie, which will skip using the default.

Phil Hord
  • 12,780
  • 1
  • 26
  • 30
Ryan
  • 216
  • 1
  • 7
0

Consider using the following approach/method:

if(isset($_GET['lang']) && $_GET['lang'] == "fr"){ 

   // do something 

} else { 
   // do something else 
}

You should also be made aware of (XSS) Cross-site scripting.

Here are a few links to read up on:

You can further your research by using "XSS injection php" as keywords in your favorite search engine.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141