Jump to content

Using Google to Translate Text


Zinthose
 Share

Recommended Posts

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

--- TTFN

Link to comment
Share on other sites

Very Good discovery and way to use regex with the web, have you seen this post? http://www.autoitscript.com/forum/index.php?showtopic=65961 Might help you alot with further development.. ;)

Boo! someone beat me to it... No I didn't see that post. ;)

Oh, well... at least mine "looks" simpler. :D

And here I was all excited that I found something new.

Is the search capability of the forum broke? It seams like a search usually yields everything but the kitchen sink when I search for "Kitchen Sink".

--- TTFN

Link to comment
Share on other sites

  • 10 months later...
  • 6 months later...

if you set translate to "en" from "auto", is there a way to get what language Google says it is? You can do this manually by translating language detect to English from (auto detect)

according to waht the script does,

"http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=In Soviet Russia, code document you!&langpair=en%7Cru"

is normal, but this

"http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=In Soviet Russia, code document you!&langpair=auto%7Cru"

won't work. Why? That is what the Google URL uses when you translate manually.

Edited by corgano

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

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...