define("pagetitle", "Multi-language application/site example.");
We will have to define how many constant as are the text strings of our site/app... I hear you saying: "but this is terribly annoying!" Oh yes, it is. But since we haven't a magic wand, this is the only way to do it. Once you have written your english.lang, it's time to write another lang file, for instance italian.lang (but obviously you can choose the language you prefer: I choose Italian because... I'm Italian!). In the archive you find two lang file, italian.lang and english.lang ready to use to test my example.
<table>
<tr>
<td>
<form id="" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="hidden" id="" name="lang" value="english" /><input
class="flagen" type="submit" id="submit" name="submit" value="" />
</form>
</td>
<td>
<form id="" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="hidden" id="" name="lang" value="italian" /><input
class="flagit" type="submit" id="submit" name="submit" value="" />
</form>
</td>
</tr>
</table>
Notice the hidden field to pass lang value when form is submitted. Also notice that submit button has a class attribute: this is to give it an appropriate flag image as background (for this you can see multilang.css file in the archive).
<?php
session_start();
if (isset($_POST['lang'])){
$lang = $_POST['lang'];
}elseif (isset($_SESSION['lang'])){
$lang = $_SESSION['lang'];
}else{
$lang = 'english';
}
$_SESSION['lang'] = $lang;
include ($lang.".lang");
}
?>
First, we start a php sesison so $lang variable will be kept during all session without other efforts.
<title><?php echo pagetitle ?></title>
Now the title of our page will be displayed in the correct language. Let me do another example. Suppose to have defined a constant to display some text to invite our user to choose a language:
define("chooselang", "Choose language: "); (english.lang)
define("chooselang", "Scegli la lingua: "); (italian.lang)
<table>
<tr>
<td>
<?php echo chooselang ?>
</td>
<td>
<form id="" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="hidden" id="" name="lang" value="english" /><input
class="flagen" type="submit" id="submit" name="submit" value="" />
</form>
</td>
<td>
<form id="" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="hidden" id="" name="lang" value="italian" /><input
class="flagit" type="submit" id="submit" name="submit" value="" />
</form>
</td>
</tr>
</table>
So our text will be displayed in the correct language. And so on.
[embed=file 346446]
multilanguage.exe
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)