From 9fbf953216c7beb86f7b3232498c1ac052f43267 Mon Sep 17 00:00:00 2001 From: Kit Sunde Date: Fri, 24 Feb 2012 18:37:21 +0800 Subject: [PATCH] Show bing suggest errors. --- rosetta/templates/rosetta/js/rosetta.js | 68 +++++++++++-------------- 1 file changed, 29 insertions(+), 39 deletions(-) diff --git a/rosetta/templates/rosetta/js/rosetta.js b/rosetta/templates/rosetta/js/rosetta.js index 74b3162..f779063 100644 --- a/rosetta/templates/rosetta/js/rosetta.js +++ b/rosetta/templates/rosetta/js/rosetta.js @@ -4,53 +4,43 @@ google.setOnLoadCallback(function() { }, function() { $('.hide', $(this).parent()).hide(); }); + {% if ENABLE_TRANSLATION_SUGGESTIONS and BING_APP_ID %} - $('a.suggest').click(function() { - var a = $(this), - str = a.html(), - orig = $('.original .message', a.parents('tr')).html(), - trans=$('textarea',a.parent()), - sourceLang = '{{MESSAGES_SOURCE_LANGUAGE_CODE}}', - destLang = '{{rosetta_i18n_lang_code|slice:":2"}}', - app_id = '{{BING_APP_ID}}'; - - orig = unescape(orig).replace(//g,'\n').replace(//g,'').replace(/<\/code>/g,'').replace(/>/g,'>').replace(/</g,'<'); + $('a.suggest').click(function(e){ + e.preventDefault(); + var a = $(this); + var str = a.html(); + var orig = $('.original .message', a.parents('tr')).html(); + var trans=$('textarea',a.parent()); + var sourceLang = '{{ MESSAGES_SOURCE_LANGUAGE_CODE }}'; + var destLang = '{{ rosetta_i18n_lang_code|slice:":2" }}'; + var app_id = '{{ BING_APP_ID }}'; + var apiUrl = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate"; + + orig = unescape(orig).replace(//g,'\n').replace(//,'').replace(/<\/code>/g,'').replace(/>/g,'>').replace(/</g,'<'); a.attr('class','suggesting').html('...'); window.onTranslationComplete = function(resp) { trans.val(unescape(resp).replace(/'/g,'\'').replace(/"/g,'"').replace(/%\s+(\([^\)]+\))\s*s/g,' %$1s ')); a.hide(); - }; - var s = document.createElement("script"); - s.src = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?oncomplete=onTranslationComplete&appId="+app_id +"&from=" + sourceLang + "&to=" + destLang + "&text=" + orig; - document.getElementsByTagName("head")[0].appendChild(s); - - /* - google.language.translate(orig, , function(result) { - if (!result.error) { - trans.val(unescape(result.translation).replace(/'/g,'\'').replace(/"/g,'"').replace(/%\s+(\([^\)]+\))\s*s/g,' %$1s ')); - a.hide(); - } else { - a.hide().before($(''+result.error.message+'')); - } - }); - */ - return false; + window.onTranslationError = function(response){ + a.text(response); + }; + var apiData = { + onerror: 'onTranslationError', + appid: app_id, + from: sourceLang, + to: destLang, + text: orig + }; + $.ajax({ + url: apiUrl, + data: apiData, + dataType: 'jsonp', + jsonpCallback: 'onTranslationComplete'}); }); - - - $('#translate-all').submit(function() { - $('a.suggest').click(); - return false; - }); - $('.checkall').click(function(){ - $('td.c input').attr('checked', ''); - $('td.c input').attr('value', '0'); - }); - - - {% endif %} + $('td.plural').each(function(i) { var td = $(this), trY = parseInt(td.closest('tr').offset().top); $('textarea', $(this).closest('tr')).each(function(j) {