Updated 29 October 2021
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.
1 2 3 4 5 6 |
dependencies { // ... implementation 'com.google.firebase:firebase-ml-natural-language:22.0.0' implementation 'com.google.firebase:firebase-ml-natural-language-translate-model:20.0.7' } |
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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
String text = "Hello How are you" FirebaseTranslatorOptions options = new FirebaseTranslatorOptions.Builder() .setSourceLanguage(FirebaseTranslateLanguage.EN) .setTargetLanguage(FirebaseTranslateLanguage.HI) .build(); final FirebaseTranslator translator = FirebaseNaturalLanguage.getInstance().getTranslator(options); translator.translate(text) .addOnSuccessListener( new OnSuccessListener<String>() { @Override public void onSuccess(@NonNull String translatedText) { //translatedText in hindi language - नमस्ते आप कैसे हैं } }) .addOnFailureListener( new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Error. // ... } }); |
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.
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 –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
String text = "Hello How are you" FirebaseTranslatorOptions options = new FirebaseTranslatorOptions.Builder() .setSourceLanguage(FirebaseTranslateLanguage.EN) .setTargetLanguage(FirebaseTranslateLanguage.HI) .build(); final FirebaseTranslator translator = FirebaseNaturalLanguage.getInstance().getTranslator(options); translator.downloadModelIfNeeded() .addOnSuccessListener( new OnSuccessListener<Void>() { @Override public void onSuccess(Void v) { // Model downloaded successfully. Okay to start translating. // (Set a flag, unhide the translation UI, etc.) startTranslate(translator,text) } }) .addOnFailureListener( new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Model couldn’t be downloaded or other internal error. // ... } }); //Translation process working here. private void startTranslate(FirebaseTranslator translater,String text){ translator.translate(text) .addOnSuccessListener( new OnSuccessListener<String>() { @Override public void onSuccess(@NonNull String translatedText) { // Translation successful.translatedText in hindi language - नमस्ते आप कैसे हैं } }) .addOnFailureListener( new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Error. // ... } }); } |
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
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
4 comments
If you just want to type the hindi spellings of the english words then this will not be the right tool as of now.
But if you want to completely translate the words with their meaning from one laguage to another, then this tool is defnitely for you.
i have getting above error only.when i call first time it will going failure only
Hello karthikeyan,
Firstly, please do share your complete logcat output for the error that you are getting as with the current comment it is hard to deduce what could be the cause of the issue yu are facing.
Secondly, if you could provide the complete code so that I can run it and see for myself then that will be great.
Now guessing for the probable causes of the issues that you are facing would be some what related to the firebase version of the dependencies you are using.
Also, have you made sure that the language model for which you are translating has actually been downloaded on your device.
Further, as per the recent updates from firebase and Google, you should use
“TranslatorOptions.Builder” from the library ” ‘com.google.mlkit:translate:17.0.3‘ “
Hope this helps you out.
Feel free to connect in case you need any further assistance