Changeset 668


Ignore:
Timestamp:
11/20/2011 11:12:19 AM (6 months ago)
Author:
ofer
Message:

Use a closure

File:
1 edited

Legend:

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

    r667 r668  
    1717 
    1818/*global Date, Math, alert, escape, clearTimeout, document, jQuery, setTimeout, t_jp, t_be, window */ 
    19  
    20 var timer; 
    21 var items = 0; 
    22 var translations = []; 
    23 var tokens = []; 
    24 var langs = []; 
    25 var sources = []; 
    26 var BATCH_SIZE = 512; 
    27 var pair_count = 0; 
    28 var curr_pair = 0; 
    29  
    30 // move the progress bar a bit 
    31 function make_progress(translation, lang) { 
    32     curr_pair += 1; 
    33     jQuery("#progress_bar").progressbar('value', curr_pair / pair_count * 100); 
    34     jQuery('#p').text('(' + lang + ') ' + translation); 
    35     if (curr_pair === pair_count) { 
    36         jQuery("#tr_loading").data("done", true); 
    37     } 
    38 } 
    39  
    40 // batch items for posting to server.. nice touch added for different sources for same batch... 
    41 function ajax_translate_me(token, translation, lang, source) { 
    42     translation = jQuery("<div>" + jQuery.trim(translation) + "</div>").text(); // fix some char bugs 
    43     make_progress(translation, lang); 
    44     // we aggregate translations together, 200ms from the last translation we will send the timer 
    45     // so here we remove it so nothing unexpected happens 
    46     clearTimeout(timer); 
    47     items += 1; 
    48     // push translations - we'll assume something is different... 
    49     tokens.push(token); 
    50     translations.push(translation); 
    51     langs.push(lang); 
    52     sources.push(source); 
    53     timer = setTimeout(function () { 
    54         var data = { 
    55             action: 'tp_translation', 
    56             items: items 
    57         }, i; 
    58         // this is the "smart" stuff, only coding changed info 
    59         for (i = 0; i < items; i += 1) { 
    60             if (tokens[i] !== tokens[i - 1]) { 
    61                 data["tk" + i] = tokens[i]; 
    62             } 
    63             if (langs[i] !== langs[i - 1]) { 
    64                 data["ln" + i] = langs[i]; 
    65             } 
    66             if (translations[i] !== translations[i - 1]) { 
    67                 data["tr" + i] = translations[i]; 
    68             } 
    69             if (sources[i] !== sources[i - 1]) { 
    70                 data["sr" + i] = sources[i]; 
    71             } 
    72         } 
    73         jQuery.ajax({ 
    74             type: "POST", 
    75             url: t_jp.ajaxurl, // FIX ALL! 
    76             data: data, 
    77             success: function () { 
    78             }, 
    79             error: function () { 
    80             } 
    81         }); 
    82         // as we posted, we can come clean (TODO - future test of results) 
    83         items = 0; 
    84         translations = []; 
    85         tokens = []; 
    86         langs = []; 
    87         sources = []; 
    88     }, 200); // wait 200 ms... 
    89 } 
    90  
    91 // this is the mass translate for MS 
    92 /*function do_mass_ms_translate(batchtrans, callback) { 
     19(function ($) { // closure 
     20 
     21    var timer; 
     22    var items = 0; 
     23    var translations = []; 
     24    var tokens = []; 
     25    var langs = []; 
     26    var sources = []; 
     27    var BATCH_SIZE = 512; 
     28    var pair_count = 0; 
     29    var curr_pair = 0; 
     30 
     31    // move the progress bar a bit 
     32    function make_progress(translation, lang) { 
     33        curr_pair += 1; 
     34        $("#progress_bar").progressbar('value', curr_pair / pair_count * 100); 
     35        $('#p').text('(' + lang + ') ' + translation); 
     36        if (curr_pair === pair_count) { 
     37            $("#tr_loading").data("done", true); 
     38        } 
     39    } 
     40 
     41    // batch items for posting to server.. nice touch added for different sources for same batch... 
     42    function ajax_translate_me(token, translation, lang, source) { 
     43        translation = $("<div>" + $.trim(translation) + "</div>").text(); // fix some char bugs 
     44        make_progress(translation, lang); 
     45        // we aggregate translations together, 200ms from the last translation we will send the timer 
     46        // so here we remove it so nothing unexpected happens 
     47        clearTimeout(timer); 
     48        items += 1; 
     49        // push translations - we'll assume something is different... 
     50        tokens.push(token); 
     51        translations.push(translation); 
     52        langs.push(lang); 
     53        sources.push(source); 
     54        timer = setTimeout(function () { 
     55            var data = { 
     56                action: 'tp_translation', 
     57                items: items 
     58            }, i; 
     59            // this is the "smart" stuff, only coding changed info 
     60            for (i = 0; i < items; i += 1) { 
     61                if (tokens[i] !== tokens[i - 1]) { 
     62                    data["tk" + i] = tokens[i]; 
     63                } 
     64                if (langs[i] !== langs[i - 1]) { 
     65                    data["ln" + i] = langs[i]; 
     66                } 
     67                if (translations[i] !== translations[i - 1]) { 
     68                    data["tr" + i] = translations[i]; 
     69                } 
     70                if (sources[i] !== sources[i - 1]) { 
     71                    data["sr" + i] = sources[i]; 
     72                } 
     73            } 
     74            $.ajax({ 
     75                type: "POST", 
     76                url: t_jp.ajaxurl, // FIX ALL! 
     77                data: data, 
     78                success: function () { 
     79                }, 
     80                error: function () { 
     81                } 
     82            }); 
     83            // as we posted, we can come clean (TODO - future test of results) 
     84            items = 0; 
     85            translations = []; 
     86            tokens = []; 
     87            langs = []; 
     88            sources = []; 
     89        }, 200); // wait 200 ms... 
     90    } 
     91 
     92    // this is the mass translate for MS 
     93    /*function do_mass_ms_translate(batchtrans, callback) { 
    9394    var q = "["; 
    94     jQuery(batchtrans).each(function (i) { 
     95    $(batchtrans).each(function (i) { 
    9596        q += '"' + encodeURIComponent(batchtrans[i]) + '",'; 
    9697    }); 
    9798    q = q.slice(0, -1) + ']'; 
    98     jQuery.ajax({ 
     99    $.ajax({ 
    99100        url: 'http://api.microsofttranslator.com/V2/Ajax.svc/TranslateArray?appId=' + t_jp.MSN_APPID + '&to=' + t_jp.binglang + '&texts=' + q, 
    100101        dataType: "jsonp", 
     
    104105}*/ 
    105106 
    106 // and the invoker 
    107 function do_mass_ms_invoker(tokens, trans, lang) { 
    108     var binglang = lang; 
    109     // fix this in ms mass... 
    110     if (binglang === 'zh') { 
    111         binglang = 'zh-chs'; 
    112     } else if (binglang === 'zh-tw') { 
    113         binglang = 'zh-cht'; 
    114     } 
    115     t_jp.dmt(trans, function (result) { 
    116         jQuery(result).each(function (i) { 
    117             ajax_translate_me(tokens[i], this.TranslatedText, lang, 2); // notice the source 
    118         }); 
    119     }, binglang); 
    120 } 
    121  
    122 function do_mass_apertium_invoker(tokens, trans, lang) { 
    123     t_jp.dat(trans, function (result) { 
    124         // we assume that 2xx answer should be good, 200 is good, 206 is partially good (some errors) 
    125         if (result.responseStatus >= 200 && result.responseStatus < 300) { 
    126             // single items get handled differently 
    127             if (result.responseData.translatedText !== undefined) { 
    128                 ajax_translate_me(tokens[0], result.responseData.translatedText); 
     107    // and the invoker 
     108    function do_mass_ms_invoker(tokens, trans, lang) { 
     109        var binglang = lang; 
     110        // fix this in ms mass... 
     111        if (binglang === 'zh') { 
     112            binglang = 'zh-chs'; 
     113        } else if (binglang === 'zh-tw') { 
     114            binglang = 'zh-cht'; 
     115        } 
     116        t_jp.dmt(trans, function (result) { 
     117            $(result).each(function (i) { 
     118                ajax_translate_me(tokens[i], this.TranslatedText, lang, 2); // notice the source 
     119            }); 
     120        }, binglang); 
     121    } 
     122 
     123    function do_mass_apertium_invoker(tokens, trans, lang) { 
     124        t_jp.dat(trans, function (result) { 
     125            // we assume that 2xx answer should be good, 200 is good, 206 is partially good (some errors) 
     126            if (result.responseStatus >= 200 && result.responseStatus < 300) { 
     127                // single items get handled differently 
     128                if (result.responseData.translatedText !== undefined) { 
     129                    ajax_translate_me(tokens[0], result.responseData.translatedText); 
     130                } else { 
     131                    $(result.responseData).each(function (i) { 
     132                        if (this.responseStatus === 200) { 
     133                            ajax_translate_me(tokens[i], this.responseData.translatedText, lang, 3); 
     134                        } 
     135                    }); 
     136                } 
     137            } 
     138        }, lang); 
     139    } 
     140 
     141    function do_mass_google_invoker(tokens, trans, lang) { 
     142        t_jp.dgpt(trans, function (result) { 
     143            $(result.results).each(function (i) { 
     144                ajax_translate_me(tokens[i], this, lang, 1); 
     145            }); 
     146        }, lang); 
     147    } 
     148 
     149    function do_mass_google_api_invoker(tokens, trans, lang) { 
     150        t_jp.dgt(trans, function (result) { 
     151            // if there was an error we will try the other invoker 
     152            if (typeof result.error !== 'undefined') { 
     153                do_mass_google_invoker(tokens, trans, lang); 
    129154            } else { 
    130                 jQuery(result.responseData).each(function (i) { 
    131                     if (this.responseStatus === 200) { 
    132                         ajax_translate_me(tokens[i], this.responseData.translatedText, lang, 3); 
    133                     } 
     155                $(result.data.translations).each(function (i) { 
     156                    ajax_translate_me(tokens[i], this.translatedText, lang, 1); 
    134157                }); 
    135158            } 
    136         } 
    137     }, lang); 
    138 } 
    139  
    140 function do_mass_google_invoker(tokens, trans, lang) { 
    141     t_jp.dgpt(trans, function (result) { 
    142         jQuery(result.results).each(function (i) { 
    143             ajax_translate_me(tokens[i], this, lang, 1); 
    144         }); 
    145     }, lang); 
    146 } 
    147  
    148 function do_mass_google_api_invoker(tokens, trans, lang) { 
    149     t_jp.dgt(trans, function (result) { 
    150         // if there was an error we will try the other invoker 
    151         if (typeof result.error !== 'undefined') { 
    152             do_mass_google_invoker(tokens, trans, lang); 
     159        }, lang); 
     160    } 
     161 
     162    function do_invoker(batchtokens, batchtrans, currlang) { 
     163        if (t_be.m_langs.indexOf(currlang) !== -1 && t_jp.preferred === '2') { 
     164            do_mass_ms_invoker(batchtokens, batchtrans, currlang); 
     165        } else if (t_be.a_langs.indexOf(currlang) !== -1 && (t_jp.olang === 'en' || t_jp.olang === 'es')) { 
     166            do_mass_apertium_invoker(batchtokens, batchtrans, currlang); 
     167        } else if (t_jp.google_key) { 
     168            do_mass_google_api_invoker(batchtokens, batchtrans, currlang); 
    153169        } else { 
    154             jQuery(result.data.translations).each(function (i) { 
    155                 ajax_translate_me(tokens[i], this.translatedText, lang, 1); 
    156             }); 
    157         } 
    158     }, lang); 
    159 } 
    160  
    161 function do_invoker(batchtokens, batchtrans, currlang) { 
    162     if (t_be.m_langs.indexOf(currlang) !== -1 && t_jp.preferred === '2') { 
    163         do_mass_ms_invoker(batchtokens, batchtrans, currlang); 
    164     } else if (t_be.a_langs.indexOf(currlang) !== -1 && (t_jp.olang === 'en' || t_jp.olang === 'es')) { 
    165         do_mass_apertium_invoker(batchtokens, batchtrans, currlang); 
    166     } else if (t_jp.google_key) { 
    167         do_mass_google_api_invoker(batchtokens, batchtrans, currlang); 
    168     } else { 
    169         do_mass_google_invoker(batchtokens, batchtrans, currlang); 
    170     } 
    171 } 
    172  
    173 // the main translate function 
    174 function translate_post(postid) { 
    175     var currlang = '', 
    176     tokens = [], 
    177     strings = [], 
    178     lang, str, name, val, 
    179     batchlength = 0, 
    180     batchtrans = [], 
    181     batchtokens = [], 
    182     to_trans; 
    183  
    184     jQuery("#tr_loading").data("done", false); 
    185     // get the post // FIX 
    186     jQuery.ajax({ 
    187         url: ajaxurl, 
    188         dataType: 'json', 
    189         data: { 
    190             action: "tp_post_phrases", 
    191             post: postid 
    192         }, 
    193         cache: false, 
    194         success: function (json) { 
    195             // if we got no results than seems like we have nothing to translate 
    196             jQuery("#tr_translate_title").html("Translating post: " + json.posttitle); 
    197             if (json.length === undefined) { 
    198                 jQuery("#tr_loading").html('Nothing left to translate'); 
    199                 jQuery("#tr_loading").data("done", true); 
    200                 return; 
    201             } 
    202             // calculate # of pairs 
    203             pair_count = 0; 
    204             curr_pair = 0; 
    205             for (name in json.p) { 
    206                 pair_count += json.p[name].l.length; 
    207             } 
    208  
    209             // create progress bars 
    210             jQuery("#tr_loading").html('<br/>Translation: <span id="p"></span><div id="progress_bar"/>'); 
    211             jQuery("#progress_bar").progressbar({ 
    212                 value: 0 
    213             }); 
    214  
    215             // per language passing... 
    216             // this things happens when msn translate is default 
    217             for (var lang in json.langs) { 
    218                 currlang = json.langs[lang]; 
    219                 strings = []; 
    220                 tokens = []; 
     170            do_mass_google_invoker(batchtokens, batchtrans, currlang); 
     171        } 
     172    } 
     173 
     174    // the main translate function 
     175    function translate_post(postid) { 
     176        var currlang = '', 
     177        tokens = [], 
     178        strings = [], 
     179        lang, str, name, val, 
     180        batchlength = 0, 
     181        batchtrans = [], 
     182        batchtokens = [], 
     183        to_trans; 
     184 
     185        $("#tr_loading").data("done", false); 
     186        // get the post // FIX 
     187        $.ajax({ 
     188            url: ajaxurl, 
     189            dataType: 'json', 
     190            data: { 
     191                action: "tp_post_phrases", 
     192                post: postid 
     193            }, 
     194            cache: false, 
     195            success: function (json) { 
     196                // if we got no results than seems like we have nothing to translate 
     197                $("#tr_translate_title").html("Translating post: " + json.posttitle); 
     198                if (json.length === undefined) { 
     199                    $("#tr_loading").html('Nothing left to translate'); 
     200                    $("#tr_loading").data("done", true); 
     201                    return; 
     202                } 
     203                // calculate # of pairs 
     204                pair_count = 0; 
     205                curr_pair = 0; 
    221206                for (name in json.p) { 
    222                     val = json.p[name]; 
    223                     // we have a winner 
    224                     if (val.l.indexOf(currlang) !== -1) { 
    225                         // add to candidates 
    226                         strings.push(unescape(name)); 
    227                         tokens.push(val.t); 
    228                         val.l.splice(val.l.indexOf(currlang), 1); 
    229                         // if no more languages, we can remove the item from further processing 
    230                         if (val.l.length === 0) { 
    231                             json.length -= 1; 
    232                             delete json.p[name]; 
     207                    pair_count += json.p[name].l.length; 
     208                } 
     209 
     210                // create progress bars 
     211                $("#tr_loading").html('<br/>Translation: <span id="p"></span><div id="progress_bar"/>'); 
     212                $("#progress_bar").progressbar({ 
     213                    value: 0 
     214                }); 
     215 
     216                // per language passing... 
     217                // this things happens when msn translate is default 
     218                for (var lang in json.langs) { 
     219                    currlang = json.langs[lang]; 
     220                    strings = []; 
     221                    tokens = []; 
     222                    for (name in json.p) { 
     223                        val = json.p[name]; 
     224                        // we have a winner 
     225                        if (val.l.indexOf(currlang) !== -1) { 
     226                            // add to candidates 
     227                            strings.push(unescape(name)); 
     228                            tokens.push(val.t); 
     229                            val.l.splice(val.l.indexOf(currlang), 1); 
     230                            // if no more languages, we can remove the item from further processing 
     231                            if (val.l.length === 0) { 
     232                                json.length -= 1; 
     233                                delete json.p[name]; 
     234                            } 
    233235                        } 
    234236                    } 
    235                 } 
    236                 if (strings.length) { 
    237                     for (str in strings) { 
    238                         to_trans = strings[str]; 
    239                         if (batchlength + to_trans.length > BATCH_SIZE) { 
    240                             do_invoker(batchtokens, batchtrans, currlang); 
    241                             batchlength = 0; 
    242                             batchtrans = []; 
    243                             batchtokens = []; 
     237                    if (strings.length) { 
     238                        for (str in strings) { 
     239                            to_trans = strings[str]; 
     240                            if (batchlength + to_trans.length > BATCH_SIZE) { 
     241                                do_invoker(batchtokens, batchtrans, currlang); 
     242                                batchlength = 0; 
     243                                batchtrans = []; 
     244                                batchtokens = []; 
     245                            } 
     246                            batchlength += to_trans.length; 
     247                            batchtokens.push(tokens[str]); 
     248                            batchtrans.push(to_trans); 
    244249                        } 
    245                         batchlength += to_trans.length; 
    246                         batchtokens.push(tokens[str]); 
    247                         batchtrans.push(to_trans); 
     250 
     251                        // this invokation is for the remaining items 
     252                        do_invoker(batchtokens, batchtrans, currlang); 
    248253                    } 
    249  
    250                     // this invokation is for the remaining items 
    251                     do_invoker(batchtokens, batchtrans, currlang); 
    252                 } 
    253             } 
     254                } 
    254255             
     256            } 
     257        }); 
     258    } 
     259    // TODO - just expose this one, not the entire set of items 
     260    window.translate_post = translate_post; 
     261 
     262    // If we have a single post, we can just go through with it 
     263    $(document).ready(function () { 
     264        if (t_be.post) { 
     265            translate_post(t_be.post); 
    255266        } 
    256267    }); 
    257 } 
    258 // TODO - just expose this one, not the entire set of items 
    259  
    260 // If we have a single post, we can just go through with it 
    261 jQuery(document).ready(function () { 
    262     if (t_be.post) { 
    263         translate_post(t_be.post); 
    264     } 
    265 }); 
     268}(jQuery)); // end of closure 
Note: See TracChangeset for help on using the changeset viewer.