Fortunately, you can communicate with the help of a translator, including a machine-based one. Type or speak your sentence and a program can convert it into your desired language. Using the Googletrans Python module, you can build your own basic translator with just a few lines of code.

What Is Googletrans?

Googletrans is a module that uses the Google Translate API to detect and translate sentences from one language to another. A few features of this module are:

Free: You don’t have to pay any cost to use this module. Unlimited: Unlike most APIs, Googletrans isn’t rate limited, and you can use it for unlimited translations. Reliable: Google is one of the most trustworthy sources on the internet. Googletrans uses the same servers that Google Translate uses. Automatic Language Detection: Unlike other translators, you need not specify the source language. This module can automatically detect a language and work even if you don’t know what language or dialect the sentence was from. Bulk Translations: Instead of translating a sentence or a paragraph one by one, you can input a list of several.

If you’re looking for an alternative, or just want to explore the current state of translation technology, you can use these browser tools to translate web pages.

Finding the List of Languages That Googletrans Supports

Start by installing the module using your preferred method; the Pip installer is the most straightforward way.

Googletrans supports a variety of languages for conversion. To find the list of languages it supports, run the following Python code snippet:

Import the Googletrans library into your environment and use LANGUAGES to get the list of the languages and the acronym along with it.

The output from this program will be similar to this screenshot:

Using Googletrans to Detect the Language

One of the best features of Googletrans is that it automatically detects the input language, so you do not have to provide it yourself. To detect the language, import Translator present in the googletrans module and create its instance. Use the detect() method and pass a string in any desired language. Display the output using the print() statement.

This Python code snippet produces the following output:

It displays the acronym of the detected language such as en for English and displays the accuracy with which it detected the language.

Using Googletrans to Automatically Detect and Translate to the Desired Language

Import Translator from the googletrans module and pass some lines of text that you want to convert. Create an instance of Translator. Take two parameters, the text that acts as the source and the destination, the language you want to convert into, and pass it to the translate() method.

The translate() method automatically detects and translates your text into your desired language. Display the translated text. This displays the source language, the destination language, and the translated text. You can use the text function to only display the translated text.

The output for automatic detection and translation of language is:

Using Googletrans to Perform Bulk Translation

You can perform bulk translations of languages. To perform this import the required module and create an instance of Translator. Use the translate method and pass a list of textual strings along with the desired language acronym you want to convert the language into.

Iterate over the returned object and use the origin function to display the original text and text parameter to display the translated text.

The output for the Googletrans performing Bulk Translation is:

How to Translate From One Language to Other Using Audio

You can also use your voice to interact with the translator and build a simple version of Google Assistant’s real-time translations. To do so, import the speech recognition module with an alias as spr. Import the googletrans library for translation and gTTS (Google Text-to-Speech) to convert the translated text as audio file you can hear. Import os to save the audio file.

Create a class object of Recognizer for recognizing the words the user speaks as input and another object for initializing the microphone to capture the voice.

Use the with statement as a part of exception handling. With the microphone object as source, perform these actions. Initialize the translator object for translation. Set the source and your desired destination language. Inform the user to speak something using the print statement.

Use the adjust_for_ambient_noise() method to calibrate for the first time it starts listening. You do this as the threshold property of the instance is probably set to a higher value and then adjusted according to the voice heard. Calibrating this will set this threshold to a lower value automatically.

Use the listen property to record a single phrase. The program records the voice until there are seconds of silence or no more audio input. Use recognize_google() to use the Google Web API and recognize the speech from the audio source. Alternatively, you can use Bing, IBM, Sphinx and Wit too.

Display the phrase recognized from the audio. Pass the sentence, the source language and the destination language to the translate() function. Display the translated text by calling the text parameter and display it using the print statement.

Finally, use gTTS to convert the text to speech. Pass the text, the language to read in and optionally set the slow parameter to false. Save the voice as an mp3 file and use the os module to run the saved audio file.

The output you obtain for translating from one language to another using audio is:

Alternatives to Google Translate

While Google Translate is the most popular site for translation, it has limited customization so it may not be suitable for some use cases.

If Google Translate doesn’t serve your needs, you can explore alternatives such as iTranslate, Baidu Translate, Liguee, and DeepL. These are free, support many platforms, and may fit better for your use.