#FEATURE by cg
authorClaus Gittinger <cg@exept.de>
Fri, 10 Jan 2020 18:53:18 +0100
changeset 6790 886523f1233c
parent 6789 374f76212c7c
child 6791 ebd5551c19ce
#FEATURE by cg class: EditTextView added: #googleTranslate: changed: #babelFishTranslate:
EditTextView.st
--- a/EditTextView.st	Sun Jan 05 17:35:24 2020 +0100
+++ b/EditTextView.st	Fri Jan 10 18:53:18 2020 +0100
@@ -6958,28 +6958,29 @@
     "translate the selected text and paste it after the selection.
      This is now obsolete, as that web service no longer exists (sigh)"
 
-    |original translated|
-
-    original := self selectionAsString.
-    original size == 0 ifTrue:[^ self].
-
-    self withWaitCursorDo:[
-        (HostNameLookupError , SOAP::SoapImplError) handle:[:ex |
-            Dialog warn:('Translation failed - WEB Service error:\\%1.' bindWith:ex description allBold) withCRs
-        ] do:[
-            translated := SOAP::BabelFishClient new 
-                            translate:original mode:fromToModeString.
-        ]
-    ].
-
-    "/ v pasteOrReplace:translated
-    self cursorLine:(self selectionEndLine) col:(self selectionEndCol + 1).
-    self unselect.
-    self
-        undoablePaste:translated
-        info:'Translate'
-
-    "Modified: / 28-07-2007 / 13:27:21 / cg"
+    ^ self googleTranslate:fromToModeString.
+"/    |original translated|
+"/
+"/    original := self selectionAsString.
+"/    original size == 0 ifTrue:[^ self].
+"/
+"/    self withWaitCursorDo:[
+"/        (HostNameLookupError , SOAP::SoapImplError) handle:[:ex |
+"/            Dialog warn:('Translation failed - WEB Service error:\\%1.' bindWith:ex description allBold) withCRs
+"/        ] do:[
+"/            translated := SOAP::BabelFishClient new 
+"/                            translate:original mode:fromToModeString.
+"/        ]
+"/    ].
+"/
+"/    "/ v pasteOrReplace:translated
+"/    self cursorLine:(self selectionEndLine) col:(self selectionEndCol + 1).
+"/    self unselect.
+"/    self
+"/        undoablePaste:translated
+"/        info:'Translate'
+"/
+"/    "Modified: / 28-07-2007 / 13:27:21 / cg"
 !
 
 canDivertOutput
@@ -7470,6 +7471,44 @@
     "Modified: / 28-07-2007 / 13:25:10 / cg"
 !
 
+googleTranslate:fromToModeString
+    "translate the selected text and paste it after the selection."
+
+    |sourceLang targetLang response url req o original translated|
+
+    "/ 'en_de' upTo:$_.     
+    "/ 'en_de' copyAfter:$_.  
+    sourceLang := fromToModeString upTo:$_.
+    targetLang := fromToModeString copyAfter:$_.
+
+    original := self selectionAsString.
+    original size == 0 ifTrue:[^ self].
+
+    self withWaitCursorDo:[
+        url := 'http://translate.googleapis.com/translate_a/single?client=gtx&sl=%1&tl=%2&dt=t&ie=UTF-8&oe=UTF-8&q=%3' 
+                bindWith:sourceLang with:targetLang with:original utf8Encoded.
+        response := HTTPInterface get:url.
+
+        "/ url := 'http://translation.googleapis.com/language/v2'. 
+        "/ req := '{ "q": ["%3"], "source": "%1", "target": "%2" }'
+        "/         bindWith:sourceLang with:targetLang with:original.
+        "/ 
+        "/ response := HTTPInterface post:url with:req.
+    ].
+    response isErrorResponse ifTrue:[^ self].
+    o := JSONReader decode:response data.
+    translated := o first first first.
+
+    "/ v pasteOrReplace:translated
+    self cursorLine:(self selectionEndLine) col:(self selectionEndCol + 1).
+    self unselect.
+    self
+        undoablePaste:translated
+        info:'Translate'
+
+    "Modified: / 28-07-2007 / 13:27:21 / cg"
+!
+
 insertDateAndTime
     "insert the current date and time string"