Firebase ML Kit 8: Text Translation

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:

Preparing for Translation

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

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.

Finally, perform the translation.

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.

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

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

View Downloaded Models

Delete Existing Models

Download New Models

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.