Jump to content
Sign in to follow this  
Zinthose

Using Google to Translate Text

Recommended Posts

Zinthose

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

Share this post


Link to post
Share on other sites
Zinthose

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
gseller

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

  • Like 1

Share this post


Link to post
Share on other sites
Zenhari

Hello Friend

Can you help me?

I want a program that could translator a website with google translate

tanks

Share this post


Link to post
Share on other sites
corgano

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

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.