【问题标题】:How can I get wikimedia's jquery.i18n plugin to work with external files?如何让 wikimedia 的 jquery.i18n 插件与外部文件一起使用?
【发布时间】:2014-05-30 12:18:58
【问题描述】:

我无法让 wikimedia 的 jquery.i18n 插件处理外部文件。

这个:

// Localisation
$.i18n.debug = true;
$.i18n().load( {
    'fr' : {
        'message-hello' : 'bonjour!'
},
'en': '/assets/i18n/en.json'
} );
$.i18n().locale = 'fr';
console.log($.i18n('message-hello'));

运行良好。控制台说:

Loading messages from: /assets/i18n/en.json jquery.i18n.js:269
bonjour!

但是这个:

// Localisation
$.i18n.debug = true;
$.i18n().load( {
    'fr' : {
        'message-hello' : 'bonjour!'
},
'en': '/assets/i18n/en.json'
} );
$.i18n().locale = 'en';
console.log($.i18n('message-hello'));

不是。控制台说:

Loading messages from: /assets/i18n/en.json jquery.i18n.js:269
message-hello

我错过了什么?

这是我的/assets/i18n/en.json 文件:

{
    "@metadata": {
        "authors": [
            "Alice",
            "David",
            "Santhosh"
        ],
        "last-updated": "2012-09-21",
        "locale": "en",
        "message-documentation": "qqq",
        "AnotherMetadata": "AnotherMedatadataValue"
    },
    "appname-title": "Example Application",
    "appname-sub-title": "An example application with jquery.i18n",
    "appname-header-introduction": "Introduction",
    "appname-about": "About this application",
    "appname-footer": "Footer text",
    "message-hello": "hello!"
}

编辑:如果我从控制台执行$.i18n('message-hello'),它可以工作。这可能是因为当我从脚本调用$.i18n('message-hello') 时,json 文件尚未加载?如果是这样,我怎么能告诉脚本等到它呢?

【问题讨论】:

  • 如果你这样做$.i18n.prop('message-hello')会发生什么?
  • 我收到TypeError: undefined is not a function

标签: javascript jquery internationalization wikimedia


【解决方案1】:

好的,所以当我调用console.log($.i18n('message-hello')); 时,JSON 文件似乎还没有加载。我发现的解决方法是使用done() 方法并将我的所有其他代码包装在里面。

$.i18n.debug = true;
$.i18n().locale = 'en';
$.i18n().load( {
    'fr' : {
        'message-hello' : 'bonjour!'
    },
    'en': '/assets/i18n/en.json'
}).done( function() {

    // All my other JS code

});

如果有人有更好的解决方案,我会接受。

【讨论】:

  • 如果 load() 正在返回一个承诺,那可能解决方案。
猜你喜欢
  • 1970-01-01
  • 2018-07-16
  • 2020-02-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-19
  • 1970-01-01
  • 1970-01-01
  • 2017-05-20
相关资源
最近更新 更多