From a4a32dbb1c3ac2bc4b69e7c5a84ca562b05903f8 Mon Sep 17 00:00:00 2001 From: Belelros Date: Sat, 17 Aug 2013 23:51:16 +0100 Subject: [PATCH] Merging PR #4 --- jqueryTranslator.js | 340 ++++++++++++++++++++-------------------- jqueryTranslator.min.js | 4 +- 2 files changed, 172 insertions(+), 172 deletions(-) diff --git a/jqueryTranslator.js b/jqueryTranslator.js index 469dedd..af162b5 100644 --- a/jqueryTranslator.js +++ b/jqueryTranslator.js @@ -3,177 +3,177 @@ Author: Antonio Laguna Twitter: @Belelros Website: http://www.funcion13.com - Version: 1.1 + Version: 1.1.1 */ (function($, window, document, undefined){ - var Translate = { - initialize : function(pkg, options){ - this.packages = []; - this.loaded = $.Deferred(); - - this.translatable = true; - - this.options = $.extend({}, $.fn.jqTranslate.options, options); - var userLanguage = this.getUserLanguage(); - - if (typeof pkg === 'string'){ - this.packages.push(pkg); - } - else { - this.packages = pkg; - } - - if (this.isTranslatable(userLanguage)){ - this.loadLanguages(); - } - else{ - this.translatable = false; - } - - return this.loaded.promise(); - }, - getUserLanguage : function(){ - var userLang = this.options.forceLang || navigator.language || navigator.userLanguage; - $.defaultLanguage = userLang; - - userLang = userLang.replace(/_/, '-').toLowerCase(); - - if (userLang.length > 3){ - userLang = userLang.substring(0,3) + userLang.substring(3).toUpperCase(); - this.languages = [ - userLang.substring(0,2), - userLang.substring(3).toUpperCase() - ]; - } - else { - this.languages = [ - userLang - ]; - } - - $.fn.jqTranslate.userLang = userLang; - return userLang; - }, - isTranslatable : function(language){ - if (this.options.defaultLang === language) { return false; } - else { return ($.inArray(language, this.options.skip) === -1); } - }, - loadLanguages : function(){ - var loaded = 0, maxLoad = Translate.languages.length * Translate.packages.length; - this.translatedStrings = {}; - - $.each(Translate.packages,function(i, pkg){ - var ePkg = pkg; - - $.each(Translate.languages, function (i, lang){ - Translate.getLanguage(ePkg,lang) - .done(Translate.storeLangFile) - .always(function(){ - loaded++; - if (loaded >= maxLoad){Translate.loaded.resolve();} - }); - }); - }); - }, - getLanguage : function(pkg, language){ - var self = this, - path = '', - got = $.Deferred(); - - if (self.options.path){ - path = self.options.path + '/'; - } - var url = path + [pkg, language].join('-') + '.json'; - - $.ajax ({ - url : url, - dataType : "json", - cache : self.options.cache, - async: self.options.asyncLangLoad - }).done(function(data){ - got.resolve(data); - }).fail(function(){ - if (self.options.fallbackLang){ - url = path + [pkg, self.options.fallbackLang].join('-') + '.json'; - $.ajax ({ - url : url, - dataType : "json", - cache : self.options.cache, - async: self.options.asyncLangLoad - }).done(function(data){ - got.resolve(data); - }); - } - else { - got.reject(); - } - }); - - return got; - }, - storeLangFile : function(data){ - $.extend(Translate.translatedStrings, data); - }, - translate : function() { - var elem = $(this), - key = elem.data('translate'); - - if (Translate.translatable){ - if (Translate.translatedStrings[key]){ - if (Translate.translatedStrings[key].length === undefined){ - // The key have nested keys - Translate.translateElement(elem,Translate.translatedStrings[key].text); - delete Translate.translatedStrings[key].text; - elem.attr(Translate.translatedStrings[key]); - } - else{ - Translate.translateElement(elem,Translate.translatedStrings[key]); - } - } - } - - if (typeof Translate.options.onComplete === 'function'){ - Translate.options.onComplete.apply(this, arguments); - } - - return elem; - }, - translateElement : function (elem, value){ - if (elem.is('input') || elem.is('textarea')) { - if (elem.is('[placeholder]')){ - elem.attr('placeholder', value); - } - else{ - elem.val(value); - } - } - else if (elem.is('optgroup')){ - elem.attr('label', value); - } - else if (elem.is('img')) { - elem.attr('alt',value); - } - else { - elem.html(value); - } - } - }; - - $.fn.jqTranslate = function(pkg, options){ - var self = this; - Translate.initialize(pkg, options).done(function(){ - self.each(Translate.translate); + var Translate = { + initialize : function(pkg, options){ + this.packages = []; + this.loaded = $.Deferred(); + + this.translatable = true; + + this.options = $.extend({}, $.fn.jqTranslate.options, options); + var userLanguage = this.getUserLanguage(); + + if (typeof pkg === 'string'){ + this.packages.push(pkg); + } + else { + this.packages = pkg; + } + + if (this.isTranslatable(userLanguage)){ + this.loadLanguages(); + } + else{ + this.translatable = false; + } + + return this.loaded.promise(); + }, + getUserLanguage : function(){ + var userLang = this.options.forceLang || navigator.language || navigator.userLanguage; + $.defaultLanguage = userLang; + + userLang = userLang.replace(/_/, '-').toLowerCase(); + + if (userLang.length > 3){ + userLang = userLang.substring(0,3) + userLang.substring(3).toUpperCase(); + this.languages = [ + userLang.substring(0,2), + userLang.substring(3).toUpperCase() + ]; + } + else { + this.languages = [ + userLang + ]; + } + + $.fn.jqTranslate.userLang = userLang; + return userLang; + }, + isTranslatable : function(language){ + if (this.options.defaultLang === language) { return false; } + else { return ($.inArray(language, this.options.skip) === -1); } + }, + loadLanguages : function(){ + var loaded = 0, maxLoad = Translate.languages.length * Translate.packages.length; + this.translatedStrings = {}; + + $.each(Translate.packages,function(i, pkg){ + var ePkg = pkg; + + $.each(Translate.languages, function (i, lang){ + Translate.getLanguage(ePkg,lang) + .done(Translate.storeLangFile) + .always(function(){ + loaded++; + if (loaded >= maxLoad){Translate.loaded.resolve();} + }); }); - return this; - }; - $.fn.jqTranslate.options = { - asyncLangLoad : true, - cache : true, - defaultLang : null, - fallbackLang : null, - forceLang : null, - onComplete : null, - path : null, - skip : [] - }; + }); + }, + getLanguage : function(pkg, language){ + var self = this, + path = '', + got = $.Deferred(); + + if (self.options.path){ + path = self.options.path + '/'; + } + var url = path + [pkg, language].join('-') + '.json'; + + $.ajax ({ + url : url, + dataType : "json", + cache : self.options.cache, + async: self.options.asyncLangLoad + }).done(function(data){ + got.resolve(data); + }).fail(function(){ + if (self.options.fallbackLang){ + url = path + [pkg, self.options.fallbackLang].join('-') + '.json'; + $.ajax ({ + url : url, + dataType : "json", + cache : self.options.cache, + async: self.options.asyncLangLoad + }).done(function(data){ + got.resolve(data); + }); + } + else { + got.reject(); + } + }); + + return got; + }, + storeLangFile : function(data){ + $.extend(Translate.translatedStrings, data); + }, + translate : function() { + var elem = $(this), + key = elem.data('translate'); + + if (Translate.translatable){ + if (Translate.translatedStrings[key]){ + if (Translate.translatedStrings[key].length === undefined){ + // The key have nested keys + Translate.translateElement(elem,Translate.translatedStrings[key].text); + delete Translate.translatedStrings[key].text; + elem.attr(Translate.translatedStrings[key]); + } + else{ + Translate.translateElement(elem,Translate.translatedStrings[key]); + } + } + } + + if (typeof Translate.options.onComplete === 'function'){ + Translate.options.onComplete.apply(this, arguments); + } + + return elem; + }, + translateElement : function (elem, value){ + if (elem.is('input') || elem.is('textarea')) { + if (elem.is('[placeholder]')){ + elem.attr('placeholder', value); + } + else{ + elem.val(value); + } + } + else if (elem.is('optgroup')){ + elem.attr('label', value); + } + else if (elem.is('img')) { + elem.attr('alt',value); + } + else { + elem.html(value); + } + } + }; + + $.fn.jqTranslate = function(pkg, options){ + var self = this; + Translate.initialize(pkg, options).done(function(){ + self.each(Translate.translate); + }); + return this; + }; + $.fn.jqTranslate.options = { + asyncLangLoad : true, + cache : true, + defaultLang : null, + fallbackLang : null, + forceLang : null, + onComplete : null, + path : null, + skip : [] + }; })(jQuery, window, document); diff --git a/jqueryTranslator.min.js b/jqueryTranslator.min.js index 251d280..2e92e5c 100644 --- a/jqueryTranslator.min.js +++ b/jqueryTranslator.min.js @@ -3,5 +3,5 @@ Author: Antonio Laguna Twitter: @Belelros Website: http://www.funcion13.com - Version: 1.1 -*/(function(e,t,n,r){var i={initialize:function(t,n){this.packages=[];this.loaded=e.Deferred();this.translatable=!0;this.options=e.extend({},e.fn.jqTranslate.options,n);var r=this.getUserLanguage();typeof t=="string"?this.packages.push(t):this.packages=t;this.isTranslatable(r)?this.loadLanguages():this.translatable=!1;return this.loaded.promise()},getUserLanguage:function(){var t=this.options.forceLang||navigator.language||navigator.userLanguage;e.defaultLanguage=t;t=t.replace(/_/,"-").toLowerCase();if(t.length>3){t=t.substring(0,3)+t.substring(3).toUpperCase();this.languages=[t.substring(0,2),t.substring(3).toUpperCase()]}else this.languages=[t];e.fn.jqTranslate.userLang=t;return t},isTranslatable:function(e){return this.options.defaultLang===e?!1:this.options.skip.indexOf(e)===-1},loadLanguages:function(){var t=0,n=i.languages.length*i.packages.length;this.translatedStrings={};e.each(i.packages,function(r,s){var o=s;e.each(i.languages,function(e,r){i.getLanguage(o,r).done(i.storeLangFile).always(function(){t++;t>=n&&i.loaded.resolve()})})})},getLanguage:function(t,n){var r=this,i="",s=e.Deferred();r.options.path&&(i=r.options.path+"/");var o=i+[t,n].join("-")+".json";e.ajax({url:o,dataType:"json",cache:r.options.cache,async:r.options.asyncLangLoad}).done(function(e){s.resolve(e)}).fail(function(){if(r.options.fallbackLang){o=i+[t,r.options.fallbackLang].join("-")+".json";e.ajax({url:o,dataType:"json",cache:r.options.cache,async:r.options.asyncLangLoad}).done(function(e){s.resolve(e)})}else s.reject()});return s},storeLangFile:function(t){e.extend(i.translatedStrings,t)},translate:function(){var t=e(this),n=t.data("translate");if(i.translatable&&i.translatedStrings[n])if(i.translatedStrings[n].length===r){i.translateElement(t,i.translatedStrings[n].text);delete i.translatedStrings[n].text;t.attr(i.translatedStrings[n])}else i.translateElement(t,i.translatedStrings[n]);typeof i.options.onComplete=="function"&&i.options.onComplete.apply(this,arguments);return t},translateElement:function(e,t){e.is("input")||e.is("textarea")?e.is("[placeholder]")?e.attr("placeholder",t):e.val(t):e.is("optgroup")?e.attr("label",t):e.is("img")?e.attr("alt",t):e.html(t)}};e.fn.jqTranslate=function(e,t){var n=this;i.initialize(e,t).done(function(){n.each(i.translate)});return this};e.fn.jqTranslate.options={asyncLangLoad:!0,cache:!0,defaultLang:null,fallbackLang:null,forceLang:null,onComplete:null,path:null,skip:[]}})(jQuery,window,document); \ No newline at end of file + Version: 1.1.1 +*/(function(e,t,n,r){var i={initialize:function(t,n){this.packages=[];this.loaded=e.Deferred();this.translatable=!0;this.options=e.extend({},e.fn.jqTranslate.options,n);var r=this.getUserLanguage();typeof t=="string"?this.packages.push(t):this.packages=t;this.isTranslatable(r)?this.loadLanguages():this.translatable=!1;return this.loaded.promise()},getUserLanguage:function(){var t=this.options.forceLang||navigator.language||navigator.userLanguage;e.defaultLanguage=t;t=t.replace(/_/,"-").toLowerCase();if(t.length>3){t=t.substring(0,3)+t.substring(3).toUpperCase();this.languages=[t.substring(0,2),t.substring(3).toUpperCase()]}else this.languages=[t];e.fn.jqTranslate.userLang=t;return t},isTranslatable:function(t){return this.options.defaultLang===t?!1:e.inArray(t,this.options.skip)===-1},loadLanguages:function(){var t=0,n=i.languages.length*i.packages.length;this.translatedStrings={};e.each(i.packages,function(r,s){var o=s;e.each(i.languages,function(e,r){i.getLanguage(o,r).done(i.storeLangFile).always(function(){t++;t>=n&&i.loaded.resolve()})})})},getLanguage:function(t,n){var r=this,i="",s=e.Deferred();r.options.path&&(i=r.options.path+"/");var o=i+[t,n].join("-")+".json";e.ajax({url:o,dataType:"json",cache:r.options.cache,async:r.options.asyncLangLoad}).done(function(e){s.resolve(e)}).fail(function(){if(r.options.fallbackLang){o=i+[t,r.options.fallbackLang].join("-")+".json";e.ajax({url:o,dataType:"json",cache:r.options.cache,async:r.options.asyncLangLoad}).done(function(e){s.resolve(e)})}else s.reject()});return s},storeLangFile:function(t){e.extend(i.translatedStrings,t)},translate:function(){var t=e(this),n=t.data("translate");if(i.translatable&&i.translatedStrings[n])if(i.translatedStrings[n].length===r){i.translateElement(t,i.translatedStrings[n].text);delete i.translatedStrings[n].text;t.attr(i.translatedStrings[n])}else i.translateElement(t,i.translatedStrings[n]);typeof i.options.onComplete=="function"&&i.options.onComplete.apply(this,arguments);return t},translateElement:function(e,t){e.is("input")||e.is("textarea")?e.is("[placeholder]")?e.attr("placeholder",t):e.val(t):e.is("optgroup")?e.attr("label",t):e.is("img")?e.attr("alt",t):e.html(t)}};e.fn.jqTranslate=function(e,t){var n=this;i.initialize(e,t).done(function(){n.each(i.translate)});return this};e.fn.jqTranslate.options={asyncLangLoad:!0,cache:!0,defaultLang:null,fallbackLang:null,forceLang:null,onComplete:null,path:null,skip:[]}})(jQuery,window,document); \ No newline at end of file