DEV Community

Steadylearner
Steadylearner

Posted on • Updated on • Originally published at steadylearner.com

How to make a CLI translator with Python pyttsx3 and deep_translator

In this post, we will learn how to make a simple CLI translator with Python. You can use it to learn a language or test translator service etc.

Before we start, please install Python and pip if you haven't yet. Then, read the docs for the packages we will use.

  1. How to install Python

  2. How to install pip

  3. pyttsx3 to hear voice along with text

  4. deep-translator to translate your CLI input

Then, save a file with the code below after you install and set up the Python dev env with pip with these.

$python -m venv <folder>
$cd <folder>
$source bin <folder>/activate

$pip install pyttsx3 deep_translator
Enter fullscreen mode Exit fullscreen mode

You can use this command $touch test_text.py to make test_text.py file or something else for your file name.

import pyttsx3
from deep_translator import GoogleTranslator

def change_voice(engine, language, gender='VoiceGenderFemale'):
    for voice in engine.getProperty('voices'):
        if language in voice.languages and gender == voice.gender:
            engine.setProperty('voice', voice.id)
            return True

    raise RuntimeError(
        "Language '{}' for gender '{}' not found".format(language, gender))


engine = pyttsx3.init()
change_voice(engine, "en_US")
# change_voice(engine, "en_US", "VoiceGenderFemale")

rate = engine.getProperty('rate')
# print("rate")
# print(rate)
engine.setProperty('rate', 10)

volume = engine.getProperty('volume')

what_you_typed = input("Type something?\n")

engine.say(f"You typed {what_you_typed}")
engine.runAndWait()

translate_what_you_typed = input("Do you want to hear it in another language?\n")
if (translate_what_you_typed.lower().startswith("y")):
    translated = GoogleTranslator(source='auto', target='pt').translate(what_you_typed)
    print(translated)

    change_voice(engine, "pt_BR")
    engine.say(translated)
    engine.runAndWait()
Enter fullscreen mode Exit fullscreen mode

Then, you can test it with $python test_text.py and you will see the console messages similar to these.

Type something?
I like to use Python to learn how to code.

Do you want to hear it in another language?
y
Enter fullscreen mode Exit fullscreen mode

You will be able to hear a text translated from what you typed.

If you want to, you can also set the voice to male or female with this.

# VoiceGenderFemale, VoiceGenderMale
change_voice(engine, "en_US", "VoiceGenderFemale")
change_voice(engine, "en_US", "VoiceGenderMale")
Enter fullscreen mode Exit fullscreen mode

It doesn't work for all languages options. Therefore, you need to test what is available.

You can also update the target language to translate by editing the target part to what you want.

translated = GoogleTranslator(source='auto', target='pt').translate(what_you_typed)
Enter fullscreen mode Exit fullscreen mode

The language options for the pyttsx3 and the deep_translator can be different. You can test some of them for your target language.

If you want to test a GUI app built with Python Tkinter, you can read this post.

How to make a GUI translator app with Python Tkinter

If you want more posts, you can follow me at GitHub.

You can contact or hire me at Telegram.

Thanks and please share this post with others.

Top comments (0)