Posts: 12,071
Threads: 140
Joined: Dec 2002
10-07-2022, 06:58 AM
(This post was last modified: 03-20-2024, 08:01 AM by Gintaras .)
Gets UI element text (or selection, OCR) and translates with Google Cloud Translation API. Displays the result in a tooltip.
Import the attached files.
Changes:
2023-05-28:
- Added OCR.
- Added code for exe program.
2023-06-01:
- Settings.
- Now it's a project folder.
- The script renamed.
2024-03-20:
- Can use Microsoft translator.
Attached Files
@Translate text.zip (Size: 10.44 KB / Downloads: 158)
Posts: 1,031
Threads: 246
Joined: Jul 2022
Translates the control text at the mouse pointer, Set the trigger by pressing the Ctrl key twice, Is this possible?
Posts: 1,031
Threads: 246
Joined: Jul 2022
thanks, the code Works well
Do I have to create a separate script file?
How to put the code into file @Triggers and toolbars -> Triggers -> Hotkey triggers.cs ,
I tried without success
Posts: 12,071
Threads: 140
Joined: Dec 2002
10-14-2022, 01:21 PM
(This post was last modified: 10-14-2022, 01:33 PM by Gintaras .)
Menu TT -> Other triggers. It opens a file with examples.
In that file add this somewhere inside the class:
[ Triggers]
void KeyboardHookTriggers () {
WindowsHook . Keyboard ( k => {
var a = _lastKeys ??= new ();
if ( a . Count > 15 ) a . RemoveAt( a . Count - 1 );
a . Insert( 0 , new ( k . IsUp , k . Key , k . Mod , k . vkCode , k . scanCode , k . IsExtended , k . IsInjected , k . IsAlt , k . time ));
//trigger LCtrl*2
if ( k . vkCode == KKey . LCtrl ) {
if ( k . IsUp && a . Count >= 4 && a [ 1 ] . vkCode == k . vkCode && ! a [ 1 ] . isUp && a [ 2 ] . vkCode == k . vkCode && a [ 2 ] . isUp && a [ 3 ] . vkCode == k . vkCode && ! a [ 3 ] . isUp && a [ 1 ] . time - a [ 3 ] . time < 500 && ( a . Count == 4 || a [ 4 ] . vkCode != k . vkCode || a [ 3 ] . time - a [ 4 ] . time > 500 )) {
//print.it("LCtrl*2");
script . run ( "Translate UI element text.cs" );
}
}
//Add other triggers here.
// To block the key event: k.BlockEvent();
//Note: this function must be as fast as possible. Slow code here will make kayboard input slow.
// If need slow code, put it in other script or thread, and here just start the script or thread.
});
}
record _KeyEvent ( bool isUp , KKey key , KMod mod , KKey vkCode , uint scanCode , bool isExtended , bool isInjected , bool isAlt , int time );
List < _KeyEvent > _lastKeys ; //max 16 previous key events, starting from the most recent, including current event
Posts: 12,071
Threads: 140
Joined: Dec 2002
Now QTranslate can translate UI elements too. Older versions translated only through Ctrl+C. But it has high-DPI problems.
Posts: 1,031
Threads: 246
Joined: Jul 2022
thanks
#6 The code works fine
QTranslate It will pop up, which is not very convenient
Posts: 1,031
Threads: 246
Joined: Jul 2022
11-29-2022, 06:43 AM
(This post was last modified: 11-29-2022, 06:55 AM by Davider .)
It's a bit strange, some pages on the forum can't be used by Google Translate. The translation did not take effect
e.g:
https://www.libreautomate.com/forum/show...1#pid36131
Posts: 12,071
Threads: 140
Joined: Dec 2002
11-29-2022, 07:53 AM
(This post was last modified: 11-29-2022, 08:01 AM by Gintaras .)
Tested, translates that forum page. Maybe depends on language, I don't know.
Posts: 1,031
Threads: 246
Joined: Jul 2022
Only some web pages cannot be translated
Posts: 12,071
Threads: 140
Joined: Dec 2002
11-29-2022, 09:25 AM
(This post was last modified: 11-29-2022, 09:26 AM by Gintaras .)
Don't know. I translate to Lithuanian and it works.
Maybe browser caching problems. Try to hard-reload the page (Ctrl+F5).
Posts: 1,031
Threads: 246
Joined: Jul 2022
Still doesn't work, I'll try again in a few days,
Thanks for your help
Posts: 95
Threads: 25
Joined: May 2023
05-28-2023, 05:29 AM
(This post was last modified: 05-28-2023, 05:58 AM by birdywen .)
Just want to throw a thank you to Gintaras, this script is so useful to me. Saved a lot of time to google translate and OCR.
Hi Gintaras, Can this script be complied to an exe? I think my friend will definitely love it. It's so useful and powerful.
Posts: 12,071
Threads: 140
Joined: Dec 2002
Quote: Can this script be complied to an exe?
Yes. Import the updated code.
Posts: 95
Threads: 25
Joined: May 2023
Hi Gintaras, I have tried to import the new code but it seems there was an error occurs. There is only one file in the folder which is named "Translate UI element text.cs", I think the other two were missed. Please have a look into the code. Thanks
Posts: 12,071
Threads: 140
Joined: Dec 2002
I forgot to post these files. Import the attached Classes.zip.
Posts: 95
Threads: 25
Joined: May 2023
Hi Gintaras, Could you please add a drop-down list to select source language and target language? It would be very helpful and flexible that way. Now I have to compile several different versions of exe? Thanks!
Posts: 12,071
Threads: 140
Joined: Dec 2002
Added settings dialog. Languages can be changed there.
Posts: 95
Threads: 25
Joined: May 2023
This is just amazing! LA is too powerful. You shouldn't take it as free!?
Posts: 1,031
Threads: 246
Joined: Jul 2022
03-20-2024, 02:45 AM
(This post was last modified: 03-20-2024, 02:46 AM by Davider .)
My network does not allow access to Google services. Can you add the Bing and OpenAI translation engines from the following link? Thank you.
https://www.libreautomate.com/forum/show...p?tid=7592
Posts: 12,071
Threads: 140
Joined: Dec 2002
Updated. Added Microsoft Translator.
OpenAI - no.
Microsoft OCR - no.