IchBistTod Posted July 12, 2009 Posted July 12, 2009 (edited) I developed this nifty little app to translate as you type. I determined it is best to translate at every period, because more literal translations come from whole sentences than single words at a time. Tested with notepad and it works pretty well, its just a prototype for now. Anyone can improve it. =] I will be fixing some of the bugs and making it detect other characters soon(if someone else hasnt already) I noticed this wont work with MSN windows..... Help with this would be appreciated. It uses a google translate udf... forget from who...... currently is set for german but can be changed. updated code: expandcollapse popup#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 #include <WinAPI.au3> #include <WindowsConstants.au3> #include <StructureConstants.au3> ;Opt('MustDeclareVars', 1) Global $hHook, $hStub_KeyProc, $buffer = "" _Main() Func _Main() Local $hmod $hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam") $hmod = _WinAPI_GetModuleHandle(0) $hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($hStub_KeyProc), $hmod) While 1 Sleep(10) WEnd EndFunc ;==>_Main Func EvaluateKey($keycode) If (($keycode > 64) And ($keycode < 91)) _ ; a - z Or (($keycode > 96) And ($keycode < 123)) _ ; A - Z Or (($keycode > 30) And ($keycode < 58)) or $keycode = 190 Then ; 0 - 9 $buffer &= Chr($keycode) Switch $keycode case 190 ;ConsoleWrite($buffer&@CRLF) $tran = _GoogleTranslate(StringLower($buffer), "en", "de") $text = ControlGetText("", "", "") $newtext = StringReplace($text, StringTrimright($buffer, 1), " "&$tran) ControlSetText("", "", "", "") ControlSend("", "", "", $newtext) ;~ for $i=1 to StringLen($newtext) ;~ ControlSend("", "", "","{right}") ;~ Next $buffer = "" EndSwitch ElseIf ($keycode > 159) And ($keycode < 164) Then Return ElseIf ($keycode = 27) Then ; esc key Exit Else $buffer = "" EndIf EndFunc ;==>EvaluateKey ;=========================================================== ; callback function ;=========================================================== Func _KeyProc($nCode, $wParam, $lParam) Local $tKEYHOOKS $tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam) If $nCode < 0 Then Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam) EndIf If $wParam = $WM_KEYDOWN Then EvaluateKey(DllStructGetData($tKEYHOOKS, "vkCode")) Else Local $flags = DllStructGetData($tKEYHOOKS, "flags") ; Switch $flags ; Case $LLKHF_ALTDOWN ; ConsoleWrite("$LLKHF_ALTDOWN" & @LF) ; Case $LLKHF_EXTENDED ; ConsoleWrite("$LLKHF_EXTENDED" & @LF) ; Case $LLKHF_INJECTED ; ConsoleWrite("$LLKHF_INJECTED" & @LF) ; Case $LLKHF_UP ; ConsoleWrite("$LLKHF_UP: scanCode - " & DllStructGetData($tKEYHOOKS, "scanCode") & @TAB & "vkCode - " & DllStructGetData($tKEYHOOKS, "vkCode") & @LF) ;EndSwitch EndIf Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam) EndFunc ;==>_KeyProc Func OnAutoItExit() _WinAPI_UnhookWindowsHookEx($hHook) DllCallbackFree($hStub_KeyProc) EndFunc ;==>OnAutoItExit 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, $Translation ;ConsoleWrite($sText&@CRLF) $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) ;ConsoleWrite($Translation&@CRLF) If StringRegExp($Translation , $Pattern) Then $Translation = StringRegExp($Translation , $Pattern, 1) $Translation = $Translation[0] Return $Translation EndIf EndFunc Edited July 12, 2009 by IchBistTod [center][/center][center]=][u][/u][/center][center][/center]
James Posted July 12, 2009 Posted July 12, 2009 What version are you using? Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ
gseller Posted July 12, 2009 Posted July 12, 2009 This is really cool! I see it only works with a period though. Really great job!
dantay9 Posted July 12, 2009 Posted July 12, 2009 It does only work with a period. I beleive there are better translators on the web such as babelfish. You could then use more languages and translate a little better. I did find that your translator is not always consistant, even when inputting the same words. Other than that, very good concept. A little fine tuning and this will be really cool and useful.
IchBistTod Posted July 12, 2009 Author Posted July 12, 2009 (edited) you don't seem to understand(not in a bad way), you cant translate word by word, you must do it phrase by phrase or sentence by sentence, hence, why it only works period by period. And yes it is consistent, words change because different words are used in other languages, there can be 12 different ways to say "you" in german based on how your using it. =] It most be done sentence by sentence because other languages are not like english, you must say words in certain orders, and use certain forms of words based on what your talking about, its gender, it place in time, other verbs and nouns you are using it with, what it is referring to, and other factors, or its basically unreadable. Also I am using the latest version of autoit... and In studies google is a much better translator, I know because I am German, it has translated into german much better than other translators because it obeys definite articles, verb/noun usage , ect. Edited July 12, 2009 by IchBistTod [center][/center][center]=][u][/u][/center][center][/center]
IchBistTod Posted July 12, 2009 Author Posted July 12, 2009 (edited) Updated code to preserve spaces in between sentences, and to clear the control text then send it the new text rather than setting the text to new text and moving over(new method faster and more reliable). Also to change the language just change the third param of the translate function on line 35, fr=french, ja=japanese, etc. And I plan on changing from using the web per translation to using it on a dictionary file(large file of thousands to milliosn of words, usualy used for brute force attacks) in order to make several .ini files that will be used for translation. Edited July 12, 2009 by IchBistTod [center][/center][center]=][u][/u][/center][center][/center]
gseller Posted July 12, 2009 Posted July 12, 2009 Yes, very good.. I was only commenting cause one would just need to provision code to work also on exclamation, question marks, and such.. But anyone can do that for themself. Thank you for sharing your fine efforts... I like it!
IchBistTod Posted July 12, 2009 Author Posted July 12, 2009 Ah, i see what you were saying, I was assuming that you meant it should work word by word, and I suppose it could work word by word, if it used windows detection, multiple buffers in an array, and detected backspaces and such. Then it could translate word by word and when a certain phrase was reached it could translate all the words changing the translated text into how it should really be typed(proper form). Although the only real advantages I can see to this is it would look cool(because words would be constantly changing based on what is being typed) and being able to translate individual words without usinga period.... but that would be quite a bit of work...do-able but a lot of work. [center][/center][center]=][u][/u][/center][center][/center]
gseller Posted July 12, 2009 Posted July 12, 2009 I agree phrase by phrase and sentence by sentence will be the best way to translate..
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now