paint-brush
How to Detect Language and Translate text in Android with Firebase ML Kitby@timofeykrestyanov
2,626 reads
2,626 reads

How to Detect Language and Translate text in Android with Firebase ML Kit

by Timofey KrestyanovNovember 21st, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Machine learning (ML) Kit is a mobile SDK that brings Google AI capabilities. Using the ML Kit natural language API, language models can be downloaded to the device for offline translations and identification. ML Kit recognizes the language of the text in over 100 different languages. It can identify the language by the most likely language or by providing you with a set of languages. For example, for input "Hello World" the function will return "en", which is English. When translating from another language, English will be used as an intermediate language in translation.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - How to Detect Language and Translate text in Android with Firebase ML Kit
Timofey Krestyanov HackerNoon profile picture


Machine learning (ML) Kit is a mobile SDK that brings Google AI capabilities. Usually, to identify the language or translate the text, we would use the Cloud Translation API, however, this depends on the network connection and can be slow. Using the ML Kit natural language API, language models can be downloaded to the device for offline translations and identification.


Using the Firebase ML Kit, you can create an app that can be used in over 50 languages. For example, you can ask users to select the language they prefer and translate applications to that language.

Before You Begin

Add the dependencies for the ML Kit Android libraries

app/build.gradle

dependencies {
      // ...
      // Use this dependency to bundle the model with your app
      implementation 'com.google.mlkit:language-id:17.0.1'

      // Use this dependency to use the dynamically downloaded model in Google Play Services
      implementation 'com.google.android.gms:play-services-mlkit-language-id:16.0.0-beta2'
      
      // Use this dependency to use the translate text
      implementation 'com.google.mlkit:translate:16.1.2'
    }
 


To automatically download the model to your device after installing your app from the Google Play Store, add the following declaration to your app's file:

AndroidManifest.xml

  <application ...>
          ...
          <meta-data
              android:name="com.google.mlkit.vision.DEPENDENCIES"
              android:value="langid" />
          <!-- To use multiple models: android:value="langid,model2,model3" -->
      </application>
    

Identify Language

You can use ML Kit to detect the language of the text. ML Kit recognizes the language of the text in over 100 different languages.


We use LanguageIdentification to identify the language of the body text. It can identify the language by either returning the language or by providing you with a set of languages. It is a simple function that takes a string as input and then provides an output string that is the language code in the input string. For example, for the input "Hello World" the function will return "en", which is English.

LanguageIdentification.getClient().identifyLanguage("Hello World!")
            .addOnSuccessListener { languageCode ->
                when (languageCode) {
                    "en"  -> { ... }
                    "und" ->  Log.i("TAG", "Language: $languageCode")
                    else  ->  Log.i("TAG", "Can't identify language.")
                }
            }


By default, the ML Kit will only return a value other than und when it identifies the language with a confidence value of at least 0.5. You can change this threshold by passing a LanguageIdentificationOptions object to getClient():

LanguageIdentification
        .getClient(LanguageIdentificationOptions.Builder()
                .setConfidenceThreshold(0.34f)
                .build())

Translate Text

Now let's create an English to French translator with default parameters.


Translation Models ML Kit is designed to translate to and from English. When translating from another language, English will be used as an intermediate language in the translation process. This can definitely affect the quality of the translation. Let's now see how the ML Kit can be used for translation.


Create a Translator object, setting it with the source and target languages.

val frenchModel = TranslateRemoteModel.Builder(TranslateLanguage.FRENCH).build()
val modelManager = RemoteModelManager.getInstance()
modelManager.download(frenchModel, DownloadConditions.Builder().build())


Obtain the text written by the user and set the model’s download conditions.

If you don't know the language of the input text, you can use the Language Identification API

// Create an English-French translator:
val options = TranslatorOptions.Builder()
    .setSourceLanguage(TranslateLanguage.ENGLISH)
    .setTargetLanguage(TranslateLanguage.FRENCH)
    .build()
val englishFrenchTranslator = Translation.getClient(options)

var conditions = DownloadConditions.Builder()
    .requireWifi() // download models only by wifi
    .build()


englishFrenchTranslator.downloadModelIfNeeded(conditions)
    .addOnSuccessListener {
        // Model downloaded successfully. Okay to start translating.
        // (Set a flag, unhide the translation UI, etc.)
    }
    .addOnFailureListener { exception ->
        // Model couldn’t be downloaded or other internal error.
        // ...
    }


We need to ensure that the model has been downloaded before running the translate method. Obtain the text written by the user and set the model’s download conditions.


Get the available translation models stored on your device

modelManager.getDownloadedModels(TranslateRemoteModel::class.java)
    .addOnSuccessListener { models ->
        // ...
    }
    .addOnFailureListener {
        // Error.
    }


Delete unused translation models

modelManager.deleteDownloadedModel(frenchModel)


After you confirm the model has been downloaded, pass a string of text in the source language to translate():

englishFrenchTranslator.translate(text)
    .addOnSuccessListener { translatedText ->
        // Translation successful.
    }
    .addOnFailureListener { exception ->
         // Error.
         // ...
    }


Requests you make before the download has been completed produce no results.

Conclusion

This API can allow people from all over the world speaking different languages to communicate. Using this API, you can identify the language of the message and then move on to using the Translation API to translate the message into English or another language of your choice. The best thing about this API is that it is completely free and works offline on the device itself. Hopefully, this shows you how easy and fast you can add language translation capabilities to your app.