hide popup message if failure is handled - not sure what knock on effect this may...
[Pman.Core] / Pman.GoogleTranslate.js
index a3a7d75..0a4fcfb 100644 (file)
@@ -8,7 +8,7 @@
  * @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, force) {
         // load script: 
@@ -17,27 +17,32 @@ Pman.GoogleTranslate = function(str, src, dest, cb, force) {
         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);
-                    
-                }
-                
-                
-            });
-            
-            
-            
-            
-            return;
-        }
+//        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) {
@@ -106,53 +111,86 @@ Pman.GoogleTranslate = function(str, src, dest, cb, force) {
             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'
-            
-            
-        });
-        /// fix some of the languages..
-        //switch (dest) {
-        //        case 'zh-CN' : dest = 'zh-CN'; break;
-         //       case 'zh-HK' : dest = 'zh-TW'; break;
-         //           
-        //}
-         
+//                
+//                
+//                
+//                
+//          
+//        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...
         
-        x.load(
+        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;
+        }
+        
+        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)
             {
-                key :  Pman.GoogleTranslate.key,
-              //  v: '1.0',
-                q : str,
-                source : src,
-                target : dest
-                //langpair : src + '|' +dest
-            }, // end params.
-            { // reader
-                readRecords : function (o) {
-                    Roo.log(o);
-                    if (!o.data) {
-                        return o;
-                    }
-                    return o.data.translations[0].translatedText;
-                    //return escapeDecode(o.data.translations[0].translatedText);
+                if (!o.data) {
+                    return o;
+                }
+                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.");
                 }
-            }, 
-            function (result) {
-                cb(result);
+                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);
+            }
+        });
         
     };