Language Translation API with Speech To Speech for Android

Abedy Ng'ang'a
3 min readDec 23, 2018

So in my last semester I was supposed to come up with a working android application and when I thought of it, the first idea to cross my mind was a language translator. I thought it was easy till I got to writing the lines. That’s where I started scratching my head. But luckily, I came up with a working and efficient solution which I thought appropriate to share.

The requirements are as follows

  1. Firebase for authentication
  2. Google TextToSpeech
  3. Recognizer intent (For converting speech to text)
  4. String array containing a list of languages and their respective codes
  5. Language translation API (which is free)

So let’s get onto it..

First we’re going to add the dependencies to our gradle file app level.

Note: As per this time, those are the most stable versions of the dependencies. They’re prone to change with time.

In case you’ve never used Firebase before, you might want to add the following line to the end of your Gradle file,app level

apply plugin: 'com.google.gms.google-services'

Next you’ll update your Project level gradle file repositories to this

Next I’ll update my strings.xml file with the following string-array

Next, my XML file will have file major components, a record button, a text input layout(for receiving text input), a spinner(for selecting the language), a text view to display the translated text, and finally a translate button. I’m only going to show those components in my XML file so you can customize your activity to whatever you want.

Now it’s time to go Java-ing :-)

First things first, when the user opens the app, we want to authenticate them using the most less tiresome way. Quick guess?… Yes you got that right. Using their own email addresses registered on their phones. (We’re in 2018 after all, at least all smart phones have a registered email address :-) ). And that’s where firebase comes in handy.

So we’ll override the onStart() method on our MainActivity.java. So you’ll add this to your file. If you’re new to Firebase projects you might want to read this docs first

but oops! what the Hell is mAuth? Relax, let me make some more sense. Add these variables initialization at the beginning of your MainActivity Class. Now I’m using ButterKnife injections so don’t get so scared that my code looks a bit weird.

If want to use Butter knife too then you’ll just add the following to your dependencies

//ButterKnife Dependencies
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

Then in the onCreate() method add this

Now lets create the signUserIn() method.

then we’re going to override the onActivityResult() method as follows

Now two things will happen above. One, the user will get authenticated and two, when the user clicks on the record button, an intent will be launched for the user to record their voice for translations. But we’re not yet done . there’s that firebaseAuthWithGoogle(account); method we haven’t indicated yet. And here it is

and the corresponding updateUI() method

Now it’s time to bind the record button to the method that will record the audio

and when the the translate button is clicked,

And Booom! We’re done. In case you want any clarity, just post a comment. I hope you enjoyed and benefited from the read.

In case you want the whole app, here’s the link https://github.com/abedyngash/SpeechLanguageTranslator-Android

--

--