hide popup message if failure is handled - not sure what knock on effect this may...
[Pman.Core] / Pman.GoogleTranslate.js
index e0e0070..0a4fcfb 100644 (file)
@@ -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) {
@@ -48,22 +76,24 @@ Pman.GoogleTranslate = function(str, src, dest, cb) {
                 break;
             }
             
-           Roo.log("SEND : " + sbits[cur]);
-            Pman.gtranslate( sbits[cur], src, dest, function(result) {
+           // Roo.log("SEND : " + sbits[cur]);
+            Pman.GoogleTranslate( sbits[cur], src, dest, function(result) {
                 if (typeof(result) == 'object') {
                     cb(result);
                     return;
                 }
+                //padding might not be needed...
                 complete += complete.length ? ' ' : ''; 
                 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) {
@@ -81,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);
+            }
+        });
         
     };