How to Build a Translator Using Python

There are more than 7,000 languages spoken in the world today. With such diversity comes a need to learn them so we can understand each other. Due to the sheer number of languages, we are no doubt missing out on opportunities to share ideas, knowledge, and personal expression.

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.

4

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:

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

An illustration showing two hands, each holding a document in a different language with the word “translation” between them..

Finding the List of Languages That Googletrans Supports

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

You may encounter problems relating to the version of googletrans you install. If you see errors when running these examples, try installing version 3.1.0a0, i.e.pip install googletrans==3.1.0a0.

List of languages supported in Googletrans

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 useLANGUAGESto get the list of the languages and the acronym along with it.

Using Googletrans to detect the languages

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, importTranslatorpresent in thegoogletransmodule and create its instance. Use thedetect()method and pass a string in any desired language. Display the output using theprint()statement.

This Python code snippet produces the following output:

Using Googletrans to translate the language

It displays the acronym of the detected language such asenforEnglishand displays the accuracy with which it detected the language.

Using Googletrans to Automatically Detect and Translate to the Desired Language

ImportTranslatorfrom thegoogletransmodule and pass some lines of text that you want to convert. Create an instance ofTranslator. Take two parameters, the text that acts as the source and the destination, the language you want to convert into, and pass it to thetranslate()method.

Thetranslate()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 ofTranslator. Use thetranslatemethod 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 ofGoogle Assistant’s real-time translations. To do so, import thespeech recognitionmodule with an alias asspr. Import thegoogletranslibrary for translation andgTTS(Google Text-to-Speech) to convert the translated text as audio file you can hear. Importosto save the audio file.

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

Use thewithstatement 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 theadjust_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. Userecognize_google()to use the Google Web API and recognize the speech from the audio source. Alternatively, it’s possible to 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 thetranslate()function. Display the translated text by calling the text parameter and display it using theprintstatement.

Finally, usegTTSto 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 theosmodule to run the saved audio file.

The final line of this program requires thestartcommand to play the mp3 file. You may need to substitute this with a different command, depending on your local environment. For example, on macOS, it’s possible to use theafplaycommand.

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.

A tiny feature in Google Translate makes translating any language from any app on your Android a breeze.

My iPhone does it all, but I still need my dumb phone.

If an AI can roast you, it can also prep you for emergencies.

Don’t let someone else take over your phone number.

Turn these settings on, and your iPhone will be so much better than before.

Tor spoiled me forever.

Technology Explained

PC & Mobile