BUILD A TRANSLATOR WITH PYTHON IN 5 MINUTES.
Tags: python, programming, translator
Published: 2021-12-08 20:42:06

##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:

- [googletrans](https://py-googletrans.readthedocs.io/en/latest/)
- [gtts](https://gtts.readthedocs.io/en/latest/)(Google text to speech)
- [playsound](https://pypi.org/project/playsound/)
- [pprint](https://docs.python.org/3/library/pprint.html)

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:

```python
from googletrans import Translator, constants
from pprint import pprint
from playsound import playsound
from gtts import gTTS
```

##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:

```python
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)
```

####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:

```python
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()
```
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](https://pythgenie.geofsphere.com/text-to-speech-in-python/).

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.