import time from googletrans import Translator from PyQt5.QtCore import QThread, pyqtSignal from PyQt5.QtWidgets import (QApplication, QDialog, QProgressBar, QPushButton,QLabel) from PyQt5 import QtWidgets from PyQt5.QtCore import * import logging import ctypes, sys import chardet def is_admin(): try: return ctypes.windll.shell32.IsUserAnAdmin() except: return False if is_admin(): print(is_admin()) pass else: if sys.version_info[0] == 3: ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1) logging.basicConfig(level=logging.ERROR, filename='py-tran-log.txt', filemode='w', format='[%(asctime)s %(levelname)-8s] %(message)s', datefmt='%Y%m%d %H:%M:%S', ) try: translator = Translator(service_urls=['translate.googleapis.com']) try: with open("./C.txt","rb") as k: dd = chardet.detect(k.read()) print(dd['encoding']) file3 = open('./C.txt','r',encoding=str(dd['encoding'])) try: data3 = file3.readlines()[0] except: data3='是繁體中文' file3.close() except Exception as e: data3 = '是繁體中文' logging.error(e) print(data3) try: file1 = open('./A.txt','r',encoding=dd['encoding']) try: data1 = file1.read() except Exception as e: logging.error(e) data1 = 'None' file1.close() except Exception as e: logging.error(e) data1 = 'None' print(data1) if data3=='是繁體中文' and data1 != 'None': result = translator.translate(data1,dest='id').text result2 = translator.translate(data1,dest='en').text file2 = open('./ch.txt','w+',encoding= 'utf-8') file2.write(data1) file2.close() file4 = open('./en.txt','w+',encoding= 'utf-8') file4.write(result2) file4.close() file5 = open('./in.txt','w+',encoding= 'utf-8') file5.write(result) file5.close() elif data3=='不是繁體中文' and data1 != 'None': result = translator.translate(data1,dest='zh-tw').text result2 = translator.translate(data1,dest='en').text file2 = open('./in.txt','w+',encoding= 'utf-8') file2.write(data1) file2.close() file4 = open('./ch.txt','w+',encoding= 'utf-8') file4.write(result) file4.close() file5 = open('./en.txt','w+',encoding= 'utf-8') file5.write(result2) file5.close() else: app2 = QtWidgets.QApplication([]) error_dialog = QtWidgets.QErrorMessage() error_dialog.setWindowTitle('翻譯失敗') error_dialog.showMessage('翻譯失敗請重新嘗試!! / Terjemahan gagal, silakan coba lagi!!') app2.exec_() except Exception as e: app2 = QtWidgets.QApplication([]) error_dialog = QtWidgets.QErrorMessage() error_dialog.setWindowTitle('ERROR') error_dialog.showMessage('翻譯失敗請重新嘗試!! / Terjemahan gagal, silakan coba lagi!!') logging.error(e) app2.exec_()