How to use Google Translate API in Android Studio projects?

Cansu Yeksan Aktaş
6 min readMar 11, 2019

--

Cloud Translate API is one of the most useful API which Google offers to developers. Implementation of Cloud Translate API to your project is really simple, so you can prefer to use it in your translation projects.

As we know, every beautiful thing has a price. Before implementing Cloud Translate API to your project, you can check the pricing from the link: https://cloud.google.com/translate/pricing

Important Note: The steps of getting credentials file (in JSON format) from Cloud Translation API are going to be explained in detail until step 19. If you have already get the credentials file and want to see the implementation of the code, please just skip to the 19th step.

Both Java and Kotlin codes are included in this post.

Now, we are ready to start the implementation of Cloud Translate API to the project. Let’s start step by step:

  1. The first thing to do is to sign in Google API Console from the link: https://console.cloud.google.com/apis/

If you have a Google API Console account, enter your Email or phone, and your password.

If you do not have a Google API Console account, you can click on “Create account” and start to create an account for Google API Console. Then, below page will appear:

After entering your personal information, you will be directed to your profile dashboard in the Google API Console. Check “I accept the Google Platform Terms of Service and the terms of service for all applicable services and APIs.”

2. Click on the “Select a project” on the Google Cloud Platform tab:

3. Then, click on the “New Project” in the below screen:

4. Enter your Android project name and click on the “Create” button. I will name the project as “GoogleTranslate”:

5. Your project will be displayed on the Google Cloud Platform dashboard. Select “APIs and Services” on the navigation drawer at the left side:

6. Click on the “Enable APIs and Services” button:

7. Type “Cloud Translation API” in the search box and select the API:

8. You need to enable Cloud Translation API for your project by clicking on the “Enable” button:

9. Click on the “Enable Billing” in the screen below:

10. You will encounter the screen below. Click on the “Create billing account” and continue:

11. Check “I have read and agree to the Google Cloud Platform Free Trial Terms of Service.” and click on “Agree and Continue”:

12. Enter your personal information:

13. After entering your Credit Card number, click on the “Start my free trial”:

14. You are ready to manage Cloud Translation API for your project now. Click on “Manage”:

15. Click on “Create Credentials”:

16. Select “Cloud Translation API”, check “No, I’m not using them” and click on “Which credentials do I need?” :

17. Enter your personal information, select “JSON” as the key type and click on “Continue”:

18. The credentials file of your project will be downloaded to your computer automatically in JSON format. Keep this file, for now, you will need the file soon :)

19. Now, you are ready to code! First, add internet permission to your AndroidManifest.xml file:

20. Add google cloud translate dependency to module level build.gradle. (It is the latest version of google cloud translate library for now, you can use the updated version if it is available in the future)

21. In order to prevent “More the one file was found with OS independent path “project.properties” gradle error, add below code to the app level build.gradle:

22. Create a layout and add an EditText, a Button and a TextView. Therefore, when a text is entered to EditText and then Translate Button is clicked, the translated text will be displayed on a TextView.

23. We are almost done! Copy your credentials file in JSON format which you had downloaded at step 18 to raw resource directory. In order to create a raw resource directory:

Right click on res folder → Click on “New” → Click on “Android Resource Directory” and select Resource type as “raw” from the list.

Then, move your credentials file in JSON format to the raw folder. It is important to note that the name of the files in the resource folders such as raw folder cannot contain uppercase characters, etc., so you need to consider the warning of Android Studio while naming your credentials file*:

File-based resource names must contain only lowercase a-z, 0–9, or underscore” characters

*For simplicity, I just named the credentials file as “credentials.json”.

24. Add the string of no internet connection warning to the strings.xml:

25. …and the last part is MainActivity.java:

First, translate service is gotten by getTranslateService() method. In this method, basically, the credentials which had been obtained from the Google API Console previously is set and translate service is connected.

After getting service by setting credentials, the translation process is carried out in the translate() method.

Also, we are checking the internet connection with the checkInternetConnection() method. Therefore, if there is no internet connection, “no connection” warning is displayed to the user at the TextView. If there is no problem related to the internet connection, translation is carried out and translated text is displayed at the TextView.

26. If you want to write your code in Kotlin, MainActivity.kt will be like below:

27. Cloud Translation API automatically detects the language which is translated to a target language. Therefore, you do not need to define the source language, but you have to define the target language.

I chose the target language as Turkish, so I used “tr” abbreviation for my target language. Please find the abbreviation of your target language from the below link which includes supported languages:

https://cloud.google.com/translate/docs/languages

28. Here is the demo of the app:

Hope it works!

--

--