From 6ce9d0c4969b4a9e212413a3b3ffd8c15d5ef44e Mon Sep 17 00:00:00 2001 From: Brian Cooper Date: Tue, 2 Apr 2013 12:16:27 -0700 Subject: [PATCH 1/5] Load fallback dictionary and use when strings are unavailable in primary dictionary. --- README.markdown | 2 +- jquery.jsperanto.js | 57 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/README.markdown b/README.markdown index 10eb99a..c136f7c 100644 --- a/README.markdown +++ b/README.markdown @@ -11,7 +11,7 @@ Simple translation for your javascripts, yummy with your favorite templates engi * Works with : IE6+, Firefox 3+, Safari 3+, Chrome, Opera 9+ * AMD, CommonJS modules supported -Depends on jQuery 1.4+ (uses $.ajax, $.each, $.extend) +Depends on jQuery 1.5+ (uses $.ajax, $.each, $.extend, $.when, $.then) Usage example ============= diff --git a/jquery.jsperanto.js b/jquery.jsperanto.js index 2f84f86..da1656c 100644 --- a/jquery.jsperanto.js +++ b/jquery.jsperanto.js @@ -16,6 +16,7 @@ var o = {}; var dictionary = false; //not yet loaded + var fallbackDictionary = false; var currentLang = false; var count_of_replacement = 0; @@ -120,6 +121,15 @@ value = value && value[keys[i]]; i++; } + if(!value && fallbackDictionary) { + // try to find in fallback dictionary + value = fallbackDictionary; + i = 0; + while(keys[i]) { + value = value && value[keys[i]]; + i++; + } + } if(value){ value = applyReplacement(value,options); value = applyReuse(value,options); @@ -135,22 +145,43 @@ doneCallback(lang); return; } - return $.ajax({ - url: [o.dicoPath,"/", lang, '.json'].join(''), - success: function(data,status,xhr){ - dictionary = data; + return $.when( + $.ajax({ + url: [o.dicoPath,"/", lang, '.json'].join(''), + success: function(data, status, xhr) { + dictionary = data; + }, + async : o.async, + dataType: "json" + }), + $.ajax({ + url: [o.dicoPath,"/", o.fallbackLang, '.json'].join(''), + success: function(data, status, xhr) { + fallbackDictionary = data; + }, + async : o.async, + dataType: "json" + }) + ).then( + function() { + // success from both doneCallback(lang); }, - error : function(xhr,status,error){ - if(lang != o.fallbackLang){ - loadDictionary(o.fallbackLang,doneCallback); - }else{ - doneCallback(false); + function() { + // error from one + if(!(dictonary || fallbackDictionary)) + return doneCallback(false); + + // if lang couldn't be found, fallback + if(!dictionary) { + dictionary = fallbackDictionary; + doneCallback(o.fallbackLang); } - }, - async : o.async, - dataType: "json" - }); + + // if fallback lang couldn't be found, whatever + doneCallback(lang); + } + ) } function lang(){ From 1d6f7e921bc2c7df938e7e0841bf586de0057258 Mon Sep 17 00:00:00 2001 From: Brian Cooper Date: Mon, 13 May 2013 11:39:40 -0700 Subject: [PATCH 2/5] set default dictionary from options --- jquery.jsperanto.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.jsperanto.js b/jquery.jsperanto.js index da1656c..d98fa4c 100644 --- a/jquery.jsperanto.js +++ b/jquery.jsperanto.js @@ -141,7 +141,7 @@ function loadDictionary(lang,doneCallback){ if(o.dictionary){ - dictionary = o.dictionary; + dictionary = fallbackDictionary = o.dictionary; doneCallback(lang); return; } From 3aac165d316892867b46639574051675cc876597 Mon Sep 17 00:00:00 2001 From: Brian Cooper Date: Mon, 13 May 2013 11:39:50 -0700 Subject: [PATCH 3/5] fix typo --- jquery.jsperanto.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.jsperanto.js b/jquery.jsperanto.js index d98fa4c..5f3a347 100644 --- a/jquery.jsperanto.js +++ b/jquery.jsperanto.js @@ -169,7 +169,7 @@ }, function() { // error from one - if(!(dictonary || fallbackDictionary)) + if(!(dictionary || fallbackDictionary)) return doneCallback(false); // if lang couldn't be found, fallback From ae00d8f3dcfe0babc0c6191d29f92024858ccef9 Mon Sep 17 00:00:00 2001 From: Brian Cooper Date: Mon, 13 May 2013 11:40:06 -0700 Subject: [PATCH 4/5] use done instead of success method on ajax --- jquery.jsperanto.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/jquery.jsperanto.js b/jquery.jsperanto.js index 5f3a347..f6c2412 100644 --- a/jquery.jsperanto.js +++ b/jquery.jsperanto.js @@ -148,19 +148,17 @@ return $.when( $.ajax({ url: [o.dicoPath,"/", lang, '.json'].join(''), - success: function(data, status, xhr) { - dictionary = data; - }, async : o.async, dataType: "json" + }).done(function(data, status, xhr) { + dictionary = data; }), $.ajax({ url: [o.dicoPath,"/", o.fallbackLang, '.json'].join(''), - success: function(data, status, xhr) { - fallbackDictionary = data; - }, async : o.async, dataType: "json" + }).done(function(data, status, xhr) { + fallbackDictionary = data; }) ).then( function() { From af34766035dcc8bf640d8740a4da474591f6a6d6 Mon Sep 17 00:00:00 2001 From: Brian Cooper Date: Fri, 28 Jun 2013 13:44:30 -0700 Subject: [PATCH 5/5] change to when/then --- jquery.jsperanto.js | 69 ++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 32 deletions(-) diff --git a/jquery.jsperanto.js b/jquery.jsperanto.js index f6c2412..99505bc 100644 --- a/jquery.jsperanto.js +++ b/jquery.jsperanto.js @@ -145,41 +145,46 @@ doneCallback(lang); return; } - return $.when( - $.ajax({ - url: [o.dicoPath,"/", lang, '.json'].join(''), - async : o.async, - dataType: "json" - }).done(function(data, status, xhr) { - dictionary = data; - }), - $.ajax({ - url: [o.dicoPath,"/", o.fallbackLang, '.json'].join(''), - async : o.async, - dataType: "json" - }).done(function(data, status, xhr) { - fallbackDictionary = data; - }) - ).then( - function() { - // success from both - doneCallback(lang); - }, - function() { - // error from one - if(!(dictionary || fallbackDictionary)) - return doneCallback(false); - // if lang couldn't be found, fallback - if(!dictionary) { - dictionary = fallbackDictionary; - doneCallback(o.fallbackLang); - } + // seems complicated to have to define deferred manually rather than + // pass $.ajax's to $.when, but in that case, sometimes the when's + // fail will trigger before both ajax's have finished and will incorrectly + // report the fallback as being not found. + var dictDef = $.Deferred() + var fallbackDef = $.Deferred() + $.when(dictDef, fallbackDef).done(function() { + // error from both + if(!(dictionary || fallbackDictionary)) + return doneCallback(false); - // if fallback lang couldn't be found, whatever - doneCallback(lang); + // if lang couldn't be found, fallback + if(!dictionary) { + dictionary = fallbackDictionary; + return doneCallback(o.fallbackLang); } - ) + + // if fallback lang couldn't be found, whatever + doneCallback(lang); + }); + + $.ajax({ + url: [o.dicoPath,"/", lang, '.json'].join(''), + async : o.async, + dataType: "json" + }).done(function(data, status, xhr) { + dictionary = data; + }).always(function() { + dictDef.resolve(); + }); + $.ajax({ + url: [o.dicoPath,"/", o.fallbackLang, '.json'].join(''), + async : o.async, + dataType: "json" + }).done(function(data, status, xhr) { + fallbackDictionary = data; + }).always(function() { + fallbackDef.resolve(); + }); } function lang(){