Jump to content

google translator


eltorro
 Share

Recommended Posts

*** YES I KNOW ABOUT ROSETTA STONE ***

A simple language translation tool. Uses Google to do the translation.

I was working on a program and I needed a way to automate translating some strings. So i whipped up a little udf to do just that. Nothing spectacular, but could be useful for others who need to do the same.

I set the default language pair to translate English to Spanish because of where I live. This is easy to change though for ones needs.

Some languages cannot be displayed without enabling unicode text support.

Enable this in - Control Panel >> Region and Language Options>> Languages >>

Install files for complex script and right to left languages (Arabic)

Install files for East Asian languages.

Checking both will add about 240MB to your system.

The gui is just a wrapper for demo/testing.

edit: added button to paste from clipboard and paste translated text to clipboard.

; simple usage:
;#Include <_GGLTranslate.au3>
;~ Local $text = StringFormat("Error, object not passes in parameter 1.\n#Please check the paramters and try again.\n")
;~;==============================================================================
;~ Local $e = _URLEncode($text)
;~ Local $d = _URLDecode($e)
;~ Local $m = StringFormat("Orig: %s\nEncoded: %s\nDecoded: %s",$text,$e,$d)
;~ MsgBox(0,"Encodings",$m)
;~;==============================================================================
;~ MsgBox(0,"English to Spanish",_GoogleTranslateString($text))
;~ MsgBox(0,"English to Dutch",_GoogleTranslateString($text,"en|nl"))
;~;==============================================================================

#include <GUIConstants.au3>
#Include <GuiComboBox.au3>
#Include <GuiEdit.au3>
#Include <user\_GGLTRanslate.au3>

Local $aLangs[29]=["ar|en","zh|en","zh-CN|zh-TW","zh-TW|zh-CN","nl|en","en|ar" _
,"en|zh-CN","en|zh-TW","en|nl","en|fr","en|de","en|el","en|it","en|ja","en|ko" _
,"en|pt","en|ru","en|es","fr|en","fr|de","de|en","de|fr","el|en","it|en","ja|en" _
,"ko|en","pt|en","ru|en","es|en"]
Local $languages = "" & _
"Chinese to English - zh:en" & _
"|Chinese (Simplified to Traditional) - zh-CN:zh-TW" & _
"|Chinese (Traditional to Simplified) - zh-TW:zh-CN" & _
"|Dutch to English - nl:en" & _
"|English to Arabic - en:ar" & _
"|English to Chinese (Simplified) - en:zh-CN" & _
"|English to Chinese (Traditional) - en:zh-TW" & _
"|English to Dutch - en:nl" & _
"|English to French - en:fr" & _
"|English to German - en:de" & _
"|English to Greek - en:el" & _
"|English to Italian - en:it" & _
"|English to Japanese - en:ja" & _
"|English to Korean - en:ko" & _
"|English to Portuguese - en:pt" & _
"|English to Russian - en:ru" & _
"|English to Spanish - en:es" & _
"|French to English - fr:en" & _
"|French to German - fr:de" & _
"|German to English - de:en" & _
"|German to French - de:fr" & _
"|Greek to English - el:en" & _
"|Italian to English - it:en" & _
"|Japanese to English - ja:en" & _
"|Korean to English - ko:en" & _
"|Portuguese to English - pt:en" & _
"|Russian to English - ru:en" & _
"|Spanish to English - es:en"

