PHP multilanguage site

為{幸葍}努か 提交于 2020-01-06 07:08:29

问题


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. But @ now nothing working, Are there other ideas.


回答1:


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



回答2:


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

?>



回答3:


$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




回答4:


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 :)




回答5:


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


来源:https://stackoverflow.com/questions/1615209/php-multilanguage-site

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!