/* Translation library v0.2 */ // Extend jQuery (function ($) { // Get the URL query string and parse it jQuery.query = function() { var r = {length:0}; var q = location.search; if(q && q != '#'){ // remove the leading ? and trailing & q = q.replace(/^\?/,'').replace(/\&$/,''); jQuery.each(q.split('&'), function(){ var key = this.split('=')[0]; var val = this.split('=')[1]; if(/^[0-9.]+$/.test(val)) val = parseFloat(val); // convert floats r[key] = val; r['length']++; }); } return r; }; function Translator(inp){ this.q = $.query(); this.id = (inp && typeof inp.id==="string") ? inp.id : 'form'; this.master = (inp && typeof inp.master==="string") ? inp.master : ''; this.json = (inp && typeof inp.json==="string") ? inp.json : ''; this.langs = (inp && typeof inp.langs==="object") ? inp.langs : { 'en':'English' }; // Load the master language config file this.setLanguage() this.lang = this.q.lang; if(!this.lang) this.lang = "en"; this.page = $('#'+this.id); html = "
"; if($('#translate_chooser').length == 0) this.page.prepend(''); if($('#translation').length == 0) this.page.append('') $('#translate_chooser').html(html).find('#langchoice select').bind('change',{me:this},function(e){ e.data.me.setLanguage($(this).find("option:selected").val()); }); this.setLanguage(this.lang); return this; } Translator.prototype.setLanguage = function(lang){ if(lang) this.lang = lang; this.loadLanguage(this.lang,function(e){ if(e.lang){ this.lang = e.lang; this.phrasebook = (e.data) ? e.data : { 'language': {'name':'','code':e.lang } }; }else{ this.masterbook = e.data; } this.rebuildForm(); if(e.lang){ var href = $('a.langlink').attr('href'); $('a.langlink').attr('href',href.substring(0,href.indexOf('?'))+'?lang='+this.lang); $('.langname').html(this.phrasebook.language.name); } }); return this; } Translator.prototype.loadLanguage = function(lang,callback,langdummy){ var url = (lang) ? this.json.replace('%LANG%',lang) : this.master; if(typeof langdummy=="string") lang = langdummy; if(lang) this.lang = lang; $.ajax({ url: url, method: 'GET', dataType: 'json', context: this, error: function(){ // We couldn't find this language so load the English version // so there is something to work from. console.log("Couldn't load "+lang) if(lang != "en") this.loadLanguage('en',callback,lang); }, success: function(data){ // Update the language code and name if(data && data.language && typeof data.language.code==="string") data.language.code = lang; if(data && data.language && typeof data.language.code==="string") data.language.name = this.langs[lang]; if(typeof callback==="function") callback.call(this,{data:data,lang:lang}); } }); } Translator.prototype.rebuildForm = function(){ var html = ""; $('#translation').html(html); $('#translation input, #translation textarea, #translation select').attr('dir',(this.phrasebook && this.phrasebook.language && this.phrasebook.language.alignment=="right" ? "rtl" : "ltr")).unbind().bind('change',{me:this},function(e){ e.data.me.getOutput(); e.data.me.percentComplete(); }); // Update the text direction when the appropriate select box changes $('#translation select[name=".language.alignment"]').on('change',function(e){ $('#translation input, #translation textarea, #translation select').attr('dir',($(this).val()=="right" ? "rtl" : "ltr" )); }); this.getOutput(); this.percentComplete(); return this; } Translator.prototype.buildForm = function(m,p,k){ var html = ""; var newk = ""; var inp = ""; var arr = false; var n; var css; if(!k) k = ""; for(key in m){ n = parseInt(key); newk = k+"."+key; if(typeof m[key]==="object"){ if(n >= 0 && this.previousgroup != n) html += ''; if(m[key]._text && m[key]._type){ inp = ""; if(m[key]._type=="textarea"){ css = (m[key]._height) ? ' style="height:'+m[key]._height+'"' : ""; inp = ''; }else if(m[key]._type=="noedit"){ inp = ''+sanitize((p ? p[key] : "")); }else if(m[key]._type=="select"){ inp = ''; }else if(m[key]._type=="string"){ inp = ''; } html += this.row((m[key]._title ? m[key]._title : key),m[key]._text,inp); }else{ // If this section has a title if(m[key]._title) html += '