In this post, we're going to translate the text from one to another language using ml kit of a firebase in android studio.
We're translating to English but you can translate to any language you required
So, This is the output after you are done with all step:
So, now make it happen :)
Step 1: Add Firebase to your android project:
I recommend you to see how to add firebase to the android project in 5minutes to know how to add it or if you already add it then you can move on to 2nd Step.
Step 2: Add this dependency for the ml kit android libraries to your app-level build.gradle file:
as shown below and then click on Sync Now.:
Step 3: Design the layout of the activity:
as shown below:
Step 4: Identify the language of text:
I recommend you to go through 5 Simple Step To Identify The Language Of Text Using Firebase ML Kit Android Studio 2020 (Complete Guide) With Source Code before going further
Get text from EditText and pass to findLanguageOfText() ,
and now get the language code.'
See the complete list of supported languages code.
as shown here:
Step 5: Translate text from language code :
Make sure to download the required translate model first and don't call translate() until you know the model is available. Language models are around 30MB, so don't download them unnecessarily and only download them using WIFI. You should also delete unneeded models.
See Step 6 to delete the downloaded model.
Define the method used above:
as shown here:
Step 6(Optional): Manage translation model:
When you use the translation API as described above, ML Kit automatically downloads language-specific translation models to the device as required. You can also explicitly manage the translation models you want available on the device by using ML Kit's translation model management API. This can be useful if you want to download models ahead of time or delete unneeded models from the device.
Now, run the app :)
If everything is done correctly then you see the excepted output.
You can see the full source code at GitHub.
If you face any problem or have any suggestion please comment it down we love to answer it.
We're translating to English but you can translate to any language you required
So, This is the output after you are done with all step:
Step 1: Add Firebase to your android project:
I recommend you to see how to add firebase to the android project in 5minutes to know how to add it or if you already add it then you can move on to 2nd Step.
Step 2: Add this dependency for the ml kit android libraries to your app-level build.gradle file:
implementation 'com.google.firebase:firebase-ml-natural-language:22.0.0' implementation 'com.google.firebase:firebase-ml-natural-language-translate-model:20.0.7'
as shown below and then click on Sync Now.:
Step 3: Design the layout of the activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="150dp" android:gravity="center" android:text="Translate Any Text\n To English" android:textSize="18sp" android:textStyle="bold" /> <EditText android:id="@+id/originalText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button" android:layout_centerInParent="true" android:layout_margin="30dp" android:padding="10dp" android:text="Hola Mundo" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Translate text" /> <TextView android:id="@+id/translatedText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button" android:layout_centerInParent="true" android:layout_margin="30dp" android:textColor="@android:color/black" android:textSize="15sp" /></RelativeLayout>
as shown below:
Step 4: Identify the language of text:
I recommend you to go through 5 Simple Step To Identify The Language Of Text Using Firebase ML Kit Android Studio 2020 (Complete Guide) With Source Code before going further
Get text from EditText and pass to findLanguageOfText() ,
and now get the language code.'
See the complete list of supported languages code.
private void findLanguageOfText(final String sourceText) { FirebaseLanguageIdentification languageIdentifier = FirebaseNaturalLanguage.getInstance() .getLanguageIdentification(); languageIdentifier.identifyLanguage(sourceText) .addOnSuccessListener(new OnSuccessListener<String>() { @SuppressLint("SetTextI18n") @Override public void onSuccess(@Nullable String languageCode) { if (languageCode != null) { if (!languageCode.equals("und")) { //pass source Text and language code of source text translateTextOfLanguage(sourceText, languageCode); } else { targetLanguageText.setText("Can't identify language."); } } } });}
as shown here:
Step 5: Translate text from language code :
Make sure to download the required translate model first and don't call translate() until you know the model is available. Language models are around 30MB, so don't download them unnecessarily and only download them using WIFI. You should also delete unneeded models.
See Step 6 to delete the downloaded model.
Define the method used above:
private void translateTextOfLanguage(final String sourceText, String languageCode) { FirebaseTranslatorOptions options = new FirebaseTranslatorOptions.Builder() .setSourceLanguage(FirebaseTranslateLanguage.languageForLanguageCode(languageCode)) .setTargetLanguage(FirebaseTranslateLanguage.EN) .build(); final FirebaseTranslator englishTranslator = FirebaseNaturalLanguage.getInstance().getTranslator(options); FirebaseModelDownloadConditions conditions = new FirebaseModelDownloadConditions.Builder().requireWifi().build(); //download model first englishTranslator.downloadModelIfNeeded(conditions).addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void v) { englishTranslator.translate(sourceText).addOnSuccessListener(new OnSuccessListener<String>() { @Override public void onSuccess(@NonNull String translated) { // Translation successful. progressDialog.cancel(); targetLanguageText.setText(translated); }}); }}); }
as shown here:
Step 6(Optional): Manage translation model:
When you use the translation API as described above, ML Kit automatically downloads language-specific translation models to the device as required. You can also explicitly manage the translation models you want available on the device by using ML Kit's translation model management API. This can be useful if you want to download models ahead of time or delete unneeded models from the device.
private void manageTranslationModel() { FirebaseModelManager modelManager = FirebaseModelManager.getInstance();// Get translation models stored on the device. modelManager.getDownloadedModels(FirebaseTranslateRemoteModel.class) .addOnSuccessListener(new OnSuccessListener<Set<FirebaseTranslateRemoteModel>>() { @Override public void onSuccess(Set<FirebaseTranslateRemoteModel> models) { // ... } }); // Delete the German model if it's on the device. FirebaseTranslateRemoteModel deModel = new FirebaseTranslateRemoteModel.Builder(FirebaseTranslateLanguage.DE).build(); modelManager.deleteDownloadedModel(deModel) .addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void v) { // Model deleted. } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Error. } }); // Download the French model. FirebaseTranslateRemoteModel frModel = new FirebaseTranslateRemoteModel.Builder(FirebaseTranslateLanguage.FR).build(); FirebaseModelDownloadConditions conditions = new FirebaseModelDownloadConditions.Builder() .requireWifi() .build(); modelManager.download(frModel, conditions) .addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void v) { // Model downloaded. } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Error. } }); }
Now, run the app :)
If everything is done correctly then you see the excepted output.
You can see the full source code at GitHub.
If you face any problem or have any suggestion please comment it down we love to answer it.
Comment down what next topic you need a guide on? or Drop a message on our social media handle
Happy coding and designing : )
Comments
Post a Comment