PHP多语言网站

时间:2009-10-23 18:38:12

标签: php session multilingual

我希望使用基于SESSIONS的PHP制作多语言网站,例如此网站untiny.com

我尝试使用此代码但无法正常工作:

    <?

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/");}

?>

任何人都可以帮助我。 感谢


谢谢大家。 但是@现在没什么用,还有其他的想法。

5 个答案:

答案 0 :(得分:2)

可能你的问题在最后一行。它应该像:

else if ($lang != "ar" || $lang != "en") {header("Location: http://it2.in/");}

另外,我建议您创建一个单独的数组来存储可用的语言

$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/");
}

答案 1 :(得分:1)

你能解释一下究竟什么不起作用吗?

你的if语句有错误。最后一个else-if始终为true,因为您将比较结果与字符串“en”进行OR运算。否则声明将完成这项工作。

<?

    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/");}

?>

答案 2 :(得分:1)

$lang= $_GET['lang'];
include $lang . "/language.php";

默认情况下,Php会禁用这些包含,因此您必须手动启用它。

真正的问题是:language.php中的内容是什么?

// en/language.php
$MESSAGES[0] = "Hello";
// es/language.php
$MESSAGES[0] = "Hola";
// fr/language.php

然后在您的代码中执行:

print "<h1>" . $MESSAGES[0] . "</h1>";

这不会扩大规模,你的头脑会快速爆发(等等,消息是1023?或1022?或2149?)。请考虑将您的代码移植到GetText,这是一个更好的解决方案,并允许您添加新语言而无需新代码。这是谷歌的第一个热门话题,它将为您提供一个良好的开端。如果您需要更多信息,请查看arround。 http://www.phpdig.net/ref/rn26.html

答案 3 :(得分:1)

当您使用标题功能时,请始终考虑使用exit();在它之后 停止执行代码

<?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(); 
}

?>

现在你应该被重定向到你想要的页面:)

答案 4 :(得分:0)

<?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/');
?>