pip3 install googletrans
pip3 list
pip3 show googletrans
Name: googletrans
Version: 2.4.0
Summary: Free Google Translate API for Python. Translates totally free of charge.
Home-page: https://github.com/ssut/py-googletrans
Author: SuHun Han
Author-email: ssut@ssut.me
License: MIT
Location: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages
Requires: requests
Required-by:
translate --help
translate --src en --dest es "Hello, how are you?"
[en] Hello, how are you?
->
[es] ¿Hola como estas?
[pron.] Hello, how are you?
Then read the documentation in the README.rst file.
"""
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.
"""
import sys
import urllib.request
import googletrans
print("We're going to translate 3 lines from \"Romeo and Juliet\" into another language that you select.")
print("Code Language")
print("---- --------")
for key, value in googletrans.LANGUAGES.items(): #googletrans.LANGUAGES is a dict
print(f"{key:5} {value}")
language = input("Please select the language code: ")
url = "http://oit2.scps.nyu.edu/~meretzkm/python/string/romeo.txt"
try:
lines = urllib.request.urlopen(url)
except urllib.error.URLError as error:
print(error, file = sys.stderr)
sys.exit(1)
translator = googletrans.Translator()
for line in lines: #line is a sequence of bytes.
try:
s = line.decode("utf-8")
except UnicodeError as error:
print(error, file = sys.stderr)
sys.exit(1)
translation = translator.translate(s, src = "en", dest = language)
print(translation.text)
lines.close()
sys.exit(0)
We're going to translate 3 lines from "Romeo and Juliet" into another language that you select. Code Language ---- -------- af afrikaans sq albanian am amharic ar arabic hy armenian etc. Please select the language code: es Bajo pena de tortura, de esas manos ensangrentadas Lanza tus armas apagadas al suelo, Y escucha la frase de tu príncipe movèd.
Please select the language code: iw על כאב עינויים, מאותן ידיים עקובות מדם זרוק את כלי הנשק הטעויות שלך ארצה, ושמע את המשפט של נסיך המובחר שלך.