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.
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]=[[""",34],["&",38],["<",60],[">",62],[" ",3],[" ",32] _ ,["¡",161],["¢",162],["£",163],["¤",164],["¥",165],["¦",166] _ ,["§",167],["¨",168],["©",169],["ª",170],["¬",172],["­",173] _ ,["®",174],["¯",175],["°",176],["±",177],["²",178],["³",179] _ ,["´",180],["µ",181],["¶",182],["·",183],["¸",184],["¹",185] _ ,["º",186],["»",187],["¼",188],["½",189],["¾",190],["¿",191] _ ,["À",192],["Á",193],["Ã",195],["Ä",196],["Å",197],["Æ",198] _ ,["Ç",199],["È",200],["É",201],["Ê",202],["Ì",204],["Í",205] _ ,["Î",206],["Ï",207],["Ð",208],["Ñ",209],["Ò",210],["Ó",211] _ ,["Ô",212],["Õ",213],["Ö",214],["×",215],["Ø",216],["Ù",217] _ ,["Ú",218],["Û",219],["Ü",220],["Ý",221],["Þ",222],["ß",223] _ ,["à",224],["á",225],["â",226],["ã",227],["ä",228],["å",229] _ ,["æ",230],["ç",231],["è",232],["é",233],["ê",234],["ë",235] _ ,["ì",236],["í",237],["î",238],["ï",239],["ð",240],["ñ",241] _ ,["ò",242],["ó",243],["ô",244],["õ",245],["ö",246],["÷",247] _ ,["ø",248],["ù",249],["ú",250],["û",251],["ü",252],["þ",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.
Here's a link to download a zip with both files.
Edited by eltorro, 10 March 2008 - 08:40 PM.




