Sign in to follow this  
Followers 0

TransType (Translate as you type)

9 posts in this topic

#1 ·  Posted (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:

#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 = ""


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
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
            $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 = ""
    ElseIf ($keycode > 159) And ($keycode < 164) Then
    ElseIf ($keycode = 27) Then ; esc key
        $buffer = ""
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)
    If $wParam = $WM_KEYDOWN Then
        EvaluateKey(DllStructGetData($tKEYHOOKS, "vkCode"))
        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)
    Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndFunc   ;==>_KeyProc

Func OnAutoItExit()
EndFunc   ;==>OnAutoItExit

Func _GoogleTranslate($sText, $sFrom = "en", $sTo = "ja")
    Local Const $FileName = "Translation.jsn"
    Local Const $Pattern = '"translatedText":"([^"]+)"'
    Local $GoogleURL = ""
    Local $File, $Translation
    $GoogleURL = StringFormat($GoogleURL, $sText, $sFrom, $sTo)

    If Not InetGet($GoogleURL, $FileName, 1) Then Return SetError(1, 0, 0)

    $File = FileOpen($FileName, 4)
    $Translation = FileRead($File, FileGetSize($FileName))

    $Translation = BinaryToString($Translation, 4)
    If StringRegExp($Translation , $Pattern) Then
        $Translation = StringRegExp($Translation , $Pattern, 1)
        $Translation = $Translation[0]

        Return $Translation
Edited by IchBistTod


Share this post

Link to post
Share on other sites

This is really cool! I see it only works with a period though. Really great job!

Share this post

Link to post
Share on other sites

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.

[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at[/size][/size][/font]

Share this post

Link to post
Share on other sites

#5 ·  Posted (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 by IchBistTod


Share this post

Link to post
Share on other sites

#6 ·  Posted (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 by IchBistTod


Share this post

Link to post
Share on other sites

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!

Share this post

Link to post
Share on other sites

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 but a lot of work.


Share this post

Link to post
Share on other sites

I agree phrase by phrase and sentence by sentence will be the best way to translate..Posted Image

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  
Followers 0