#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("gglTranslate", 633, 363, 193, 125)
Global $Edit1 = GUICtrlCreateEdit("", 10, 30, 611, 116)
Global $Edit2 = GUICtrlCreateEdit("", 10, 185, 611, 116)
Global $Combo1 = GUICtrlCreateCombo("Arabic to English - ar:en", 185, 155, 256, 25)
GUICtrlSetData(-1,$languages,"English to Spanish - en:es")
Global $Translate = GUICtrlCreateButton("Translate", 545, 150, 75, 25, 0)
Global $Paste = GuiCtrlCreateButton("From Clip",450,150,75,25,0)
Global $Label1 = GUICtrlCreateLabel("Text to Translate:", 10, 10, 87, 17)
Global $Label2 = GUICtrlCreateLabel("Translated Text:", 10, 160, 81, 17)
Global $Label3 = GUICtrlCreateLabel("Language Pair:", 100, 155, 76, 17)
Global $Progress1 = GUICtrlCreateProgress(10, 340, 610, 17)
Global $Cancel = GUICtrlCreateButton("Cancel", 545, 305, 75, 25, 0)
Global $PasteTo = GuiCtrlCreateButton("To Clip",450,305,75,25,0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
if Not FileExists(".ggl1") Then
    GuiCtrlSetData($Edit1,"Type some text here and click translate.")
    FileWriteline(".ggl1","")
EndIf

$langpair = "en|es"
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Combo1
            $langpair = $aLangs[_GUICtrlComboBox_GetCurSel($Combo1)]
        Case $Translate
            _TranslateWithProgress()
        Case $Paste
            Local $clip = ClipGet()
            if $clip <> "" Then
                GuiCtrlSetData($Edit1,$clip)
                $clip = ""
            ;remove the remark to automate translation on paste.
            ;_TranslateWithProgress()
            EndIf
        Case $PasteTo
            If _GuiCtrlEdit_GetTextLen($Edit2) >0 Then
                ClipPut(GuiCtrlRead($Edit2))
            EndIf
    EndSwitch
WEnd
Func _TranslateWithProgress()
    Const $iSize = 512
    Local $x, $iLen,$sText,$sTemp,$sTemp1,$pos,$iMsg
    $sText = GuiCtrlRead($Edit1)
    If $sText <> "" Then
        GuiCtrlSetData($Edit2,"")
        $iLen = StringLen($sText)
        ConsoleWrite($iLen&@lf)
;       If $iLen > $iSize  Then
            $x =1
            GuiCtrlSetData($Progress1,10)
            While 1
                If $iMsg = $Cancel Then
                    If (MsgBox(4,"gglTranslate","Are you sure you wish to stop translating?")) = 6 Then
                        ExitLoop
                    EndIf
                EndIf
                $iMsg = GuiGetMsg()
                If $iLen > $iSize Then
                    $sTemp = StringMid($sText,$x,$iSize)
                    $pos = StringInstr($sTemp,@LF,0,-1)
                    If $pos =0 Then $pos = StringInstr($sTemp," ",0,-1)
                    If $pos = 0 Then $pos = StringLen($sTemp)
                    $sTemp1 = StringLeft($sTemp,$pos)
                    $x += $pos
                Else
                    $sTemp1 = $sText
                    $x += $iLen
                EndIf
                If $iMsg <> $Cancel Then $iMsg = GuiGetMsg()
                GuiCtrlSetData($Edit2,_GoogleTranslateString($sTemp1,$langpair,True),1)
                GuiCtrlSetData($Progress1,(100/$iLen)*$x)
                If $x >= $iLen then
                    ExitLoop
                EndIf
                If $iMsg <> $Cancel Then $iMsg = GuiGetMsg()
            WEnd
            sleep(750)  
            GUICtrlSetData($Progress1,0)
    EndIf               
EndFunc

Save the function below as _GGLTranslate.au3

; #INDEX# =================================================
; Title .........: _GGlTranslate
; AutoIt Version: 3.2.3++
; Language:       English
; Description ...: Translates text between languages using Google web api.
; Author, Stephen Podhajecki  gehossafats at netmdc dot com.
; =========================================================

;; simple usage:
;~#Include <_GGLTranslate.au3>
;~ Local $text = StringFormat("Error, object not passes in parameter 1.\n#Please check the paramters and try again.\n")
;~ ; =========================================================
;~ Local $e = _URLEncode($text)
;~ Local $d = _URLDecode($e)
;~ Local $m = StringFormat("Orig: %s\nEncoded: %s\nDecoded: %s",$text,$e,$d)
;~ MsgBox(0,"Encodings",$m)
;~ ; =========================================================
;~ MsgBox(0,"English to Spanish",_GoogleTranslateString($text))
;~ MsgBox(0,"English to Dutch",_GoogleTranslateString($text,"en|nl"))
;~ ; =========================================================



; #FUNCTION# ==============================================
; Description ...: Uses Google to translate a string into another language
; Parameters ....: $sText - IN - The text to translate.
;                  $sLangPair - IN/OPTIONAL - The language pair separated by a pipe.
;                  $bEncodeURL - IN/OPTIONAL - Escape "illegal" characters from the text
; Return values .: On Success - The translated string.
;                  On Failure - The original string and @error set to 1
; Author ........: Stephen Podhajecki {gehossafats at netmdc. com}
; Remarks .......: Requires internet access, 
;                  Defaults to en|es English to Spanish.
;                  Defaults to False for URL encoding the text for the web.
;                  Other possible lang pairs are:
;                  |Arabic to English - ar|en
;                  |Chinese to English - zh|en
;                  |Chinese (Simplified to Traditional) - zh-CN|zh-TW
;                  |Chinese (Traditional to Simplified) - zh-TW|zh-CN
;                  |Dutch to English - nl|en
;                  |English to Arabic - en|ar
;                  |English to Chinese (Simplified) - en|zh-CN
;                  |English to Chinese (Traditional) - en|zh-TW
;                  |English to Dutch - en|nl
;                  |English to French - en|fr
;                  |English to German - en|de
;                  |English to Greek - en|el
;                  |English to Italian - en|it
;                  |English to Japanese - en|ja
;                  |English to Korean - en|ko
;                  |English to Portuguese - en|pt
;                  |English to Russian - en|ru
;                  |English to Spanish - en|es
;                  |French to English - fr|en
;                  |French to German - fr|de
;                  |German to English - de|en
;                  |German to French - de|fr
;                  |Greek to English - el|en
;                  |Italian to English - it|en
;                  |Japanese to English - ja|en
;                  |Korean to English - ko|en
;                  |Portuguese to English - pt|en
;                  |Russian to English - ru|en
;                  |Spanish to English - es|en
; Related .......: 
; =========================================================
Func _GoogleTranslateString($sText,$sLangPair= -1,$bEncodeURL=False )
    Local $url, $sTemp, $aTranslated, $sTranslated
    If $sLangPair = -1 Then $sLangPair = "en|es"
    $regex= '(?:<(?i)DIV id="?result_box"? dir="?ltr"?>)(.*?)(?:</(?i)DIV>)'
    $sTemp = $sText
    If $bEncodeURL Then
        $sTemp = _URLEncode($sText)
    EndIf
    $url =StringFormat('http://google.com/translate_t?langpair=%s&text=%s',$sLangPair,$sTemp)
    If(InetGet($url,".temp.html",1)) Then
        
        $aTranslated = StringRegExp(StringStripWS(FileRead(".temp.html"),7),$regex,3)
        If IsArray($aTranslated) Then 
            FileDelete(".temp.html")
            $sTranslated = StringReplace($aTranslated[0],Chr(160),Chr(32))
            $sTranslated = StringRegExpReplace($sTranslated,"((\s|\n)?<(?i)br)(.*?)(>)(\s)",@CRLF)
            
            ;$sTranslated = StringReplace($aTranslated[0],"<br>",@CRLF)
            If $bEncodeURL Then
                Return _URLDecode($sTranslated)
            EndIf
            Return $sTranslated 
        EndIf
    EndIf
    Return SetError(1,0,$sText)
EndFunc
; #FUNCTION# ==============================================
; Description ...: Encodes text for use in URLs
; Parameters ....: $sURL - IN - The text to encode
; Return values .: The encoded text
; Author ........: Stephen Podhajecki {gehossafats at netmdc. com}
; Remarks .......: Replaces defined characters with escaped hex values for url encoding.
;                  Encoding based on information provided here: http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
; Related .......: _URLDecode
; ========================================================
Func _URLEncode($sURL)
    Local $aEncodable[13]=['"',"'","<",">","\","^","[","]","`","+","$",",","#"]
    ;encode % first because encoding will add more % to the url
    Local $sTemp = StringReplace($sURL,"%","%25")
    ;encode non-printable and space
    For $x = 0 to 32
        $sTemp = StringReplace($sTemp,Chr($x),"%"&Hex($x,2))
    Next
    ;encode "unsafe"
    For $x = 0 to UBound($aEncodable)-1
        $sTemp = StringReplace($sTemp,$aEncodable[$x],"%"&Hex(Asc($aEncodable[$x]),2))
    Next
    ;encode upper ascii and {}|~_
    For $x = 123 to 255
        $sTemp = StringReplace($sTemp,Chr($x),"%"&Hex($x,2))
    Next
    Return $sTemp
EndFunc
; #FUNCTION# ==============================================
; Description ...: Decodeds text for use in URLs
; Parameters ....: $sURL - IN - The text to decode
; Return values .: The decoded text
; Author ........: Stephen Podhajecki {gehossafats at netmdc. com}
; Remarks .......: Replaces escaped hex values for url encoding with ascII characters.
;                  Decoding based on information provided here: http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
; Related .......: _URLEncode
; =========================================================
Func _URLDecode($sURL)
    Local $aEncodable[13]=['"',"'","<",">","\","^","[","]","`","+","$",",","#"]
    ;decode non-printable and space
    Local $sTemp = $sURL
    For $x = 0 to 32
        $sTemp = StringReplace($sTemp,"%"&Hex($x,2),Chr($x))
    Next
    ;decode "unsafe"
    For $x = 0 to UBound($aEncodable)-1
        $sTemp = StringReplace($sTemp,"%"&Hex(Asc($aEncodable[$x]),2),$aEncodable[$x])
    Next
    ;decode upper ascii and {}|~_
    For $x = 123 to 255
        $sTemp = StringReplace($sTemp,"%"&Hex($x,2),Chr($x))
    Next
    ;decode % last 
    $sTemp = StringReplace($sTemp,"%25","%")
    $sTemp = _ConvertEntities($sTemp)
    Return $sTemp
EndFunc

; #FUNCTION# =====================================================================================================================
; Description ...: _ConvertEntities
; Parameters ....: $sURL - IN - The Text to convert
; Return values .: Success - Converted string
; Author ........: Stephen Podhajecki {gehossafats at netmdc. com}
; Remarks .......: Replaces HTML escape sequences with character representation
;                  Based on information found here: http://www.theukwebdesigncompany.com/articles/entity-escape-characters.php
;                  nbsp is changed to 32 instead of 160
; Related .......: 
; ================================================================================================================================
Func _ConvertEntities($sURL)
    Local $sTemp = $sUrl
    Local $aEntities[96][2]=[["&quot;",34],["&amp;",38],["&lt;",60],["&gt;",62],["&nbsp;",3],["&nbsp;",32] _
        ,["&iexcl;",161],["&cent;",162],["&pound;",163],["&curren;",164],["&yen;",165],["&brvbar;",166] _
        ,["&sect;",167],["&uml;",168],["&copy;",169],["&ordf;",170],["&not;",172],["&shy;",173] _
        ,["&reg;",174],["&macr;",175],["&deg;",176],["&plusmn;",177],["&sup2;",178],["&sup3;",179] _
        ,["&acute;",180],["&micro;",181],["&para;",182],["&middot;",183],["&cedil;",184],["&sup1;",185] _
        ,["&ordm;",186],["&raquo;",187],["&frac14;",188],["&frac12;",189],["&frac34;",190],["&iquest;",191] _
        ,["&Agrave;",192],["&Aacute;",193],["&Atilde;",195],["&Auml;",196],["&Aring;",197],["&AElig;",198] _
        ,["&Ccedil;",199],["&Egrave;",200],["&Eacute;",201],["&Ecirc;",202],["&Igrave;",204],["&Iacute;",205] _
        ,["&Icirc;",206],["&Iuml;",207],["&ETH;",208],["&Ntilde;",209],["&Ograve;",210],["&Oacute;",211] _
        ,["&Ocirc;",212],["&Otilde;",213],["&Ouml;",214],["&times;",215],["&Oslash;",216],["&Ugrave;",217] _
        ,["&Uacute;",218],["&Ucirc;",219],["&Uuml;",220],["&Yacute;",221],["&THORN;",222],["&szlig;",223] _
        ,["&agrave;",224],["&aacute;",225],["&acirc;",226],["&atilde;",227],["&auml;",228],["&aring;",229] _
        ,["&aelig;",230],["&ccedil;",231],["&egrave;",232],["&eacute;",233],["&ecirc;",234],["&euml;",235] _
        ,["&igrave;",236],["&iacute;",237],["&icirc;",238],["&iuml;",239],["&eth;",240],["&ntilde;",241] _
        ,["&ograve;",242],["&oacute;",243],["&ocirc;",244],["&otilde;",245],["&ouml;",246],["&divide;",247] _
        ,["&oslash;",248],["&ugrave;",249],["&uacute;",250],["&ucirc;",251],["&uuml;",252],["&thorn;",254]]
    For $x = 0 to Ubound($aEntities)-1
        $sTemp = StringReplace($sTemp,$aEntities[$x][0],Chr($aEntities[$x][1]))
    Next
    For $x = 32 to 255
        $sTemp = StringReplace($sTemp,"&#"&$x&";",chr($x))
    Next
    Return $sTemp
EndFunc

edit: shortened "====".

edit: Added larger notice about displaying text.

edit: Added paste buttons, and fixed multi-line problem.

The GUI is just an example for the translator script and is really just to show that it works.

I will add conversion of some HTML entities to their character representation soon. Done.

Here's a link to download a zip with both files.

Edited by eltorro
Link to comment
Share on other sites

It seems to work but it does have a few issues on my system.

Some language pairs don't work (en|ar)

Progress never runs. ( might be caused from text string being too short)

Appears to be lacking unicode support (try English to Greek or Chinese)

Good for most though and quite fast.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Works quick.

A button "Get from clipboard" would be handy.

Here the results from testing:

Problem with multiline text

Type some text here and click translate.

This is similar to this text.

result:

Geben Sie Text ein und klicken Sie hier translate.This ist vergleichbar mit diesem Text.

Problems with special signs:

Type some text here and click translate.

result:

Geben Sie hier einen Text und klicken Sie auf &quot;übersetzen.

I think a statement stringReplace(..,"&quot;") can already solve this.

The tool I use at the moment resides in the taskbar and if i click on it, it trys to translate direct the clipboard content.

Thanks for the tool,

Reinhard

Edited by ReFran
Link to comment
Share on other sites

Works quick.

A button "Get from clipboard" would be handy.

Here the results from testing:

Problem with multiline text

result:

Problems with special signs:

result:

I think a statement stringReplace(..,"&quot;") can already solve this.

The tool I use at the moment resides in the taskbar and if i click on it, it trys to translate direct the clipboard content.

Thanks for the tool,

Reinhard

I added a couple of buttons to the GUI to cut and paste from the clipboard and I fixed the multi-line problem.

I'll get to the entities as soon as I can. It should not be hard, there is just a few common ones.

Link to comment
Share on other sites

I added a couple of buttons to the GUI to cut and paste from the clipboard and I fixed the multi-line problem.

I'll get to the entities as soon as I can. It should not be hard, there is just a few common ones.

Thanks. Now it works perfect - at least for me.

Best regards, Reinhard

Link to comment
Share on other sites

I have added converting html entities back to their character representations.

I also added a link to a zip with the files.

Link to comment
Share on other sites

I have added converting html entities back to their character representations.

I also added a link to a zip with the files.

What about the problem with unicode? Have you looked at that yet?

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

What about the problem with unicode? Have you looked at that yet?

Do you have Asian fonts enabled?

Link to comment
Share on other sites

Do you have Asian fonts enabled?

Yes and I read that in your first post but I'll have another look later. You still did a good job. Congratulations.

When I look at the source for the Google page I see the same characters that were being displayed from the translator. I have another page I've been trying out for Arabic so I'll have a look at the source from when I get that far.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Yes and I read that in your first post but I'll have another look later. You still did a good job. Congratulations.

When I look at the source for the Google page I see the same characters that were being displayed from the translator. I have another page I've been trying out for Arabic so I'll have a look at the source from when I get that far.

If you have another page that returns a better translation for arabic, then it might be possible to add that url into the script too.

Please let me know.

Link to comment
Share on other sites

  • 10 months later...
  • 3 weeks later...

Hi all,

Please look at this intresting program.

Google Translator Source

BRs

Borys

*** YES I KNOW ABOUT ROSETTA STONE ***

A simple language translation tool. Uses Google to do the translation.

I was working on a program and I needed a way to automate translating some strings. So i whipped up a little udf to do just that. Nothing spectacular, but could be useful for others who need to do the same.

I set the default language pair to translate English to Spanish because of where I live. This is easy to change though for ones needs.

Some languages cannot be displayed without enabling unicode text support.

Enable this in - Control Panel >> Region and Language Options>> Languages >>

Install files for complex script and right to left languages (Arabic)

Install files for East Asian languages.

Checking both will add about 240MB to your system.

The gui is just a wrapper for demo/testing.

edit: added button to paste from clipboard and paste translated text to clipboard.

; simple usage:
;#Include <_GGLTranslate.au3>
;~ Local $text = StringFormat("Error, object not passes in parameter 1.\n#Please check the paramters and try again.\n")
;~;==============================================================================
;~ Local $e = _URLEncode($text)
;~ Local $d = _URLDecode($e)
;~ Local $m = StringFormat("Orig: %s\nEncoded: %s\nDecoded: %s",$text,$e,$d)
;~ MsgBox(0,"Encodings",$m)
;~;==============================================================================
;~ MsgBox(0,"English to Spanish",_GoogleTranslateString($text))
;~ MsgBox(0,"English to Dutch",_GoogleTranslateString($text,"en|nl"))
;~;==============================================================================

#include <GUIConstants.au3>
#Include <GuiComboBox.au3>
#Include <GuiEdit.au3>
#Include <user\_GGLTRanslate.au3>

Local $aLangs[29]=["ar|en","zh|en","zh-CN|zh-TW","zh-TW|zh-CN","nl|en","en|ar" _
,"en|zh-CN","en|zh-TW","en|nl","en|fr","en|de","en|el","en|it","en|ja","en|ko" _
,"en|pt","en|ru","en|es","fr|en","fr|de","de|en","de|fr","el|en","it|en","ja|en" _
,"ko|en","pt|en","ru|en","es|en"]
Local $languages = "" & _
"Chinese to English - zh:en" & _
"|Chinese (Simplified to Traditional) - zh-CN:zh-TW" & _
"|Chinese (Traditional to Simplified) - zh-TW:zh-CN" & _
"|Dutch to English - nl:en" & _
"|English to Arabic - en:ar" & _
"|English to Chinese (Simplified) - en:zh-CN" & _
"|English to Chinese (Traditional) - en:zh-TW" & _
"|English to Dutch - en:nl" & _
"|English to French - en:fr" & _
"|English to German - en:de" & _
"|English to Greek - en:el" & _
"|English to Italian - en:it" & _
"|English to Japanese - en:ja" & _
"|English to Korean - en:ko" & _
"|English to Portuguese - en:pt" & _
"|English to Russian - en:ru" & _
"|English to Spanish - en:es" & _
"|French to English - fr:en" & _
"|French to German - fr:de" & _
"|German to English - de:en" & _
"|German to French - de:fr" & _
"|Greek to English - el:en" & _
"|Italian to English - it:en" & _
"|Japanese to English - ja:en" & _
"|Korean to English - ko:en" & _
"|Portuguese to English - pt:en" & _
"|Russian to English - ru:en" & _
"|Spanish to English - es:en"

#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("gglTranslate", 633, 363, 193, 125)
Global $Edit1 = GUICtrlCreateEdit("", 10, 30, 611, 116)
Global $Edit2 = GUICtrlCreateEdit("", 10, 185, 611, 116)
Global $Combo1 = GUICtrlCreateCombo("Arabic to English - ar:en", 185, 155, 256, 25)
GUICtrlSetData(-1,$languages,"English to Spanish - en:es")
Global $Translate = GUICtrlCreateButton("Translate", 545, 150, 75, 25, 0)
Global $Paste = GuiCtrlCreateButton("From Clip",450,150,75,25,0)
Global $Label1 = GUICtrlCreateLabel("Text to Translate:", 10, 10, 87, 17)
Global $Label2 = GUICtrlCreateLabel("Translated Text:", 10, 160, 81, 17)
Global $Label3 = GUICtrlCreateLabel("Language Pair:", 100, 155, 76, 17)
Global $Progress1 = GUICtrlCreateProgress(10, 340, 610, 17)
Global $Cancel = GUICtrlCreateButton("Cancel", 545, 305, 75, 25, 0)
Global $PasteTo = GuiCtrlCreateButton("To Clip",450,305,75,25,0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
if Not FileExists(".ggl1") Then
    GuiCtrlSetData($Edit1,"Type some text here and click translate.")
    FileWriteline(".ggl1","")
EndIf

$langpair = "en|es"
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Combo1
            $langpair = $aLangs[_GUICtrlComboBox_GetCurSel($Combo1)]
        Case $Translate
            _TranslateWithProgress()
        Case $Paste
            Local $clip = ClipGet()
            if $clip <> "" Then
                GuiCtrlSetData($Edit1,$clip)
                $clip = ""
        ;remove the remark to automate translation on paste.
        ;_TranslateWithProgress()
            EndIf
        Case $PasteTo
            If _GuiCtrlEdit_GetTextLen($Edit2) >0 Then
                ClipPut(GuiCtrlRead($Edit2))
            EndIf
    EndSwitch
WEnd
Func _TranslateWithProgress()
    Const $iSize = 512
    Local $x, $iLen,$sText,$sTemp,$sTemp1,$pos,$iMsg
    $sText = GuiCtrlRead($Edit1)
    If $sText <> "" Then
        GuiCtrlSetData($Edit2,"")
        $iLen = StringLen($sText)
        ConsoleWrite($iLen&@lf)
;       If $iLen > $iSize  Then
            $x =1
            GuiCtrlSetData($Progress1,10)
            While 1
                If $iMsg = $Cancel Then
                    If (MsgBox(4,"gglTranslate","Are you sure you wish to stop translating?")) = 6 Then
                        ExitLoop
                    EndIf
                EndIf
                $iMsg = GuiGetMsg()
                If $iLen > $iSize Then
                    $sTemp = StringMid($sText,$x,$iSize)
                    $pos = StringInstr($sTemp,@LF,0,-1)
                    If $pos =0 Then $pos = StringInstr($sTemp," ",0,-1)
                    If $pos = 0 Then $pos = StringLen($sTemp)
                    $sTemp1 = StringLeft($sTemp,$pos)
                    $x += $pos
                Else
                    $sTemp1 = $sText
                    $x += $iLen
                EndIf
                If $iMsg <> $Cancel Then $iMsg = GuiGetMsg()
                GuiCtrlSetData($Edit2,_GoogleTranslateString($sTemp1,$langpair,True),1)
                GuiCtrlSetData($Progress1,(100/$iLen)*$x)
                If $x >= $iLen then
                    ExitLoop
                EndIf
                If $iMsg <> $Cancel Then $iMsg = GuiGetMsg()
            WEnd
            sleep(750)  
            GUICtrlSetData($Progress1,0)
    EndIf               
EndFunc

Save the function below as _GGLTranslate.au3

; #INDEX# =================================================
; Title .........: _GGlTranslate
; AutoIt Version: 3.2.3++
; Language:       English
; Description ...: Translates text between languages using Google web api.
; Author, Stephen Podhajecki  gehossafats at netmdc dot com.
; =========================================================

;; simple usage:
;~#Include <_GGLTranslate.au3>
;~ Local $text = StringFormat("Error, object not passes in parameter 1.\n#Please check the paramters and try again.\n")
;~ ; =========================================================
;~ Local $e = _URLEncode($text)
;~ Local $d = _URLDecode($e)
;~ Local $m = StringFormat("Orig: %s\nEncoded: %s\nDecoded: %s",$text,$e,$d)
;~ MsgBox(0,"Encodings",$m)
;~ ; =========================================================
;~ MsgBox(0,"English to Spanish",_GoogleTranslateString($text))
;~ MsgBox(0,"English to Dutch",_GoogleTranslateString($text,"en|nl"))
;~ ; =========================================================



; #FUNCTION# ==============================================
; Description ...: Uses Google to translate a string into another language
; Parameters ....: $sText - IN - The text to translate.
;                  $sLangPair - IN/OPTIONAL - The language pair separated by a pipe.
;                  $bEncodeURL - IN/OPTIONAL - Escape "illegal" characters from the text
; Return values .: On Success - The translated string.
;                  On Failure - The original string and @error set to 1
; Author ........: Stephen Podhajecki {gehossafats at netmdc. com}
; Remarks .......: Requires internet access, 
;                  Defaults to en|es English to Spanish.
;                  Defaults to False for URL encoding the text for the web.
;                  Other possible lang pairs are:
;                  |Arabic to English - ar|en
;                  |Chinese to English - zh|en
;                  |Chinese (Simplified to Traditional) - zh-CN|zh-TW
;                  |Chinese (Traditional to Simplified) - zh-TW|zh-CN
;                  |Dutch to English - nl|en
;                  |English to Arabic - en|ar
;                  |English to Chinese (Simplified) - en|zh-CN
;                  |English to Chinese (Traditional) - en|zh-TW
;                  |English to Dutch - en|nl
;                  |English to French - en|fr
;                  |English to German - en|de
;                  |English to Greek - en|el
;                  |English to Italian - en|it
;                  |English to Japanese - en|ja
;                  |English to Korean - en|ko
;                  |English to Portuguese - en|pt
;                  |English to Russian - en|ru
;                  |English to Spanish - en|es
;                  |French to English - fr|en
;                  |French to German - fr|de
;                  |German to English - de|en
;                  |German to French - de|fr
;                  |Greek to English - el|en
;                  |Italian to English - it|en
;                  |Japanese to English - ja|en
;                  |Korean to English - ko|en
;                  |Portuguese to English - pt|en
;                  |Russian to English - ru|en
;                  |Spanish to English - es|en
; Related .......: 
; =========================================================
Func _GoogleTranslateString($sText,$sLangPair= -1,$bEncodeURL=False )
    Local $url, $sTemp, $aTranslated, $sTranslated
    If $sLangPair = -1 Then $sLangPair = "en|es"
    $regex= '(?:<(?i)DIV id="?result_box"? dir="?ltr"?>)(.*?)(?:</(?i)DIV>)'
    $sTemp = $sText
    If $bEncodeURL Then
        $sTemp = _URLEncode($sText)
    EndIf
    $url =StringFormat('http://google.com/translate_t?langpair=%s&text=%s',$sLangPair,$sTemp)
    If(InetGet($url,".temp.html",1)) Then
        
        $aTranslated = StringRegExp(StringStripWS(FileRead(".temp.html"),7),$regex,3)
        If IsArray($aTranslated) Then 
            FileDelete(".temp.html")
            $sTranslated = StringReplace($aTranslated[0],Chr(160),Chr(32))
            $sTranslated = StringRegExpReplace($sTranslated,"((\s|\n)?<(?i)br)(.*?)(>)(\s)",@CRLF)
            
            ;$sTranslated = StringReplace($aTranslated[0],"<br>",@CRLF)
            If $bEncodeURL Then
                Return _URLDecode($sTranslated)
            EndIf
            Return $sTranslated 
        EndIf
    EndIf
    Return SetError(1,0,$sText)
EndFunc
; #FUNCTION# ==============================================
; Description ...: Encodes text for use in URLs
; Parameters ....: $sURL - IN - The text to encode
; Return values .: The encoded text
; Author ........: Stephen Podhajecki {gehossafats at netmdc. com}
; Remarks .......: Replaces defined characters with escaped hex values for url encoding.
;                  Encoding based on information provided here: http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
; Related .......: _URLDecode
; ========================================================
Func _URLEncode($sURL)
    Local $aEncodable[13]=['"',"'","<",">","\","^","[","]","`","+","$",",","#"]
    ;encode % first because encoding will add more % to the url
    Local $sTemp = StringReplace($sURL,"%","%25")
    ;encode non-printable and space
    For $x = 0 to 32
        $sTemp = StringReplace($sTemp,Chr($x),"%"&Hex($x,2))
    Next
    ;encode "unsafe"
    For $x = 0 to UBound($aEncodable)-1
        $sTemp = StringReplace($sTemp,$aEncodable[$x],"%"&Hex(Asc($aEncodable[$x]),2))
    Next
    ;encode upper ascii and {}|~_
    For $x = 123 to 255
        $sTemp = StringReplace($sTemp,Chr($x),"%"&Hex($x,2))
    Next
    Return $sTemp
EndFunc
; #FUNCTION# ==============================================
; Description ...: Decodeds text for use in URLs
; Parameters ....: $sURL - IN - The text to decode
; Return values .: The decoded text
; Author ........: Stephen Podhajecki {gehossafats at netmdc. com}
; Remarks .......: Replaces escaped hex values for url encoding with ascII characters.
;                  Decoding based on information provided here: http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
; Related .......: _URLEncode
; =========================================================
Func _URLDecode($sURL)
    Local $aEncodable[13]=['"',"'","<",">","\","^","[","]","`","+","$",",","#"]
    ;decode non-printable and space
    Local $sTemp = $sURL
    For $x = 0 to 32
        $sTemp = StringReplace($sTemp,"%"&Hex($x,2),Chr($x))
    Next
    ;decode "unsafe"
    For $x = 0 to UBound($aEncodable)-1
        $sTemp = StringReplace($sTemp,"%"&Hex(Asc($aEncodable[$x]),2),$aEncodable[$x])
    Next
    ;decode upper ascii and {}|~_
    For $x = 123 to 255
        $sTemp = StringReplace($sTemp,"%"&Hex($x,2),Chr($x))
    Next
    ;decode % last 
    $sTemp = StringReplace($sTemp,"%25","%")
    $sTemp = _ConvertEntities($sTemp)
    Return $sTemp
EndFunc

; #FUNCTION# =====================================================================================================================
; Description ...: _ConvertEntities
; Parameters ....: $sURL - IN - The Text to convert
; Return values .: Success - Converted string
; Author ........: Stephen Podhajecki {gehossafats at netmdc. com}
; Remarks .......: Replaces HTML escape sequences with character representation
;                  Based on information found here: http://www.theukwebdesigncompany.com/articles/entity-escape-characters.php
;                  nbsp is changed to 32 instead of 160
; Related .......: 
; ================================================================================================================================
Func _ConvertEntities($sURL)
    Local $sTemp = $sUrl
    Local $aEntities[96][2]=[["&quot;",34],["&amp;",38],["&lt;",60],["&gt;",62],["&nbsp;",3],["&nbsp;",32] _
        ,["&iexcl;",161],["&cent;",162],["&pound;",163],["&curren;",164],["&yen;",165],["&brvbar;",166] _
        ,["&sect;",167],["&uml;",168],["&copy;",169],["&ordf;",170],["&not;",172],["&shy;",173] _
        ,["&reg;",174],["&macr;",175],["&deg;",176],["&plusmn;",177],["&sup2;",178],["&sup3;",179] _
        ,["&acute;",180],["&micro;",181],["&para;",182],["&middot;",183],["&cedil;",184],["&sup1;",185] _
        ,["&ordm;",186],["&raquo;",187],["&frac14;",188],["&frac12;",189],["&frac34;",190],["&iquest;",191] _
        ,["&Agrave;",192],["&Aacute;",193],["&Atilde;",195],["&Auml;",196],["&Aring;",197],["&AElig;",198] _
        ,["&Ccedil;",199],["&Egrave;",200],["&Eacute;",201],["&Ecirc;",202],["&Igrave;",204],["&Iacute;",205] _
        ,["&Icirc;",206],["&Iuml;",207],["&ETH;",208],["&Ntilde;",209],["&Ograve;",210],["&Oacute;",211] _
        ,["&Ocirc;",212],["&Otilde;",213],["&Ouml;",214],["&times;",215],["&Oslash;",216],["&Ugrave;",217] _
        ,["&Uacute;",218],["&Ucirc;",219],["&Uuml;",220],["&Yacute;",221],["&THORN;",222],["&szlig;",223] _
        ,["&agrave;",224],["&aacute;",225],["&acirc;",226],["&atilde;",227],["&auml;",228],["&aring;",229] _
        ,["&aelig;",230],["&ccedil;",231],["&egrave;",232],["&eacute;",233],["&ecirc;",234],["&euml;",235] _
        ,["&igrave;",236],["&iacute;",237],["&icirc;",238],["&iuml;",239],["&eth;",240],["&ntilde;",241] _
        ,["&ograve;",242],["&oacute;",243],["&ocirc;",244],["&otilde;",245],["&ouml;",246],["&divide;",247] _
        ,["&oslash;",248],["&ugrave;",249],["&uacute;",250],["&ucirc;",251],["&uuml;",252],["&thorn;",254]]
    For $x = 0 to Ubound($aEntities)-1
        $sTemp = StringReplace($sTemp,$aEntities[$x][0],Chr($aEntities[$x][1]))
    Next
    For $x = 32 to 255
        $sTemp = StringReplace($sTemp,"&#"&$x&";",chr($x))
    Next
    Return $sTemp
EndFunc

edit: shortened "====".

edit: Added larger notice about displaying text.

edit: Added paste buttons, and fixed multi-line problem.

The GUI is just an example for the translator script and is really just to show that it works.

I will add conversion of some HTML entities to their character representation soon. Done.

Here's a link to download a zip with both files.

Link to comment
Share on other sites

  • 2 years later...

translator:

#include <inet.au3>

Global $language = ''
;$language &= "auto Detect Language|"
$language &= "af Afrikaans|"
$language &= "sq Albanian|"
$language &= "ar Arabic|"
$language &= "hy Armenian|"
$language &= "az Azeri|"
$language &= "eu Basque|"
$language &= "bn Bengali|"
$language &= "be Belarusian|"
$language &= "bg Bulgarian|"
$language &= "zh CN Chinese|"
$language &= "hr Croatian|"
$language &= "cs Czech|"
$language &= "da Danish|"
$language &= "en English|"
$language &= "et Estonian|"
$language &= "tl Filipino|"
$language &= "fi Finnish|"
$language &= "fr French|"
$language &= "gl Galician|"
$language &= "de German|"
$language &= "el Greek|"
$language &= "ka Georgian|"
$language &= "gu Gujarati|"
$language &= "iw Hebrew|"
$language &= "hi Hindi|"
$language &= "hu Hungarian|"
$language &= "id Indonesian|"
$language &= "it Italian|"
$language &= "ga Irish|"
$language &= "is Icelandic|"
$language &= "ja Japanese|"
$language &= "yi Yiddish|"
$language &= "kn Kannada|"
$language &= "ca Catalan|"
$language &= "ko Korean|"
$language &= "ht Haitian Creole|"
$language &= "lt Lithuanian|"
$language &= "la Latin|"
$language &= "lv Latvian|"
$language &= "mk Macedonian|"
$language &= "ms Malay|"
$language &= "mt Maltese|"
$language &= "nl Netherlands|"
$language &= "no Norwegian|"
$language &= "fa Persian|"
$language &= "pl Polish|"
$language &= "pt Portuguese|"
$language &= "ru Russian|"
$language &= "ro Romanian|"
$language &= "sr Serbian|"
$language &= "sk Slovak|"
$language &= "sl Slovenian|"
$language &= "sw Swahili|"
$language &= "sv Swedish|"
$language &= "es Spanish|"
$language &= "th Thai|"
$language &= "ta Tamil|"
$language &= "te Telugu|"
$language &= "tr Turkish|"
$language &= "uk Ukrainian|"
$language &= "ur Urdu|"
$language &= "cy Welsh|"
$language &= "vi Vietnamese"


Global $string = "you like bananas?"
Global $trans
Global $arrayLang = StringSplit($language, "|", 3)

For $i = 0 To UBound($arrayLang) - 1

$trans = myGoogleTranslate($string, "en", $arrayLang[$i], "en")
ConsoleWrite($arrayLang[$i] & ' = ' & $trans & @CRLF)

Next

ConsoleWrite(@CRLF)
ConsoleWrite(myGoogleTranslate("kociak", "PL", "EN", "EN") & @CRLF)


Exit



#region Google Translate

Func myGoogleTranslate($sText, $sTo = "en", $sFrom = "pl", $sCode = "en")
$sTo = StringRegExpReplace($sTo, '(\w+)|(.*)', '\1')
$sFrom = StringRegExpReplace($sFrom, '(\w+)|(.*)', '\1')
$sCode = StringRegExpReplace($sCode, '(\w+)|(.*)', '\1')

If (Not $sText) Or (Not $sTo) Or (Not $sFrom) Or (Not $sCode) Then Return SetError(1, 0, 0)

If StringRegExpReplace($sText, '([a-zA-Z0-9]+)', '') Then
$sText = StringToBinary($sText)
$sText = StringTrimLeft($sText, 2)
$sText = StringRegExpReplace($sText, '(.{2})', '%\1')
EndIf

Local $GoogleURL = 'http://translate.google.com/translate_a/t?client=t&text=' & $sText & '&hl=' & $sCode & '&sl=' & $sTo & '&tl=' & $sFrom & '&multires=1&ssel=0&tsel=0&sc=1'
;ConsoleWrite($GoogleURL & @CRLF)
Local $sSource = _INetGetSource($GoogleURL)
;If @error Then
; Local $TempFileDir = @ScriptDir & "\t.js"
; Local $hDownload = InetGet($GoogleURL, $TempFileDir, 1, 1)
; Local $begin = TimerInit()
; Do
; Until (InetGetInfo($hDownload, 2)) Or (TimerDiff($begin) >= 4000)
; InetClose($hDownload)
; $sSource = FileRead($TempFileDir)
; FileDelete($TempFileDir)
;EndIf
;ConsoleWrite($sSource & @CRLF)
If (Not $sSource) Then Return SetError(2, 0, 0)

Local $s_saveSource = $sSource
Local $sPattern = '],,"' & $sTo & '",,['

$sSource = StringLeft($sSource, StringInStr($sSource, $sPattern))
If $sSource Then ; jesli to cale zdanie
$sSource = StringRegExpReplace($sSource, '[[]["](.*?)["][,]["]|(.*?)', '\1')
$sSource = myGoogleTranslate_Hex_to_ChrW($sSource)
Else ; jesli to pojedynczy wyraz
$sPattern = '],"' & $sTo & '",,['
$sSource = StringLeft($s_saveSource, StringInStr($s_saveSource, $sPattern))
$sSource = StringRegExpReplace($sSource, '[,][[][[]?(.*?)[[](.*?)[]]|(.*?)', '\1\2')
$sSource = myGoogleTranslate_Hex_to_ChrW($sSource)

$sSource = StringReplace($sSource, '","', @CRLF & @TAB)
$sSource = StringReplace($sSource, '"', @CRLF)
EndIf
$sSource = StringReplace($sSource, '\"', '"')
$sSource = StringFormat($sSource)

If (Not $sSource) Then Return SetError(3, 0, 0)
Return $sSource
EndFunc ;==>myGoogleTranslate


Func myGoogleTranslate_Hex_to_ChrW($sText)
Local $searchHex, $aText
Do
$searchHex = StringInStr($sText, '\u')
If $searchHex Then
$aText = StringRegExp($sText, '(.{' & ($searchHex - 1) & '})(.{2})(.{4})(.*)', 3)
;_ArrayDisplay($aText)
If IsArray($aText) Then
$sText = $aText[0] & ChrW(Dec($aText[2])) & $aText[3]
EndIf
EndIf
Until $searchHex <= 0
Return $sText
EndFunc ;==>myGoogleTranslate_Hex_to_ChrW

#endregion Google Translate
Edited by akurakkauaaa
Link to comment
Share on other sites

  • 2 weeks later...

url decoding working ?

Because , I do not understand , how copy this : ą, ń, ę, ż, dż, ź, ó, ł, ś, ć

from google translator

When I copy text to autoit , I have ±, ę, ż, dż, Ľ, ó, ł, ¶, ć,

Edited by tele123
Link to comment
Share on other sites

@akurakkauaaa: great piece of code!

url decoding working ?

Because , I do not understand , how copy this : ą, ń, ę, ż, dż, ź, ó, ł, ś, ć

from google translator

When I copy text to autoit , I have ±, ę, ż, dż, Ľ, ó, ł, ¶, ć,

The text is encoded in ISO8859-2. If you want, you can use the following function to get the text in ANSI (works only for Polish):

Func ISOtoANSI($sText)
; http://en.wikipedia.org/wiki/Polish_code_pages
$sText = StringReplace($sText, Chr(Dec("A1")), "Ą")
$sText = StringReplace($sText, Chr(Dec("A6")), "Ś")
$sText = StringReplace($sText, Chr(Dec("AC")), "Ź")
$sText = StringReplace($sText, Chr(Dec("B1")), "ą")
$sText = StringReplace($sText, Chr(Dec("B6")), "ś")
$sText = StringReplace($sText, Chr(Dec("BC")), "ź")
Return $sText
EndFunc   ;==>ISOtoANSI
Link to comment
Share on other sites

  • 2 weeks later...

hmm....

xB9 (utf-8)= ą ( ANSI ) = b9 (hex)

xE6 (utf-8)= ć ( ANSI ) = e6 (hex )

ą, ń, ę, ż, dż, ź, ó, ł, ś, ć

ą, ń, ę, ż, dż, ź, ó, ł, ś, ć, --&gt; utf-8 to ANSI

xB9, xF1, xEA, xBF, dxBF, x9F, xF3, xB3, x9C, xE6 --&gt; ANSI to utf-8

Ok , now I know , I must change Format utf-8 to ANSI in notepad++ :)

And I use Windows-1250 file , but from url ISO8859-2 is similar , but example, greatly helped.

Thanks slawekc

;)

I have .txt in fiile

<v>6, ą, ń, ę, ż, dż, ź, ó, ł, ś, ć</v>

<v>6, Ą, Ń, Ę, Ż DŻ, Ź, ó, Ł, Ś, Ć</v>

from google

<v>6, a, n, ę, ż, dż, ź, ó, ł, ś, ć</v>

<v>6, A, N, E, Z DZ, Z, O, L, S, C</v>

I don't know...no matter, now it is a little better

Edited by tele123
Link to comment
Share on other sites

  • 7 months later...

I use this function to translate to Georgian KA.

But when i use in GUICtrlCreateEdit:

GUICtrlSetData($cText, $Translated)

not see strange characters.

Do it is possible to solve?

i must to enable unicode text support in Windows control pannel?

Edited by Ontosy
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...