1

Intro Hi, we are building a multi-language website for a little hotel as a project to deepen our web development skills. It's a cool project so far and together with Stackoverflow, we could solve a lot of doubts. Dope! However, I've been researching a lot but none of the multi-language php posts (although somewhat helpful) seem to answer what we're looking for (or we can't translate the problem's solution to our case). We have content for different sections of in total 5 pages (Home; About;The Rooms; Gallery; Contact) stored in a php MyAdmin database in 4 different languages and defined $_Sessions for each language retrieval. The titles and smaller amounts of text, e.g. a button saying "We guarantee you the lowest rate" are stored in arrays for each language. When initiating the page, all the content is displayed in English, so that we know that both the arrays work and the database is connected.

The Problem Whenever I try to select the language choosing the dropdown language menu on the website, the page (and any other page) just reloads with english. How do I make the Sessions change? Do I need cookies? Do I need to use "Get?" If yes, how?

Here's part of my index.PHP. Each list object has a lang_id that we defined in numbers (1,2,3,4,5, and so on) as opposed normally to en, ge, fr, pt, es etc.
I want each list item to change according to its session, e.g. English to 1, German to 2... so then it can at the same time retrieve the data from the arrays + the values stored within the database.

<div class="language">
  <span><?php echo _t_language; ?></span> //_t_language comes from the array lang_en/gr...
  <ul>
    <li>
      <a href="languages/switch_lang.php?lang=1">
        <?php echo _t_english; ?>
      </a>
    </li>
    <li>
      <a href="languages/switch_lang.php?lang=2">
        <?php echo _t_german; ?>
      </a>
    </li>
    <li>
      <a href="languages/switch_lang.php?lang=3">
        <?php echo _t_french; ?>
      </a>
    </li>
    (...)
  </ul>
</div>

I made a languages folder that includes switch_lang.php and define_lang.php. The session is started in define_lang.php, that is included before starting the HTML. Here's the code of both of them:

switch_lang.php:

<?php
//get current url
$goback=$_SERVER['HTTP_REFERER'];
$lang=$_GET['lang'];
$_SESSION['lang']=$lang;

header("location: $goback");
?>

define_lang.php:

<?php
session_start();

//give session when first load the page
    if($_SESSION["lang"]==""){
    $_SESSION["lang"]="1";
    }
    if($_SESSION['lang']=='1') {
        include('lang_en.php');
    }else if($_SESSION['lang']=='2') {
        include('lang_gr.php');
    }else if($_SESSION['lang']=='3') {
        include('lang_fr.php');
    }else if($_SESSION['lang']=='4') {
        include('lang_it.php');
    }else if($_SESSION['lang']=='5') {
        include('lang_pt.php');
    }else if($_SESSION['lang']=='6') {
        include('lang_es.php');
    }     
?>

Do we have to redirect anything? Do we have to use cookies? Do we have to use $_Get? Does it work without $_Get, too? How does it all fit together??

It's almost 12 'o clock and, as many other people here, we are beyond realizing what's wrong. Any help would be so much appreciated and at any time later, we are ready to give back advice. Thanks and have a nice night, Spastnudel & Nina

2
  • 2
    is that how your switch_lang.php file is actually formatted? All on one line? If so, the comment is blocking everything from executing. If that's not how it's formatted, please format it so we can read it easier. Commented Feb 27, 2016 at 22:43
  • 1
    hi, I edited the code. Commented Feb 28, 2016 at 1:10

2 Answers 2

1
  1. You should always call session_start() before working with $_SESSION. You miss that in the switch_lang.php. Look here.
  2. Could you format your code pls, its hard to read it this one line form.
Sign up to request clarification or add additional context in comments.

1 Comment

I formatted the code here for better readability.I also added Session_start and, EPIC, it works. Before, I had the session_start but the page displayed a Loop mistake, so that's why I took it away. Now it works flawlessly. Thanks to you! I will post the final result of the website when it's done.
1

This is mostly comments, but space is limited in the comment box.

I am struggling to understand both your problem and the architecture of your application.

Why default to English rather than using the language preference sent by the browser? Certainly providing a means for the user to override the language preference makes sense. Personally I would have used a dedicated sub domain for persisting the choice, but the session is an appropriate place to keep the value.

How do I make the Sessions change?

Just assign the selected language to a session variable...

$_SESSION['prefers_lang']=....

How you get the value from the front end to the back end is up to you, but using your select widget above,

$_SESSION['prefers_lang']=$_GET['lang'];

But why use integers to represent the languages when there are semantically significant standard codes in ASCII (en, fr, de....).

1 Comment

Hi, thanks for the answer. Sorry for the comments. We assigned integers to the lang_id to facilitate the code: First, I started to create arrays.To associate those arrays with a Session, I created the code define_lang.php where I associated the $_Session['lang'] with a number. I reckoned it was easier to do the association because that's what I learned in this video: [link] (youtube.com/watch?v=tb2HpNlqmaY). The video guy actually built an amazingly easy database (!) and some code for a multilanguage website but then failed to explain more. And that's where I got stuck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.