Jump to content

Multilingual website


Bravat

Recommended Posts

I want to create multilingual website using php script.

I have MySQL table with language (with this fields: id_lang, lang, image, pozicija).

So far I did this:

 

	if(isset($_GET['language'])){
$_SESSION['jezik'] = $_GET['language'];	
} elseif(!isset($_GET['language'])){
	$_SESSION['jezik'] = 2;
	}

 

When I choose new language it is working, but when I choose another link inside that language it is back to the default one.

Here is the code for menu:

 

 $query_meni = "SELECT * FROM pages WHERE lang_id = '$jezik' ORDER BY pozicija";
     $result = mysql_query($query_meni);
     $count = mysql_affected_rows();
	if($count >0 ){
	while($row_meni = mysql_fetch_array($result)){
		$link = $row_meni['id_pages'];
		$show = $row_meni['naslov'];
		echo "<li><a href=\"index.php?page=$link\">$show</a>";
		$query_subject = "SELECT * FROM subjects WHERE pages_id = '$row_meni[id_pages]' AND lang_id = '$jezik' ORDER BY pozicija ASC";	
        $result1 = mysql_query($query_subject);
$count1 = mysql_affected_rows();	
		if($count1 == 0){
		echo "</li>"; }
	else {
			echo "<ul class=\"subnav\">"; 
					while($row_subjects = mysql_fetch_array($result1)){
					$link1 = $row_subjects['subject_id'];
					$show1 = $row_subjects['naslov'];
					echo "<li> <a href=\"index.php?subject=$link1\">$show1</a></li>";
					}
				echo "</ul>"; 
		}
			echo "</li>"; 
	}
		echo "</li>"; 
		}
		echo "</li>"; 

?>           

 

What am I doing wrong?

Link to comment
Share on other sites

Here is what you need to do.

 

1. Have a default language to use when no language is set. Alternatively, you could redirect the user to a page to select a language if they have not set one before.

 

2. When the user selects a language you need to store that value. Then when the user selects another page you would first check if they selected a language (i.e. POST value) or if there is a stored selection. You can choose to store the selected language in a SESSION or COOKIE or both.

 

Here is a function I built previously. You would just call getLanguage() on each page load and it will return the language to use for that page. There are plenty of comments so you should be able to modify if needed. The code below is a complete working example

<?php

// Function: getLanguage($languageList [, $selectedLang] [, $defaultLang])
//
// Parameters:
//   - $languageList: (Required) An array of all available languages for the user
//   - $selectedLang: (Optional) The language the user has selected (GET or POST)
//   - $defaultLang:  (Optional) the default language to use if unable to determine
//                               user's language from seleted or saved values.

function getLanguage($languageList, $selectedLang=null, $defaultLang=null)
{
    //Set the default value (or first option in $languageList)
    $userLanguage = (!in_array($defaultLang, $languageList)) ? $languageList[0] : $defaultLang;

    //Detemine selected/saved user language
    if (!is_null($selectedLang) && in_array($selectedLang, $languageList))
    {
        //Request was made to change the language
        $userLanguage = $selectedLang;
        setcookie('language', $userLanguage, time()+3600*24*365); //Expires in 1 year
        $_SESSION['language'] = $userLanguage;
}
    else if (isset($_SESSION['language']) && in_array($_SESSION['language'], $languageList))
    {
       //There is a saved language value in the SESSION data
        $userLanguage = $_SESSION['language'];
    }
    else if (isset($_COOKIE['language']) && in_array($_COOKIE['language'], $languageList))
    {
        //There is a saved language value in the COOKIE data
        $userLanguage = $_COOKIE['language'];
        $_SESSION['language'] = $userLanguage;
    }

    //return the user's language
    return $userLanguage;
}

//Example usage
//
//Create list of available languages
$languages = array ('Spanish'=>'sp', 'French'=>'fr', 'English'=>'en');
//Get the language to show the user
$currentLanguage = getLanguage($languages, $_GET['lang'], 'en');
//Create vars for illustrative purposes
$sessionLang = (isset($_SESSION['language'])) ? $_SESSION['language'] : 'Not set' ;
$cookieLang = (isset($_COOKIE['language'])) ? $_COOKIE['language'] : 'Not set' ;
?>
<html>
<body>
Current language: <?php echo array_search($currentLanguage, $languages) . " ($currentLanguage)"; ?>
<br /><br />
$_GET['lang']: <?php echo $_GET['lang']; ?><br />
Session language: <?php echo $sessionLang; ?><br />
Cookie language: <?php echo $cookieLang; ?><br /><br />

Change Language
<?php
foreach ($languages as $name => $value)
{
    echo "<a href=\"?lang={$value}\">{$name}</a> ";
}
?>
</body>
</html>

Link to comment
Share on other sites

It is working :), but I have a bit of a problem. Language selection is working, but when I go to some link within that language I get this error:

 

Notice: Undefined index: lang in C:\wamp\www\ads\layout\head.php on line 4

 

and this is line 4:

 

$languages = array ('Serbian'=>'1', 'English'=>'2');

 

And one more question, how I get language list from DB and put it into array (First go name of the language and then id of the language from DB)?

Link to comment
Share on other sites

That line would not produce that error. That error would come from trying to reference a value in the array using a key that does not exist in the array. I haven't touched this code in a long time and I must have edited it at some point without testing it. I suspect the error is from this line:

$userLanguage = (!in_array($defaultLang, $languageList)) ? $languageList[0] : $defaultLang;

 

Looking at the format of the array of languages, there wouldn't be a value with the index of 0. You can just hard-code the default language there or fix the function.

 

As for how you get the text for the specifid language from the database, that is up to you. I would suggest a table with a column for keyname (a placeholder value to use for replacements) and a separate column for each language (using the same value as returned from the getLanguage function. Then you simply do a query of that table pulling the columns for the keyname and the selected language.

 

However, depending on the size of your site, that could return a LOT of records - most of which you wouldn't even need. You only need the text for the current page being displayed. So, you could also have an optional column to specify the page/area that the text is for. That way you can limit the results to those that would be applicable to the page being displayed.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.