PHP multilanguage site
I want make multilanguage site with PHP based on SESSIONS like this site untiny.com
I try with this code but not working :
<?
session_start();
$lang = $_GET['lang'];
if (!isset($lang)) {
include ('ar/language.php');
$lang = "ar";
}
else if ($lang == "en" ) {include ('en/language.php'); $SESSION["lang"] = "en"; header("Location: http://it2.in/");}
else if ($lang == "ar" ) {include ('ar/language.php'); $SESSION["lang"] = "ar"; header("Location: http://it2.in/");}
else if ($lang != "ar" || "en") {header("Location: http://it2.in/"); header("Location: http://it2.in/");}
?>
Anyone can help me. Thanks
Thank you all. B开发者_StackOverflowut @ now nothing working, Are there other ideas.
Probably your problem is in last line. It should work like:
else if ($lang != "ar" || $lang != "en") {header("Location: http://it2.in/");}
Also I suggest you to create a separate array to store available languages
$known_languages = array('en', 'ar'); ## just add new language here when you need
session_start();
## if language is stored in SESSION then use it, otherwise use GET params
if (array_key_exists('lang', $_SESSION)) {
$lang = $_SESSION['lang'];
include($lang.'/language.php');
## echo "You current language is <strong>$lang</strong>";
include("page.php");
}
else {
$lang = $_GET['lang'];
## if language is not set or is not available, then use default value
if (!isset($lang) || !in_array($lang, $known_languages) {
$lang = "ar";
}
include($lang.'/language.php');
$SESSION["lang"] = $lang;
header("Location: http://it2.in/");
}
Could you explain what exactly doesn't work?
There's an error in your if-statement. The last else-if is always true, because you're ORing the result from the comparison with the string "en". An else statement will do the job.
<?
session_start();
$lang = $_GET['lang'];
if (!isset($lang)) {
include ('ar/language.php');
$lang = "ar";
}
else if ($lang == "en" ) {include ('en/language.php'); $SESSION["lang"] = "en"; header("Location: http://it2.in/");}
else if ($lang == "ar" ) {include ('ar/language.php'); $SESSION["lang"] = "ar"; header("Location: http://it2.in/");}
else {header("Location: http://it2.in/"); header("Location: http://it2.in/");}
?>
$lang= $_GET['lang'];
include $lang . "/language.php";
Php by default disables those kind of includes, so you will have to enable it manually.
The real question is: what is in language.php?
// en/language.php
$MESSAGES[0] = "Hello";
// es/language.php
$MESSAGES[0] = "Hola";
// fr/language.php
Then in your code you do:
print "<h1>" . $MESSAGES[0] . "</h1>";
This will not scale up, and your head will explote very fast (wait, the message is 1023? or 1022? or 2149?). Please consider porting your code to GetText, which IMHO is a better solution and lets you add new languages without new code. Here is the first hit from google which will give you a head start. If you need more info, please look arround. http://www.phpdig.net/ref/rn26.html
When you are using header function always consider using exit(); after it to stop the execution of the code
<?php
session_start();
$lang = $_GET['lang'];
if (!isset($lang)) {
include ('ar/language.php');
$lang = "ar";
}
else if ($lang == "en" ) {
include ('en/language.php');
$SESSION["lang"] = "en";
header("Location: http://it2.in/");
exit();
}
else if ($lang == "ar" ) {
include ('ar/language.php');
$SESSION["lang"] = "ar";
header("Location: http://it2.in/");
exit();
}
else if ($lang != "ar" || $lang != "en") {
header("Location: http://it2.in/");
exit();
}
?>
now you should be redirected to your wanted page :)
<?php
session_start();
$lang = &$_SESSION['lang'];
$lang = $_GET['lang'];
switch ($lang)
{
case 'en' :
include ($lang . '/language.php');
break;
case 'ar' :
default :
include ('ar/language.php');
$lang = 'ar';
}
header('Location: http://it2.in/');
?>
精彩评论