Jump to content
Sign in to follow this  

Using Google to Translate Text

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

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)
    $Translation = FileRead($File, FileGetSize($FileName))
    $Translation = BinaryToString($Translation, 4)

    If StringRegExp($Translation , $Pattern) Then
        $Translation = StringRegExp($Translation , $Pattern, 1)
        $Translation = $Translation[0]
        Return $Translation

--- TTFN

Share this post

Link to post
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

Share this post

Link to post
Share on other sites

I agree.. LOL Seems like it only pulls the sticky's then whatever is the most popular post at the time... Google is what I mainly use to search the forum tho. Start it out with like: autoit blah blah and it does pretty well..

Share this post

Link to post
Share on other sites

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


Share this post

Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...