0%

Octrans

Octrans是一款基于百度api的翻译软件,由本人自制,功能一般,bug也很多,欢迎各位指正!

当前版本:V2.1

下载地址:https://pan.baidu.com/s/1N8ADTEyUZFUs2NYZceDkmA

提取码:hgqq

说明

V1.0

  • 使用PyQt5编写了简单的图形界面
  • 可以选择翻译使用的语言与系统语言
  • 实现单行文字的翻译

V2.0

  • 通过在输入栏打入多行文字或导入txt文件进行翻译

  • 可以将翻译结果导出到txt文件

V2.1

  • 优化了系统语言设置
  • 为文件的保存和读取设置了快捷键

V2.2

  • 将应用程序进一步封装,并隐藏了控制台
  • 修改了版本显示错误

源码

UI部分

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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from PyQt5 import QtCore, QtGui, QtWidgets
import os

cndic={
"汉语":'zh',
"英语":'en',
"粤语":'yue',
"文言文":'wyw',
"法语":'fra',
"德语":'de',
"日语":'jp',
"韩语":'kor',
"西班牙语":'spa',
"葡萄牙语":'pt',
"意大利语":'it',
"阿拉伯语":'ara',
}
endic={
"Mandarin":'zh',
"English":'en',
"Cantonese":'yue',
"Classical Chinese":'wyw',
"French":'fra',
"German":'de',
"Japanese":'jp',
"Korean":'kor',
"Spanish":'spa',
"Portuguese":'pt',
"Italian":'it',
"Arabic":'ara',
}

ch_intro="欢迎使用Octrans!这是一个基于百度api的简易翻译器,支持多种语言的翻译,感谢您的使用和支持!"
en_intro="Welcome to Octrans! This is a simle translator based on Baidu api, which supports translation between varies of languages. Thanks for using!"

class Ui_Octrans(object):
def setupUi(self, Octrans):
Octrans.setObjectName("Octrans")
Octrans.resize(780, 587)
self.centralwidget = QtWidgets.QWidget(Octrans)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.lanchoice = QtWidgets.QComboBox(self.centralwidget)
self.lanchoice.setObjectName("lanchoice")
self.lanchoice.addItem("")
self.lanchoice.addItem("")
self.lanchoice.addItem("")
self.lanchoice.addItem("")
self.lanchoice.addItem("")
self.lanchoice.addItem("")
self.lanchoice.addItem("")
self.lanchoice.addItem("")
self.lanchoice.addItem("")
self.lanchoice.addItem("")
self.lanchoice.addItem("")
self.lanchoice.addItem("")
self.gridLayout.addWidget(self.lanchoice, 1, 0, 1, 1)
self.inptext = QtWidgets.QTextEdit(self.centralwidget)
self.inptext.setObjectName("inptext")
self.gridLayout.addWidget(self.inptext, 2, 0, 1, 1)
self.transbut = QtWidgets.QPushButton(self.centralwidget)
self.transbut.setObjectName("transbut")
self.gridLayout.addWidget(self.transbut, 3, 0, 1, 1)
self.introtext = QtWidgets.QLabel(self.centralwidget)
self.introtext.setTextFormat(QtCore.Qt.MarkdownText)
self.introtext.setObjectName("introtext")
self.gridLayout.addWidget(self.introtext, 0, 0, 1, 1)
self.outext = QtWidgets.QTextEdit(self.centralwidget)
self.outext.setObjectName("outext")
self.gridLayout.addWidget(self.outext, 4, 0, 1, 1)
Octrans.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(Octrans)
self.menubar.setGeometry(QtCore.QRect(0, 0, 780, 22))
self.menubar.setObjectName("menubar")
self.Filem = QtWidgets.QMenu(self.menubar)
self.Filem.setObjectName("Filem")
self.settingm = QtWidgets.QMenu(self.menubar)
self.settingm.setObjectName("settingm")
self.menulanm = QtWidgets.QMenu(self.settingm)
self.menulanm.setObjectName("menulanm")
self.Instm = QtWidgets.QMenu(self.menubar)
self.Instm.setObjectName("Instm")
Octrans.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(Octrans)
self.statusbar.setObjectName("statusbar")
Octrans.setStatusBar(self.statusbar)
self.openm = QtWidgets.QAction(Octrans)
self.openm.setObjectName("openm")
self.savem = QtWidgets.QAction(Octrans)
self.savem.setObjectName("savem")
self.chm = QtWidgets.QAction(Octrans)
self.chm.setObjectName("chm")
self.enm = QtWidgets.QAction(Octrans)
self.enm.setObjectName("enm")
self.Filem.addAction(self.openm)
self.Filem.addAction(self.savem)
self.menulanm.addAction(self.chm)
self.menulanm.addAction(self.enm)
self.settingm.addAction(self.menulanm.menuAction())
self.menubar.addAction(self.Filem.menuAction())
self.menubar.addAction(self.settingm.menuAction())

