Python Language Translation using Google Translator
Published January 31, 2022The Google Translator tool is one of the most popular and free language translation tools. With Google Translator, you can translate a text from a particular language into a number of languages. As of now, the tool is available for developers, and its API is available for a number of programming languages. This means that a program can be written in one language and translated into many others.
In this post, we are going to look at how to translate languages in Python using the API tool from Google Translate. We will create a Python program that will translate a text written in English into Hindi.
Let’s go; just follow the steps below:
-
Step 1: Install the Google Translate API into your application by running the following command:
pip install googletrans==4.0.0rc1 |
![]() |
The command pip install googletrans must be run with the specified version; otherwise,, the program will install the googletrans-3.0.0 library, which causes a number of errors.
Now, let's test whether the Google Translate API that we installed is working properly. To do this, simply run the following code snippet in your Python IDE. It should display all languages that Google Translate supports.
|
Output:
Our Google Translator API works fine. Now, let’s proceed and create our Translating application.
Step 2: Initiate the Translator module and create the object translator
translator = Translator() |
Step 3: We'll need to collect the English-language text from the user and translate it to Hindi. Here are the lines to add to the code:
text = input("Enter your Text in English: ") source_lan = "en" translated_to= "hi" |
Step 4: Now, output the “Hindu” translated language and its pronunciation. To do this, add the following code:
print(f"English Language: {text}") print(f"Hindu Language: {translated_text.text}") print(f"Pronunciation {translated_text.pronunciation}") |
Step 5: Run your application, test it with an English phrase: "India is a very good country"
Code
import googletrans from googletrans import Translator translator = Translator() text = input("Enter English Text: ") source_lan = "en" translated_to= "hi" translated_text = translator.translate(text, src=source_lan, dest = translated_to) print(f"English Language: {text}") print(f"Hindu Language: {translated_text.text}") print(f"Pronunciation {translated_text.pronunciation}") |
Conclusion: In this python example we created Python Translator with Google Translator library.
Article Contributed By :
|
|
|
|
974 Views |