In this post we will be creating a Python script that will translate English text to Spanish. To do this we will be using the Googletrans library, this library makes it easy to translate text from one language to another. It uses the Google Translate API and supports over 100 languages.
At the time of writing this there are issues affecting the library although you can use an alternative version of the library. To install it run the following in command line.
pip install googletrans==3.1.0a0
Fun Fact: Did you know that Spanish is one of the most widely spoken and studied languages in the world? With over 460 million native speakers, it ranks as the second most widely spoken language globally, after Mandarin Chinese. Additionally, it’s the third most commonly studied language in the world, after English and French.
See the snippet of Python code below where we translate English text to Spanish. For this example we will be taking user input which will be in English and return the translation in Spanish to output.
from googletrans import Translator while True: translator = Translator() text=input("Enter text: ") translation = translator.translate(text, dest='es') print(translation.text,"n")
An example of the script running can be seen below.
Enter text: hello Hola Enter text: whats your name cuál es tu nombre Enter text: my name is Jennifer Mi nombre es Jennifer >>>
Take a look at some of our other content around the Python programming language by clicking here.