使用JavaScript和JSON在Web应用程序中进行本地化

木川 *_* 炎星 17 javascript json localization web-applications

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

我更喜欢将所有本地化的所有字符串放在一个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."
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

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