self.retranslateUi(Octrans)
QtCore.QMetaObject.connectSlotsByName(Octrans)

def retranslateUi(self, Octrans):
_translate = QtCore.QCoreApplication.translate
Octrans.setWindowTitle(_translate("Octrans", "Octrans-Version 2.0"))
self.lanchoice.setItemText(0, _translate("Octrans", "汉语"))
self.lanchoice.setItemText(1, _translate("Octrans", "英语"))
self.lanchoice.setItemText(2, _translate("Octrans", "粤语"))
self.lanchoice.setItemText(3, _translate("Octrans", "文言文"))
self.lanchoice.setItemText(4, _translate("Octrans", "法语"))
self.lanchoice.setItemText(5, _translate("Octrans", "德语"))
self.lanchoice.setItemText(6, _translate("Octrans", "日语"))
self.lanchoice.setItemText(7, _translate("Octrans", "韩语"))
self.lanchoice.setItemText(8, _translate("Octrans", "西班牙语"))
self.lanchoice.setItemText(9, _translate("Octrans", "葡萄牙语"))
self.lanchoice.setItemText(10, _translate("Octrans", "意大利语"))
self.lanchoice.setItemText(11, _translate("Octrans", "阿拉伯语"))
self.transbut.setText(_translate("Octrans", "翻译"))
self.introtext.setText(_translate("Octrans", ch_intro))
self.introtext.setWordWrap(True)
self.Filem.setTitle(_translate("Octrans", "文件"))
self.settingm.setTitle(_translate("Octrans", "设置"))
self.Instm.setTitle(_translate("Octrans","说明"))
self.menulanm.setTitle(_translate("Octrans", "系统语言"))
self.openm.setText(_translate("Octrans", "打开"))
self.openm.setShortcut('ctrl+d')
self.savem.setText(_translate("Octrans", "保存"))
self.savem.setShortcut('ctrl+s')
self.chm.setText(_translate("Octrans", "中文"))
self.enm.setText(_translate("Octrans", "English"))

def userlangchoice(self):
if (self.lanchoice.currentText() in cndic)==True:
return cndic[self.lanchoice.currentText()]
if (self.lanchoice.currentText() in endic)==True:
return endic[self.lanchoice.currentText()]

def set_sys_ch(self):
self.Filem.setTitle("文件")
self.settingm.setTitle("设置")
self.menulanm.setTitle("系统语言")
self.Instm.setTitle("说明")
self.openm.setText("打开")
self.savem.setText("保存")
self.introtext.setText(ch_intro)
self.transbut.setText("翻译")
self.lanchoice.setItemText(0, "汉语")
self.lanchoice.setItemText(1, "英语")
self.lanchoice.setItemText(2, "粤语")
self.lanchoice.setItemText(3, "文言文")
self.lanchoice.setItemText(4, "法语")
self.lanchoice.setItemText(5, "德语")
self.lanchoice.setItemText(6, "日语")
self.lanchoice.setItemText(7, "韩语")
self.lanchoice.setItemText(8, "西班牙语")
self.lanchoice.setItemText(9, "葡萄牙语")
self.lanchoice.setItemText(10, "意大利语")
self.lanchoice.setItemText(11, "阿拉伯语")

