; #Au3Stripper_Parameters=/MO #include #include #include Global $searchstring Global $bigtranslation $bigtranslation = "" #cs Rename your script from simplegoog###.au3 to simplegoog#YOUR_API_KEY#source_language#target_language.au3 1. Script gets API key, SL and TL from its own name. 2. Script asks for input and output files. 3. Ask user if it's okay to split by sentence. If yes: - replace ESPs with ESP## - find/replace abbreviation+## with abbreviation - replace ## with CRLF (if fileexists abbreviations.txt then use that file, one abbr per line) 4. Split input file by CRLF and TAB. 5. Use 10 lines at a time, encode them, adds them to the URL, and sends it to GT. 6. Parse replies To parce JSON reply: Split by: "translatedText": " Then split [2] by " Also encode/decode any URL text. http://www.w3schools.com/tags/ref_urlencode.asp And write to a file. With a TrayTip telling the progress. #ce If StringInStr (@ScriptName, "###", 1) Then $apikey = InputBox ("Enter API key", "What is your API key?") If @error = 1 Then Exit EndIf $SL = InputBox ("Enter source language", "What is your source language?") If @error = 1 Then Exit EndIf $TL= InputBox ("Enter target language", "What is your target language?") If @error = 1 Then Exit EndIf Else $scriptnamesplit = StringSplit (@ScriptName, "#", 1) If $scriptnamesplit[0] = 4 Then $apikey = $scriptnamesplit[2] $SL = $scriptnamesplit[3] $TL = StringTrimRight ($scriptnamesplit[4], 4) $scriptnamesplitinfo = MsgBox (4, "Is this information correct?", "API key: " & $apikey & @CRLF & "Source language code: " & $SL & @CRLF & "Target language code: " & $TL, 0) If $scriptnamesplitinfo = 7 Then $apikey = InputBox ("Enter API key", "What is your API key?", $apikey) If @error = 1 Then Exit EndIf $SL = InputBox ("Enter source language", "What is your source language?", $SL) If @error = 1 Then Exit EndIf $TL= InputBox ("Enter target language", "What is your target language?", $TL) If @error = 1 Then Exit EndIf EndIf Else MsgBox (0, "Error 2", "Could not get API key, source language code or target language code from script file name." & @CRLF & "The script will now exit.", 0) Exit EndIf EndIf $mustaskforfile = 0 If $CmdLine[0] > 0 Then $inputfileopendialog = $CmdLine[1] $inputfile = FileRead (FileOpen ($inputfileopendialog, 128)) $cliporno = 7 Else $cliporno = MsgBox (4, "Translate the clipboard?", "Do you want to translate the clipboard, or a file?" & @CRLF & "Say 'Yes' to translate the clipboard.", 0) If $cliporno = 6 Then $inputfile = ClipGet () If @error > 0 Then MsgBox (0, "Clipboard error", "Unable to access clipboad, or clipboard is empty.", 0) Exit EndIf Else $mustaskforfile = 1 EndIf EndIf If FileExists (@ScriptDir & "\abbreviationlist.txt") Then $abbreviationlistfile = FileRead (FileOpen (@ScriptDir & "\abbreviationlist.txt", 128)) $abbreviationlist = StringSplit ($abbreviationlistfile, ",", 1) Else $abbreviationlist = StringSplit ("Apr.,Aug.,Corp.,D.,Dec.,Dept.,Dr.,D.C.,e.g.,etc.,Feb.,Jan.,Jun.,Jul.,Jr.,Inc.,i.e.,Lt.,Ltd.,Mar.,max.,min.,Mr.,Mrs.,Ms.,Oct.,Nov.,Pr.,Pres.,Rev.,rev.,Sep.,Sgt.,St.,Str.,vol.,vs.", ",", 1) EndIf $splitbysentence = MsgBox (4, "Segment further by sentence?", "Try to segment further by sentence?" & @CRLF & "Say 'No' if your text has one sentence per line already.", 0) If $mustaskforfile = 1 Then $inputfileopendialog = FileOpenDialog ("Source text file", @ScriptDir, "Text files (*.txt)|All files (*.*)") $inputfile = FileRead (FileOpen ($inputfileopendialog, 128)) EndIf If $splitbysentence = 6 Then $inputfile = StringReplace ($inputfile, ".", ".###") $inputfile = StringReplace ($inputfile, "!", "!###") $inputfile = StringReplace ($inputfile, "?", "?###") For $i = 1 to $abbreviationlist[0] $inputfile = StringReplace ($inputfile, $abbreviationlist[$i] & "###", $abbreviationlist[$i], 0, 1) Next $inputfile = StringReplace ($inputfile, "###", @CRLF) $inputfile = StringReplace ($inputfile, @CRLF & " ", @CRLF) EndIf $inputfile = $inputfile & @CRLF ; kludge, because now we're going to split by @CRLF $inputfilesplit = StringSplit ($inputfile, @CRLF, 1) $startrowindex = 1 If $inputfilesplit[0] > 9 Then $endrowindex = 10 Else $endrowindex = $inputfilesplit[0] EndIf $numofstrings = 0 $numberofrepeats = Int ($inputfilesplit[0] / 10) ; + 1 $finalitems = $inputfilesplit[0] - ($numberofrepeats * 10) For $j = 1 to $numberofrepeats + 1 $searchstring = "&q=" & _ArrayToString ($inputfilesplit, "&q=", $startrowindex, $endrowindex) If $j < $numberofrepeats Then $startrowindex = $startrowindex + 10 $endrowindex = $endrowindex + 10 ElseIf $j = $numberofrepeats Then $startrowindex = $startrowindex + 10 $endrowindex = $endrowindex + $finalitems EndIf If StringRight ($searchstring, 3) = "&q=" Then $searchstring = StringTrimRight ($searchstring, 3) EndIf $searchstring = StringToBinary ($searchstring, 4) $searchstring = StringTrimLeft ($searchstring, 2) $searchstring = StringRegExpReplace ($searchstring, "(..)", "%\1") $searchstring = StringReplace ($searchstring, "%26%71%3D", "&q=") If StringRight ($searchstring, 2) = "00" Then $searchstring = StringTrimRight ($searchstring, 2) EndIf $searchstring = StringReplace ($searchstring, "&q=&q=", "&q=") $searchstring = StringReplace ($searchstring, "&q=&q=", "&q=") $searchstring = StringReplace ($searchstring, "&q=&q=", "&q=") #cs All of this is not necessary because we now convert URLs. $searchstring = StringReplace ($searchstring, "%", "%25") $searchstring = StringReplace ($searchstring, "#", "%23") $searchstring = StringReplace ($searchstring, "<", "%3C") $searchstring = StringReplace ($searchstring, ">", "%3E") $searchstring = StringReplace ($searchstring, "&", "%26") $searchstring = StringReplace ($searchstring, "=", "%3D") $searchstring = StringReplace ($searchstring, "%26q%3D", "&q=") ; $searchstring = StringReplace ($searchstring, "&q=-", "&q=%2D") ; $searchstring = StringReplace ($searchstring, "$", "%24") ; $searchstring = StringReplace ($searchstring, "/", "%2F") ; $searchstring = StringReplace ($searchstring, "@", "%40") ; $searchstring = StringReplace ($searchstring, ":", "%3A") #ce $foo = InetRead ("https://www.googleapis.com/language/translate/v2?key=" & $apikey & "&source=" & $SL & "&target=" & $TL & $searchstring, 0) If @error Then $error = @error ClipPut ("https://www.googleapis.com/language/translate/v2?key=" & $apikey & "&source=" & $SL & "&target=" & $TL & $searchstring) MsgBox (0, "Error #" & $error, "Google is unhappy with the request. We've added the URL to the clipboard. Try pasting it in a browser and see if there's an error message. Also double-check if the URL you paste is the same as the URL that is left after the page is visited. After you click 'OK', it will no longer be on the clipboard.", 0) EndIf $foo = BinaryToString ($foo, 4) If StringInStr ($foo, '"errors": [') Then MsgBox (0, "Google throws an error...", $foo, 0) Else $foosplit = StringSplit ($foo, '"translatedText": "', 1) For $m = 2 to $foosplit[0] $foosplit2 = StringSplit ($foosplit[$m], '"', 1) $translation = $foosplit2[1] $translation = StringReplace ($translation, "\u200b", "") $translation = StringReplace ($translation, "'", "'") $translation = StringReplace ($translation, """, '"') $translation = StringReplace ($translation, "&", '&') $translation = StringReplace ($translation, "\u003c", '<') $translation = StringReplace ($translation, "\u003e", '>') $translation = StringReplace ($translation, "<", '<') $translation = StringReplace ($translation, ">", '>') $bigtranslation = $bigtranslation & $translation & @CRLF $numofstrings = $numofstrings + 1 TrayTip ("Progress...", $numofstrings & " done so far out of " & $inputfilesplit[0], 100) Next EndIf Next ; Then give user end-report $len = StringLen ($inputfile) $price = $len / 1000000 * 20 If $cliporno = 6 Then ClipPut ($bigtranslation) MsgBox (0, "Translation on clipboard", "The translation is on the clipboard." & @CRLF & "Estimated cost: USD " & $price, 0) Else $outputfile = FileOpen ($inputfileopendialog & "_output.txt", 129) FileWrite ($outputfile, $bigtranslation & @CRLF) MsgBox (0, "Translated file completed", $inputfilesplit[0] & " lines sent to Google, and translation saved to" & @CRLF & $inputfileopendialog & "_output.txt" & @CRLF & "Estimated cost: USD " & $price, 0) EndIf