Simplifying Communication By Using Google Cloud Translate API And Speech To Text Conversion

Boemo Mmopelwa
5 min readOct 31, 2020

The language barrier becomes more intense when your mobile app is downloaded by many people who do not understand the English language. Users may uninstall your app due to this language barrier. It is essential to add the translate content option to your mobile application to reduce the number of uninstalls.

In this article I will show you how to translate text using google translate API and also convert speech to text.

The speech to text feature improves accessibility for people with sight problems and dexterity.

Content Overview

  • Translating text.
  • Implementing the speech to text feature.

Prerequisites

  • A google account for google cloud API.

Adding Google Cloud Translate API to an Android App

credit: google cloud

We will use the google cloud translate API to translate English to French. We will start off by creating google cloud platform project and enabling the translate API. After enabling the API we will implement the functions for performing the translation tasks.

Enabling Translate API in Google cloud

Click on the link below to access the console button in google cloud webpage.

1.Next go to the dashboard and click on the drop down adjacent to the search bar and click on create new project on the top right corner.

2.Enter the project details. After entering details click on the create button.

3.Click on the Go to APIs overview in APIs section.

4.Click on ENABLE APIS AND SERVICES.

5.Next search for cloud translate API.

6.Click on the enable API button and the API will be enabled.

7.Click on the create credentials button and select help me choose.

8.Select the translate API. Please ensure the translate API is enabled in your google cloud project or else the the cloud translate will not show in the list below .Next click on no am not using any computing engine option , followed by clicking on the what credentials do I need button.

9. Enter account service details and select json as the key type and the select get credentials to download the credentials json file.

10. And lastly add the json file to raw file in the resources folder in Android studio.

Adding Libraries and excluding properties

Add the the internet permission

Check internet connection

The translate service needs internet connection before connecting ensure that the device has access to internet connection.

Translate Service

The StrictMode class catches accidental disk or network access on the application’s main thread. The permitall() method disables the detection of everything. This enables you to access network on the main thread as we will call the getTranslateService in the onCreate() method which requires access internet connection.

Open the credential file in the raw folder and get the google credentials from the credentials json file .

Next set the credentials then get the service.

Translate

To translate text first call the getTranslateService() in onCreate method in order to get the translate service. The translate() method takes in the text to be translated, the target language locale and also the model which will be base. For this article our language locale will be French ,its locale code is fr. Basically here we translate the users input to the French language .

The following link has a list of android locale codes you can use specifically for your android app audience.

Output.

Speech to Text

  • This task does not require an internet connection.
  • It uses the devices default locale but you can specify if you want to use a different locale.

activity_main.xml contents.

Add the following layout parents and children to receive user input and also display output.

Recording Speech

To carry out a speech to text conversion use the RecognizerIntent.ACTION_RECOGNIZE_SPEECH intent. This intent will prompt the user for voice input and a microphone dialogue box will be displayed. The dialog box will close automatically when the user has stopped talking and speech input has been received. If the user is not audible enough and no speech input is received the dialog box will prompt the user to try again.

ACTION_RECOGNIZE_SPEECH sends the recorded audio through a speech recognizer.

Handling the result

In conclusion

In this article we learned how to add translate feature to your app and also convert speech to text .I hope you use these features to add convenience and improve accessibility to your mobile application.

Get the full code here:https://github.com/xTrilton/CloudTranslator

--

--

Boemo Mmopelwa

Android developer| Tech Enthusiast|Innovative thinking is my passion | Learn and experience more to discover more