def set_sys_en(self):
self.Filem.setTitle("File")
self.settingm.setTitle("Setting")
self.menulanm.setTitle("System Language")
self.Instm.setTitle("Instruction")
self.openm.setText("open")
self.savem.setText("save")
self.introtext.setText(en_intro)
self.transbut.setText("Translate")
self.lanchoice.setItemText(0, "Mandarin")
self.lanchoice.setItemText(1, "English")
self.lanchoice.setItemText(2, "Cantonese")
self.lanchoice.setItemText(3, "Classical Chinese")
self.lanchoice.setItemText(4, "French")
self.lanchoice.setItemText(5, "German")
self.lanchoice.setItemText(6, "Japanese")
self.lanchoice.setItemText(7, "Korean")
self.lanchoice.setItemText(8, "Spanish")
self.lanchoice.setItemText(9, "Portuguese")
self.lanchoice.setItemText(10, "Italian")
self.lanchoice.setItemText(11, "Arabic")

def print_text(self):
user_input = self.inptext.toPlainText()
return(user_input)

def openfile(self):
filename,_=QtWidgets.QFileDialog.getOpenFileName(self.centralwidget,'choose a file',os.getcwd(),"Text Files(*.txt)")
if filename:
try:
with open(filename, 'r', encoding='utf-8') as f:
data = f.read()
except Exception:
data=None
if data is not None:
self.inptext.setText(data)
else:
data=None
return data
f.close()

def savefile(self):
filename,_=QtWidgets.QFileDialog.getOpenFileName(self.centralwidget,'choose a file',os.getcwd(),"Text Files(*.txt)")
if filename:
with open(filename,'w',encoding='utf-8') as f:
data=self.outext.toPlainText()
f.write(data)
f.close()

主程序部分

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
import sys
import requests
from hashlib import md5
import json
import random
from PyQt5.QtWidgets import QApplication, QMainWindow

import translate

appid = '20230508001670915'
appkey = 'sx9jtxYrNpwcMeWl2TjI'

from_lang ='auto'

endpoint = 'http://api.fanyi.baidu.com'
path = '/api/trans/vip/translate'
url = endpoint + path

def make_md5(s, encoding='utf-8'):
return md5(s.encode(encoding)).hexdigest()

def trans(userinputs):
for userinput in userinputs.split('\n'):
salt = random.randint(32768, 65536)
sign = make_md5(appid + userinput + str(salt) + appkey)
# Build request
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
payload = {'appid': appid, 'q': userinput, 'from': from_lang, 'to': to_lang, 'salt': salt, 'sign': sign}
# Send request
r = requests.post(url, params=payload, headers=headers)
result = json.loads(r.text)
if 'trans_result' in result:
trans_result = result['trans_result']
for item in trans_result:
if 'dst' in item:
dst = item['dst']
# Show response
ui.outext.append(dst)

def makesure_trans():
userinputs=ui.print_text()
global to_lang
to_lang=ui.userlangchoice()
ui.outext.setText('')
trans(userinputs)

if __name__ == '__main__':
app = QApplication(sys.argv)
Octrans = QMainWindow()
ui = translate.Ui_Octrans()
ui.setupUi(Octrans)
Octrans.show()

ui.chm.triggered.connect(ui.set_sys_ch)
ui.enm.triggered.connect(ui.set_sys_en)
ui.openm.triggered.connect(ui.openfile)
ui.savem.triggered.connect(ui.savefile)
ui.transbut.clicked.connect(makesure_trans)

sys.exit(app.exec_())