How To Integrate A Language Detector & Translator Using Firebase ML Kit

ZealousWeb
4 min readFeb 10, 2021

--

Globalisation has increased and in turn, lowered production costs by increasing artificial intelligence (AI). Today we’ve allowed AI to deliver accurate translation applications, such as Google Translate, Amazon Translate and Microsoft Translator which have evolved in late years.

The COVID-19 pandemic and its associated limitations have cooled face to face encounters but intensified the use of interactive and virtual meetings. When these meetings and communication are arranged between people who speak different languages, translations or explanations from one language to another are required.

Because of their simplicity, the applications gain millions and are used by customers hourly! In making applications accessible to local citizens, translation has an essential function. You have a greater audience scope if you can translate your application into local spoken languages. The app’s success also grows as usability increases. Translating your application is a must for users who use text or call functionality to communicate with their loved ones.

Translation apps have helped retail companies and services companies enter broader markets and serve customers from numerous countries. It is easier for smaller businesses, without having to worry about high translation costs, to sell products/services internationally.

It is vital to choose the correct translation service because many details can be confused or lost. Translation miscommunications can damage more than one way to your company. In addition to a loss of money, reputational harm and industrial disasters will render you vulnerable.

To create a language translator app or add a small module of a translator in the mobile apps, Firebase MLKit is providing the easiest way.

One of the essential features of Firebase MLKit is Language Identification. When you will get the user-defined data & you have to perform any text operation with that text, at that time, you will not have any information about the language of that text. Language identification can be beneficial in that scenario.

With ML Kit’s on-device language identification API, you can determine a string of text’s language.

We have already discussed the powerful features of Firebase MLKit in the previous article:
Everything You Need To Know About ML Kit For Firebase

Now let’s see how you can implement a language translator & how to identify the language in your Android application.

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

Identify The Language Of The Text

Below are the steps to implement language identifier in Android using Kotlin as a programming language:

Step: 2 — Add Below Dependencies In Your App-Level Build Gradle File

implementation ‘com.google.firebase:firebase-core:16.0.6’
implementation ‘com.google.firebase:firebase-ml-vision:18.0.2’

Step: 3 — Write Below Kotlin Code

FirebaseLanguageIdentification languageIdentifier =
FirebaseNaturalLanguage.getInstance().getLanguageIdentification();
languageIdentifier.identifyLanguage(text)
.addOnSuccessListener(
new OnSuccessListener() {
@Override
public void onSuccess(@Nullable String languageCode) {
if (languageCode != “und”) {
Log.i(TAG, “Language: “ + languageCode);
} else {
Log.i(TAG, “Can’t identify language.”);
}
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Model couldn’t be loaded or other internal error.
// …
}
});

That’s it. This way, you can detect the language from the added text.

Firebase supports more than 100 languages to identify the text. You can check supported languages from:
https://developers.google.com/ml-kit/language/identification/langid-support

Language Translation:

Please follow the below steps to add a language translator in the android project.

Step: 1 — Add Below Dependencies In Your Build Gradle File For Translating The Language Of The Text

implementation
‘com.google.firebase:firebase-ml-natural-language:22.0.0’
implementation
‘com.google.firebase:firebase-ml-natural-language-language-id-model:20.0.7’

Step: 2 — Write Below Kotlin Code

textTranslated().execute(
intDetectLanguageCode.toString(),
intTranslateLanguageCode.toString(),
)

val options = FirebaseTranslatorOptions.Builder()
//from language
.setSourceLanguage(strDetectLanguageCode.toInt())
// to language
.setTargetLanguage(strTargetLanguageCode.toInt())
.build()

val translator = FirebaseNaturalLanguage.getInstance()
.getTranslator(options)

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

translator.downloadModelIfNeeded(conditions).addOnSuccessListener(OnSuccessListener {
translator.translate(strTranslateLanguage)
.addOnSuccessListener(
OnSuccessListener { translatedText ->
strTranslatedData = translatedText
})
.addOnFailureListener(
OnFailureListener { e ->

Your process of translation is done. Moreover, as described in the top, you can interpret in several languages.

Result

Check below video for the output of the done sample application:
https://drive.google.com/file/d/1oKdQAVJwTdqezeES153epU3Lu4fi0gad/view

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

Wrapping Up

As explained in the above demo, Firebase MLKit is an open-source & feasible way to implement the features like translation & detection of language in the android application.

From now onwards, MLKit features can be implemented using MLKit SDK without using Firebase also. ML Kit is essentially a cross-platform mobile SDK (Android and iOS) developed by Google. That brings Google’s on-device machine learning. MLKit SDK is also providing features like ML Kit Text Recognition, Language Identification, Translation APIs etc.

You can download the full source code from GitHub Link. If you want to know about any other features of Firebase MLKit, then please let us know.

We at Zealousweb always help you to enhance the features of your application in a better way. Firebase MLKit can also be used for iOS application development. We are going to publish the article on this soon. Stay tuned !!

Originally published at https://www.zealousweb.com.

--

--

ZealousWeb

Helping businesses Solve The Unsolved with a tech-first approach to expedite digital transformation.