使用 JQuery 进行本地化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1215920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Localization with JQuery?
提问by opHASnoNAME
I have no idea how to handle localization with JQuery. I want to set an innerHTML
with German text, but if the browser is configured to use English, then I want to set English text.
我不知道如何使用 JQuery 处理本地化。我想设置一个innerHTML
德语文本,但是如果浏览器配置为使用英语,那么我想设置英语文本。
In PHP I use gettext for such stuff, but how to do it in JavaScript/jQuery?
在 PHP 中,我将 gettext 用于此类内容,但如何在 JavaScript/jQuery 中执行此操作?
回答by RaYell
There is no easy solution for that. What I would probably do is create several scripts with language texts for every language and include proper one in PHP. So if someone is using english version of your site you would include only english file, if someone is using german version you would include german language file etc.
对此没有简单的解决方案。我可能会做的是为每种语言创建几个带有语言文本的脚本,并在 PHP 中包含适当的脚本。因此,如果有人使用您网站的英文版本,您将只包含英文文件,如果有人使用德语版本,您将包含德语文件等。
Example:
例子:
// your script logic
myscript.js
// language texts
myscript.en.js
myscript.de.js
myscript.it.js
...
You can define all language files like that:
您可以像这样定义所有语言文件:
LANG = {
txt1: 'English text1',
txt2: 'English text2'
...
};
Make sure you are including only one of those in your HTML and make sure to include language file first i.e.
确保在 HTML 中只包含其中一个,并确保首先包含语言文件,即
<script type="text/javascript" src="myscript.de.js"></script>
<script type="text/javascript" src="myscript.js"></script>
Then you can use those localized texts in your main script for example:
然后您可以在主脚本中使用这些本地化文本,例如:
$(document).ready(function () {
alert(LANG.txt1);
});
What's best about it is that your logic (myscript.js
in this example) doesn't have to worry about localization and you won't have to change it if you want to add a new language file.
它的最佳之处在于您的逻辑(myscript.js
在此示例中)不必担心本地化,如果您想添加新的语言文件,则不必更改它。
回答by Martin Owen
I've not used it yet, but I'm thinking about using jquery-localizefor a project. If you don't mind basing the translation on 'rel' attributes defined on your elements then it looks like a good option.
我还没有使用它,但我正在考虑在一个项目中使用jquery-localize。如果您不介意基于元素上定义的 'rel' 属性进行翻译,那么它看起来是一个不错的选择。
Example from the README:
README 中的示例:
HTML
HTML
<p rel="localize[title]">Tracker Pro XT Deluxe</p>
<p rel="localize[search.placeholder]">Search...</p>
<p rel="localize[search.button]">Go!</p>
<p rel="localize[footer.disclaimer]">Use at your own risk.</p>
<p rel="localize[menu.dashboard]">Dashboard</p>
<p rel="localize[menu.list]">Bug List</p>
<p rel="localize[menu.logout]">Logout</p>
application-es.json
应用程序-es.json
{
title: "Tracker Pro XT Deluxo",
search: {
placeholder: "Searcho...",
button: "Vamos!"
},
footer: {
disclaimer: "Bewaro."
},
menu: {
dashboard: "Dashboardo",
list: "Bug Listo",
logout: "Exito"
}
}
Localize it!
本地化!
$("rel*=localize").localize("application", { language: "es" })
回答by Martin Owen
You actually don't need JQuery at all.
您实际上根本不需要 JQuery。
The navigator object in javascript (http://www.comptechdoc.org/independent/web/cgi/javamanual/javanavigator.html) contains a userLanguage(IE) and language(Netscape/Firefox) property that is set by the browser's current locale.
javascript ( http://www.comptechdoc.org/independent/web/cgi/javamanual/javanavigator.html) 中的导航器对象包含由浏览器的当前语言环境设置的 userLanguage(IE) 和 language(Netscape/Firefox) 属性.
To solve your example you could use something like;
为了解决你的例子,你可以使用类似的东西;
var textToSet = null; var userLang = null; /* -if userLanguage exists they're in IE, else firefox -get the first two letters in lowercase to guarantee an easily evaluated base language */ if(navigator.userLanguage) baseLang = substring(navigator.userLanguage,0,2).toLowerCase(); else baseLang = substring(navigator.language,0,2).toLowerCase(); //check languages switch(baseLang) { case "de": //German textToSet = "german text"; break; default: textToSet = "english text"; } document.getElementById('elementToSetTextInto').innerHTML = textToSet;
Keep in mind, you might want different text based off of base language and locale.. in that case look for "en-us", "de-de". Sites for culture codes are easily googled (1 link on first post only ;))
请记住,您可能需要基于基本语言和区域设置的不同文本……在这种情况下,请查找“en-us”、“de-de”。文化代码的网站很容易用谷歌搜索(只有第一篇文章有一个链接;))
Hope that works out for ya.
希望这对你有用。
回答by getSurreal
Since you already use gettext with your php application, you should look into using this javascript implementation of gettext
由于您已经在 php 应用程序中使用了 gettext,因此您应该考虑使用gettext 的这个javascript 实现
It doesn't use the compiled .mo files but it will use your .po files you already have.
它不使用已编译的 .mo 文件,但会使用您已有的 .po 文件。
This method has the advantage of managing all your translations in one place.
这种方法的优点是可以在一个地方管理所有翻译。
回答by ctrlShiftBryan
回答by 34m0
Another option you could look into is the Microsoft proposed jquery-global plugin. It is still in beta mind.
您可以研究的另一个选项是 Microsoft 提出的jquery-global plugin。它仍然处于测试阶段。