Jump to content

Recommended Posts

Posted

Hey,

So I'm trying to create a translation script to translate text from english to spanish. It seems my program crashes if i have more than 1 child in the html.(i will highlight it via comment) I am also occasionally crashing after my 5 second, while busy, loop. Any help or insight would be greatly appreciated.

 

$langFrom = "en"
$langTo = "es"
While 1
    Sleep(250)
    Translate()
Wend



Func Translate()
    $ie = ObjCreate("InternetExplorer.Application")
    $ie.visible = True
    $toTrans = InputBox("Translate", "Enter text to translate [" & $langTo & "]")
    $ie.Navigate("https://translate.google.ca/?ie=UTF-8&hl=" & $langFrom & "&client=tw-ob#auto/" & $langTo & "/" & $toTrans)
    
    $result = ""
    
    While($ie.busy)
    Sleep(5000)
    WEnd ;occasionally crashing here (unsure cause)
    
    While($result = "")
        $result = $ie.document.getElementById("result_box").innerHTML
        sleep(250)
    WEnd
    
    $children = $ie.document.getElementById("result_box").childNodes ;suspect i did something wrong here
    
    $result = ""
    
    for $child in $children
        $result = $result & $child.innerHTML & " " ;crashing here
    Next
    
    ;cleanup
    $result = StringReplace($result,"  "," ")
    $result = StringReplace($result," ."," ")
    
    $ie.quit()
    MsgBox(0,"Translated", $result)
    
    EndFunc

 

Posted

When it crashes do you get any error messages?
Do you run it compiled or from SciTE?

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

I wrote this using IE UDF.

#include <IE.au3>


MsgBox(0,"",_GoogleTraslate("Hello World. I love AutoIt"))

Func _GoogleTraslate($sTexttoTraslate, $sFrom = "en", $sTo = "es")
    Local $sResult = ""
    Local $sURL = "http://translate.google.com/#" & $sFrom & "/" & $sTo & "/" & $sTexttoTraslate
    Local $oIE = _IECreate($sURL,Default,False)
    _IELoadWait($oIE)
    Local $oResultBox = _IEGetObjById($oIE, "result_box")

    If IsObj($oResultBox) Then
        $sResult = $oResultBox.innertext
    EndIf
    _IEQuit($oIE)
    Return $sResult
EndFunc   ;==>_GoogleTraslate

Saludos

Posted (edited)

For a translation tool you could use a translation api (google or another one)
This one currently still works  :)

$mytext = "So I'm trying to create a translation script to translate text from english to spanish. " & @crlf & "It seems my program crashes..."
$from = "en"
$to = "es"
$url = "https://translate.googleapis.com/translate_a/single?client=gtx"
$url &= "&sl=" & $from & "&tl=" & $to & "&dt=t&q=" & $mytext

$oHTTP = ObjCreate("Microsoft.XMLHTTP")
$oHTTP.Open("POST", $url, False)
$oHTTP.Send()
$sData = $oHTTP.ResponseText
$sData = StringRegExpReplace($sData, '.*?\["(.*?)(?<!\\)"[^\[]*', "$1" & @crlf)
Msgbox(0,"", $sData)

 

Edited by mikell
typo
Posted

I think this is realted to this track ticket:

https://www.autoitscript.com/trac/autoit/ticket/3167

So you should probably try this:

Local $oDocument = $ie.document
Local $oTemp = Null
While 1
    $oTemp = $oDocument.getElementById("result_box")
    $result = $oTemp.innerHtml
    if not @error Then ExitLoop
    sleep(250)
Wend

instead of this:

While($result = "")
        $result = $ie.document.getElementById("result_box").innerHTML
        sleep(250)
    WEnd

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

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
×
×
  • Create New...