M.Bwe
M.Bwe

Reputation: 159

mutible Language Website

My website is working with 2 languages.

I used session for that purpose. When a user clicks on a language icon the language is changed and everything is fine, but when the website is opening for the first time without choosing the language the default language is not loaded and the words do not appear.

This is my language.inc.php file

<?php
session_start();
$langForSelection = isset($_SESSION['lang']) ? $_SESSION['lang'] : 'en'; //default to english language
if(isset($_GET['langSelect'])){
    //allow only 2 for now ( english / turkish)
    $langForSelection = "";
    switch($_GET['langSelect']){
        case 'en':
        $langForSelection = 'en';
        break;
        case 'tr':
        $langForSelection = 'tr';
        break;
        default:
        break;
    }
    if(isset($langForSelection)){
		$langForSelection2=$_SESSION['lang']=$langForSelection;
       // setcookie('lang',$langForSelection,time()+24*7*60*60);//set cookie to expire in 7 days
    }
}

here is the icons where user choose the language

    <div><a href="?langSelect=en" title="English" id="English" class="active_lang"><img src="images/united-kingdom.png" class= "active" style="float: right; width: 24px;height:24px ;padding: 4px"> </a>

<a href="?langSelect=tr" title="Turkish"  id="Turkish"><img src="images/turkey.png" style="float: right; width: 24px;height:24px; padding: 4px"> </a>

here I print the translate of the words this is an example

require_once('inc/languages.inc.php'); 
require_once('Languages/common.inc.php'); 
$langForSelection2=$_SESSION['lang'];


   <li><a href="Logs.php?id=access" class="details"   data-ajax="false"><?php echo $arrLang[$langForSelection2]['log_login']; ?></a></li>

Again my problem is reproduced when the web page is loaded and before a user clicks on language icon the words not appear but when he click on the language click everything works fine.

Upvotes: 1

Views: 37

Answers (1)

Lajos Arpad
Lajos Arpad

Reputation: 76943

You can change your switch to default to English:

switch($_GET['langSelect']){
    case 'en':
    $langForSelection = 'en';
    break;
    case 'tr':
    $langForSelection = 'tr';
    break;
    default:
    $langForSelection = 'en';
    break;
}

The reason of your problem probably is that your $_SESSION has a lang value, which is different from your expectations, like 'en-us'. If you want to know the exact cause, you can reproduce the problem with a new session and calling var_dump($_SESSION); If it has a lang, then it is the exact reason of your problem.

EDIT:

The solution shown here was not enough, since this part

if(isset($langForSelection)){
    $langForSelection2=$_SESSION['lang']=$langForSelection;
   // setcookie('lang',$langForSelection,time()+24*7*60*60);//set cookie to expire in 7 days
}

was inside the if. You will need to move this outside the if.

Upvotes: 0

Related Questions