Changeset 364
- Timestamp:
- 01/26/2010 11:07:35 PM (2 years ago)
- Location:
- trunk/WordPress/plugin/transposh
- Files:
-
- 5 edited
-
js/transposh.js (modified) (9 diffs)
-
js/transposhedit.js (modified) (7 diffs)
-
transposh.php (modified) (2 diffs)
-
wp/transposh_admin.php (modified) (4 diffs)
-
wp/transposh_options.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/WordPress/plugin/transposh/js/transposh.js
r362 r364 15 15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 16 16 */ 17 // source - 0 is human, 1 is g t - 2and higher reserved for future engines17 // source - 0 is human, 1 is google translate - 2 is msn translate , 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, 21 var langLoaded, loadLang, getMSN, 22 22 // number of phrases that might be translated 23 23 possibly_translateable, … … 25 25 progressbar_id = t_jp.prefix + "pbar", 26 26 progressbar_posted_id = progressbar_id + "_s", 27 source = 1, 27 28 //Ajax translation 28 29 done_posted = 0, /*Timer for translation aggregation*/ timer, tokens = [], translations = [], 29 30 // the languages supported externally 30 31 // extracted using function above + he|zh-tw|pt that we know 31 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' ;32 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', 32 33 // got this using Microsoft.Translator.GetLanguages() with added zh and zh-tw for our needs 34 bing_langs = 'ar,bg,zh-chs,zh-cht,cs,da,nl,en,ht,fi,fr,de,el,he,it,ja,ko,pl,pt,ru,es,sv,th,zh,zh-tw'; 33 35 34 36 // This function fixes the page, it gets a token and translation and fixes this, … … 74 76 var data = { 75 77 ln0: t_jp.lang, // implicit 76 sr0: 1, // implicit auto translate...78 sr0: source, // implicit auto translate... 1 if google, 2 if msn 77 79 translation_posted: "2", 78 80 items: tokens.length // we can do this here because all tokens will be different … … 126 128 function do_auto_translate() { 127 129 // auto_translated_previously... 128 var auto_translated_phrases = [] ;130 var auto_translated_phrases = [], binglang = t_jp.lang; 129 131 jQuery("." + t_jp.prefix + '[source=""]').each(function (i) { 130 132 // not needed! … … 139 141 if (auto_translated_phrases[to_trans] !== 1) { 140 142 auto_translated_phrases[to_trans] = 1; 141 google.language.translate(to_trans, "", t_jp.lang, function (result) { 142 if (!result.error) { 143 // we no longer refer to segment IDs, just tokens 144 //var segment_id = translated_id.substr(translated_id.lastIndexOf('_') + 1); 145 // No longer need because now included in the ajax translate 146 //fix_page(jQuery("<div>" + result.translation + "</div>").text(), 1, segment_id); 147 to_trans = jQuery(this).attr('orig'); 148 ajax_translate(token, jQuery("<div>" + result.translation + "</div>").text()); 149 // update the regular progress bar 150 // done = possibly_translateable - jQuery("." + t_jp.prefix + '[source=""]').size(); 151 jQuery('#' + progressbar_id).progressbar('value', (possibly_translateable - jQuery("." + t_jp.prefix + '[source=""]').size()) / possibly_translateable * 100); 152 } 153 }); 143 if (typeof Microsoft !== 'undefined') { 144 145 if (binglang === 'zh') { 146 binglang = 'zh-chs'; 147 } else if (binglang === 'zh-tw') { 148 binglang = 'zh-cht'; 149 } 150 try { 151 Microsoft.Translator.translate(to_trans, "", binglang, function (translation) { 152 ajax_translate(token, jQuery("<div>" + translation + "</div>").text()); 153 jQuery('#' + progressbar_id).progressbar('value', (possibly_translateable - jQuery("." + t_jp.prefix + '[source=""]').size()) / possibly_translateable * 100); 154 }); 155 } 156 catch (err) { 157 // Maybe fallback? 158 // console.log("There was an error using Microsoft.Translator - probably a bad key or URL used in key. (" + err + ")"); 159 } 160 161 162 } else { 163 google.language.translate(to_trans, "", t_jp.lang, function (result) { 164 if (!result.error) { 165 // we no longer refer to segment IDs, just tokens 166 //var segment_id = translated_id.substr(translated_id.lastIndexOf('_') + 1); 167 // No longer need because now included in the ajax translate 168 //fix_page(jQuery("<div>" + result.translation + "</div>").text(), 1, segment_id); 169 // ???? 170 //to_trans = jQuery(this).attr('orig'); 171 ajax_translate(token, jQuery("<div>" + result.translation + "</div>").text()); 172 // update the regular progress bar 173 // done = possibly_translateable - jQuery("." + t_jp.prefix + '[source=""]').size(); 174 jQuery('#' + progressbar_id).progressbar('value', (possibly_translateable - jQuery("." + t_jp.prefix + '[source=""]').size()) / possibly_translateable * 100); 175 } 176 }); 177 } 154 178 } 155 179 }); … … 203 227 204 228 now = new Date(); 229 // we make sure script sub loaded are cached 230 jQuery.ajaxSetup({ 231 cache: true 232 }); 205 233 // 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... 206 if ((possibly_translateable > 5 || (now.getHours() === 4 && possibly_translateable > 0)) && google_langs.indexOf(t_jp.lang) > -1) { 207 // TODO - FIX ME! (islands) 208 jQuery.ajaxSetup({ 209 cache: true 210 }); 234 if ((possibly_translateable > 5 || (now.getHours() === 4 && possibly_translateable > 0)) && 235 (google_langs.indexOf(t_jp.lang) > -1 || bing_langs.indexOf(t_jp.lang) > -1)) { 211 236 // 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 212 237 langLoaded = function () { 213 238 if (t_jp.progress) { 214 jQuery.getScript(t_jp.plugin_url + '/js/lazy.js',function () {239 var loaduiandtranslate = function () { 215 240 jQuery.xLazyLoader({ 216 241 js: 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js', … … 221 246 } 222 247 }); 223 }); 248 }; 249 if (typeof jQuery.xLazyLoader === 'function') { 250 loaduiandtranslate(); 251 } else { 252 jQuery.getScript(t_jp.plugin_url + '/js/lazy.js', loaduiandtranslate); 253 } 224 254 } else { 225 255 do_auto_translate(); … … 227 257 }; 228 258 // we now start the chain that leads to auto-translate (with or without progress) 229 jQuery.getScript('http://www.google.com/jsapi', loadLang); 259 //if supported in msn and msn is prefered or not supported in google than msn and we have the msn key 260 if (((bing_langs.indexOf(t_jp.lang) > -1 && t_jp.preferred === 2) || (google_langs.indexOf(t_jp.lang) < 0)) && t_jp.msnkey !== '') { 261 source = 2; 262 getMSN = function () { 263 jQuery.getScript('http://api.microsofttranslator.com/V1/Ajax.svc/Embed?appId=' + t_jp.msnkey, langLoaded); 264 }; 265 // don't know why, but that's how it works 266 if (t_jp.edit && t_jp.progress) { 267 jQuery.getScript(t_jp.plugin_url + '/js/lazy.js', getMSN); 268 } else { 269 getMSN(); 270 } 271 } else { 272 jQuery.getScript('http://www.google.com/jsapi', loadLang); 273 } 230 274 } 231 275 //} … … 233 277 // this is the part when we have editor support 234 278 if (t_jp.edit) { 235 jQuery.getScript(t_jp.plugin_url + '/js/transposhedit.js'); // , loadLang); // TODO!!!279 jQuery.getScript(t_jp.plugin_url + '/js/transposhedit.js'); 236 280 } 237 281 }); -
trunk/WordPress/plugin/transposh/js/transposhedit.js
r362 r364 19 19 // fetch translation from google translate... 20 20 (function () { // closure 21 var 21 var loadLang, langLoaded, 22 22 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', 23 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'; 23 // got this using Microsoft.Translator.GetLanguages() with added zh and zh-tw for our needs 24 bing_langs = 'ar,bg,zh-chs,zh-cht,cs,da,nl,en,ht,fi,fr,de,el,he,it,ja,ko,pl,pt,ru,es,sv,th,zh,zh-tw'; 24 25 25 26 function fix_page_human(token, translation) { 26 new_text = translation;27 27 //reset to the original content - the unescaped version if translation is empty 28 28 // TODO! 29 /*if (jQuery.trim(translation).length === 0) {30 new_text = jQuery("#" + t_jp.prefix + segment_id).attr('orig');31 } */32 33 var fix_image = function () { // handle the image changes29 if (jQuery.trim(translation).length === 0) { 30 translation = jQuery("[token='" + token + "']").attr('orig'); 31 } 32 33 var fix_image = function () { // handle the image changes 34 34 var img_segment_id = jQuery(this).attr('id').substr(jQuery(this).attr('id').lastIndexOf('_') + 1), 35 35 img = jQuery("#" + t_jp.prefix + "img_" + img_segment_id); 36 36 jQuery("#" + t_jp.prefix + img_segment_id).attr('source', 0); // source is 0 human 37 37 img.removeClass('tr-icon-yellow').removeClass('tr-icon-green').addClass('tr-icon-green'); 38 // TODO if (jQuery.trim(translation).length !== 0) { remove green on zero length?38 // TODO if (jQuery.trim(translation).length !== 0) { remove green on zero length? 39 39 40 40 }; 41 41 // rewrite text for all matching items at once 42 42 jQuery("*[token='" + token + "'][hidden!='y']") 43 .html( new_text)43 .html(translation) 44 44 .each(fix_image); 45 45 46 46 // FIX hidden elements too (need to update father's title) 47 47 jQuery("*[token='" + token + "'][hidden='y']") 48 .attr('trans', new_text)48 .attr('trans', translation) 49 49 .each(fix_image); 50 50 } … … 72 72 data: data, 73 73 success: function () { 74 // Success now only updates the save progress bar (green)75 /* THINK if (t_jp.progress) {74 // Success now only updates the save progress bar (green) 75 /* THINK if (t_jp.progress) { 76 76 if (togo > 4 && source > 0) { 77 77 jQuery("#progress_bar2").progressbar('value', done_p / togo * 100); … … 82 82 83 83 error: function (req) { 84 alert("Error !!! failed to translate.\n\nServer's message: " + req.statusText);84 alert("Error !!! failed to translate.\n\nServer's message: " + req.statusText); 85 85 } 86 86 }); … … 90 90 { 91 91 if (typeof google === 'undefined') { 92 loadLang = function () { 93 google.load("language", "1", { 94 "callback" : langLoaded 95 }); 96 }; 92 97 langLoaded = function () { 93 98 getgt(); … … 227 232 jQuery("table thead tr", ui.panel).addClass("ui-widget-header"); 228 233 //jQuery("table tbody tr", ui.panel).append('<td/>'); 234 jQuery("table tbody td[source='2']", ui.panel).append('<span title="computer" style="display: inline-block; margin-right: 0.3em;" class="ui-icon ui-icon-gear"></span>'); 229 235 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>'); 230 236 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>'); … … 306 312 } 307 313 308 314 309 315 // lets add the images 310 316 jQuery("." + t_jp.prefix).each(function (i) { … … 336 342 'padding': '0px' 337 343 }); 338 if (jQuery(this).attr('source') === '1') {344 if (jQuery(this).attr('source')) { 339 345 img.addClass('tr-icon-yellow'); 340 346 } -
trunk/WordPress/plugin/transposh/transposh.php
r359 r364 487 487 wp_enqueue_script("jquery","http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js",array(),'1.3.2'); 488 488 // toying around - for later... 489 //wp_enqueue_script("jquery","http://code.jquery.com/jquery-1.4rc1.js",array(),'1.4rc');489 // wp_enqueue_script("jquery","http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js",array(),'1.4.1'); 490 490 } 491 491 } … … 509 509 echo "t_jp.prefix='".SPAN_PREFIX."';"; 510 510 echo "t_jp.msnkey='{$this->options->get_msn_key()}';"; 511 echo "t_jp.preferred='{$this->options->get_preferred_translator()}';"; 511 512 echo "t_jp.progress=".($this->edit_mode || $this->options->get_widget_progressbar() ? 'true' : 'false').";"; 512 513 -
trunk/WordPress/plugin/transposh/wp/transposh_admin.php
r346 r364 117 117 $this->transposh->options->set_enable_search_translate($_POST[ENABLE_SEARCH_TRANSLATE]); 118 118 $this->transposh->options->set_enable_msn_translate($_POST[ENABLE_MSN_TRANSLATE]); 119 $this->transposh->options->set_preferred_translator($_POST[PREFERRED_TRANSLATOR]); 119 120 $this->transposh->options->set_msn_key($_POST[MSN_TRANSLATE_KEY]); 120 121 $this->transposh->options->update_options(); … … 169 170 add_meta_box('transposh-contentbox-languages', 'Supported languages', array(&$this, 'on_contentbox_languages_content'), $this->pagehook, 'normal', 'core'); 170 171 add_meta_box('transposh-contentbox-translation', 'Translation settings', array(&$this, 'on_contentbox_translation_content'), $this->pagehook, 'normal', 'core'); 172 add_meta_box('transposh-contentbox-autotranslation', 'Automatic translation settings', array(&$this, 'on_contentbox_auto_translation_content'), $this->pagehook, 'normal', 'core'); 171 173 add_meta_box('transposh-contentbox-general', 'Generic settings', array(&$this, 'on_contentbox_generic_content'), $this->pagehook, 'normal', 'core'); 172 174 } … … 408 410 409 411 /* 410 * Insert the option to enable/disable automatic translation.411 * Enabled by default.412 */413 echo '<h4>Enable automatic translation</h4>';414 echo '<input type="checkbox" value="1" name="'.ENABLE_AUTO_TRANSLATE.'" '.$this->checked($this->transposh->options->get_enable_auto_translate()).'/> '.415 'Allow automatic translation of pages (currently using Google Translate)';416 417 /**418 * Insert the option to enable/disable automatic translation upon publishing.419 * Disabled by default.420 * @since 0.3.5 */421 echo '<h4>New - Enable automatic translation after posting</h4>';422 echo '<input type="checkbox" value="1" name="'.ENABLE_AUTO_POST_TRANSLATE.'" '.$this->checked($this->transposh->options->get_enable_auto_post_translate()).'/> '.423 'Do automatic translation immediately after a post has been published';424 425 /*426 * Insert the option to enable/disable msn translations.427 * Disabled by default because an API key is needed.428 */429 echo '<h4>Support for Bing (MSN) translation hinting (experimental)</h4>';430 echo '<input type="checkbox" value="1" name="'.ENABLE_MSN_TRANSLATE.'" '.$this->checked($this->transposh->options->get_enable_msn_translate()).'/> '.431 'Allow MSN (Bing) translator hinting (get key from <a href="http://www.microsofttranslator.com/Dev/Ajax/Default.aspx">here</a>)<br/>'.432 'Key: <input type="text" size="35" class="regular-text" value="'.$this->transposh->options->get_msn_key().'" id="'.MSN_TRANSLATE_KEY.'" name="'.MSN_TRANSLATE_KEY.'"/>';433 434 /*435 412 * Insert the option to enable/disable default language translation. 436 413 * Disabled by default. … … 451 428 } 452 429 430 function on_contentbox_auto_translation_content($data) { 431 432 /* 433 * Insert the option to enable/disable automatic translation. 434 * Enabled by default. 435 */ 436 echo '<h4>Enable automatic translation</h4>'; 437 echo '<input type="checkbox" value="1" name="'.ENABLE_AUTO_TRANSLATE.'" '.$this->checked($this->transposh->options->get_enable_auto_translate()).'/> '. 438 'Allow automatic translation of pages (currently using Google Translate)'; 439 440 /** 441 * Insert the option to enable/disable automatic translation upon publishing. 442 * Disabled by default. 443 * @since 0.3.5 */ 444 echo '<h4>New - Enable automatic translation after posting</h4>'; 445 echo '<input type="checkbox" value="1" name="'.ENABLE_AUTO_POST_TRANSLATE.'" '.$this->checked($this->transposh->options->get_enable_auto_post_translate()).'/> '. 446 'Do automatic translation immediately after a post has been published'; 447 448 /* 449 * Insert the option to enable/disable msn translations. 450 * Disabled by default because an API key is needed. 451 */ 452 echo '<h4>Support for Bing (MSN) translation</h4>'; 453 echo '<input type="checkbox" value="1" name="'.ENABLE_MSN_TRANSLATE.'" '.$this->checked($this->transposh->options->get_enable_msn_translate()).'/> '. 454 'Allow MSN (Bing) translator hinting (get key from <a href="http://www.microsofttranslator.com/Dev/Ajax/Default.aspx">here</a>)<br/>'. 455 'Key: <input type="text" size="35" class="regular-text" value="'.$this->transposh->options->get_msn_key().'" id="'.MSN_TRANSLATE_KEY.'" name="'.MSN_TRANSLATE_KEY.'"/>'; 456 457 /* 458 * TODO 459 */ 460 echo '<h4>Select preferred translatation engine</h4>'; 461 echo '<label for="'.PREFERRED_TRANSLATOR.'">Translation engine:'. 462 '<select name="'.PREFERRED_TRANSLATOR.'">'. 463 '<option value="1"' . ($this->transposh->options->get_preferred_translator() == 1 ? ' selected="selected"' : '').'>Google</option>'. 464 '<option value="2"' . ($this->transposh->options->get_preferred_translator() == 2 ? ' selected="selected"' : '').'>Bing</option>'. 465 '</select>'. 466 '</label>'; 467 468 } 469 453 470 function on_contentbox_generic_content($data) { 454 471 /* -
trunk/WordPress/plugin/transposh/wp/transposh_options.php
r346 r364 61 61 //Option to store the msn API key 62 62 define("MSN_TRANSLATE_KEY", "msn_key"); 63 //Option to store translator preference @since 0.4.2 64 define("PREFERRED_TRANSLATOR", "preferred_translator"); 63 65 //Option to enable/disable default language translation 64 66 define("ENABLE_DEFAULT_TRANSLATE", "enable_default_translate"); … … 225 227 } 226 228 229 function get_preferred_translator() { 230 if (!isset($this->options[PREFERRED_TRANSLATOR])) return 1; // default is google (2 is msn) 231 return $this->options[PREFERRED_TRANSLATOR]; 232 } 233 227 234 /** 228 235 * Gets the default language setting, i.e. the source language which normally should not be translated. … … 338 345 } 339 346 347 function set_preferred_translator($val) { 348 $this->set_value($val, $this->options[PREFERRED_TRANSLATOR]); 349 } 350 340 351 /** 341 352 * Sets the default language setting, i.e. the source language which
Note: See TracChangeset
for help on using the changeset viewer.
