Skip to content

Commit

Permalink
remove error links
Browse files Browse the repository at this point in the history
  • Loading branch information
taseikyo committed Dec 30, 2019
1 parent 542e7f8 commit 42ed150
Showing 1 changed file with 92 additions and 47 deletions.
139 changes: 92 additions & 47 deletions google-translate/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,44 @@
# @Version : Python3.6

from mwin import Ui_MWin

from PyQt5.QtCore import QTranslator, QUrl, QThread, pyqtSignal, Qt
from PyQt5.QtWidgets import QWidget, QApplication, QMainWindow, QFileDialog, QMessageBox
from PyQt5.QtGui import QDesktopServices, QPixmap
from PyQt5.QtGui import QDesktopServices, QPixmap
import sys
from googletrans import Translator
import re

GTransData = ''
GTransData = ""


class MyWindow(QMainWindow, Ui_MWin):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.setupUi(self)
try:
with open('style.qss') as f:
style = f.read() # 读取样式表
with open("style.qss") as f:
style = f.read() # 读取样式表
self.setStyleSheet(style)
except:
print("open stylesheet error")
self.originText.setFocus(True)
# Translator
self.trans = QTranslator()
# destination language
self.dest = 'zh-CN'
self.dest = "zh-CN"
# ui language : 0->zh-CN, 1->en
self.lan = 0
self.lan = 0
# real-time translate
self.isRealTimeTrans = False
self.isCopyFromTrans = False
self.connectSlots()

def connectSlots(self):
self.alwaysFront.clicked.connect(self.alwaysFrontFunc) # windows top
self.realTimeTrans.clicked.connect(self.realTimeTransFunc) # real-time translate
self.alwaysFront.clicked.connect(self.alwaysFrontFunc) # windows top
self.realTimeTrans.clicked.connect(
self.realTimeTransFunc
) # real-time translate

self.go.clicked.connect(self.transTextToZhCN)
self.go.setShortcut("CTRL+Return")
Expand All @@ -60,16 +63,30 @@ def connectSlots(self):
self.actionDestJapanese.triggered.connect(lambda: self.destinationLanguage(2))
self.actionDestKorean.triggered.connect(lambda: self.destinationLanguage(3))

self.about.triggered.connect(lambda: QDesktopServices.openUrl(QUrl('https://github.com/LewisTian/PyQt5-Apps/tree/PyQt5-Apps/google-translate')))
self.about.triggered.connect(
lambda: QDesktopServices.openUrl(
QUrl(
"https://github.com/taseikyo/PyQt5-Apps/tree/master/google-translate"
)
)
)
self.about.setShortcut("CTRL+H")
self.donate.triggered.connect(lambda: QDesktopServices.openUrl(QUrl('https://lewistian.github.io/2018/01/01/donate/')))
self.reportBug.triggered.connect(lambda: QDesktopServices.openUrl(QUrl('https://github.com/LewisTian/PyQt5-Apps/issues')))
self.donate.triggered.connect(
lambda: QDesktopServices.openUrl(
QUrl("https://github.com/taseikyo/PyQt5-Apps#donation")
)
)
self.reportBug.triggered.connect(
lambda: QDesktopServices.openUrl(
QUrl("https://github.com/taseikyo/PyQt5-Apps/issues")
)
)

