forked from pythonguis/pythonguis-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.py
159 lines (132 loc) · 3.72 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from MainWindow import Ui_MainWindow
try:
from googletrans import Translator
GOOGLE_TRANSLATE_AVAILABLE = True
except ImportError:
GOOGLE_TRANSLATE_AVAILABLE = False
import json
from urllib import parse
import requests
LANGUAGES = {
'<Detect language>': None,
'Afrikaans': 'af',
'Albanian': 'sq',
'Arabic': 'ar',
'Azerbaijani': 'az',
'Basque': 'eu',
'Bengali': 'bn',
'Belarusian': 'be',
'Bulgarian': 'bg',
'Catalan': 'ca',
'Chinese Simplified': 'zh-CN',
'Chinese Traditional': 'zh-TW',
'Croatian': 'hr',
'Czech': 'cs',
'Danish': 'da',
'Dutch': 'nl',
'English': 'en',
'Esperanto': 'eo',
'Estonian': 'et',
'Filipino': 'tl',
'Finnish': 'fi',
'French': 'fr',
'Galician': 'gl',
'Georgian': 'ka',
'German': 'de',
'Greek': 'el',
'Gujarati': 'gu',
'Haitian Creole': 'ht',
'Hebrew': 'iw',
'Hindi': 'hi',
'Hungarian': 'hu',
'Icelandic': 'is',
'Indonesian': 'id',
'Irish': 'ga',
'Italian': 'it',
'Japanese': 'ja',
'Kannada': 'kn',
'Korean': 'ko',
'Latin': 'la',
'Latvian': 'lv',
'Lithuanian': 'lt',
'Macedonian': 'mk',
'Malay': 'ms',
'Maltese': 'mt',
'Norwegian': 'no',
'Persian': 'fa',
'Polish': 'pl',
'Portuguese': 'pt',
'Romanian': 'ro',
'Russian': 'ru',
'Serbian': 'sr',
'Slovak': 'sk',
'Slovenian': 'sl',
'Spanish': 'es',
'Swahili': 'sw',
'Swedish': 'sv',
'Tamil': 'ta',
'Telugu': 'te',
'Thai': 'th',
'Turkish': 'tr',
'Ukrainian': 'uk',
'Urdu': 'ur',
'Vietnamese': 'vi',
'Welsh': 'cy',
'Yiddish': 'yi'
}
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setupUi(self)
self.translator = Translator()
self.destTextEdit.setReadOnly(True)
if GOOGLE_TRANSLATE_AVAILABLE:
self.srcLanguage.addItems(LANGUAGES.keys())
self.srcLanguage.currentTextChanged[str].connect(self.update_src_language)
self.srcLanguage.setCurrentText('English')
else:
self.srcLanguage.hide()
self.translateButton.pressed.connect(self.translate)
self.show()
def update_src_language(self, l):
self.language_src = LANGUAGES[l]
def google_translate(self, text):
params = dict(
dest='en',
text=text
)
if self.language_src:
params['src'] = self.language_src
try:
tr = self.translator.translate(**params)
except Exception:
self.destTextEdit.setPlainText('Google translate error :(. Try translating from English')
return False
else:
return tr.text
def translate(self):
# Perform pre-translation to English via Google Translate.
if self.language_src != 'en':
text = self.google_translate(self.srcTextEdit.toPlainText())
if not text:
return False
# Already in English.
else:
text = self.srcTextEdit.toPlainText()
# Perform translation to piraat.
r = requests.get(
'http://api.funtranslations.com/translate/pirate.json?%s' %
parse.urlencode({'text': text})
)
data = json.loads(r.text)
if 'error' in data:
self.destTextEdit.setPlainText("%s\n\n%s" % (data['error']['message'], text))
else:
self.destTextEdit.setPlainText(data['contents']['translated'])
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
app.exec_()