简体   繁体   English

PHP多语言站点

[英]PHP multilanguage site

I want make multilanguage site with PHP based on SESSIONS like this site untiny.com 我想使用基于SESSIONS的PHP创建多语言站点,例如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. 谢谢你们。 But @ 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. 您的if陈述有误。 The last else-if is always true, because you're ORing the result from the comparison with the string "en". 最后的else-if始终为true,因为您正在对比较结果与字符串“ en”进行“或”运算。 An else statement will do the job. else语句将完成此工作。

<?

    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. 默认情况下,Php禁用这些包含,因此您必须手动启用它。

The real question is: what is in language.php? 真正的问题是: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?). 这不会扩大规模,并且您的头部会飞得非常快(等待,消息是1023?或1022?或2149?)。 Please consider porting your code to GetText, which IMHO is a better solution and lets you add new languages without new code. 请考虑将代码移植到GetText,IMHO是更好的解决方案,可让您添加新语言而无需新代码。 Here is the first hit from google which will give you a head start. 这是google的首个热门产品,它将为您提供良好的开端。 If you need more info, please look arround. 如果您需要更多信息,请查看周围。 http://www.phpdig.net/ref/rn26.html http://www.phpdig.net/ref/rn26.html

When you are using header function always consider using exit(); 使用标头函数时,请始终考虑使用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/');
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM