1. 程式人生 > >python google翻譯

python google翻譯

google_translate.py

#coding=utf-8
from translate import Translator
import threading
import Queue

listQueue=Queue.Queue()



def getTranslateStr(text,toLang):
    translator= Translator(toLang)
    translation = translator.translate(text)
    return translation
def rank():
    TranslateLangList = ["en"
, "zh", "am", "ar", "bn", "es", "fa", "fr", "gu", "ha", "hi", "km", "lo", "my", "ne", "pt", "ru", "so", "sw", "th", "tl", "ur", "vi"] TranslateLangList1=["Indonesian","ms","om"] for toLang in TranslateLangList: listQueue.put(toLang) def doWork(): while True
: toLang=listQueue.get() try: print toLang+"-->"+getTranslateStr(u"turn off",toLang) except Exception as e: pass #print (e) listQueue.task_done() class MyThread(threading.Thread): def __init__(self,func): threading.Thread.__init__(self) self.func=func def
run(self):
self.func() if __name__=="__main__": rank() for i in range(500): mThread = MyThread(doWork) mThread.setDaemon(True) mThread.start() listQueue.join()