Javascript 如何使显示文本多语言?
如何在jquery中创建具有多语言内容的窗口?Javascript 如何使显示文本多语言?,javascript,jquery,multilingual,Javascript,Jquery,Multilingual,如何在jquery中创建具有多语言内容的窗口? 例如,对于西班牙语,关闭窗口应该类似于“Maximamar la ventana”。您的服务器上可以有一组名为en.php,sp.php等的文件。 在每个文件中,可以存储所有文本消息 就像在sp.php, $text[“关闭窗口”]=“maximizar la ventana” $text[“谢谢”]=“Gracias” 在en.php, $text[“关闭窗口”]=“关闭此窗口” $text[“谢谢”]=“Gracias” 在主文件(index.
例如,对于西班牙语,关闭窗口应该类似于“Maximamar la ventana”。您的服务器上可以有一组名为
en.php
,sp.php
等的文件。在每个文件中,可以存储所有文本消息 就像在
sp.php
,$text[“关闭窗口”]=“maximizar la ventana”代码>
$text[“谢谢”]=“Gracias”代码>
在en.php
,
$text[“关闭窗口”]=“关闭此窗口”代码>
$text[“谢谢”]=“Gracias”代码>
在主文件(index.foo
)中,可以使用echo$text[“关闭窗口”]代码>或
echo$text[“谢谢”]
您希望在何处显示此文本
然后根据用户字符串或其他一些数据,您可以根据用户的语言在服务器端有条件地包括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";
?>
<?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";
?>
//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
index.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";
?>
<?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";
?>
//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
//检查浏览器是否发送了用户语言代码
如果(isset($\u服务器[“HTTP\u接受\u语言”])//如果浏览器已发送
//而且不是空的
&&($\u服务器[“HTTP\u接受\u语言”]!=“”)
){//如果条件结束
//从它那里得到前两封信
$user\u lang=substr($\u SERVER[“HTTP\u ACCEPT\u LANGUAGE”],0,2);
}
if(isset($\u POST[“lang”])//如果用户选择了它
//并且是我们想要的格式
&&(strlen($_POST[“lang”])==2)
){
$user_lang=$\u POST[“lang”];
}
//如果用户要求更改语言,请询问他
如果(isset($\u POST[“语言更改”])
&&($\u POST[“lang\u change”])//是真的吗?
{
问_lang();
exit();//立即退出脚本。。
}
如果(!isset($user\u lang)){//如果我们还没有$user\u lang,请询问
问_lang();
退出();
}
//主要索引文件内容
包括(“lang/”$user_lang.php”);
?>
|Example.com
, !
|
|
|
|
您的服务器上可以有一组名为en.php
、sp.php
等的文件。
在每个文件中,可以存储所有文本消息
就像在sp.php
,
$text[“关闭窗口”]=“maximizar la ventana”代码>
$text[“谢谢”]=“Gracias”代码>
在en.php
,
$text[“关闭窗口”]=“关闭此窗口”代码>
$text[“谢谢”]=“Gracias”代码>
在主文件(index.foo
)中,可以使用echo$text[“关闭窗口”]代码>或
echo$text[“谢谢”]
您希望在何处显示此文本
然后根据用户字符串或其他一些数据,您可以根据用户的语言在服务器端有条件地包括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";
?>
<?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";
?>
//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
index.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";
?>
<?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";
?>
//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
//检查浏览器是否发送了用户语言代码
如果(isset($\u服务器[“HTTP\u接受\u语言”])//如果浏览器已发送
//而且不是空的
&&($\u服务器[“HTTP\u接受\u语言”]!=“”)
){//如果条件结束
//从它那里得到前两封信
$user\u lang=substr($\u SERVER[“HTTP\u ACCEPT\u LANGUAGE”],0,2);
}
if(isset($\u POST[“lang”])//如果用户选择了它
//并且是我们想要的格式
&&(strlen($_POST[“lang”])==2)
){
$user_lang=$\u POST[“lang”];
}
//如果用户要求更改语言,请询问他
如果(isset($\u POST[“语言更改”])
&&($\u POST[“lang\u change”])//是真的吗?
{
问_lang();
exit();//立即退出脚本。。
}
如果(!isset($user\u lang)){//如果我们还没有$user\u lang,请询问
问_lang();
退出();
}
//主要索引文件内容
包括(“lang/”$user_lang.php”);
?>
|Example.com
, !
|
|
|
|
您可以使用替代方法变量。Crockford写了这样一个东西,并将其作为字符串的扩展来写。这是一种必不可少的模板:
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);
var西班牙语_message=”“.替换(西班牙语);
var german_message=“”.替换(德语);
这不是jQuery,但工作得很好。但是,您仍然需要确定自己使用哪种语言的条件。我建议在服务器端使用一些东西,例如为要使用的语言使用一个额外的参数。如果您有不同的窗口,您知道哪些窗口使用哪种语言,那么您还可以显示每个窗口使用的语言对象
此外,如果您的翻译变得更复杂,我建议您将它们放在外部js文件中
lg
flo您可以使用替代方法变量。Crockford写了这样一个东西,并将其作为字符串的扩展来写。这是一种必不可少的模板:
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);
var西班牙语_message=”“.替换(西班牙语);
var german_message=“”.替换(德语);
这不是jQuery,但工作得很好。但是,您仍然需要确定自己使用哪种语言的条件。我建议在服务器端使用一些东西,例如为要使用的语言使用一个额外的参数。如果您有不同的窗口,您知道哪些窗口使用哪种语言,那么您还可以显示每个窗口使用的语言对象
另外,如果你的跨