Changeset 364


Ignore:
Timestamp:
01/26/2010 11:07:35 PM (2 years ago)
Author:
ofer
Message:

Support auto-translate by MSN translate (with key) this adds Haitian support.

Location:
trunk/WordPress/plugin/transposh
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/WordPress/plugin/transposh/js/transposh.js

    r362 r364  
    1515 *      Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
    1616 */ 
    17 // source - 0 is human, 1 is gt - 2 and higher reserved for future engines 
     17// source - 0 is human, 1 is google translate - 2 is msn translate , and higher reserved for future engines 
    1818/*global Date, Math, Microsoft, alert, clearTimeout, document, google, jQuery, setTimeout, t_jp, window */ 
    1919// fetch translation from google translate... 
    2020(function () { // closure 
    21     var langLoaded, loadLang, 
     21    var langLoaded, loadLang, getMSN, 
    2222    // number of phrases that might be translated 
    2323    possibly_translateable, 
     
    2525    progressbar_id = t_jp.prefix + "pbar", 
    2626    progressbar_posted_id = progressbar_id + "_s", 
     27    source = 1, 
    2728    //Ajax translation 
    2829    done_posted = 0, /*Timer for translation aggregation*/ timer, tokens = [], translations = [], 
    2930    // the languages supported externally 
    3031    // 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', 
    3233    // 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'; 
    3335 
    3436    // This function fixes the page, it gets a token and translation and fixes this, 
     
    7476            var data = { 
    7577                ln0: t_jp.lang, // implicit 
    76                 sr0: 1, // implicit auto translate... 
     78                sr0: source, // implicit auto translate... 1 if google, 2 if msn 
    7779                translation_posted: "2", 
    7880                items: tokens.length // we can do this here because all tokens will be different 
     
    126128    function do_auto_translate() { 
    127129        // auto_translated_previously... 
    128         var auto_translated_phrases = []; 
     130        var auto_translated_phrases = [], binglang = t_jp.lang; 
    129131        jQuery("." + t_jp.prefix + '[source=""]').each(function (i) { 
    130132            // not needed! 
     
    139141            if (auto_translated_phrases[to_trans] !== 1) { 
    140142                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                } 
    154178            } 
    155179        }); 
     
    203227 
    204228            now = new Date(); 
     229            // we make sure script sub loaded are cached 
     230            jQuery.ajaxSetup({ 
     231                cache: true 
     232            }); 
    205233            // 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)) { 
    211236                // 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 
    212237                langLoaded = function () { 
    213238                    if (t_jp.progress) { 
    214                         jQuery.getScript(t_jp.plugin_url + '/js/lazy.js', function () { 
     239                        var loaduiandtranslate = function () { 
    215240                            jQuery.xLazyLoader({ 
    216241                                js: 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js', 
     
    221246                                } 
    222247                            }); 
    223                         }); 
     248                        }; 
     249                        if (typeof jQuery.xLazyLoader === 'function') { 
     250                            loaduiandtranslate(); 
     251                        } else { 
     252                            jQuery.getScript(t_jp.plugin_url + '/js/lazy.js', loaduiandtranslate); 
     253                        } 
    224254                    } else { 
    225255                        do_auto_translate(); 
     
    227257                }; 
    228258                // 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                } 
    230274            } 
    231275            //} 
     
    233277            // this is the part when we have editor support 
    234278            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'); 
    236280            } 
    237281        }); 
  • trunk/WordPress/plugin/transposh/js/transposhedit.js

    r362 r364  
    1919// fetch translation from google translate... 
    2020(function () { // closure 
    21     var 
     21    var loadLang, langLoaded, 
    2222    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'; 
    2425 
    2526    function fix_page_human(token, translation) { 
    26         new_text = translation; 
    2727        //reset to the original content - the unescaped version if translation is empty 
    2828        // 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 changes 
     29        if (jQuery.trim(translation).length === 0) { 
     30            translation = jQuery("[token='" + token + "']").attr('orig'); 
     31        } 
     32 
     33        var fix_image = function () { // handle the image changes 
    3434            var img_segment_id = jQuery(this).attr('id').substr(jQuery(this).attr('id').lastIndexOf('_') + 1), 
    3535            img = jQuery("#" + t_jp.prefix + "img_" + img_segment_id); 
    3636            jQuery("#" + t_jp.prefix + img_segment_id).attr('source', 0); // source is 0 human 
    3737            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? 
    3939 
    4040        }; 
    4141        // rewrite text for all matching items at once 
    4242        jQuery("*[token='" + token + "'][hidden!='y']") 
    43         .html(new_text) 
     43        .html(translation) 
    4444        .each(fix_image); 
    4545 
    4646        // FIX hidden elements too (need to update father's title) 
    4747        jQuery("*[token='" + token + "'][hidden='y']") 
    48         .attr('trans', new_text) 
     48        .attr('trans', translation) 
    4949        .each(fix_image); 
    5050    } 
     
    7272            data: data, 
    7373            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) { 
    7676                    if (togo > 4 && source > 0) { 
    7777                        jQuery("#progress_bar2").progressbar('value', done_p / togo * 100); 
     
    8282 
    8383            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); 
    8585            } 
    8686        }); 
     
    9090    { 
    9191        if (typeof google === 'undefined') { 
     92            loadLang = function () { 
     93                google.load("language", "1", { 
     94                    "callback" : langLoaded 
     95                }); 
     96            }; 
    9297            langLoaded = function () { 
    9398                getgt(); 
     
    227232            jQuery("table thead tr", ui.panel).addClass("ui-widget-header"); 
    228233            //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>'); 
    229235            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>'); 
    230236            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>'); 
     
    306312    } 
    307313 
    308  
     314    
    309315    // lets add the images 
    310316    jQuery("." + t_jp.prefix).each(function (i) { 
     
    336342            'padding': '0px' 
    337343        }); 
    338         if (jQuery(this).attr('source') === '1') { 
     344        if (jQuery(this).attr('source')) { 
    339345            img.addClass('tr-icon-yellow'); 
    340346        } 
  • trunk/WordPress/plugin/transposh/transposh.php

    r359 r364  
    487487            wp_enqueue_script("jquery","http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js",array(),'1.3.2'); 
    488488            // 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'); 
    490490        } 
    491491    } 
     
    509509        echo "t_jp.prefix='".SPAN_PREFIX."';"; 
    510510        echo "t_jp.msnkey='{$this->options->get_msn_key()}';"; 
     511        echo "t_jp.preferred='{$this->options->get_preferred_translator()}';"; 
    511512        echo "t_jp.progress=".($this->edit_mode || $this->options->get_widget_progressbar() ? 'true' : 'false').";"; 
    512513 
  • trunk/WordPress/plugin/transposh/wp/transposh_admin.php

    r346 r364  
    117117        $this->transposh->options->set_enable_search_translate($_POST[ENABLE_SEARCH_TRANSLATE]); 
    118118        $this->transposh->options->set_enable_msn_translate($_POST[ENABLE_MSN_TRANSLATE]); 
     119        $this->transposh->options->set_preferred_translator($_POST[PREFERRED_TRANSLATOR]); 
    119120        $this->transposh->options->set_msn_key($_POST[MSN_TRANSLATE_KEY]); 
    120121        $this->transposh->options->update_options(); 
     
    169170        add_meta_box('transposh-contentbox-languages', 'Supported languages', array(&$this, 'on_contentbox_languages_content'), $this->pagehook, 'normal', 'core'); 
    170171        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'); 
    171173        add_meta_box('transposh-contentbox-general', 'Generic settings', array(&$this, 'on_contentbox_generic_content'), $this->pagehook, 'normal', 'core'); 
    172174    } 
     
    408410 
    409411        /* 
    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         /* 
    435412         * Insert the option to enable/disable default language translation. 
    436413         * Disabled by default. 
     
    451428    } 
    452429 
     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 
    453470    function on_contentbox_generic_content($data) { 
    454471        /* 
  • trunk/WordPress/plugin/transposh/wp/transposh_options.php

    r346 r364  
    6161//Option to store the msn API key 
    6262define("MSN_TRANSLATE_KEY", "msn_key"); 
     63//Option to store translator preference @since 0.4.2 
     64define("PREFERRED_TRANSLATOR", "preferred_translator"); 
    6365//Option to enable/disable default language translation 
    6466define("ENABLE_DEFAULT_TRANSLATE", "enable_default_translate"); 
     
    225227    } 
    226228 
     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 
    227234    /** 
    228235     * Gets the default language setting, i.e. the source language which normally should not be translated. 
     
    338345    } 
    339346 
     347    function set_preferred_translator($val) { 
     348        $this->set_value($val, $this->options[PREFERRED_TRANSLATOR]); 
     349    } 
     350 
    340351    /** 
    341352     * Sets the default language setting, i.e. the source language which 
Note: See TracChangeset for help on using the changeset viewer.