In this video it shows the steps to implement and use google’s ML Kit to translate languages in your Android App.
It refers to google developers page to get the required implementation for this App’s development:
https://developers.google.com/ml-kit/language/translation/android#java
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
This App is also published on Play store at:
https://play.google.com/store/apps/details?id=com.programmerworld.translatorapp
Complete source code and other details:
package com.programmerworld.translatorapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.mlkit.common.model.DownloadConditions;
import com.google.mlkit.nl.translate.TranslateLanguage;
import com.google.mlkit.nl.translate.Translation;
import com.google.mlkit.nl.translate.Translator;
import com.google.mlkit.nl.translate.TranslatorOptions;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private TextView textView;
private Translator translatorGerman;
private Translator translatorArabic;
private Translator translatorKorean;
private Boolean booleanGerman = false;
private Boolean booleanArabic = false;
private Boolean booleanKorean = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
textView = findViewById(R.id.textView);
TranslatorOptions translatorOptionsGerman = new TranslatorOptions.Builder()
.setSourceLanguage(TranslateLanguage.ENGLISH)
.setTargetLanguage(TranslateLanguage.GERMAN)
.build();
TranslatorOptions translatorOptionsArabic = new TranslatorOptions.Builder()
.setSourceLanguage(TranslateLanguage.ENGLISH)
.setTargetLanguage(TranslateLanguage.ARABIC)
.build();
TranslatorOptions translatorOptionsKorean = new TranslatorOptions.Builder()
.setSourceLanguage(TranslateLanguage.ENGLISH)
.setTargetLanguage(TranslateLanguage.KOREAN)
.build();
translatorGerman = Translation.getClient(translatorOptionsGerman);
translatorArabic = Translation.getClient(translatorOptionsArabic);
translatorKorean = Translation.getClient(translatorOptionsKorean);
downloadModel();
}
private void downloadModel(){
DownloadConditions downloadConditions = new DownloadConditions.Builder()
.requireWifi()
.build();
translatorGerman.downloadModelIfNeeded(downloadConditions)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
booleanGerman = true;
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
booleanGerman = false;
}
});
translatorArabic.downloadModelIfNeeded(downloadConditions)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
booleanArabic = true;
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
booleanArabic = false;
}
});
translatorKorean.downloadModelIfNeeded(downloadConditions)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
booleanKorean = true;
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
booleanKorean = false;
}
});
}
public void buttonDownloadModel(View view){
downloadModel();
}
public void buttonGerman(View view){
if (booleanGerman){
translatorGerman.translate(editText.getText().toString())
.addOnSuccessListener(new OnSuccessListener<String>() {
@Override
public void onSuccess(String s) {
textView.setText(s);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
textView.setText(e.toString());
}
});
}
}
public void buttonArabic(View view){
if (booleanGerman){
translatorArabic.translate(editText.getText().toString())
.addOnSuccessListener(new OnSuccessListener<String>() {
@Override
public void onSuccess(String s) {
textView.setText(s);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
textView.setText(e.toString());
}
});
}
}
public void buttonKorean(View view){
if (booleanGerman){
translatorKorean.translate(editText.getText().toString())
.addOnSuccessListener(new OnSuccessListener<String>() {
@Override
public void onSuccess(String s) {
textView.setText(s);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
textView.setText(e.toString());
}
});
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="238dp"
android:layout_height="99dp"
android:text="Hello World!"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.4" />
<EditText
android:id="@+id/editText"
android:layout_width="219dp"
android:layout_height="56dp"
android:layout_marginStart="91dp"
android:layout_marginTop="59dp"
android:ems="10"
android:hint="English Text here ..."
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="39dp"
android:onClick="buttonGerman"
android:text="German"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="40dp"
android:onClick="buttonArabic"
android:text="Arabic"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="47dp"
android:layout_marginTop="41dp"
android:onClick="buttonKorean"
android:text="Korean"
app:layout_constraintStart_toEndOf="@+id/button2"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="212dp"
android:layout_marginTop="8dp"
android:onClick="buttonDownloadModel"
android:text="Download Model"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
plugins {
id 'com.android.application'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.programmerworld.translatorapp"
minSdk 32
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.6.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.google.mlkit:translate:17.0.0'
}
No changes in Manifest or any other file required.