使用JavaScript和JSON在Web应用程序中进行本地化 [英] Localization in a web app using JavaScript and JSON

查看:104
本文介绍了使用JavaScript和JSON在Web应用程序中进行本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置脚本以自动本地化Web应用程序。由于Web应用程序旨在使用HTML5的脱机缓存功能,因此它需要能够脱机工作。因此,JavaScript在我看来是最好的选择。

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.

我更喜欢将所有本地化的所有字符串放在一个JSON文件中,如下所示:

I'd prefer to put all the strings for all localizations in a single JSON file like so:

{
    "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."
    }
}

我想的可能是提供所有元素本地化链接到预期消息的自定义属性(例如:i18n =application_name)。

I was thinking maybe of giving all elements that will be localized a custom attribute linking to the intended message (eg: i18n="application_name").

我可以使用 navigator.language检索浏览器的语言,虽然我认为有些结果必须分成第一部分(例如:en-USen-GB和en-AU都应该引用en) 。

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").

但是,我的JavaScript知识是基本的,虽然我已经考虑过了,但我无法确定如何实际实现它。

However, my JavaScript knowledge is basic, and while I've thought about it, I cannot determine how to actually implement this.

非常感谢任何能提供任何提示或示例的人!

Many thanks to anyone who can offer any tips or examples!

推荐答案

您可以使用Wikimedia的 JQuery.i18n

You can use Wikimedia's JQuery.i18n.

  • Documentation: https://github.com/wikimedia/jquery.i18n/wiki/API
  • Demo: http://thottingal.in/projects/js/jquery.i18n/demo/
  • Message Syntax: https://github.com/wikimedia/jquery.i18n/wiki/Message-Syntax

这篇关于使用JavaScript和JSON在Web应用程序中进行本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