API stands forApplication 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 ingoogle translator.
Installation
Now to Check what languages it supports we have to usegoogle 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# To Print all the languages that google# translator supportsimportgoogletransprint(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 pyaudioclick here.
pip install pyaudio
pip install SpeechRecognition
pip install gtts
Below is the implementation.
Python# Importing necessary modules requiredimportspeech_recognitionassprfromgoogletransimportTranslatorfromgttsimportgTTSimportos# Creating Recogniser() class objectrecog1=spr.Recognizer()# Creating microphone instancemc=spr.Microphone()# Function to capture voice and recognize textdefrecognize_speech(recog,source):try:recog.adjust_for_ambient_noise(source,duration=0.2)# Adjust for background noiseaudio=recog.listen(source)# Capture audio inputrecognized_text=recog.recognize_google(audio)# Recognize using Google's recognizerreturnrecognized_text.lower()exceptspr.UnknownValueError:print("Google Speech Recognition could not understand the audio.")returnNoneexceptspr.RequestErrorase:print(f"Could not request results from Google Speech Recognition service;{e}")returnNone# Capture initial voicewithmcassource:print("Speak 'hello' to initiate the Translation!")print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")MyText=recognize_speech(recog1,source)# Check if the input contains 'hello'ifMyTextand'hello'inMyText:# Translator method for translationtranslator=Translator()# Source and target languagesfrom_lang='en'to_lang='hi'withmcassource:print("Speak a sentence to translate...")get_sentence=recognize_speech(recog1,source)# If sentence recognized properlyifget_sentence:try:# Print sentence to be translatedprint(f"Phrase to be Translated:{get_sentence}")# Translate the texttext_to_translate=translator.translate(get_sentence,src=from_lang,dest=to_lang)translated_text=text_to_translate.text# Convert translated text to speechspeak=gTTS(text=translated_text,lang=to_lang,slow=False)# Save the translated speech to a filespeak.save("captured_voice.mp3")# Play the speechos.system("start captured_voice.mp3")exceptExceptionase:print(f"An error occurred:{e}")else:print("Unable to capture the sentence for translation.")
Output:
Speak 'hello' to initiate the Translation !
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Speak a stentence...
Phase to be Translated :what are you doing
Language Translator Using Google API in Python