Skip to content

Firebase ML Kit 8: Text Translation

A month past I/O and I’ve only just discovered they’ve expanded the ML Kit yet again with some of the neatest additions yet.

The first of which being ML Kit’s Translation and it’s pretty much what it sounds like. You pass in some text and the code translates it. The Kit currently supports 59 languages so it’s not the whole Google Translate package but it’s a pretty strong lineup. It does however use the same models as Google Translate’s offline mode.

It runs on the device (no server calls needed), and on-device storage requirements can be managed by dynamically downloading language packs. Each language model is roughly 30MB.

As you should probably know from Google Translate itself, translations can sometimes be a bit iffy especially when translating between two non-English languages. The model is trained to translate to and from English, so if you’re translating between two other languages, English will be used as an intermediate translation and this can affect quality.

Now that all the info’s out of the way, let’s get writing.

Prerequisites

As with all Firebase products, you’ll need to connect your app to Firebase and in addition, add these dependencies to your app/build.gradle file:

dependencies {
  implementation 'com.google.firebase:firebase-ml-natural-language:19.0.1'
  implementation 'com.google.firebase:firebase-ml-natural-language-translate-model:19.0.1'
}

Preparing for Translation

Start by creating a FirebaseTranslator object configured with the source and target languages.

// Create an English-German translator:
val options = FirebaseTranslatorOptions.Builder()
        .setSourceLanguage(FirebaseTranslateLanguage.EN)
        .setTargetLanguage(FirebaseTranslateLanguage.DE)
        .build()
val englishGermanTranslator = FirebaseNaturalLanguage.getInstance().getTranslator(options)

Then check if the desired language models are available. Remember that each model is roughly 30MB so you’d do your users good to only download over WiFi unless they opt-in to data download. You can do this via a FirebaseModelDownloadConditions which I explained further below in Setting Download Conditions.

englishGermanTranslator.downloadModelIfNeeded()
        .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.
            // ...
        }

Finally, perform the translation.

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

Setting Download Conditions

In big countries like the United Kingdom, 30MB is a good portion of your data. In less developed countries like the Philippines, that’s your whole year’s worth of data. For good User Experience, you should only download models over WiFi unless the user opts-in to data download.

val conditions = FirebaseModelDownloadConditions.Builder()
        .requireWifi()
        .build()

On top of this,  you can also configure downloads to require charging or device idle.

Managing Translation Models

30MB may not sound like much in an era where even the super power budget phone Pixel 3A comes with 64GB of on-device memory storage but let the language packs accumulate and they get pretty beefy.

That’s why Firebase bestowed upon us the Model Manager

val modelManager = FirebaseTranslateModelManager.getInstance()

This lets us view what models are on-device, delete them, download more.

View Downloaded Models

modelManager.getAvailableModels(FirebaseApp.getInstance())
        .addOnSuccessListener { models ->
            // ...
        }
        .addOnFailureListener {
            // Error.
        }

Delete Existing Models

val deModel = FirebaseTranslateRemoteModel.Builder(FirebaseTranslateLanguage.DE).build()
modelManager.deleteDownloadedModel(deModel)
        .addOnSuccessListener {
            // Model deleted.
        }
        .addOnFailureListener {
            // Error.
        }

Download New Models

val frModel = FirebaseTranslateRemoteModel.Builder(FirebaseTranslateLanguage.FR)
        .setDownloadConditions(conditions)
        .build()

modelManager.downloadRemoteModelIfNeeded(frModel)
        .addOnSuccessListener {
            // Model downloaded.
        }
        .addOnFailureListener {
            // Error.
        }

Disclaimer

Most of the info and snippets above are taken directly from the official Firebase documentation. These posts are created primarily for the ease of understanding of other developers of the discussed topic.

Tags: