jquery.i18n icon indicating copy to clipboard operation
jquery.i18n copied to clipboard

Waiting for Resouces to Load

Open toddca opened this issue 11 years ago • 4 comments

Love this project - just have one issue...I have the following code where i call initResources() as soon as the document ready fires. I have a race condition in that other js code executes before the load() completes and I cannot get the string resource. How can I block/wait for the load to complete before trying to access the string resources in a safe way?

that.getResource = function(key, param1) {

    if (!localeInitialized) {
        that.log('resource not loaded!');
    }

    return $.i18n(key, param1);
}, 
that.initResources = function() {
    i18N = $.i18n();
    i18N.locale = $('html').attr('lang');
    i18N.load('/scripts/lang/' + i18N.locale + '.json', i18N.locale).done(function () {
        localeInitialized = true;
    });
},

toddca avatar Jul 17 '14 00:07 toddca

In most of the usecases, the resource files(message json) is at server and it takes a while to get loaded at client. It is not recommended to block the code execution by using synchronous message file loading.

Following is a convenient wrapper around $.fn.i18n that take care of message loading as well.

        $.fn.i18nText = function ( key, params ) {
            var i18n = $.i18n(),
                $element = $( this );
            i18n.load( 'i18n/en.json', 'en' ).done( function () {
                if ( $element.data( 'i18n' ) ) {
                    $element.i18n();
                } else {
                    $element.text( $.i18n( key, params ) );
                }
            } );
            return $element;
        }
        $( document ).ready( function ( $ ) {
            $( 'span' ).i18nText( 'key-one', 3 );
        } )

I hope that helps

santhoshtr avatar Jul 20 '14 12:07 santhoshtr

Now that I wrote this, I am thinking why can't this the jquery.i18n have the above feature? :). I will investigate it as an enhancement. Thanks

santhoshtr avatar Jul 20 '14 13:07 santhoshtr

Not sure how I get around blocking for a resource, when my page loads I have script which sometimes runs which needs to show the user a message (error/warning, etc) this script calls into my wrapper to load the string resources for the dismiss buttons on the dialog being created. If I show the message before the resources are fully loaded I get the key of the message rather than the message string.

toddca avatar Jul 20 '14 16:07 toddca