API stands for Application Programming Interface. It acts as an intermediate between two applications or software. In simple terms, API acts as a messenger that takes your request to destinations and then brings back its response for you. Google API is developed by Google to allow communications with their servers and use their API keys to develop projects.
In this tutorial, we are going to use Google API to build a Language Translator which can translate one language to another language. On the internet, we can see lots of projects on Speech Recognitions, Speech to text, text to speech, etc. but here in this project we are going to build something more advance than that.
Let’s assume a scenario, we are traveling in Spain and we don’t know how to speak Spanish or we are in any other country and we don’t know their native language, then we can use this tool to overcome the problem. We can translate between all those languages which are present in google translator.
Installation
Now to Check what languages it supports we have to use google trans library. We can use pip to install it.
pip install googletrans
Now to check which languages it supports to run the following code.
Python3
import googletrans
print (googletrans.LANGUAGES)
|
Output:

Now let’s start building Language Translator. To begin with the coding part, we need to install some dependencies. While installing Pyaudio you might get an error of portaudio. For details of installation of pyaudio click here.
pip install pyaudio
pip install SpeechRecognition
pip install gtts
Below is the implementation.
Python3
import speech_recognition as spr
from googletrans import Translator
from gtts import gTTS
import os
recog1 = spr.Recognizer()
mc = spr.Microphone()
with mc as source:
print ( "Speak 'hello' to initiate the Translation !" )
print ( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" )
recog1.adjust_for_ambient_noise(source, duration = 0.2 )
audio = recog1.listen(source)
MyText = recog1.recognize_google(audio)
MyText = MyText.lower()
if 'hello' in MyText:
translator = Translator()
from_lang = 'en'
to_lang = 'hi'
with mc as source:
print ( "Speak a stentence..." )
recog1.adjust_for_ambient_noise(source, duration = 0.2 )
audio = recog1.listen(source)
get_sentence = recog1.recognize_google(audio)
try :
print ( "Phase to be Translated :" + get_sentence)
text_to_translate = translator.translate(get_sentence,
src = from_lang,
dest = to_lang)
text = text_to_translate.text
speak = gTTS(text = text, lang = to_lang, slow = False )
speak.save( "captured_voice.mp3" )
os.system( "start captured_voice.mp3" )
except spr.UnknownValueError:
print ( "Unable to Understand the Input" )
except spr.RequestError as e:
print ( "Unable to provide Required Output" . format (e))
|
Output:
Speak 'hello' to initiate the Translation !
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Speak a stentence...
Phase to be Translated :what are you doing
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
24 Sep, 2021
Like Article
Save Article