Google Cloud Translate API in Android
These days, the trending technologies such as Artificial Intelligence, Machine Learning and so on are buzzing. That’s probably making the smartphone users look for smarter apps which makes their life simple.
But, the real challenge for the Android Developers starts here for creating the most innovating apps which users are looking for. This is due to the lack of resources needed to create such applications from scratch.
Google has recently launched a Cloud Machine Learning Platform, which offers Neural Networks and its models are highly accurate and constantly updating. Guess what? The life of Android Developers is simplified now in creating the applications by just making the REST API calls.
There are four REST APIs based on Cloud Machine Learning Platform namely:
- Speech API
- Translate API
- Vision API
- Natural Languages API
In this blog article, you will be introduced to the Google Cloud Translate API and one of its real-time applications converting from one language to the other language (For example, converting text from the English to the German language).
First, let’s have a quick look at what is Google Cloud Translation API.
What is Google Cloud Translation API?
Google Cloud Translation API implements Machine Learning concept to translate from one language to other supporting languages. This is highly responsive and known for its ease of use so that applications can integrate with Translation API for fast and dynamic translation of source language to the target language. One of the best applications is that it can detect the language even if the source is unknown. This Machine translation technology is very useful and is updated constantly.
Here are some of the exciting features of Google Cloud Translation API. Let’s have a look at them briefly:
- Provides a speedy and easy interface for different language conversation/translation.
- Converts about 100+ languages using Translate API and provides great support for that.
- Identifies the local language in your native language without any errors.
- Increases the daily quota of usage limit (this can be increased by sending request to concerned teams)
- Provides higher scalability compared to the other APIs in the platform
- Available at a lower price and the price will be calculated as per your usage. Check the below for the same.
- For Text Translation – $20 per Million Characters
- For Language Detection – $20 per Million Characters
Here comes the most awaited section of how to convert the text in the English language to the German Language.
Step 1 – Add Dependencies and Services into build.gradle(Module:app):
Step 2 – Add Google Cloud Translate API Services as shown in the below code snippet:
Step 3 – Add Google Cloud Translate API dependencies with the excluding libraries as shown in the below code snippet:
Step 4 – Add Internet Permission in AndroidManifest.xml as shown in the below code snippet:
Add Text View in the Layout and add Text as “Hello World”.
Once the text is added in the layout, add project with Google Cloud Translate API and retrieve the API key.
Step 5 – To create API key, Go to Google Cloud Platform Console and check the screenshot where you need to click, Here “My Project” is a sample project which you can see in screenshot:
Create a new project in Google Cloud Console, You need to click on + sign to create new project as shown in below screenshot:
Step 6 – Give a Project Name for the created project (User-defined project name) and click on Create button as shown below:
Step 7: After creating a project name, You need to click again to select project which you have created to get API key:
Step 8: Select a Project which you have created from a List of Projects, click on it and open project:
Step 9:You can see a Dashboard in below Screenshot, Click on it to enable API key:
Step 10:Here you can see Enable APIs and Services, click on it as shown in Screenshot:
Step 11:You can see Google Cloud Translation API in below list, You can select it by clicking and enable it.
Step 12:Now, You are in the Page of Google Cloud Translation API, You can see a button to Enable it, Click on Enable and it will automatically Enabled.
Step 13:Now, Go back to Dashboard, Select APIs and Services and Click on Credentials, it will help you to create API key as below:
Step 14:Inside Credentials Tab, You can see a dialog for API Credentials, Click on Create credentials:
Step 15: When you will click on Create Credentials, You will get below options, You need to select API key as below:
Step 16:Finally, You got an API key, which you can see below
You can copy and Paste your API key for using in your code:
|TranslateOptions||Class for specifying translate options.|
|Translate||This class will be reference for the Google Cloud Translation API.|
|targetLanguage ()||This method will be set the targeted language where you want to convert into expected Language.|
|getTranslatedText()||It will return the translated Text into expected language.|
Now You will get API_KEY and You need to add it into MainActivity.java as below:
Go to MainActivity.java and Add API_Key.
In MainActivity.java, You can create a new class named as TranslateOptions and You can call a method getService() inside Translate Class:
Finally, create Translation class and use the method translate to convert your text into desired language, You can check code below:
Now your work has done. Try to execute your App into Emulator or your Device, you will get an output as below:
Before Translate: Input
After Translate: Output
Using this sample app, you can convert any text from One Language to another Language. This API is very useful for those, who don’t know a language and want to communicate with it. You can customize your app with Text-to-Speech conversion also. This API is Open-Source, So You can get all type of Conversion using Google Cloud Translate API.