在上一篇文章中,我们看到了使用Firebase ML套件在Android中使用语言检测器。在本文中,我们将介绍使用Android中的Firebase ML Kit在Android中实现语言翻译器的方法。
我们将在本文中构建什么?
我们将构建一个简单的应用程序,其中将显示一个EditText字段,并将所有输入添加到该TextField 。随之,我们将显示一个Button,以将该文本翻译成德语。单击该按钮后,我们的文本将被翻译成德语,我们可以在文本视图中看到该文本。下面提供了一个示例视频,以使您对本文中的工作有个大概的了解。注意,我们将使用Java语言实现该项目。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。
第2步:将您的应用连接到Firebase
在Android Studio中创建新项目后,将您的应用连接到Firebase。用于将您的应用程序连接到Firebase。导航到顶部栏上的“工具”。之后,单击Firebase。右侧将打开一个新窗口。在该窗口中,单击Firebase ML,然后在Android中单击“使用Firebase ML工具包”。您可以在下面的屏幕截图中看到该选项。
单击此选项后,您将看到以下屏幕。在此屏幕上,单击“连接到Firebase”选项以将您的应用程序连接到Firebase。单击“连接”选项,将您的应用程序连接到Firebase,并将以下依赖项添加到build.gradle文件中。
步骤3:添加语言翻译的依赖关系到build.gradle文件
导航到Gradle脚本> build.gradle(Module:app)并将以下依赖项添加到“依赖项”部分。
// dependancy for firebase core.
implementation’com.google.firebase:firebase-core:15.0.2′
// below two dependancy are used for language detection
implementation ‘com.google.firebase:firebase-ml-natural-language:22.0.0’
implementation ‘com.google.firebase:firebase-ml-natural-language-translate-model:20.0.8’
步骤4:在您的Android应用中添加访问Internet的权限
导航至应用程序> AndroidManifest.xml文件,然后将以下代码添加到其中。在代码中添加了注释,以便更详细地了解。
XML
XML
Java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.ml.common.modeldownload.FirebaseModelDownloadConditions;
import com.google.firebase.ml.naturallanguage.FirebaseNaturalLanguage;
import com.google.firebase.ml.naturallanguage.translate.FirebaseTranslateLanguage;
import com.google.firebase.ml.naturallanguage.translate.FirebaseTranslator;
import com.google.firebase.ml.naturallanguage.translate.FirebaseTranslatorOptions;
public class MainActivity extends AppCompatActivity {
// creating variables for our image view,
// text view and two buttons.
private EditText edtLanguage;
private TextView translateLanguageTV;
private Button translateLanguageBtn;
// create a variable for our
// firebase language translator.
FirebaseTranslator englishGermanTranslator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// on below line we are creating our firebase translate option.
FirebaseTranslatorOptions options =
new FirebaseTranslatorOptions.Builder()
// below line we are specifying our source language.
.setSourceLanguage(FirebaseTranslateLanguage.EN)
// in below line we are displaying our target language.
.setTargetLanguage(FirebaseTranslateLanguage.DE)
// after that we are building our options.
.build();
// below line is to get instance
// for firebase natural language.
englishGermanTranslator = FirebaseNaturalLanguage.getInstance().getTranslator(options);
// on below line we are initializing our variables.
edtLanguage = findViewById(R.id.idEdtLanguage);
translateLanguageTV = findViewById(R.id.idTVTranslatedLanguage);
translateLanguageBtn = findViewById(R.id.idBtnTranslateLanguage);
// adding on click listener for button
translateLanguageBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// calling method to download language
// modal to which we have to translate.
String string = edtLanguage.getText().toString();
downloadModal(string);
}
});
}
private void downloadModal(String input) {
// below line is use to download the modal which
// we will require to translate in german language
FirebaseModelDownloadConditions conditions = new FirebaseModelDownloadConditions.Builder().requireWifi().build();
// below line is use to download our modal.
englishGermanTranslator.downloadModelIfNeeded(conditions).addOnSuccessListener(new OnSuccessListener() {
@Override
public void onSuccess(Void aVoid) {
// this method is called when modal is downloaded successfully.
Toast.makeText(MainActivity.this, "Please wait language modal is being downloaded.", Toast.LENGTH_SHORT).show();
// calling method to translate our entered text.
translateLanguage(input);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "Fail to download modal", Toast.LENGTH_SHORT).show();
}
});
}
private void translateLanguage(String input) {
englishGermanTranslator.translate(input).addOnSuccessListener(new OnSuccessListener() {
@Override
public void onSuccess(String s) {
translateLanguageTV.setText(s);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "Fail to translate", Toast.LENGTH_SHORT).show();
}
});
}
}
步骤5:使用activity_main.xml文件
导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。
XML格式
步骤6:使用MainActivity。 Java文件
转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。
Java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.ml.common.modeldownload.FirebaseModelDownloadConditions;
import com.google.firebase.ml.naturallanguage.FirebaseNaturalLanguage;
import com.google.firebase.ml.naturallanguage.translate.FirebaseTranslateLanguage;
import com.google.firebase.ml.naturallanguage.translate.FirebaseTranslator;
import com.google.firebase.ml.naturallanguage.translate.FirebaseTranslatorOptions;
public class MainActivity extends AppCompatActivity {
// creating variables for our image view,
// text view and two buttons.
private EditText edtLanguage;
private TextView translateLanguageTV;
private Button translateLanguageBtn;
// create a variable for our
// firebase language translator.
FirebaseTranslator englishGermanTranslator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// on below line we are creating our firebase translate option.
FirebaseTranslatorOptions options =
new FirebaseTranslatorOptions.Builder()
// below line we are specifying our source language.
.setSourceLanguage(FirebaseTranslateLanguage.EN)
// in below line we are displaying our target language.
.setTargetLanguage(FirebaseTranslateLanguage.DE)
// after that we are building our options.
.build();
// below line is to get instance
// for firebase natural language.
englishGermanTranslator = FirebaseNaturalLanguage.getInstance().getTranslator(options);
// on below line we are initializing our variables.
edtLanguage = findViewById(R.id.idEdtLanguage);
translateLanguageTV = findViewById(R.id.idTVTranslatedLanguage);
translateLanguageBtn = findViewById(R.id.idBtnTranslateLanguage);
// adding on click listener for button
translateLanguageBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// calling method to download language
// modal to which we have to translate.
String string = edtLanguage.getText().toString();
downloadModal(string);
}
});
}
private void downloadModal(String input) {
// below line is use to download the modal which
// we will require to translate in german language
FirebaseModelDownloadConditions conditions = new FirebaseModelDownloadConditions.Builder().requireWifi().build();
// below line is use to download our modal.
englishGermanTranslator.downloadModelIfNeeded(conditions).addOnSuccessListener(new OnSuccessListener() {
@Override
public void onSuccess(Void aVoid) {
// this method is called when modal is downloaded successfully.
Toast.makeText(MainActivity.this, "Please wait language modal is being downloaded.", Toast.LENGTH_SHORT).show();
// calling method to translate our entered text.
translateLanguage(input);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "Fail to download modal", Toast.LENGTH_SHORT).show();
}
});
}
private void translateLanguage(String input) {
englishGermanTranslator.translate(input).addOnSuccessListener(new OnSuccessListener() {
@Override
public void onSuccess(String s) {
translateLanguageTV.setText(s);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "Fail to translate", Toast.LENGTH_SHORT).show();
}
});
}
}
现在运行您的应用程序,并查看该应用程序的输出。
输出:
Note: When you are using the app for the first time. It will take some time because it will download the modal in the background. We are not adding multiple language support in this application because for each language we have to download the language conversion model so it will make the app heavier and language translation will take so much time.
在以下链接上查看项目: https : //github.com/ChinmayMunje/Firebase-ML-Kit