Pman.GoogleTranslate.js
[Pman.Core] / Pman.GoogleTranslate.js
index 1c730ae..d6a153a 100644 (file)
@@ -24,14 +24,19 @@ Pman.GoogleTranslate = function(str, src, dest, cb, force) {
                 url : baseURL + '/Core/GoogleKey',
                 success : function(data)
                 {
-                    Pman.GoogleTranslate.key = data.key;
+                    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;
                 }
                 
                 
-            })
+            });
             
             
             
@@ -112,23 +117,60 @@ Pman.GoogleTranslate = function(str, src, dest, cb, force) {
                 
           
         var x = new Roo.data.ScriptTagProxy({ 
-            url:  'http://ajax.googleapis.com/ajax/services/language/translate', 
-            callbackParam : 'callback' 
+            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...
+        if (src  == 'zh-HK')  src = 'zh-TW';
+        if (dest == 'zh-HK') dest = 'zh-TW';
+     
+        new Pman.Request({
+            url : baseURL + '/Roo/GoogleTranslate.php',
+            method :'POST',
+            mask : 'Translating',
+            maskEl : document.body,
+            params : {
+                text : str,
+                src  : src,
+                dest : dest,
+            },
+            success: function(o)
+            {
+                if (!o.data) {
+                        return o;
+                    }
+//                Roo.MessageBox.alert("Success", "We logged in OK")
+                return o.data.translations[0].translatedText;
+            },
+            failure: function (res) {
+                Roo.log(res);
+                Roo.MessageBox.alert("Failure", "Failed?")
+            }
         });
         
         x.load(
             {
-                v: '1.0',
+                key :  Pman.GoogleTranslate.key,
+              //  v: '1.0',
                 q : str,
-                langpair : src + '|' +dest
+                source : src,
+                target : dest
+                //langpair : src + '|' +dest
             }, // end params.
             { // reader
                 readRecords : function (o) {
-                    if (!o.responseData) {
+                    Roo.log(o);
+                    if (!o.data) {
                         return o;
                     }
-                    return escapeDecode(o.responseData.translatedText);
+                    return o.data.translations[0].translatedText;
+                    //return escapeDecode(o.data.translations[0].translatedText);
                 }
             }, 
             function (result) {