ist ja hier im Deutschen Bereich noch recht mau, da wollt ich auch mal was beisteuern
Ich hab im alten Forum ein Script gefunden welches den Google Translator nutzt um per Tooltip übersetzungen anzuzeigen. Ich habe dieses als Grundlage genommen und etwas angepasst, statt ToolTip ist es nun ein GUI die auch eine Möglichkeit zur Direkteingabe bietet.
Hier der Code:
Code: Select all
;============================================================================
; Translate text using translate.google.com
;
; Original author: Mikhail Kuropyatnikov ([email protected])
; Changed by fump2000
; Thanks to just me
;============================================================================
#NoEnv
#Persistent
#SingleInstance ignore
SetBatchLines, -1
OnMessage(0x201, "WM_LBUTTONDOWN") ; Necessary for Guis without caption.
CoordMode, Mouse, Screen
;============================================================================
;============================================================================
; To = die Sprache zu der im Grunde immer übersetzt werden soll.
; Anti = die Sprache zu der Übersetzt wird wenn Deutsch erkannt wird.
;============================================================================
^t::
TransText:=
TextToT:=GetTextFromClipboard()
IfWinExist, ahk_id %h_GetText%
{
GuiControl, GetText:,NewText,%TextToT%
GoSub NewTranslate
Return
}
To:="de"
Anti:="en"
GoSub StartTranslate
Return
;----------------------------------------------------------------------------
; Funktion um Inhalte zu kopieren.
;----------------------------------------------------------------------------
GetTextFromClipboard()
{
ClipSaved := ClipboardAll
Clipboard :=
SendInput, ^c
ClipWait, 0.5
If ErrorLevel
{
Clipboard := ClipSaved
ClipSaved =
Return
}
v := Clipboard
Clipboard := ClipSaved
ClipSaved :=
Return v
}
;----------------------------------------------------------------------------
; Wurde kein Inhalt kopiert wird nur die GUI aufgerufen.
;----------------------------------------------------------------------------
StartTranslate:
UseAnti:=0
TranslateTo:=To
if (TextToT="")
{
GoSub GetText
return
}
else
GoSub translate
Return
;----------------------------------------------------------------------------
; Startet die Übersetzung
;----------------------------------------------------------------------------
translate:
if (FromLang!=To)
TranslateTo := To
else
TranslateTo := Anti
UriCli := UriEncode(TextToT)
anti_translate:
Url := "http://translate.google.com/translate_a/t?client=x&text=" . UriCli . "&tl=" . TranslateTo
; Simulate Google Chrome
JsonTrans := UrlDownloadToVar(Url,"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/18.6.872.0 Safari/535.2")
; thanks to Grey (http://forum.script-coding.com/profile.php?id=25792)
StringReplace, JsonTrans, JsonTrans, \r\n,`r`n, All
StringReplace, JsonTrans, JsonTrans, \", ", All
FromLang:=JSON(JsonTrans, "src")
ElapsedTime:=JSON(JsonTrans, "server_time")
if (FromLang == To and TranslateTo <> Anti) ; Sollte die eingegeben Sprache = To sein wird neu in Anti übersetzt.
{
TranslateTo := Anti
GoSub anti_translate
Return
}
Loop ; Holt die Übersetzung aus dem String.
{
Sentence:=JSON(JsonTrans, "sentences["(A_Index-1)"].trans")
If Sentence
TransText.=Sentence
Else
Break
}
If (A_IsUnicode)
StrPutVar(TransText, TmpUTF8, "CP0")
Else
TmpUTF8 := TransText
TransText := StrGet(&TmpUTF8, "UTF-8") ; Codiert den ANSI Text in Unicode
TmpUTF8 := ""
StringReplace, TransText, TransText, \n, `n`r, 1
if (GuiOn=1)
GuiControl, GetText:,NewTransText,%TransText%
else
{
WithText:=1
Gosub GetText
}
Return
;----------------------------------------------------------------------------
; Gui zum anzeigen von übersetzen Text oder zur Eingabe von neuem Text.
;----------------------------------------------------------------------------
GetText:
MouseGetPos, TextX, TextY
Gui, GetText:Color, AAAAAA
Gui, GetText:+LastFound +AlwaysOnTop -Caption +ToolWindow +Border +HWNDh_GetText
Gui, GetText:Font, s10
Gui, GetText:Add, Text, x0 y3 w350 h17 +Center, Text der Übersetzt werden soll:
Gui, GetText:Add, Edit, x0 y20 w350 h60 cBlue vNewText +border gNewTranslate,%TextToT%
Gui, GetText:Add, Text, x0 y83 w350 h17 +Center +ReadOnly, Übersetzung:
Gui, GetText:Add, Edit, x0 y100 w350 h60 cGreen +border vNewTransText,%TransText%
Gui, GetText:Add, Text, x0 y163 w350 h17 vLine1, ESC = Schließen - Klicke ins Graue zum verschieben.
Gui, GetText:Font
Gui, GetText:Font, s8
Gui, GetText:Add, Text, x313 y163 w35 h16 +Border cWhite +Center gWithTextGuiClose, close
if (WithText=1)
GuiControl, GetText:Focus, Line1
else
GuiControl, GetText:Focus, NewText
Gui, GetText:Show, h180 w350 x%TextX% y%TextY%
Hotkey, Escape, WithTextGuiClose, on
Return
;----------------------------------------------------------------------------
; Bei der Eingabe von neuem Text wird direkt neu Übersetzt.
;----------------------------------------------------------------------------
NewTranslate:
GuiControl, GetText:,NewTransText,
GuiControlGet, NewText, GetText:, NewText
if (NewText="")
GuiControl, GetText:,NewTransText,
GuiOn:=1
TextToT:=NewText
TransText:=
GoSub translate
Return
;----------------------------------------------------------------------------
; Schließt die GUI und leert die Var's.
;----------------------------------------------------------------------------
WithTextGuiClose:
GuiOn:=0
WithText:=0
TransText:=
Gui, GetText:Destroy
Hotkey, Escape, WithTextGuiClose, off
Return
;----------------------------------------------------------------------------
; Lädt den Inhalt einer URL in eine Variable.
;----------------------------------------------------------------------------
UrlDownloadToVar(URL, UserAgent = "", Proxy = "", ProxyBypass = "") {
pFix:=a_isunicode ? "W" : "A"
hModule := DllCall("LoadLibrary", "Str", "wininet.dll")
AccessType := Proxy != "" ? 3 : 1
;INTERNET_OPEN_TYPE_PRECONFIG 0 // use registry configuration
;INTERNET_OPEN_TYPE_DIRECT 1 // direct to net
;INTERNET_OPEN_TYPE_PROXY 3 // via named proxy
;INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY 4 // prevent using java/script/INS
io := DllCall("wininet\InternetOpen" . pFix
, "Str", UserAgent ;lpszAgent
, "UInt", AccessType
, "Str", Proxy
, "Str", ProxyBypass
, "UInt", 0) ;dwFlags
iou := DllCall("wininet\InternetOpenUrl" . pFix
, "UInt", io
, "Str", url
, "Str", "" ;lpszHeaders
, "UInt", 0 ;dwHeadersLength
, "UInt", 0x80000000 ;dwFlags: INTERNET_FLAG_RELOAD = 0x80000000 // retrieve the original item
, "UInt", 0) ;dwContext
If (ErrorLevel != 0 or iou = 0) {
DllCall("FreeLibrary", "UInt", hModule)
return 0
}
VarSetCapacity(buffer, 10240, 0)
VarSetCapacity(BytesRead, 4, 0)
Loop
{
;http://msdn.microsoft.com/library/en-us/wininet/wininet/internetreadfile.asp
irf := DllCall("wininet\InternetReadFile", "UInt", iou, "UInt", &buffer, "UInt", 10240, "UInt", &BytesRead)
VarSetCapacity(buffer, -1) ;to update the variable's internally-stored length
BytesRead_ = 0 ; reset
Loop, 4 ; Build the integer by adding up its bytes. (From ExtractInteger-function)
BytesRead_ += *(&BytesRead + A_Index-1) << 8*(A_Index-1) ;Bytes read in this very DllCall
; To ensure all data is retrieved, an application must continue to call the
; InternetReadFile function until the function returns TRUE and the lpdwNumberOfBytesRead parameter equals zero.
If (irf = 1 and BytesRead_ = 0)
break
Else ; append the buffer's contents
{
a_isunicode ? buffer:=StrGet(&buffer, "CP0")
Result .= SubStr(buffer, 1, BytesRead_ * (a_isunicode ? 2 : 1))
}
/* optional: retrieve only a part of the file
BytesReadTotal += BytesRead_
If (BytesReadTotal >= 30000) ; only read the first x bytes
break ; (will be a multiple of the buffer size, if the file is not smaller; trim if neccessary)
*/
}
DllCall("wininet\InternetCloseHandle", "UInt", iou)
DllCall("wininet\InternetCloseHandle", "UInt", io)
DllCall("FreeLibrary", "UInt", hModule)
Return Result
}
;----------------------------------------------------------------------------
; Nimmt das URL Ergebnis auseinander und holt aus dem String die Übersetzung
; bzw. die source Sprache. Sollte die source Sprache = de sein wird erneut
; übersetzt ins Englische.
;----------------------------------------------------------------------------
JSON(ByRef js, s, v = "") {
j = %js%
Loop, Parse, s, .
{
p = 2
RegExMatch(A_LoopField, "([+\-]?)([^[]+)((?:\[\d+\])*)", q)
Loop {
If (!p := RegExMatch(j, "(?<!\\)(""|')([^\1]+?)(?<!\\)(?-1)\s*:\s*((\{(?:[^{}]++|(?-1))*\})|(\[(?:[^[\]]++|(?-1))*\])|"
. "(?<!\\)(""|')[^\7]*?(?<!\\)(?-1)|[+\-]?\d+(?:\.\d*)?|true|false|null?)\s*(?:,|$|\})", x, p))
Return
Else If (x2 == q2 or q2 == "*") {
j = %x3%
z += p + StrLen(x2) - 2
If (q3 != "" and InStr(j, "[") == 1) {
StringTrimRight, q3, q3, 1
Loop, Parse, q3, ], [
{
z += 1 + RegExMatch(SubStr(j, 2, -1), "^(?:\s*((\[(?:[^[\]]++|(?-1))*\])|(\{(?:[^{\}]++|(?-1))*\})|[^,]*?)\s*(?:,|$)){" . SubStr(A_LoopField, 1) + 1 . "}", x)
j = %x1%
}
}
Break
}
Else p += StrLen(x)
}
}
If v !=
{
vs = "
If (RegExMatch(v, "^\s*(?:""|')*\s*([+\-]?\d+(?:\.\d*)?|true|false|null?)\s*(?:""|')*\s*$", vx)
and (vx1 + 0 or vx1 == 0 or vx1 == "true" or vx1 == "false" or vx1 == "null" or vx1 == "nul"))
vs := "", v := vx1
StringReplace, v, v, ", \", All
js := SubStr(js, 1, z := RegExMatch(js, ":\s*", zx, z) + StrLen(zx) - 1) . vs . v . vs . SubStr(js, z + StrLen(x3) + 1)
}
Return, j == "false" ? 0 : j == "true" ? 1 : j == "null" or j == "nul"
? "" : SubStr(j, 1, 1) == """" ? SubStr(j, 2, -1) : j
}
;----------------------------------------------------------------------------
; HTML encode/decode
;----------------------------------------------------------------------------
StrPutVar(string, ByRef var, encoding)
{
; Ensure capacity.
SizeInBytes := VarSetCapacity( var, StrPut(string, encoding)
; StrPut returns char count, but VarSetCapacity needs bytes.
* ((encoding="utf-16"||encoding="cp1200") ? 2 : 1) )
; Copy or convert the string.
StrPut(string, &var, encoding)
Return SizeInBytes
}
UriEncode(str)
{
b_Format := A_FormatInteger
data := ""
SetFormat,Integer,H
SizeInBytes := StrPutVar(str,var,"utf-8")
Loop, %SizeInBytes%
{
ch := NumGet(var,A_Index-1,"UChar")
If (ch=0)
Break
if ((ch>0x7f) || (ch<0x30) || (ch=0x3d))
s .= "%" . ((StrLen(c:=SubStr(ch,3))<2) ? "0" . c : c)
Else
s .= Chr(ch)
}
SetFormat,Integer,%b_format%
return s
}
;============================================================================
WM_LBUTTONDOWN()
{
if (A_Gui="GetText")
{
If (A_GuiControl = "") ; Klick auf den Hintergrund der GUI
PostMessage, 0xA1, 2, 0 ; sehr sehr alter Trick von SKAN: 0xA1 = WM_NCLBUTTONDOWN
}
}
Return
Zur Funktion:
Markiere einen Text (egal welche Sprache) und drücke STRG+T, Der Text wird kopiert und übersetzt und anschließend in der GUI dargestellt.
die GUI kann aber auch ohne markierten Text geöffnet werden. Dann einfach Text eingeben und on the fly die Übersetzung erhalten.
Ich hoffe es findet Anwendung
Hier noch ein Bild der GUI:
MfG
fump