Changeset 353
- Timestamp:
- 01/12/2010 10:20:17 PM (2 years ago)
- File:
-
- 1 edited
-
trunk/WordPress/plugin/transposh/js/transposh.js (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/WordPress/plugin/transposh/js/transposh.js
r352 r353 16 16 */ 17 17 // source - 0 is human, 1 is gt - 2 and higher reserved for future engines 18 18 /*global Date, Math, Microsoft, alert, clearTimeout, document, google, jQuery, setTimeout, t_jp, window */ 19 19 // fetch translation from google translate... 20 20 (function () { // closure 21 var langLoaded, loadLang, 22 //Ajax translation 23 done_p = 0, togo = 0, /*Timer for translation aggregation*/ timer, tokens = [], translations = [], 24 // the languages supported externally 25 // extracted using function above + he|zh-tw|pt that we know 26 google_langs = 'af|sq|ar|be|bg|ca|zh|zh-CN|zh-TW|hr|cs|da|nl|en|et|tl|fi|fr|gl|de|el|iw|hi|hu|is|id|ga|it|ja|ko|lv|lt|mk|ms|mt|no|fa|pl|pt-PT|ro|ru|sr|sk|sl|es|sw|sv|tl|th|tr|uk|vi|cy|yi|he|zh-tw|pt', 27 // got this using Microsoft.Translator.GetLanguages() with added zh and zh-tw for our needs 28 bing_langs = 'ar,bg,zh-chs,zh-cht,cs,da,nl,en,fi,fr,de,el,he,it,ja,ko,pl,pt,ru,es,sv,th,zh,zh-tw'; 29 21 30 function getgt() 22 31 { 23 if (typeof google == 'undefined') {24 langLoaded = function () {32 if (typeof google === 'undefined') { 33 langLoaded = function () { 25 34 getgt(); 26 35 }; 27 36 jQuery.xLazyLoader({ 28 // js: 'http://www.google.com/jsapi?callback=loadLang'37 // js: 'http://www.google.com/jsapi?callback=loadLang' 29 38 js: 'http://www.google.com/jsapi', 30 success: loadLang39 success: loadLang 31 40 }); 32 41 } else { 33 jQuery(":button:contains('Suggest - Google')").attr("disabled", "disabled").addClass("ui-state-disabled");34 google.language.translate(jQuery("#" +_tr_p.prefix+"original").val(), "", _tr_p.lang, function(result) {42 jQuery(":button:contains('Suggest - Google')").attr("disabled", "disabled").addClass("ui-state-disabled"); 43 google.language.translate(jQuery("#" + t_jp.prefix + "original").val(), "", t_jp.lang, function (result) { 35 44 if (!result.error) { 36 jQuery("#" +_tr_p.prefix+"translation").val(jQuery("<div>"+result.translation+"</div>").text())45 jQuery("#" + t_jp.prefix + "translation").val(jQuery("<div>" + result.translation + "</div>").text()) 37 46 .keyup(); 38 47 } … … 44 53 function getbt() 45 54 { 46 if (typeof Microsoft == 'undefined') {55 if (typeof Microsoft === 'undefined') { 47 56 jQuery.xLazyLoader({ 48 js: 'http://api.microsofttranslator.com/V1/Ajax.svc/Embed?appId=' +_tr_p.msnkey,49 success: function () {57 js: 'http://api.microsofttranslator.com/V1/Ajax.svc/Embed?appId=' + t_jp.msnkey, 58 success: function () { 50 59 getbt(); 51 60 } … … 53 62 54 63 } else { 55 jQuery(":button:contains('Suggest - Bing')").attr("disabled", "disabled").addClass("ui-state-disabled");56 var binglang = _tr_p.lang;57 if (binglang == 'zh') {58 binglang = 'zh-chs' 59 } 60 if (binglang == 'zh-tw') {61 binglang = 'zh-cht' 64 jQuery(":button:contains('Suggest - Bing')").attr("disabled", "disabled").addClass("ui-state-disabled"); 65 var binglang = t_jp.lang; 66 if (binglang === 'zh') { 67 binglang = 'zh-chs'; 68 } 69 if (binglang === 'zh-tw') { 70 binglang = 'zh-cht'; 62 71 } 63 72 try { 64 Microsoft.Translator.translate(jQuery("#" +_tr_p.prefix+"original").val(), "", binglang, function(translation) {65 jQuery("#" +_tr_p.prefix+"translation").val(jQuery("<div>"+translation+"</div>").text())73 Microsoft.Translator.translate(jQuery("#" + t_jp.prefix + "original").val(), "", binglang, function (translation) { 74 jQuery("#" + t_jp.prefix + "translation").val(jQuery("<div>" + translation + "</div>").text()) 66 75 .keyup(); 67 76 }); 68 77 } 69 78 catch (err) { 70 alert("There was an error using Microsoft.Translator - probably a bad key or URL used in key. ("+err+")"); 71 } 72 } 73 } 74 75 //Ajax translation 76 var done_p = 0; 77 var togo = 0; 78 //Timer for translation aggregation 79 var timer; 80 var tokens = new Array(); 81 var translations = new Array(); 82 83 function ajax_translate(translation,source,segment_id) { 84 // we aggregate translations together, 200ms from the last translation we will send the timer 85 // so here we remove it so nothing unexpected happens 86 clearTimeout(timer); 87 // push translations 88 tokens.push(jQuery("#"+_tr_p.prefix + segment_id).attr('token')); 89 translations.push(translation); 90 // This is a change - as we fix the pages before we got actual confirmation (worked well for auto-translation) 91 fix_page(translation,source,segment_id); 92 timer = setTimeout(function() { 93 var data = { 94 lang: _tr_p.lang, 95 source: source, 96 translation_posted: "1", 97 items: tokens.length 98 }; 99 for (var i = 0; i < tokens.length; i++) { 100 data["tk"+i] = tokens[i]; 101 data["tr"+i] = translations[i]; 102 // We are pre-accounting the progress bar here - which is not very nice 103 if (source > 0) { 104 done_p += jQuery("*[token='"+tokens[i]+"']").size(); 105 } 106 } 107 jQuery.ajax({ 108 type: "POST", 109 url: _tr_p.post_url, 110 data: data, 111 success: function() { 112 // Success now only updates the save progress bar (green) 113 if (_tr_p.progress) { 114 if (togo > 4 && source > 0) { 115 jQuery("#progress_bar2").progressbar('value' , done_p/togo*100); 116 } 117 118 } 119 }, 120 121 error: function(req) { 122 if (source == 0) { 123 alert("Error !!! failed to translate.\n\nServer's message: " + req.statusText); 124 } 125 } 126 }); 127 translations = []; 128 tokens = []; 129 }, 200); // wait 200 ms... 130 } 131 132 function fix_page(translation,source,segment_id) { 133 var token = jQuery("#"+_tr_p.prefix + segment_id).attr('token'); 134 var new_text = translation; 79 alert("There was an error using Microsoft.Translator - probably a bad key or URL used in key. (" + err + ")"); 80 } 81 } 82 } 83 84 function fix_page(translation, source, segment_id) { 85 var token = jQuery("#" + t_jp.prefix + segment_id).attr('token'), 86 new_text = translation; 135 87 //reset to the original content - the unescaped version if translation is empty 136 if (jQuery.trim(translation).length === 0) {137 new_text = jQuery("#" +_tr_p.prefix + segment_id).attr('orig');88 if (jQuery.trim(translation).length === 0) { 89 new_text = jQuery("#" + t_jp.prefix + segment_id).attr('orig'); 138 90 } 139 91 // rewrite text for all matching items at once 140 jQuery("*[token='" +token+"'][hidden!='y']")92 jQuery("*[token='" + token + "'][hidden!='y']") 141 93 .html(new_text) 142 94 .each(function (i) { // handle the image changes 143 var img_segment_id = jQuery(this).attr('id').substr(jQuery(this).attr('id').lastIndexOf('_') +1);144 jQuery("#"+_tr_p.prefix+img_segment_id).attr('source',source);145 var img = jQuery("#"+_tr_p.prefix+"img_" + img_segment_id);95 var img_segment_id = jQuery(this).attr('id').substr(jQuery(this).attr('id').lastIndexOf('_') + 1), 96 img = jQuery("#" + t_jp.prefix + "img_" + img_segment_id); 97 jQuery("#" + t_jp.prefix + img_segment_id).attr('source', source); 146 98 img.removeClass('tr-icon-yellow').removeClass('tr-icon-green'); 147 if (jQuery.trim(translation).length !== 0) {148 if (source == 1) {99 if (jQuery.trim(translation).length !== 0) { 100 if (source === 1) { 149 101 //switch to the auto img 150 102 img.addClass('tr-icon-yellow'); … … 157 109 158 110 // FIX hidden elements too (need to update father's title) 159 jQuery("*[token='" +token+"'][hidden='y']")160 .attr('trans', new_text)111 jQuery("*[token='" + token + "'][hidden='y']") 112 .attr('trans', new_text) 161 113 .each(function (i) { // handle the image changes 162 var img_segment_id = jQuery(this).attr('id').substr(jQuery(this).attr('id').lastIndexOf('_') +1);163 jQuery("#"+_tr_p.prefix+img_segment_id).attr('source',source);164 var img = jQuery("#"+_tr_p.prefix+"img_" + img_segment_id);114 var img_segment_id = jQuery(this).attr('id').substr(jQuery(this).attr('id').lastIndexOf('_') + 1), 115 img = jQuery("#" + t_jp.prefix + "img_" + img_segment_id); 116 jQuery("#" + t_jp.prefix + img_segment_id).attr('source', source); 165 117 img.removeClass('tr-icon-yellow').removeClass('tr-icon-green'); 166 if (jQuery.trim(translation).length !== 0) {167 if (source == 1) {118 if (jQuery.trim(translation).length !== 0) { 119 if (source === 1) { 168 120 //switch to the auto img 169 121 img.addClass('tr-icon-yellow'); … … 177 129 } 178 130 131 function ajax_translate(translation, source, segment_id) { 132 // we aggregate translations together, 200ms from the last translation we will send the timer 133 // so here we remove it so nothing unexpected happens 134 clearTimeout(timer); 135 // push translations 136 tokens.push(jQuery("#" + t_jp.prefix + segment_id).attr('token')); 137 translations.push(translation); 138 // This is a change - as we fix the pages before we got actual confirmation (worked well for auto-translation) 139 fix_page(translation, source, segment_id); 140 timer = setTimeout(function () { 141 var data = { 142 lang: t_jp.lang, 143 source: source, 144 translation_posted: "1", 145 items: tokens.length 146 }, i; 147 for (i = 0; i < tokens.length; i += 1) { 148 data["tk" + i] = tokens[i]; 149 data["tr" + i] = translations[i]; 150 // We are pre-accounting the progress bar here - which is not very nice 151 if (source > 0) { 152 done_p += jQuery("*[token='" + tokens[i] + "']").size(); 153 } 154 } 155 jQuery.ajax({ 156 type: "POST", 157 url: t_jp.post_url, 158 data: data, 159 success: function () { 160 // Success now only updates the save progress bar (green) 161 if (t_jp.progress) { 162 if (togo > 4 && source > 0) { 163 jQuery("#progress_bar2").progressbar('value', done_p / togo * 100); 164 } 165 166 } 167 }, 168 169 error: function (req) { 170 if (source === 0) { 171 alert("Error !!! failed to translate.\n\nServer's message: " + req.statusText); 172 } 173 } 174 }); 175 translations = []; 176 tokens = []; 177 }, 200); // wait 200 ms... 178 } 179 180 179 181 //function for auto translation 180 182 function do_auto_translate() { 181 if (_tr_p.progress) { 182 togo = jQuery("."+_tr_p.prefix+'[source=""]').size(); 183 // auto_translated_previously... 184 var auto_t_p = [], done; 185 if (t_jp.progress) { 186 togo = jQuery("." + t_jp.prefix + '[source=""]').size(); 183 187 //alert(togo); 184 188 // progress bar is for alteast 5 items 185 189 if (togo > 4) { 186 jQuery("#" +_tr_p.prefix+"credit").append('<div style="float: left;width: 90%;height: 10px" id="progress_bar"/><div style="margin-bottom:10px;float:left;width: 90%;height: 10px" id="progress_bar2"/>')190 jQuery("#" + t_jp.prefix + "credit").append('<div style="float: left;width: 90%;height: 10px" id="progress_bar"/><div style="margin-bottom:10px;float:left;width: 90%;height: 10px" id="progress_bar2"/>'); 187 191 jQuery("#progress_bar").progressbar({ 188 192 value: 0 … … 193 197 // color the "save" bar 194 198 jQuery("#progress_bar2 > div").css({ 195 'background': '#28F828',199 'background': '#28F828', 196 200 'border' : "#08A908 1px solid" 197 201 }); 198 202 } 199 var done = 0; 200 } 201 // auto_translated_previously... 202 var auto_t_p = new Array(); 203 jQuery("."+_tr_p.prefix+'[source=""]').each(function (i) { 204 var translated_id = jQuery(this).attr('id'); 203 done = 0; 204 } 205 jQuery("." + t_jp.prefix + '[source=""]').each(function (i) { 206 var translated_id = jQuery(this).attr('id'), 205 207 //alert(translated_id); 206 var to_trans = jQuery(this).attr('orig'); 207 if (to_trans == undefined) to_trans = jQuery(this).html(); 208 if (!(auto_t_p[to_trans] == 1)) { 208 to_trans = jQuery(this).attr('orig'); 209 if (to_trans === undefined) { 210 to_trans = jQuery(this).html(); 211 } 212 if (auto_t_p[to_trans] !== 1) { 209 213 auto_t_p[to_trans] = 1; 210 google.language.translate(to_trans, "", _tr_p.lang, function(result) {214 google.language.translate(to_trans, "", t_jp.lang, function (result) { 211 215 if (!result.error) { 212 var segment_id = translated_id.substr(translated_id.lastIndexOf('_') +1);216 var segment_id = translated_id.substr(translated_id.lastIndexOf('_') + 1); 213 217 // No longer need because now included in the ajax translate 214 //fix_page(jQuery("<div>" +result.translation+"</div>").text(),1,segment_id);215 ajax_translate(jQuery("<div>" +result.translation+"</div>").text(),1,segment_id);216 if ( _tr_p.progress) {217 done = togo - jQuery("." +_tr_p.prefix+'[source=""]').size();218 //fix_page(jQuery("<div>" + result.translation + "</div>").text(), 1, segment_id); 219 ajax_translate(jQuery("<div>" + result.translation + "</div>").text(), 1, segment_id); 220 if (t_jp.progress) { 221 done = togo - jQuery("." + t_jp.prefix + '[source=""]').size(); 218 222 if (togo > 4) { 219 jQuery("#progress_bar").progressbar('value' , done/togo*100);223 jQuery("#progress_bar").progressbar('value', done / togo * 100); 220 224 } 221 225 } … … 230 234 bgiframe: true, 231 235 resizable: false, 232 height: 140,236 height: 140, 233 237 modal: true, 234 238 overlay: { … … 237 241 }, 238 242 buttons: { 239 'Discard': function () {240 jQuery("#" +_tr_p.prefix+"translation").data("edit", {243 'Discard': function () { 244 jQuery("#" + t_jp.prefix + "translation").data("edit", { 241 245 changed: false 242 246 }); 243 247 jQuery(this).dialog('close'); 244 jQuery("#" +_tr_p.prefix+"d-tabs").dialog('close');248 jQuery("#" + t_jp.prefix + "d-tabs").dialog('close'); 245 249 }, 246 Cancel: function () {250 Cancel: function () { 247 251 jQuery(this).dialog('close'); 248 252 } … … 253 257 //Open translation dialog 254 258 function translate_dialog(segment_id) { 255 jQuery("#"+_tr_p.prefix+"d-tabs").remove(); 256 jQuery('<div id="'+_tr_p.prefix+'d-tabs" title="Edit Translation"/>').appendTo("body"); 257 jQuery("#"+_tr_p.prefix+"d-tabs").append('<ul/>').tabs({ 259 var tButtons = {}, hButtons = {}; 260 //only add button is bing support is defined for the language (and we got some key) 261 if (bing_langs.indexOf(t_jp.lang) > -1 && t_jp.msnkey !== '') { 262 //ar,zh-chs,zh-cht,nl,en,fr,de,he,it,ja,ko,pl,pt,ru,es 263 tButtons['Suggest - Bing'] = function () { 264 getbt(); 265 }; 266 } 267 268 // Only add button if google supports said language 269 if (google_langs.indexOf(t_jp.lang) > -1) { 270 tButtons['Suggest - Google'] = function () { 271 getgt(); 272 }; 273 } 274 /* 'Next': function () { 275 alert(parseInt(segment_id) + 1); 276 translate_dialog(parseInt(segment_id) + 1); 277 }, 278 'Combine - Next': function () { 279 something?(); 280 //.next? .next all? 281 },*/ 282 tButtons.Ok = function () { 283 var translation = jQuery('#' + t_jp.prefix + 'translation').val(); 284 if (jQuery('#' + t_jp.prefix + 'translation').data("edit").changed) { 285 ajax_translate(translation, 0, segment_id); 286 jQuery("#" + t_jp.prefix + "translation").data("edit", { 287 changed: false 288 }); 289 } 290 jQuery(this).dialog('close'); 291 }; 292 //tButtons["beep"] = function () {alert(Microsoft.Translator.GetLanguages())}; 293 hButtons = { 294 Close: function () { 295 jQuery(this).dialog('close'); 296 } 297 }; 298 299 jQuery("#" + t_jp.prefix + "d-tabs").remove(); 300 jQuery('<div id="' + t_jp.prefix + 'd-tabs" title="Edit Translation"/>').appendTo("body"); 301 jQuery("#" + t_jp.prefix + "d-tabs").append('<ul/>').tabs({ 258 302 cache: true 259 303 }) 260 .tabs('add', "#"+_tr_p.prefix+"d-tabs-1",'Translate')261 .tabs('add', _tr_p.post_url+'?tr_token_hist='+jQuery("#"+_tr_p.prefix + segment_id).attr('token')+'&lang='+_tr_p.lang,'History')262 .css("text-align", "left")263 .css("padding", 0)264 .bind('tabsload', function (event, ui) {304 .tabs('add', "#" + t_jp.prefix + "d-tabs-1", 'Translate') 305 .tabs('add', t_jp.post_url + '?tr_token_hist=' + jQuery("#" + t_jp.prefix + segment_id).attr('token') + '&lang=' + t_jp.lang, 'History') 306 .css("text-align", "left") 307 .css("padding", 0) 308 .bind('tabsload', function (event, ui) { 265 309 //TODO, formatting here, not server side 266 jQuery("table", ui.panel).addClass("ui-widget ui-widget-content").css({310 jQuery("table", ui.panel).addClass("ui-widget ui-widget-content").css({ 267 311 'width' : '95%', 268 312 'padding' : '0' 269 313 }); 270 314 //jQuery("table thead th:last",ui.panel).after("<th/>"); 271 jQuery("table thead tr", ui.panel).addClass("ui-widget-header");272 //jQuery("table tbody tr", ui.panel).append('<td/>');273 jQuery("table tbody td[source='1']", ui.panel).append('<span title="computer" style="display: inline-block; margin-right: 0.3em;" class="ui-icon ui-icon-gear"></span>');274 jQuery("table tbody td[source='0']", ui.panel).append('<span title="human" style="display: inline-block; margin-right: 0.3em;" class="ui-icon ui-icon-person"></span>');275 //jQuery("table tbody tr:first td:last", ui.panel).append('<span title="remove this translation" id="'+_tr_p.prefix+'revert" style="float: left; margin-right: 0.3em;" class="ui-icon ui-icon-scissors"/>');276 //jQuery("#" +_tr_p.prefix+"revert").click(function () {315 jQuery("table thead tr", ui.panel).addClass("ui-widget-header"); 316 //jQuery("table tbody tr", ui.panel).append('<td/>'); 317 jQuery("table tbody td[source='1']", ui.panel).append('<span title="computer" style="display: inline-block; margin-right: 0.3em;" class="ui-icon ui-icon-gear"></span>'); 318 jQuery("table tbody td[source='0']", ui.panel).append('<span title="human" style="display: inline-block; margin-right: 0.3em;" class="ui-icon ui-icon-person"></span>'); 319 //jQuery("table tbody tr:first td:last", ui.panel).append('<span title="remove this translation" id="' + t_jp.prefix + 'revert" style="float: left; margin-right: 0.3em;" class="ui-icon ui-icon-scissors"/>'); 320 //jQuery("#" + t_jp.prefix + "revert").click(function () { 277 321 //alert ('hi'); 278 322 //}); 279 323 }) 280 .bind('tabsselect', function (event, ui) {324 .bind('tabsselect', function (event, ui) { 281 325 // Change buttons 282 if (jQuery(ui.tab).text() == 'Translate') {283 jQuery("#" +_tr_p.prefix+"d-tabs").dialog('option', 'buttons', tButtons);326 if (jQuery(ui.tab).text() === 'Translate') { 327 jQuery("#" + t_jp.prefix + "d-tabs").dialog('option', 'buttons', tButtons); 284 328 } else { 285 jQuery("#" +_tr_p.prefix+"d-tabs").dialog('option', 'buttons', hButtons);329 jQuery("#" + t_jp.prefix + "d-tabs").dialog('option', 'buttons', hButtons); 286 330 } 287 331 }) 288 .bind('dialogbeforeclose', function (event, ui) {289 if (jQuery("#"+_tr_p.prefix+"translation").data("edit").changed) {332 .bind('dialogbeforeclose', function (event, ui) { 333 if (jQuery("#" + t_jp.prefix + "translation").data("edit").changed) { 290 334 confirm_close(); 291 335 return false; … … 294 338 }); 295 339 // fix for templates messing with li 296 jQuery("#" +_tr_p.prefix+"d-tabs li").css("list-style-type","none").css("list-style-position","outside");297 jQuery("#" +_tr_p.prefix+"d-tabs-1").css("padding", "1px").append(340 jQuery("#" + t_jp.prefix + "d-tabs li").css("list-style-type", "none").css("list-style-position", "outside"); 341 jQuery("#" + t_jp.prefix + "d-tabs-1").css("padding", "1px").append( 298 342 /*'<table><tr><td>'+*/ 299 '<form id="' +_tr_p.prefix+'form">' +343 '<form id="' + t_jp.prefix + 'form">' + 300 344 '<fieldset>' + 301 345 '<label for="original">Original Text</label>' + 302 '<textarea cols="80" row="3" name="original" id="' +_tr_p.prefix+'original" class="text ui-widget-content ui-corner-all" readonly="y"/>' +346 '<textarea cols="80" row="3" name="original" id="' + t_jp.prefix + 'original" class="text ui-widget-content ui-corner-all" readonly="y"/>' + 303 347 '<label for="translation">Translate To</label>' + 304 '<textarea cols="80" row="3" name="translation" id="' +_tr_p.prefix+'translation" value="" class="text ui-widget-content ui-corner-all"/>' +348 '<textarea cols="80" row="3" name="translation" id="' + t_jp.prefix + 'translation" value="" class="text ui-widget-content ui-corner-all"/>' + 305 349 '</fieldset>' + 306 '</form>'/* +307 '</td><td style="width:32px">' +308 '<img src="/wp-content/plugins/transposh/img/knob/knobs/left.png"/>' +309 '<img src="/wp-content/plugins/transposh/img/knob/knobs/right.png"/>' +350 '</form>'/* + 351 '</td><td style="width:32px">' + 352 '<img src="/wp-content/plugins/transposh/img/knob/knobs/left.png"/>' + 353 '<img src="/wp-content/plugins/transposh/img/knob/knobs/right.png"/>' + 310 354 '<img id="smart" src="/wp-content/plugins/transposh/img/knob/knobs/smart.png"/>'+ 311 355 '<img src="/wp-content/plugins/transposh/img/knob/knobs/merge.png"/>'+ 312 356 '</td></tr></table>'*/); 313 /*jQuery("#smart").click(function () {357 /*jQuery("#smart").click(function () { 314 358 grabnext(segment_id); 315 359 });*/ 316 jQuery("#" +_tr_p.prefix+"d-tabs-1 label").css("display","block");317 jQuery("#" +_tr_p.prefix+"d-tabs-1 textarea.text").css({318 'margin-bottom': '12px',360 jQuery("#" + t_jp.prefix + "d-tabs-1 label").css("display", "block"); 361 jQuery("#" + t_jp.prefix + "d-tabs-1 textarea.text").css({ 362 'margin-bottom': '12px', 319 363 'width' : '95%', 320 364 'padding' : '.4em' 321 365 }); 322 jQuery("#" +_tr_p.prefix+"original").val(jQuery("#"+_tr_p.prefix + segment_id).attr('orig'));323 jQuery("#" +_tr_p.prefix+"translation").val(jQuery("#"+_tr_p.prefix + segment_id).html());324 if (jQuery("#" +_tr_p.prefix + segment_id).attr('trans')) {325 jQuery("#" +_tr_p.prefix+"translation").val(jQuery("#"+_tr_p.prefix + segment_id).attr('trans'));326 } 327 jQuery("#" +_tr_p.prefix+"translation").data("edit", {366 jQuery("#" + t_jp.prefix + "original").val(jQuery("#" + t_jp.prefix + segment_id).attr('orig')); 367 jQuery("#" + t_jp.prefix + "translation").val(jQuery("#" + t_jp.prefix + segment_id).html()); 368 if (jQuery("#" + t_jp.prefix + segment_id).attr('trans')) { 369 jQuery("#" + t_jp.prefix + "translation").val(jQuery("#" + t_jp.prefix + segment_id).attr('trans')); 370 } 371 jQuery("#" + t_jp.prefix + "translation").data("edit", { 328 372 changed: false 329 373 }); 330 jQuery("#" +_tr_p.prefix+"translation").keyup(function(e){331 if (jQuery("#" +_tr_p.prefix + segment_id).text() != jQuery(this).val()) {332 jQuery(this).css("background", "yellow");374 jQuery("#" + t_jp.prefix + "translation").keyup(function (e) { 375 if (jQuery("#" + t_jp.prefix + segment_id).text() !== jQuery(this).val()) { 376 jQuery(this).css("background", "yellow"); 333 377 jQuery(this).data("edit", { 334 378 changed: true 335 379 }); 336 380 } else { 337 jQuery(this).css("background", "");381 jQuery(this).css("background", ""); 338 382 jQuery(this).data("edit", { 339 383 changed: false … … 341 385 } 342 386 }); 343 var tButtons = {}; 344 //only add button is bing support is defined for the language (and we got some key) 345 if (bing_langs.indexOf(_tr_p.lang) > -1 && _tr_p.msnkey != '') { 346 //ar,zh-chs,zh-cht,nl,en,fr,de,he,it,ja,ko,pl,pt,ru,es 347 tButtons['Suggest - Bing'] = function() { 348 getbt(); 349 }; 350 } 351 352 // Only add button if google supports said language 353 if ( google_langs.indexOf(_tr_p.lang) > -1) { 354 tButtons['Suggest - Google'] = function() { 355 getgt(); 356 }; 357 } 358 /* 'Next': function() { 359 alert(parseInt(segment_id)+1); 360 translate_dialog(parseInt(segment_id)+1); 361 }, 362 'Combine - Next': function() { 363 something?(); 364 //.next? .next all? 365 },*/ 366 tButtons['Ok'] = function() { 367 var translation = jQuery('#'+_tr_p.prefix+'translation').val(); 368 if(jQuery('#'+_tr_p.prefix+'translation').data("edit").changed) { 369 ajax_translate(translation,0,segment_id); 370 jQuery("#"+_tr_p.prefix+"translation").data("edit", { 371 changed: false 372 }); 373 } 374 jQuery(this).dialog('close'); 375 }; 376 //tButtons["beep"] = function() {alert(Microsoft.Translator.GetLanguages())}; 377 var hButtons = { 378 Close: function() { 379 jQuery(this).dialog('close'); 380 } 381 }; 382 jQuery("#"+_tr_p.prefix+"d-tabs").dialog({ 387 jQuery("#" + t_jp.prefix + "d-tabs").dialog({ 383 388 bgiframe: true, 384 389 modal: true, … … 391 396 // We first try to avoid conflict with other frameworks 392 397 jQuery.noConflict(); 393 // the languages supported externally 394 // extracted using function above + he|zh-tw|pt that we know 395 google_langs = 'af|sq|ar|be|bg|ca|zh|zh-CN|zh-TW|hr|cs|da|nl|en|et|tl|fi|fr|gl|de|el|iw|hi|hu|is|id|ga|it|ja|ko|lv|lt|mk|ms|mt|no|fa|pl|pt-PT|ro|ru|sr|sk|sl|es|sw|sv|tl|th|tr|uk|vi|cy|yi|he|zh-tw|pt'; 396 // got this using Microsoft.Translator.GetLanguages() with added zh and zh-tw for our needs 397 bing_langs = 'ar,bg,zh-chs,zh-cht,cs,da,nl,en,fi,fr,de,el,he,it,ja,ko,pl,pt,ru,es,sv,th,zh,zh-tw'; 398 399 function loadLang() { 398 399 loadLang = function () { 400 400 google.load("language", "1", { 401 401 "callback" : langLoaded 402 402 }); 403 } 403 }; 404 404 405 405 jQuery(document).ready( 406 function () {406 function () { 407 407 // this is the set_default_language function 408 408 // attach a function to the set_default_language link if its there 409 jQuery('#' +_tr_p.prefix+'setdeflang').click(function () {410 jQuery.get( _tr_p.post_url+"?tr_cookie="+Math.random());409 jQuery('#' + t_jp.prefix + 'setdeflang').click(function () { 410 jQuery.get(t_jp.post_url + "?tr_cookie=" + Math.random()); 411 411 jQuery(this).hide("slow"); 412 412 return false; 413 }) 413 }); 414 414 415 415 // now lets check if auto translate is needed 416 translationstats = window["eval"]("(" + jQuery("meta[name=translation-stats]").attr("content") + ")");417 if (translationstats != undefined) {418 possibly_translateable = (translationstats.total_phrases - translationstats.translated_phrases - (translationstats.meta_phrases - translationstats.meta_translated_phrases));419 now = new Date 416 var translationstats = window["eval"]("(" + jQuery("meta[name=translation-stats]").attr("content") + ")"), possibly_translateable, now; 417 if (translationstats !== undefined) { 418 possibly_translateable = (translationstats.total_phrases - translationstats.translated_phrases - (translationstats.meta_phrases - translationstats.meta_translated_phrases)); 419 now = new Date(); 420 420 // we'll only auto-translate and load the stuff if we either have more than 5 candidate translations, or more than one at 4am, and this language is supported... 421 if ((possibly_translateable > 5 || (now.getHours() == 4 && possibly_translateable > 0)) && google_langs.indexOf(_tr_p.lang) > -1) {421 if ((possibly_translateable > 5 || (now.getHours() === 4 && possibly_translateable > 0)) && google_langs.indexOf(t_jp.lang) > -1) { 422 422 // TODO - FIX ME! (islands) 423 423 jQuery.ajaxSetup({ … … 425 425 }); 426 426 // if we have a progress bar, we need to load the jqueryui before the auto translate, after the google was loaded, otherwise we can just go ahead 427 langLoaded = function () {428 if ( _tr_p.progress) {429 jQuery.getScript( _tr_p.plugin_url +'/js/lazy.js', function() {427 langLoaded = function () { 428 if (t_jp.progress) { 429 jQuery.getScript(t_jp.plugin_url + '/js/lazy.js', function () { 430 430 jQuery.xLazyLoader({ 431 431 js: 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js', 432 432 css: 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/ui-lightness/jquery-ui.css', 433 433 success: function () { 434 do_auto_translate() 434 do_auto_translate(); 435 435 } 436 436 }); 437 437 }); 438 438 } else { 439 do_auto_translate() 439 do_auto_translate(); 440 440 } 441 441 }; 442 442 // we now start the chain that leads to auto-translate (with or without progress) 443 jQuery.getScript('http://www.google.com/jsapi', loadLang);443 jQuery.getScript('http://www.google.com/jsapi', loadLang); 444 444 } 445 445 } 446 446 447 447 // this is the part when we have editor support 448 if ( _tr_p.edit) {448 if (t_jp.edit) { 449 449 // lets add the images 450 jQuery("." +_tr_p.prefix).each(function (i) {451 var translated_id = jQuery(this).attr('id').substr(jQuery(this).attr('id').lastIndexOf('_') +1);452 jQuery(this).after('<span id="' +_tr_p.prefix+'img_'+translated_id+'" class="tr-icon" title="'+jQuery(this).attr('orig')+'"></span>');453 var img = jQuery('#'+_tr_p.prefix+'img_'+translated_id);450 jQuery("." + t_jp.prefix).each(function (i) { 451 var translated_id = jQuery(this).attr('id').substr(jQuery(this).attr('id').lastIndexOf('_') + 1), img; 452 jQuery(this).after('<span id="' + t_jp.prefix + 'img_' + translated_id + '" class="tr-icon" title="' + jQuery(this).attr('orig') + '"></span>'); 453 img = jQuery('#' + t_jp.prefix + 'img_' + translated_id); 454 454 img.click(function () { 455 455 // if we detect that jQuery.ui is missing (TODO - check tabs - etal) we load it first … … 458 458 cache: true 459 459 }); 460 jQuery.getScript( _tr_p.plugin_url +'/js/lazy.js', function() {460 jQuery.getScript(t_jp.plugin_url + '/js/lazy.js', function () { 461 461 jQuery.xLazyLoader({ 462 462 js: 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js', 463 463 css: 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/ui-lightness/jquery-ui.css', 464 success: function () {464 success: function () { 465 465 translate_dialog(translated_id); 466 466 } … … 472 472 return false; 473 473 }).css({ 474 'border': '0px',475 'margin': '1px',476 'padding': '0px'474 'border': '0px', 475 'margin': '1px', 476 'padding': '0px' 477 477 }); 478 if (jQuery(this).attr('source') == '1')478 if (jQuery(this).attr('source') === '1') { 479 479 img.addClass('tr-icon-yellow'); 480 else if (jQuery(this).attr('source') == '0') 480 } 481 else if (jQuery(this).attr('source') === '0') { 481 482 img.addClass('tr-icon-green'); 482 // if the image is sourced from a hidden element - kinly "show" this 483 if (jQuery(this).attr('hidden') == 'y') { 483 } 484 // if the image is sourced from a hidden element - kindly "show" this 485 if (jQuery(this).attr('hidden') === 'y') { 484 486 img.css({ 485 'opacity': '0.6'487 'opacity': '0.6' 486 488 }); 487 489 } … … 489 491 } 490 492 }); 491 } )(); // end of closure493 }()); // end of closure
Note: See TracChangeset
for help on using the changeset viewer.
