moonwalker
moonwalker

Reputation: 485

Switch language javascript/php

I know I'm doing something wrong, but I can't figure out what I'm doing wrong.

I use a simple select box to switch languages on my website. I linked this function to the select box:

function changeLang(){
    var chosenLang = $('#chooseLang').val();

    if(chosenLang != 0){
        window.location = "index.php?lang="+chosenLang
        }
}

On the language.php file I use the following code:

<?php
$lang = $_GET['lang'];
$myLang = $_COOKIE["myLang"];

// one month to expire
$expire = time()+60*60*24*30;

if (!isset($_COOKIE["myLang"])){
    setcookie("myLang", "en", $expire);
    include "languages/en.php";
    }else{
    include "languages/$myLang.php";
}



if($lang == "en"){
    include "languages/en.php";
    setcookie("myLang", "en", $expire);
    }else if($lang == "fr"){
        include "languages/fr.php";
        setcookie("myLang", "fr", $expire);
        }else if($lang == "nl"){
            include "languages/nl.php";
            setcookie("myLang", "nl", $expire);
}

?>

Everything works fine, the cookies value is changed to the new language successfully every time. All the variables are also changing. The only problem is the queries I use to retrieve data from the database in the chosen language. It seems like it's always a step behind. Example: If I start the website, the language is in English. Once I switch to French, all variables are immediately changed to the French language, but the rows from the queries are still in English. Only when I manually refresh the page, the rows change to the desired language.

The data in the database is entered like this: language_id categories

Example: language_id = 1 (English) categories = Singer

language_id = 2 (French) categories = Chanteur

language_id = 3 (Dutch) categories = Zanger

I use the following code to retrieve the data:

if($myLang == "en" || !$myLang){
$lang = 1;                      
}else if($myLang == "fr"){
$lang = 2;
}else if($myLang == "nl"){                                
$lang = 3;                          
}

$stmt = $dbh->prepare("SELECT cat_title, cat_group
            FROM categories
            WHERE cat_lang_id = '$lang'
            ORDER BY  category_id ASC");
/*** execute the prepared statement ***/
$stmt->execute();
/*** fetch the results ***/
$result = $stmt->fetchAll();

I tried everything I know to solve this, but without any luck.

Thanks in advance.

Upvotes: 1

Views: 3938

Answers (1)

cdhowie
cdhowie

Reputation: 169281

You need to set $myLang too. Calling setcookie() will not update this variable. For example:

if($lang == "en"){
    include "languages/en.php";
    setcookie("myLang", "en", $expire);
    $myLang = 'en';  // <-- add this line

You need to update all of the blocks like this example.

However, I cannot help but suggest rewriting it to eliminate all the duplicate code:

// Put $languages in a common header file.
$languages = array('en' => 1, 'fr' => 2, 'nl' => 3);

if (array_key_exists($lang, $languages)) {
    include "languages/{$lang}.php";
    setcookie("myLang", $lang, $expire);
    $myLang = $lang;
}

In the DB code:

$lang = array_key_exists($myLang, $languages) ? $languages[$myLang] : 1;

Upvotes: 3

Related Questions