-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.py
47 lines (38 loc) · 1.23 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
47
from googletrans import Translator
import json, sys, keyboard
# open input json file
termsFile = open('terms.json')
terms = json.load(termsFile)
# read which languages from input
src = sys.argv[1] if len(sys.argv) > 1 else None
if not bool(src):
src = input('Which language would you like to translate your words from? (it) ') or 'it'
dest = sys.argv[2] if len(sys.argv) > 2 else None
if not bool(dest):
dest = input('In which language would you like to translate to? (en) ') or 'en'
# prepare elaboration
translator = Translator()
translations = dict()
withErrors = 0
continueElaboration = True
# prepare quitting
def setQuitExecution():
print('User interrupt', file=sys.stderr)
global continueElaboration
continueElaboration = False
keyboard.add_hotkey("ctrl+q", setQuitExecution)
# execute elaboration
for key, value in terms.items():
if not continueElaboration:
break
try:
translated = translator.translate(value, src=src, dest=dest)
except Exception as e:
print(e, file=sys.stderr)
withErrors = 1
translations[key] = translated.text
print('Translating: ' + key, file=sys.stderr, end='\r')
#print result
print(json.dumps(translations))
#quit execution
sys.exit(withErrors)