0

请帮助组织双语网站。

所以首先有两个文件 eng.php,es.php,它们将存储在翻译站点中。

例子:

$lang['hi'] = 'Hi';

如何在网站上组织进一步的语言选择并在 cookie 中记录有关语言的信息?

4

2 回答 2

2

你可以有两个这样的文件。

来源en.php

$lang = array(
    'hi' => 'Hi'
);

来源es.php

$lang = array(
    'hi' => 'Hello'
);

而对于主要内容文件,来源应该是这样的:

<?php
    session_start(); // Make sure you initialize cookies / session
    $allowedLangs = array('en', 'es'); // Array with allowed values
    if(isset($_SESSION['lang'])) { // If already user had stored language in session
        include $_SESSION['lang'] . ".php";
    } elseif(isset($_GET['lang']) && in_array($_GET['lang'], allowedLangs)) { // If user had requested like index.php?lang=en
        include $_GET['lang'] . ".php";
        $_SESSION['lang'] = $_GET['lang']; // Update the session with the language
    } else { // If user is visiting for the first time, then...
        include "en.php";
    }

    echo $lang['hi'];
?>
于 2012-06-06T12:06:08.497 回答
0
<?php
    if(!isset($_COOKIE['lang'])){
        ?>
        Choose Language...
        <a href="es.php">ES</a><a href="eng.php">ENG</a>
        <?php
    } else {
        if($_COOKIE['lang']=='es'){
            header("location:es.php");
        }
        elseif($_COOKIE['lang']=='eng'){
            header("location:eng.php");
        }
    }
?>

es.php // 英文.php

<!--Your Content-->
<?php
    setcookie("lang","es/eng",time()+SECONDS_YOU_WANT)
?>
于 2012-06-06T12:09:15.150 回答