How to write code for multilingual in php? [closed]
I tried this code but can not able to execute it. Can you help me with it?
///////////////////////////////
index.php
//////////////////////////////
<?php
require("decide-lang.php");
?>
<html><title>Exercice </title>
<body>
<?php echo TXT_INDEX; ?>
<p><br>
News: <?php echo TXT_NEWS; ?> <br>
Conseil du jour: <?php echo TXT_CONSEIL_INDEX ; ?> </p>
<p> </p>
<p><a href="index.php?lang=e开发者_StackOverflow中文版n">Not french??</a></p>
</body>
</html>
//////////////////////////////
decide-lang.php
//////////////////////////////
<?php
if ($_get['lang']=='fr') { // si la langue est 'fr' (français) on inclut le fichier fr-lang.php
include('fr-lang.php');
}
else if ($_get['lang']=='en') { // si la langue est 'en' (anglais) on inclut le fichier en-lang.php
include('en-lang.php');
}
else { // si aucune langue n'est déclarée on inclut le fichier fr-lang.php par défaut
include('fr-lang.php');
}
?>
//////////////////////////////
en-lang.php
/////////////////////////////
<?php
define('TXT_INDEX', 'Welcome on YOu_Site.com!');
define('TXT_NEWS', 'The sun is shining !');
define('TXT_CONSEIL_INDEX', 'Lets do some PHP !');
?>
//////////////////////////////
fr-lang.php
/////////////////////////////
<?php
define('TXT_INDEX', 'Bienvenue sur votre_site.com !');
define('TXT_NEWS', 'Il fait un soleil radieux !');
define('TXT_CONSEIL_INDEX', 'Faites du PHP !');
?>
$_get should be $_GET
And your decide lang condition could be simpler:
if (@$_GET['lang'] == 'en') include('en-lang.php');
else include('fr-lang.php');
You should also consider using session vars, based on $_GETs if you like.
This is a little long winded but might be a slightly better approach:
//////////////////////////////
decide-lang.php
//////////////////////////////
<?php
session_start();
if (isset($_SESSION['lang'])) $lang = $_SESSION['lang'];
if (isset($_GET['lang'])) {
$lang = $_GET['lang'];
$_SESSION['lang'] = $lang;
}
if (!isset($lang)) $lang = 'fr';
$langfile = $lang . '-lang.php';
if (file_exists($langfile)) include ($langfile);
else include('fr-lang.php');
?>
精彩评论