This tutorial is 7th part in this the series, ML Kit for Mobile Developers. If you’re not quite caught up, you can start here:

Series Pit Stops

Introducing ML Kit’s Language ID API

Following up on the previous blog posts covering the APIs available in Firebase’s ML Kit, this post will be covering one of the most recently released APIs — Language ID, which allows you to automatically detect the language of a provided text snippet.

This API allows you to pass in a text snippet, and in turn it identifies the language of that text snippet.

While the API in-and-of itself might not sound super useful, it can be combined with other APIs to create a lot of unique use cases.

A very common use-case could be for an app that allows people speaking different languages to communicate. Using this API, you can infer the language of the message and then proceed to use the translation API to translate the message into English or another language of the user’s choice. As a note, the API supports more than 100 languages from around the world.

You might have already seen apps like AirBnb doing something very similar, wherein your host is not a native English speaker.

For this tutorial, we’ll be looking at how to utilize the Language ID API to build a Google Translate clone of our own, which takes in text in any language and converts it to English.

Before we get started, here are some screenshots that showcase the end result:

Sí Señor

The best part about this API is that it runs locally on your device; hence, it’s not only much faster, but it also doesn’t require any network connectivity to function.

Interested in mobile development powered by machine learning? Sign up for the Heartbeat Newsletter and join the largest community focused on mobile ML.

Getting Started

Let’s get started with our implementation and take a look at the actual code that powers the app shown above.

Setup Firebase in your project and add the Language ID dependency

This is a simple one — set up Firebase in your project. You can find a good tutorial here. In order to use this API, you also need to add the following dependency:

Prepare your app for receiving user input

This part involves creating a simple xml layout that can receive input from the user (the text that is to be translated) and then display the translated text inside another TextView. You can also find the sample code for the app shown in the screenshot above over here:

Infer the language of input text

We’ll be using a simple function that takes in a string as input and then provides an output string that’s the identified language code in the input string. As an example, for input “Hello World”, the function will return “en”, i.e. English.

Setting up Google Cloud’s Language Translation API

Once we have the input text and its language available, we’d like to then translate it to English. For this, we’ll be using the Google Cloud Language Translation API.

While the setup process isn’t exactly straightforward for Android apps, stay with me, and I’ll help you enable this API and obtain an API key for communicating with it.

  • Create a new project and enable the Google Translation API
    Go to the following page and click on the button that says “SET UP A PROJECT”
Click the Button and then either create or select an existing project from the dropdown list
  • Either create a new project or select an existing project
    Once selected, press next. You might want to enable billing for the project by adding a credit card, but don’t worry, you won’t be charged anything, as the API we’ll be using is free for up to 500,000 characters translated.
  • Make a note of the service account name and safely secure the downloaded JSON file.
  • Open a cloud shell in your project’s google cloud console
    Go to and open the cloud shell in your project’s window.
Click on the icon highlighted in the top right to bring up the cloud shell terminal
  • Upload the downloaded JSON to the cloud shell
    Drag ands drop the downloaded JSON file above to the cloud shell.
  • Obtain the access token to be used for calling the translation API
    Once the above steps are completed, run the following command in your cloud shell to obtain your access token:

Copy the access token received, we will be using it in the next steps

Studying the Request and Responses of Google Translate API

Since the Google Translate API doesn’t support Android clients yet, we’ll be consuming the API via a normal HTTP call with the auth token in the header of our request.

The input params and the response of the API look something like this :

For our convenience, I’ve created wrapper Kotlin classes for the Request and Response. You can find them here:

Making network calls to the Translate API

For this part, we create a function that takes in the input language that is to be translated along with its language ID and returns a string that’s the input language translated to English.

And that’s it!

Of course, there are endless possibilities for layering additional user experiences on top of this feature—for example, using the ML Kit Text Recognition API to extract text from an image and allow users to translate text from that image.

And remember, the Language Identification API is totally free of charge, and it runs without network connectivity on the device itself.

If you want to play around with the app shown in the screenshots, you can build it from the GitHub repository linked below, and it should work well after adding it to your own Firebase Project.

Thanks for reading! If you enjoyed this story, please click the 👏 button and share to help others find it! Feel free to leave a comment 💬 below.

Have feedback? Let’s connect on Twitter.

Editor’s Note: Join Heartbeat on Slack and follow us on Twitter and LinkedIn for the all the latest content, news, and more in machine learning, mobile development, and where the two intersect.

Source: Artificial Intelligence on Medium