Using Google to Translate Text

Zinthose
By Zinthose in AutoIt Example Scripts,
The title Says it all! #Region - Demo _GoogleToMsgBox("Hello World", "en", "en") _GoogleToMsgBox("Hello World", "en", "ja") _GoogleToMsgBox("Hello World", "en", "es") _GoogleToMsgBox("Hello World", "en", "it") _GoogleToMsgBox("Hello World", "en", "zh-cn") _GoogleToMsgBox("Hello World", "en", "ar") _GoogleToMsgBox("Hello World", "en", "bg") _GoogleToMsgBox("Hello World", "en", "fr") _GoogleToMsgBox("Hello World", "en", "de") _GoogleToMsgBox(InputBox("You try it!", "Enter some text to translate to Russian", "In Soviet Russia, code document you!"), "en", "ru") Func _GoogleToMsgBox($sText, $sFrom, $sTo) Msgbox(4096, "Google Translate [" & $sFrom & ":" & $sTo & "]", $sText & @TAB & @TAB & @TAB & @CRLF & @CRLF & _GoogleTranslate($sText, $sFrom, $sTo)) EndFunc #EndRegion Func _GoogleTranslate($sText, $sFrom = "en", $sTo = "ja") Local Const $FileName = "Translation.jsn" Local Const $Pattern = '"translatedText":"([^"]+)"' Local $GoogleURL = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=%s&langpair=%s%%7C%s" Local $File $GoogleURL = StringFormat($GoogleURL, $sText, $sFrom, $sTo) If Not InetGet($GoogleURL, $FileName, 1) Then Return SetError(1, 0, 0) $File = FileOpen($FileName, 4) FileGetSize($FileName) $Translation = FileRead($File, FileGetSize($FileName)) FileClose($File) FileDelete($FileName) $Translation = BinaryToString($Translation, 4) If StringRegExp($Translation , $Pattern) Then $Translation = StringRegExp($Translation , $Pattern, 1) $Translation = $Translation[0] Return $Translation EndIf EndFunc
  • 5 replies