2

I can't get wikimedia's jquery.i18n plugin to work with external files.

This :

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

is working perfectly. Console says:

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

but this :

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

is not. Console says:

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

What am I missing ?

Here is my /assets/i18n/en.json file :

{
    "@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!"
}

EDIT : If i do $.i18n('message-hello') from the console, it works. Could this be because the json file is not yet loaded when I call $.i18n('message-hello') from my script ? And if so, how can I tell the script to wait until it is ?

4

1 回答 1

3

好的,所以当我调用时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

});

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

于 2014-05-30T16:30:41.087 回答