Jump to content

Multi language site


scar5308

Recommended Posts

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:
[URL=http://www.webwhiz.co.uk/lang12/tree.gif]tree[/URL]

I have the following main files: index.php and select.php
Working versions at: [URL=http://webwhiz.co.uk/lang12/index.php]index.php[/URL] [URL=http://webwhiz.co.uk/lang12/select.php]select.php[/URL]

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.co.uk/lang12/index.php?language=en if the user selects English from the list?

The code I have so far is:
index.php
[CODE]<?
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?>[/CODE]

select.php:
[CODE]<?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) ?>[/CODE]
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
[CODE]<!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>[/CODE]

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
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.