Language translation through Firebase MLKit

Updated 29 October 2021

Save

Language Translation

We are going to learn language translation for the Android app, In this blog, we will use Firebase Translation API to translate the word, sentence, or paragraph in any language, For a better understanding, we take an example to translate sentence English to the Hindi language.

Firebase provides free API to translate between multiple languages. Firebase supports 59 languages. You can easily integrate Firebase Translation API in your project

If you haven’t already, add Firebase to your Android project.

Add 2 dependency in your app-level build Gradle file.

After adding dependencies click on ‘sync project’ on the top right corner.

These dependencies will add all the Firebase language translation classes in your project.

We are taking an example to translate English into the Hindi language.
1.1)

We have to set Source language and Target language for translation, As we are translating English to Hindi So set FirebaseTranslateLanguage.EN as the source language and  FirebaseTranslateLanguage.HI for target language in FirebaseTranslatorOptions.

To start the translation process call translate method and pass text which you want to translate(It should be in the source language). This method translates text into the target language. see above code (1.1)
To get the success translated text you have to implement a callback listener. We implemented the success and failure listener. When successfully translate your text then it will receive in the onSuccess() method and for any error onFailure() method will be called.

Note – Firebase translation works only when the source and target language models exist in your device.

– How to check language models that exist in the device or download?

Don’t worry! Firebase provides a solution for this. You have to add a few lines before starting the translation to check the language models’ existence in the device or download the model if not exist.
Call the downloadModelIfNeeded() before start the translation. This method will check the source and target language models exist in the device, If not then this will start the downloading of the language models automatically. To get the download completed event we have to implement callback listener when models downloaded successfully

If you want all things done automatically means checks both (source and target) model downloaded or not if not, then download automatically and start the translation process after download completed onSuccess() called. This is the correct time when you should start the translation process as we did in the final code see below.

Final code –

Finally, your translation process completed. Besides, you can translate in many languages as mentioned in the top.

Most importantly – After downloaded the language models in the device you can translate the words, sentence, or paragraph offline because of the Firebase translation process do in the device using models. No internet required.

Reference  – https://firebase.google.com/docs/ml-kit/android/translate-text

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


2 comments

  • Sai
    • anchit (Moderator)
  • Start a Project


      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home