coderifous / jquery-localize

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

Loading external json files #9

Closed lookitsatravis closed 12 years ago

lookitsatravis commented 13 years ago

Hello!

First of all, excellent plugin. I'm in a situation with a site where this could be quite useful. However, one problem I ran into was the inability to load cross-domain json files. I tweaked the build file a little bit to accommodate this scenario. I forked the repo before realizing that this was in coffeescript, which I am not familiar with, so I figure I will just post this js code here if you'd like to use it. If I can find some time next week, I'll be happy to take a look at coffeescript and do a proper pull request...

...

$.localize = function(pkg, options) { var defaultCallback, intermediateLangData, jsonCall, lang, loadLanguage, notifyDelegateLanguageLoaded, regexify, valueForKey, wrappedSet; if (options == null) { options = {}; } options.sameDomain = (typeof options.sameDomain == "undefined") ? true : options.sameDomain;

...

jsonCall = function (file, pkg, lang, level) {

...

ajaxOptions = { url: file, dataType: (options.sameDomain) ? "json" : "jsonp", async: (options.sameDomain) ? false : true, timeout: options.timeout != null ? options.timeout : 500, success: successFunc, jsonpCallback: (options.sameDomain) ? '' : 'successFunc' };

...

Pretty simple change. Now by adding the option sameDomain: false (default is true) the script will use jsonp instead. I went ahead and specified the callback as the successFunc, since that's what it will wind up passing through anyway. Then all that's left would be formatting your external json file like so:

successFunc({ someString: "some text" });

Anyway, i'm sure others might find this useful. Great plugin and thanks for your work!