coderifous / jquery-localize

a jQuery plugin that makes it easy to internationalize your web site.
465 stars 142 forks source link

Caching JSON #20

Closed blowsie closed 9 years ago

blowsie commented 11 years ago

I think the plugin should really be cahing the json file something like this...

var fileCache = {};

jsonCall = function (file, pkg, lang, level) {
            if (!fileCache[lang]) {
                var ajaxOptions, successFunc;
                if (options.pathPrefix != null) {
                    file = "" + options.pathPrefix + "/" + file;
                }
                successFunc = function (d) {
                    $.extend(intermediateLangData, d);
                    notifyDelegateLanguageLoaded(intermediateLangData);
                    return loadLanguage(pkg, lang, level + 1);
                };
                ajaxOptions = {
                    url: file,
                    dataType: "json",
                    async: false,
                    timeout: options.timeout != null ? options.timeout : 500,
                    success: successFunc
                };
                if (window.location.protocol === "file:") {
                    ajaxOptions.error = function (xhr) {
                        return successFunc($.parseJSON(xhr.responseText));
                    };
                }
                fileCache[lang] = $.ajax(ajaxOptions);
            }
                return fileCache[lang];
        };

I dont know coffee otherwise i would submit a pull

SongChen commented 11 years ago

Wouldn't jquery ajax cache things by default?

SongChen commented 11 years ago

I did some experiment. I think if you would like to call the localize function multiple times after initialization. The cache will help out. But blowsie's code seems not working by caching the ajax object.

I end of using the "$.localize.data" (currently it seems useless):

jsonCall = function(file, pkg, lang, level) {
  if($.localize.data[pkg+lang+level]) {
    intermediateLangData = $.localize.data[pkg+lang+level];
    notifyDelegateLanguageLoaded(intermediateLangData);
    return loadLanguage(pkg, lang, level + 1);
  }
  else {
    ... <-- Original function content goes here.
  }

};

coderifous commented 9 years ago

Normal HTTP caching applies, which is handled by the browser and web server.