def openFileSlot(self):
filename, filetype = QFileDialog.getOpenFileName(self, 'Open File', '.')
filename, filetype = QFileDialog.getOpenFileName(self, "Open File", ".")
if filename:
print(filename)
with open(filename, encoding = 'utf-8') as f:
with open(filename, encoding="utf-8") as f:
try:
self.originText.setPlainText(str(f.read()))
except Exception as e:
Expand All @@ -78,9 +95,11 @@ def openFileSlot(self):
def exportFileSlot(self):
if not self.transText.toPlainText():
return
filename, filetype = QFileDialog.getSaveFileName(self, 'Save File', '.', '*.txt;;*')
filename, filetype = QFileDialog.getSaveFileName(
self, "Save File", ".", "*.txt;;*"
)
if filename:
with open(filename, 'w', encoding = 'utf-8') as f:
with open(filename, "w", encoding="utf-8") as f:
try:
f.write(self.transText.toPlainText())
except Exception as e:
Expand All @@ -96,7 +115,7 @@ def changeLanguage(self, lan):
print("[MainWindow] Change to zh_CN")
self.trans.load("zh_CN")
elif lan == 1 and self.lan != 1:
self.lan =1
self.lan = 1
print("[MainWindow] Change to English")
self.trans.load("en")
else:
Expand All @@ -111,29 +130,40 @@ def destinationLanguage(self, lan):
change destination language
"""
if lan == 0:
self.dest = 'zh-CN'
self.dest = "zh-CN"
elif lan == 1:
self.dest = 'en'
self.dest = "en"
elif lan == 2:
self.dest = 'ja'
self.dest = "ja"
elif lan == 3:
self.dest = 'ko'
self.dest = "ko"
else:
self.dest = 'en'
self.dest = "en"
print(self.dest)

def transTextToZhCN(self):
text = self.originText.toPlainText()
if text:
if self.paperMode.isChecked(): # if paper mode is true, line breaks will re replaced by blanks
text = re.sub(r'\n|\s+', ' ', text)
text = re.sub(r'', '', text)
if (
self.paperMode.isChecked()
): # if paper mode is true, line breaks will re replaced by blanks
text = re.sub(r"\n|\s+", " ", text)
text = re.sub(r"", "", text)
# add on 19/05/16
text = text.replace('€', 'fi').replace('', 'ffi').replace('‚', 'ff').replace('ƒ', 'fl').replace('Œ', 'th').replace('‡', 'ft').replace('‰', 'ft').replace('Š', 'tt')
text = (
text.replace("€", "fi")
.replace("", "ffi")
.replace("‚", "ff")
.replace("ƒ", "fl")
.replace("Œ", "th")
.replace("‡", "ft")
.replace("‰", "ft")
.replace("Š", "tt")
)
self.originText.setPlainText(text)
try:
# self.transText.setPlainText(trans_To_zh_CN(text))
self.t=GTranslator(self.dest, text)
self.t = GTranslator(self.dest, text)
self.t.start()
self.transText.setPlainText("")
self.transText.setPlaceholderText("...")
Expand All @@ -155,7 +185,9 @@ def alwaysFrontFunc(self):
"""
if self.alwaysFront.isChecked():
# print("Front", self.windowFlags())
self.setWindowFlags(self.windowFlags() | Qt.WindowStaysOnTopHint) # always top
self.setWindowFlags(
self.windowFlags() | Qt.WindowStaysOnTopHint
) # always top
self.show()
else:
# print("Back", self.win.windowFlags())
Expand Down Expand Up @@ -185,16 +217,27 @@ def onClipboradChanged(self):
if text and self.isRealTimeTrans:
content = str(text)
# print(content)
content = content.replace('€', 'fi').replace('', 'ffi').replace('‚', 'ff').replace('ƒ', 'fl').replace('Œ', 'th').replace('‡', 'ft').replace('‰', 'ft').replace('Š', 'tt')
if self.paperMode.isChecked(): # if paper mode is true, line breaks will re replaced by blanks
content = re.sub(r'\n|\s+', ' ', content)
content = re.sub(r'', '', content)
content = (
content.replace("€", "fi")
.replace("", "ffi")
.replace("‚", "ff")
.replace("ƒ", "fl")
.replace("Œ", "th")
.replace("‡", "ft")
.replace("‰", "ft")
.replace("Š", "tt")
)
if (
self.paperMode.isChecked()
): # if paper mode is true, line breaks will re replaced by blanks
content = re.sub(r"\n|\s+", " ", content)
content = re.sub(r"", "", content)
self.originText.setPlainText(content)
self.transText.setPlainText(content)
try:
# data = trans_To_zh_CN(content)
# self.transText.setPlainText(data)
self.t=GTranslator(self.dest, content)
self.t = GTranslator(self.dest, content)
self.t.start()
self.transText.setPlainText("")
self.transText.setPlaceholderText("...")
Expand All @@ -203,29 +246,31 @@ def onClipboradChanged(self):
self.transText.setPlainText("error!")


class GTranslator(QThread):
class GTranslator(QThread):
trigger = pyqtSignal()

def __init__(self, dest, content):
super().__init__()
self.content = content
self.dest = dest
def run(self):

def run(self):
Data = []
global GTransData
T = Translator(service_urls=['translate.google.cn'])
T = Translator(service_urls=["translate.google.cn"])
# ts = T.translate(['The quick brown fox', 'jumps over', 'the lazy dog'], dest='zh-CN')
try:
ts = T.translate(self.content, dest=self.dest)
if isinstance(ts.text, list):
for i in ts:
Data.append(i.text)
GTransData = Data
else:
GTransData = ts.text
ts = T.translate(self.content, dest=self.dest)
if isinstance(ts.text, list):
for i in ts:
Data.append(i.text)
GTransData = Data
else:
GTransData = ts.text
except:
GTransData = 'An error happended. Please retry...'
self.trigger.emit() # emit signal once translati is finfished
GTransData = "An error happended. Please retry..."
self.trigger.emit() # emit signal once translatation is finfished


if __name__ == "__main__":
app = QApplication(sys.argv)
Expand Down

0 comments on commit 42ed150

Please sign in to comment.