Language Translator Using Python (original) (raw)

Last Updated : 18 Apr, 2026

A language translator is a tool that converts text or speech from one language to another, helping people understand and communicate in different languages. In this tutorial, we will build a Voice-based Language Translator in Python

The translator will:

Libraries Installation

We will use the following Python libraries:

Install the libraries using pip:

pip install googletrans
pip install SpeechRecognition
pip install PyAudio
pip install gTTS

**Note: Installing PyAudio on Windows may require portaudio setup

Voice Translator

The following Python code captures your speech, translates it to another language, and plays the translated audio.

Python `

import speech_recognition as sr from googletrans import Translator from gtts import gTTS import os

recog = sr.Recognizer() mic = sr.Microphone()

def recognize_speech(recog, source): try: recog.adjust_for_ambient_noise(source, duration=0.2)
audio = recog.listen(source)
text = recog.recognize_google(audio)
return text.lower() except sr.UnknownValueError: print("Could not understand the audio.") return None except sr.RequestError as e: print(f"Error from Google Speech Recognition service; {e}") return None

with mic as source: print("Speak 'hello' to start the translation!") user_input = recognize_speech(recog, source)

if user_input and 'hello' in user_input: translator = Translator()

from_lang = 'en'  
to_lang = 'hi'    

with mic as source:
    print("Speak a sentence to translate...")
    sentence = recognize_speech(recog, source)
    
    if sentence:
        try:
            print(f"Phrase to be Translated: {sentence}")
            translated_text = translator.translate(sentence, src=from_lang, dest=to_lang).text
            print(f"Translated Text: {translated_text}")
            
            speech = gTTS(text=translated_text, lang=to_lang, slow=False)
            speech.save("translated_voice.mp3")
            
            os.system("start translated_voice.mp3")
        
        except Exception as e:
            print(f"An error occurred: {e}")
    else:
        print("Could not capture the sentence.")

`

**Output

Speak 'hello' to start the translation!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Speak a sentence to translate...
Phrase to be Translated: What are you doing
Translated Text: आप क्या कर रहे हैं

**Explanation: