如何在 jquery 中创建具有多语言内容的窗口?
例如。对于西类牙语,关闭窗口应该类似于“maximizar la ventana”。
最佳答案
您的服务器上可能有一组名为 en.php
、sp.php
等的文件。
在每个文件中,可以存储所有消息文本。
就像sp.php
中一样,
$text["close_window"] = "maximizar la ventana";
$text["thank_you"] = "Gracias";
和en.php
中,
$text["close_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
文件夹中。 - 用户输入已被净化。
- Google 根据上下文正确翻译了所有这些短语。
这是一个 Live Snippet at Codepad.viper-7.com
实时代码段和答案内代码有细微差别。在这里,我的代码使用 include 来获取外部语言文件,而在 Viper-7 的键盘中,我使用脚本内函数。
原因:因为我无法将文件写入键盘系统。
关于javascript - 如何使显示文本成为多语言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11840483/