Home News How to set up Google Translate API in Google API Console for...

How to set up Google Translate API in Google API Console for use in Android Studio Projects

One of the most useful APIs available to developers is Google’s own Cloud Translate API. It’s also incredibly easy to implement and has all manners of uses. Many users say it’s far more accurate than Google Translate and can deliver streaming results, which means even spoken language can be translated in real-time.

In this article, we’ll go over how to set up Google Translate API for use in Android Studio Projects. You’ll typically work with the Google Translate API in Java, but libraries exist for languages such as Python, including the free and unlimited Googletrans Python library.

Setting Up Google Translate API in Google API Console

You’ll begin by signing into the Google API console or registering an account. While the Google Translate API isn’t free, there is a free trial for Google Cloud Platform products with some limitations that you can avail for this follow-along tutorial.

In the profile dashboard, agree to the terms of service and start a new project.

Give your Android project a name and create it, and it will be displayed in the Cloud Platform dashboard.

On the left side, choose “APIs and Services”, then click “Enable APIs and Services”.

In the search box, type “Cloud Translation API” and choose it. Next, you need to enable the API for your project, enable billing, and provide your credit card information.

After you’ve successfully availed the trial offer, you can click “Manage” on your project, then click Create Credentials > Cloud Translation API, followed by “No, I’m not using them” and “Which credentials do I need?”

After entering your personal information, choose JSON as the key type and continue. It will download a credentials file for your project to your computer, in JSON format. You’ll need this file later.

You now need to add internet permission in your AndroidManifest.xml file and add the Google Cloud to Translate dependency to module-level build.gradle.

Create a layout and add an EditText, button, and TextView. When a text is entered to EditText and the Translate button is clicked, the TextView area will display the translated text from the API.

Now you need to copy your credentials from the JSON file into your raw resource directory. If you need to create a raw resource directly, you can right-click on the res folder, followed by New > Android Resource Directory > Raw.

After that, move your credentials file to the raw folder, and add the string of “no internet connection” warnings to your strings.xml file.

Now for the last part which is MainActivity.java. Understand that the translation service is retrieved by the getTranslateService() method, where the credentials from the Google API console are obtained and connected to the translation service.

So after getting service, the translation process is performed in translate() method, and the state of internet connectivity is checked with checkInternetConnection(). So if no connection is detected, it will display the no connection warning in the TextView area.

This is a very basic example of creating an Android Studio project with the Google Translate API and should give you a start.

Previous articleThe 411 on Selenium Software Testing
Next articleTop 5 gaming smartphones with cooling technology in India
Kanisha is an all-around geek who loves learning new stuff every day. With a background in computer science and a passion for web-based technologies and Gadgets, she focuses on writing about Web Trends, Smartphones and Tablets. You can contact her at kanisha@pc-tablet.com.