Page 1 of 1

include() ?

Posted: Wed Aug 27, 2003 12:12 pm
by Dimka
Wise people help!

i was trying to make a multilanguage system for my web site and got into trouble

this is how it looks like

Code: Select all

examples:
index.php

PHP:--------------------------------------------------------------------------------

<?php
// lets start with setting the language
if ($lang == 'ru')
{
include"lang/ru.php";
}
elseif ($lang == 'cz')
{
include"lang/cz.php";
}
elseif ($lang == 'en')
{
include"lang/en.php";
}
else
{
include"lang/en.php";
}
// end of setting language
?>
<title><?php echo $lang['pagetitle'] ?></title>
<br>
<style type="text/CSS">
body {
    background: #FFFFFF;
    font-size: 8pt;
    font-family: Tahoma;
}
A:link            { COLOR: #56728e; TEXT-DECORATION: none; FONT-FAMILY: tahoma; }
 A:active        { COLOR: #56728e; TEXT-DECORATION: none; FONT-FAMILY: tahoma; }
 A:visited         { COLOR: #56728e; TEXT-DECORATION: none; FONT-FAMILY: tahoma; }
 A:hover        { COLOR: #96b2ce; TEXT-DECORATION: bold; FONT-FAMILY: tahoma; }
INPUT, TEXTAREA, SELECT    { FONT-SIZE: 8pt; FONT-FAMILY: tahoma; }
</style>
<br><br><br><center><b><?php echo $lang['hello'] ?></center><br><br><br>
Choose your language: 

<form method="post" name="lang_form" action="<?php echo $HTTP_REFERER ?>" onSubmit="if(document.jumpbox.f.value == -1){return false;}">
<select name="lang">
<option value="en" selected>English</option>
<option value="ru">Russian</option>
<option value="cz">Czech</option>
</select> <input type="submit" value="Go">
</form>

<a href="index.php?lang=en">English</a> - <a href="index.php?lang=ru">Russian</a> - <a href="index.php?lang=cz">Czech</a>
<br><br><br>
$lang['test_text']<br><br><br><br><p>&nbsp;</p><center>© 2003 <a href=mailto:dimak@volny.cz>Dimak</a></center>
--------------------------------------------------------------------------------

example language file: lang/en.php

PHP:--------------------------------------------------------------------------------

<?php
$lang['pagetitle'] = 'Testpage';
$lang['hello'] = 'Hello, welcome to the test page. You can use every html tag here and it will appear as HTML. like this one: ';
?>


but the problem is it only shows firs leters that are in the lang file
www.intelia.ru

your advices are welcome!

thanks!

Posted: Wed Aug 27, 2003 12:22 pm
by JAM
1. Title - You are missing a ; after the ]. Same goes with $HTTP_REFERER.
2. $lang['test_text'] - You are missing the entire <?php echo $lang['test_text']; ?>

Also... If you like, please check the last link in my signature (Passing Variables) for more info about using a form like yours.

Rewritten:

Code: Select all

<title><?php echo $lang['pagetitle']; ?></title> 
...
<br><br><br><center><b><?php echo $lang['hello']; ?></center><br><br><br> 
...
<form method="post" name="lang_form" action="<?php echo $HTTP_REFERER; ?>" onSubmit="if(document.jumpbox.f.value == -1){return false;}"> 
...
<?php echo $lang['test_text']; ?><br><br><br><br><p> </p><center>© 2003 <a href=mailto:dimak@volny.cz>Dimak</a></center>

Posted: Wed Aug 27, 2003 12:33 pm
by Unipus
Also, if this is the extent of your example, and you have full control over the languages available, it would be much simpler to write your language switcher like this, instead of the big if/else that you have:

Code: Select all

if (!$lang) {
$lang = "en";
}

include "/lang/" . $lang . ".php";
That way it's all automated and you don't ever have to update it again. The disadvantage is that if $lang somehow gets set to something that there's not a corresponding file for, the user won't see any content.

Posted: Wed Aug 27, 2003 12:37 pm
by JAM
Unipus wrote: The disadvantage is that if $lang somehow gets set to something that there's not a corresponding file for, the user won't see any content.
Well put.
Tho, used with file_exists(), the $lang file wanted could be searched for, and if not found, display a default. There are endless ways with this type. =)