DEV Community

Cover image for BUILD A TRANSLATOR WITH PYTHON IN 5 MINUTES.
Geof
Geof

Posted on

BUILD A TRANSLATOR WITH PYTHON IN 5 MINUTES.

BUILDING A LANGUAGE TRANSLATOR WITH PYTHON IS FUN.

Why pay a translator or purchase an expensive translation app when you can build an effective multilingual translator with python in 5 minutes?

Going furthermore, we’ll make this translator even more fun by adding a text to speech capability to it. This will also make it easy for one to use this translator to learn a new language. Sounds interesting right?

Talk is cheap..I know right? Well then let’s get down to the business of the day. To build this amazing translator with python, we’ll be needing a few python modules:

To familiarize yourself with the aforementioned just click on them and view the module’s documentation.

LET THE CODING BEGIN..

First we’ll start by importing those modules:

from googletrans import Translator, constants
from pprint import pprint
from playsound import playsound
from gtts import gTTS
Enter fullscreen mode Exit fullscreen mode

WHY DO WE NEED THESE MODULES?

  • So we’ll use google trans to translate to any language we choose.
  • Pretty printer(pprint) will print out language codes in a cleaner format.
  • Meanwhile we’ll use gtts to convert the translated language text to speech/audio format(mp3).Then we’ll use playsound to play the translated words(mp3).

Let’s put it all together in a function:

def trnslate():
    tr = Translator()
    pprint(f'The available languages and their codes are: 
 {constants.LANGUAGES}')
    lan = input('Please enter your desired language:\n')
    phrase = input('Say something nice:\n')
    translated = tr.translate(phrase, dest = lan)
    print(translated.text)
Enter fullscreen mode Exit fullscreen mode

Let’s quickly explain the code:

We created a function called trnslate(), we didn’t call it translate to avoid issues since that’s a method in the module. Then we created an instance of Translator class and assigned to a variable tr. The we used the constants.LANGUAGES to print out all the available languages and their codes to our program user.

We set up the words to translate and preferred language in the variables named lan and phrase, you can call yours anything you like.

Then we translated and saved the phrase/words to the variable named translated and use pprint to print them out. Ordinarily if you don’t add the .text at the end of the variable translated you’ll get extra information about the translated words, but to limit things to only the translated texts we append the .text.

LET’S MAKE IT EVEN MORE FUN WITH TEXT TO SPEECH:

def trnslate():
    tr = Translator()
    pprint(f'The available languages and their codes are: {constants.LANGUAGES}')
    lan = input('Please enter your desired language:\n')
    phrase = input('Say something nice:\n')
    translated = tr.translate(phrase, dest = lan)
    print(translated.text)
    aud = gTTS(translated.text, lang = lan)
    aud.save('trantest.mp3')
    playsound('trantest.mp3')

trnslate()
Enter fullscreen mode Exit fullscreen mode

So the extra codes added was to convert the translated words to speech. We used ggts to translate the text to the same language the user preferred.

But be advised: all the languages available in googletrans might not all be in gtts. So make sure the language you use is available in both modules. To confirm that, check the languages on googletrans with print(constants.LANGUAGES), then go to your command line and run gtts-cli –all. With the information you’ll be able to figure out all the languages the both modules have in common.

To avoid errors make sure all the modules are already installed and imported before running the codes. However if you wish to translate only without having the text to speech part, probably because the language you want is not on gtts, then you may comment or remove all the codes associated with gtts and your code will still work perfectly.

To see another tutorial on text to speech checkout this short tutorial here.

Hope you enjoyed building this quick translator with python codes. Remember you need internet for this codes to work effectively.

Click the follow button to automatically get the first hand information when the next post or more advanced translator with python tutorial is published.

Top comments (0)