Jump to content


Photo

Multi language site


  • Please log in to reply
No replies to this topic

#1 scar5308

scar5308
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 25 October 2006 - 02:16 PM

I am trying to create a multi language site which open the index page in the users' browser language but allows the user to change it if needed.

My file tree is:
tree

I have the following main files: index.php and select.php
Working versions at: index.php select.php

They both work fine as they are. Index.php displays the correct language page, calling up the appropriate content from the language folders. select.php, displays the content according to the language selected.
What I want to do, is merge the code somehow so that the user initially arrives at the page apropriate to their browser as in index.php but has the option of changing their language preference as in select.php. I like the 'on select' method used here as it keeps the interface clean. How can I get the script to redirect to for instance; http://www.webwhiz.c...php?language=en if the user selects English from the list?

The code I have so far is:
index.php
<?
class object {};
$config = new object;
$config->dir     = "/home_path/lang12/lang_sets/";
session_start();
if (! isset($lang)) {
	$lang = "en";
	$lang = "pt";
	$lang = "sp";
	$lang = "lv";
}
else if ($lang == "pt-br" ) { $_SESSION["lang"] = "pt"; }
else if ($lang == "en" ) { $_SESSION["lang"] = "en"; }
else if ($lang == "sp" ) { $_SESSION["lang"] = "sp"; }
else if ($lang == "lv" ) { $_SESSION["lang"] = "lv"; }
else { $_SESSION["lang"] = "en"; }
$config->global = $config->dir.$lang."/language.php";
include ($config->global);
session_destroy();
?>


<?=$content1?>
<?=$content3?>
<?=$content2?>
<?=$content4?>

select.php:
<?php 
$lang = $_GET["language"]; 
if ($lang=="") {
    $lang = 1; 
}
?> 
<select onChange="window.location='select.php?language='+this.value"> 
<?php 

$language = array(

    "",

    "English",

    "Portuguese",

    "Spanish",

    "Latvian");

for ($i = 1; $i <= 4; $i++)

{

?>

<option value="<?=$i?>" <? if ($lang == $i) { print "SELECTED";}?>> <?=$language[$i]?></option>

<?

}

?>

</select>
<!-- content here -  Just to show it works! -->
<br>

<img src = " <?=$lang?>.gif"><!--posibly a flag here?-->
<?
include("./$lang.htm");
?><br />
<? if ($lang==1) { ?>
English
<? }
elseif ($lang==2) { ?>
Portuguese
<? }
elseif ($lang==3) { ?>
Spanish
<? }
elseif ($lang==4) { ?>
Latvian
<? }
else ($lang==1) ?>
I think this way would make updating and adding new files very easy as I could use a similar method for all the other pages including language specific menus and content from the other pages (I think)

The typical content for the language.php file could be:
lang_sets/lv/language.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>webwhiz lang12 lv</title>

</head>

<body>
<?
$content3 = "text";
$content2 = " in Latvian";

$content4 = include_once("../include/footer.php");
?>
</body>
</html>

I hope you can see what I am aiming at here. It will keep file organisation simple and allow additional languages to be added easily as some of the code could be inserted as 'include' files.

I a sure that once I get the answer for this step, I will hit more walls but  I am learning all the time :-)

Simon





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users