def g_trans(lang, data): try: translate = Translator().translate #str = "mi nombre es gonsalves anthony" #decoded_string = str.encode('utf8', 'ignore').strip().replace("\'", r"\'") trans = translate(data, str(lang), 'en') #file = open('translated_text.txt', 'a') #pretty = pprint.pprint(trans) #file.write(pretty) pprint(trans.decode('string_escape').decode('utf8')) #file.close() except TranslationError, e: pprint(e)
""" translate.py Loop through the lines of a text file downloaded from the Internet. Convert each line from a sequence of bytes to a string of characters and translate them. need to run pip install googletrans on BMCC Mac, or py -3 -m pip install googletrans """ # Prob doesnt work yet, couldnt test it at work because of firewalls import sys import urllib.request from xgoogle.translate import Translator translator = Translator() language_code = """ Language Code -------- ---- Afrikaans af Albanian sq Arabic ar Belarusian be Bulgarian bg Catalan ca Chinese Simplified zh-CN Chinese Traditional zh-TW Croatian hr Czech cs Danish da Dutch nl English en Estonian et