X-Git-Url: http://git.roojs.org/?p=Pman.Core;a=blobdiff_plain;f=Pman.GoogleTranslate.js;h=0a4fcfbf672e0f6609abc1ecb0434fb73b9198a9;hp=e03d4736b317d649c7acafbb68aa942e80aa5ecc;hb=586e4eb470252d837ba18b67e4c3c1702131fd1d;hpb=70fb821384cf0ece4c1db445078a6ab217f7375b diff --git a/Pman.GoogleTranslate.js b/Pman.GoogleTranslate.js index e03d4736..0a4fcfbf 100644 --- a/Pman.GoogleTranslate.js +++ b/Pman.GoogleTranslate.js @@ -8,14 +8,43 @@ * @param str {String} input string * @param src {String} source language (or '' for autoguess) * @param str {String} target language - * @param str {String} callback - calls back with result or an error object. + * @param cb {String} callback - calls back with result or an error object. */ -Pman.GoogleTranslate = function(str, src, dest, cb) { +Pman.GoogleTranslate = function(str, src, dest, cb, force) { // load script: var cur = 0; var sbits = []; var complete = ''; + +// if (!Pman.GoogleTranslate.key) { +// +// new Pman.Request({ +// method : 'POST', +// url : baseURL + '/Core/GoogleKey', +// success : function(data) +// { +// Pman.GoogleTranslate.key = data.data; +// +// Pman.GoogleTranslate(str, src, dest, cb, force); +// +// }, +// failure : function() { +// Roo.log("Google key fetch failed"); +// // do not display error message... +// return true; +// } +// +// +// }); +// +// +// +// +// return; +// } + + function escapeDecode(encodedString) { var output = encodedString; var binVal, thisString; @@ -31,8 +60,7 @@ Pman.GoogleTranslate = function(str, src, dest, cb) { return Roo.util.Format.htmlDecode( output); } - - + function transbits() { while (true) { @@ -49,7 +77,7 @@ Pman.GoogleTranslate = function(str, src, dest, cb) { } // Roo.log("SEND : " + sbits[cur]); - Pman.gtranslate( sbits[cur], src, dest, function(result) { + Pman.GoogleTranslate( sbits[cur], src, dest, function(result) { if (typeof(result) == 'object') { cb(result); return; @@ -59,12 +87,13 @@ Pman.GoogleTranslate = function(str, src, dest, cb) { complete += result; cur++; transbits(); - }); + }, true); } // chunk up long strings.. - if (str.length > 200) { - var bits = str.split(/(\s+|[0-9+])/); + // we should roo.encode to test lenght.. + if (!force && str.length > 200) { + var bits = str.split(/(\s+|[0-9\u3002\uff0c\u3001\u201c\u201d]+)/); sbits[0] = ''; for (var i =0; i < bits.length; i++) { if (sbits[cur].length + bits[i].length > 190) { @@ -82,42 +111,86 @@ Pman.GoogleTranslate = function(str, src, dest, cb) { transbits(); return; } - - - - - - +// +// +// +// +// +// var x = new Roo.data.ScriptTagProxy({ +// url: 'https://www.googleapis.com/language/translate/v2', +// //'http://ajax.googleapis.com/ajax/services/language/translate', +// callbackParam : 'callback' +// +// +// }); +// + src = src.replace('_','-'); + dest = dest.replace('_','-'); + // google does not recognize HK... + switch(dest){ + case 'zh': + src = 'zh-CN'; + break; + case 'zh-HK': + src = 'zh-TW'; + break; + } + switch(dest){ + case 'zh': + dest = 'zh-CN'; + break; + case 'zh-HK': + dest = 'zh-TW'; + break; + } - var x = new Roo.data.ScriptTagProxy({ - url: 'http://ajax.googleapis.com/ajax/services/language/translate', - callbackParam : 'callback' - - }); - x.load( + new Pman.Request({ + url : baseURL + '/Core/GoogleTranslate.php', + method :'POST', + mask : 'Translating', + maskEl : document.body, + params : { + text : str, + src : src, + dest : dest + }, + success: function(o) { - v: '1.0', - q : str, - langpair : src + '|' +dest - }, // end params. - { // reader - readRecords : function (o) { - if (!o.responseData) { - return o; - } - return escapeDecode(o.responseData.translatedText); + if (!o.data) { + return o; } - }, - function (result) { - cb(result); + if(typeof(o.data.error) != 'undefined'){ + Roo.get(document.body).unmask(); + Roo.MessageBox.alert("Failure ", o.data.error.message); + return; + } + if(typeof(o.data.translations[0].translatedText) == 'undefined'){ + Roo.MessageBox.alert("Failure ", "Does not found the translated text."); + } + cb(o.data.translations[0].translatedText); }, - this, - [] - ); - - + failure: function (res) { + Roo.log(res); + if (!res.data) { + return res; + } + + if(typeof(res.data.error) != 'undefined'){ + Roo.get(document.body).unmask(); + Roo.MessageBox.alert("Failure ", res.data.error.message); + return; + } + + if(typeof(res.data.translations[0].translatedText) == 'undefined'){ + Roo.MessageBox.alert("Failure ", "Does not found the translated text."); + } + cb(res.data.translations[0].translatedText); + + //Roo.MessageBox.alert("Failure ", res.message); + } + }); };