-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.py
48 lines (40 loc) · 1.62 KB
/
translate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import os
from papago import Translator
import pyautogui as pya
from pynput.keyboard import Key, Listener
import pyperclip
import time
from window import Window
from langdetect import detect
CLIENT_ID = "********************"
CLIENT_SECRET = "********"
translator = Translator(CLIENT_ID, CLIENT_SECRET)
class Translate:
def __init__(self):
self.get_selected()
self.decide_translate()
self.display() # we past the translated text, and the korean for naver dict access.
def decide_translate(self):
toTranslate = self.toTranslate
lang = detect(toTranslate) # Detect which language we want to translate
if lang == 'ko': # if korean, translate korean to english.
self.translated = translator.translate(toTranslate, 'ko', 'en').text
self.korean = toTranslate
else: # if english, translate english to korean
self.translated = translator.translate(toTranslate, 'en', 'ko').text
self.korean = self.translated
def get_selected(self):
pya.hotkey('ctrl', 'c')
time.sleep(.01) # ctrl-c is usually very fast but program may execute faster
self.toTranslate = pyperclip.paste()
def display(self):
self.opened = True
currentMouseX, currentMouseY = pya.position()
self.window = Window(self.translated, currentMouseX - 5, currentMouseY - 100)
self.window.run(self.korean) # passing korean so that we can click and go to naver dict
def on_press(key):
if key == Key.scroll_lock:
Translate()
while True:
with Listener(on_press=on_press) as listener:
listener.join()