How to build a translator using Python

Currently more than 7,000 languages ​​are spoken in the world. With such diversity comes the need to learn them so that we can understand each other. Due to the sheer number of languages, we are certainly 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 to your desired language. With the Googletrans Python module, you can create 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. Some features of this module are:

  • Free: You do not have to pay any cost to use this module.
  • Unlimited – Unlike most APIs, Googletrans is not rate capped and you can use it for unlimited translations.
  • Trustworthy: Google is one of the most trusted sources on the Internet. Googletrans uses the same servers that Google Translate uses.
  • Automatic language detection: unlike other translators, you don’t need to specify the source language. This module can automatically detect a language and work even if it doesn’t know what language or dialect the sentence comes from.
  • Bulk translations: Instead of translating a sentence or paragraph one by one, you can enter 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 navigation tools to translate web pages.

Find the list of languages ​​supported by Googletrans

Start by installing the module using your preferred method; the pip installer is the easiest way.

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

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

import googletrans\nprint(googletrans.LANGUAGES)

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

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

List of languages ​​supported in Googletrans

Using Googletrans to detect language

One of the best features of Googletrans is that it automatically detects the input language, so you don’t have to provide it yourself. To detect the language, import Translator present in the google trad module and create its instance. Use the detect() method and pass a string in any desired language. Show the output using the Print() statement.

from googletrans import Translator\ntranslator = Translator()\nprint(translator.detect('이 문장은 한글로 쓰여졌습니다.'))\nprint(translator.detect('この文章は日本語で書かれました。'))\nprint(translator.detect('This sentence is written in English.'))

This Python snippet produces the following output:

Using Googletrans to detect the languages

Displays the acronym of the detected language, such as it is by English and shows how accurately it detected the language.

Using Googletrans to automatically detect and translate into the desired language

to import Translator from the google trad module and pass a few lines of text that you want to convert. create an instance of Translator. Take two parameters, the text that acts as the source and destination, the language you want to convert to, and pass it to the translate() method.

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

from googletrans import Translator\ntest = ''' आयरन मैन मार्वल कॉमिक्स का सुपर हीरो है। चरित्र लेखक और संपादक स्टैन ली द्वारा बनाया गया था,\nऔर इसकी स्क्रिप्ट लारी लिबियर द्वारा विकसित की गई थी,\nऔर डिजाइन कलाकार डॉन हैक और जैक किर्बी द्वारा बनाया गया था।\nचरित्र ने अपनी पहली उपस्थिति टेल्स ऑफ सस्पेंस #३९ (कवर मार्च १९६३) में दर्ज की। '''\ntranslator = Translator()\ntranslated = translator.translate(test, dest = 'en')\nprint(translated.text)

The output for automatic language detection and translation is:

Using Googletrans to translate the language

Use of Googletrans to carry out mass translations

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

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

from googletrans import Translator\ntranslator = Translator()\ntranslations = translator.translate(['What do you know?', 'Is this true?', 'We will eat a burger tomorrow'], dest='hi')\nfor translation in translations:\n print(translation.origin, ' -> ', translation.text)

The result of Googletrans that performs the mass translation is:

Using Googletrans to translate languages ​​in bulk

How to translate from one language to another using audio

You can also use your voice to interact with the translator and create a simple version of Google Assistant’s real-time translations. To do this, import the speech recognition module with an alias like spring. import the google trad translation library and gTTS (Google Text-to-Speech) to convert the translated text into an audio file that you can listen to. to import operating system to save the audio file.

import speech_recognition as spr\nfrom googletrans import Translator\nfrom gtts import gTTS\nimport os

Create a class object recognizer to recognize the words that the user says as input and another object to initialize the microphone to capture the voice.

recog1 = spr.Recognizer()\nmc = spr.Microphone()

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

Use the adjust_for_ambient_noise() method to calibrate for the first time you start listening. It does this because the instance’s threshold property is likely set to a higher value and then adjusted according to the voice being heard. Calibrating this will set this threshold to a lower value automatically.

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

with mc as source:\n translator = Translator()\n from_lang = 'en'\n to_lang = 'hi'\n print("Speak something...")\n recog1.adjust_for_ambient_noise(source, duration=0.2)\n audio = recog1.listen(source)\n get_sentence = recog1.recognize_google(audio)

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

print("Phrase to be Translated :" + get_sentence)\n text_to_translate = translator.translate(get_sentence,\n src=from_lang,\n dest=to_lang)\n text = text_to_translate.text\n print("The translated text is: ", text)

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

speak = gTTS(text=text, lang=to_lang, slow=False)\n"captured_voice.mp3")\n os.system("start captured_voice.mp3")

The bottom line of this program requires the beginning command to play the mp3 file. You may need to replace this with a different command, depending on your local environment. For example, on macOS, you can use the play domain.

The result you get when translating from one language to another using audio is:

Translate sentences using voice command

Alternatives to Google Translate

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

If Google Translate doesn’t meet your needs, you can explore alternatives like iTranslate, Baidu Translate, Liguee, and DeepL. These are free, support many platforms, and may be better suited to your use.

Leave a Comment