使用 JavaScript 和 JSON 在 Web 应用程序中进行本地化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5090641/
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 in a web app using JavaScript and JSON
提问by u365975
I'm trying to set up a script to automatically localize a web application. As the web application is intended to use HTML5's offline caching ability, it needs to be able to work offline. As such, JavaScript seems to me to be the best option.
我正在尝试设置一个脚本来自动本地化 Web 应用程序。由于 Web 应用程序旨在使用 HTML5 的离线缓存能力,因此它需要能够离线工作。因此,在我看来 JavaScript 是最好的选择。
I'd prefer to put all the strings for all localizations in a single JSON file like so:
我更愿意将所有本地化的所有字符串放在一个 JSON 文件中,如下所示:
{
"en": {
"application_description": "This is the application's description in English.",
"application_name": "This is the application's name in English."
},
"ja": {
"application_description": "This is the application's description in Japanese.",
"application_name": "This is the application's name in Japanese."
}
}
I was thinking maybe of giving all elements that will be localized a custom attribute linking to the intended message (eg: i18n="application_name").
我在想也许会给所有将被本地化的元素提供一个链接到预期消息的自定义属性(例如:i18n="application_name")。
I can retrieve the browser's language using navigator.language, though I think some results will have to be split into just their first section (eg: "en-US" "en-GB" and "en-AU" should all refer to "en").
我可以使用 检索浏览器的语言navigator.language,但我认为某些结果必须仅拆分为它们的第一部分(例如:“en-US”、“en-GB”和“en-AU”都应指代“en”) .
However, my JavaScript knowledge is basic, and while I've thought about it, I cannot determine how to actually implement this.
然而,我的 JavaScript 知识是基本的,虽然我已经考虑过了,但我无法确定如何实际实现这一点。
Many thanks to anyone who can offer any tips or examples!
非常感谢任何可以提供任何提示或示例的人!
回答by Jishnu
You can use Wikimedia's JQuery.i18n.
您可以使用维基媒体的JQuery.i18n。
回答by HyderA
http://keith-wood.name/localisation.html
http://keith-wood.name/localisation.html
https://github.com/coderifous/jquery-localize
https://github.com/coderifous/jquery-localize
I don't have any personal experience in either but both appear to be fairly well documented.
我在这两个方面都没有任何个人经验,但两者似乎都有很好的记录。
回答by kflorence
Depending on your needs, the jquery-localize function might work well. If you need a more complex structure that mirrors that of your website, the localize library might be more useful: https://github.com/kflorence/localize
根据您的需要,jquery-localize 函数可能运行良好。如果您需要更复杂的结构来反映您网站的结构,则 localize 库可能更有用:https: //github.com/kflorence/localize

