1

如何在 jquery 中创建具有多语言内容的窗口?
例如。对于西班牙语,关闭窗口应该类似于“maximizar la ventana”。

4

2 回答 2

1

您的服务器上可以有一组名为 等的en.php文件sp.php
在每个文件中,可以存储所有消息文本。

就像在sp.php,
$text["close_window"] = "maximizar la ventana";
$text["thank_you"] = "Gracias";

并且在en.php,
$text["close_window"] = "Close this window"; $text["thank_you"] = "Gracias";

在您的主文件(index.foo)中,您可以使用echo $text["close_window"];
echo $text["thank_you"]在您希望显示此文本的位置。

然后根据用户字符串或其他一些数据,您可以根据用户的语言有条件地在服务器端包含english.lang或spanish.lang。

文件结构:

index.php //主文件 lang //语言文件文件夹 lang/en.php //英语语言文件 lang/sp.php //西班牙语语言文件

示例代码:

en.php:

<?php
$text["close_window"] = "Close this window";
$text["thank_you"] = "Thank you";
$text["welcome"] = "Welcome";
$text["home"] = "Home";
$text["about_us"] = "About Us";
$text["company_history"] = "Company History";
$text["company_profile"] = "Company Profile";
$text["contact_us"] = "Contact Us";
$text["greetings"] = "You have selected English";
?>

sp.php:

<?php
$text["close_window"] = "maximizar la ventana";
$text["thank_you"] = "Gracias";
$text["welcome"] = "Bienvenida";
$text["home"] = "Casa";
$text["about_us"] = "Sobre Nosotros";
$text["company_history"] = "Historia de la Empresa";
$text["company_profile"] = "Perfil de la Empresa";
$text["contact_us"] = "Contact Us";
$text["greetings"] = "Usted ha seleccionado Español";
?>

index.php:

 //Check if browser sent the User Language code
if(isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])      // if browser sent
    //AND is NOT empty
    && ($_SERVER["HTTP_ACCEPT_LANGUAGE"] != "")
    ){ //if conditions END
    // get first two letters from it
    $user_lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0, 2);
}
if(isset($_POST["lang"]) //if user selected it
  //and is in our desired format
  && (strlen($_POST["lang"]) == 2)
){
    $user_lang = $_POST["lang"];
}
//if User requested to change language, ask him
if(isset($_POST["lang_change"])
  && ($_POST["lang_change"])) // is true ?
{
    ask_lang();
    exit(); // exit the script now..
}
if(!isset($user_lang)){ //if we dont have $user_lang yet, ask
    ask_lang();
    exit();
}
//Main index file contents
include("lang/".$user_lang."php");
?>
<html>
    <head>
        <title><?php echo $text["welcome"]; ?> | Example.com</title>
    <head>
    <body>
    <?php echo $text["welcome"]; ?>, <?php echo $text["greetings"]; ?>!<br />
    <a href="index.php" title="<?php echo $text["home"]; ?>" >
        <?php echo $text["home"]; ?></a> |
    <a href="about_us.php" title="<?php echo $text["about_us"]; ?>" >
        <?php echo $text["about_us"]; ?></a> |
    <a href="history.php" title="<?php echo $text["company_history"]; ?>" >
        <?php echo $text["company_history"]; ?></a> |
    <a href="profile.php" title="<?php echo $text["company_profile"]; ?>" >
        <?php echo $text["company_profile"]; ?></a> |
    <a href="contact_us.php" title="<?php echo $text["contact_us"]; ?>" >
        <?php echo $text["contact_us"]; ?></a>
    <p>
        <form method="POST" action="">
            <input type="hidden" name="lang_change" value="true" />
            <input type="submit" value="<?php echo $text["change_language"]; ?>" name="change_language" />
        </form>
    </p>
    </body>
</html>

<?php
//Main index file contents ENDS
function ask_lang(){
?>
<html>
    <head>
        <title>Please Select Language</title>
    <head>
    <body>
        <form method="POST" action="">
            <fieldset>
                <legend>Please select language:</legend>
                <input type="radio" value="en" name="lang" />English<img src="en.png"><br />
                <input type="radio" value="sp" name="lang" />Spanish<img src="sp.png"><br />
                <input type="submit" value="OK" name="sumbit" />
            </fieldset>
        </form>
    </body>
</html>
<?php
} //function ask_lang() ENDS

假设:

  • 所有类型的可能翻译文件都在Lang文件夹中。
  • 用户输入被净化。
  • 谷歌根据上下文正确翻译了所有这些短语。

这是Codepad.viper-7.com 上的实时片段

Live Snippet & In-Answer 代码有一点不同。在这里,我的代码使用include来获取外部语言文件,而在 Viper-7 的键盘中,我使用脚本内函数。
原因:因为我无法将文件放入/写入键盘系统。

于 2012-08-07T06:44:39.270 回答
0

您可以使用supplant-method 变体。Crockford 在这里写过这样的事情,并将其作为String. 必不可少的一种模板:

if (!String.prototype.supplant) {
    String.prototype.supplant = function (o) {
        return this.replace(/{([^{}]*)}/g,
            function (a, b) {
                var r = o[b];
                return typeof r === 'string' || typeof r === 'number' ? r : a;
            }
        );
    };
}

关于您的问题,您可以使用包含您的翻译的对象:

var spanish = {close: "maximizar la ventana", thanks: "Gracias"};
var german  = {close: "Fenster schließen", thanks: "Danke"};
var english = {close: "Close window", thanks: "Thank you"};

然后在字符串上使用它:

var spanish_message = "<a href='javascript:void(0);'>{close}</a>".supplant(spanish);

var german_message = "<a href='javascript:void(0);'>{close}</a>".supplant(german);

这不是 jQuery,但效果很好。但是,您仍然需要确定自己使用哪种语言的条件。我建议在服务器端使用一些东西,例如要使用的语言的额外参数。如果您有不同的窗口,您知道哪种语言,您还可以显示每种语言使用的语言对象。

另外,如果你的翻译变得更复杂,我建议你把它们放在外部的 js 文件中。

lg,

弗洛

于 2012-08-07T07:02:23.083 回答