你可以使用 ML Kit 翻譯不同語言的文字。機器學習套件 目前支援 59 種語言。
事前準備
- 如果還沒試過 將 Firebase 新增至您的 Android 專案。
- 將 ML Kit Android 程式庫的依附元件新增至模組
(應用程式層級) Gradle 檔案 (通常是
app/build.gradle
):apply plugin: 'com.android.application' apply plugin: 'com.google.gms.google-services' dependencies { // ... implementation 'com.google.firebase:firebase-ml-natural-language:22.0.0' implementation 'com.google.firebase:firebase-ml-natural-language-translate-model:20.0.8' }
翻譯一段文字
如何在兩種語言之間翻譯字串:
建立
FirebaseTranslator
物件,使用來源設定該物件,並 指定語言:Java
// Create an English-German translator: FirebaseTranslatorOptions options = new FirebaseTranslatorOptions.Builder() .setSourceLanguage(FirebaseTranslateLanguage.EN) .setTargetLanguage(FirebaseTranslateLanguage.DE) .build(); final FirebaseTranslator englishGermanTranslator = FirebaseNaturalLanguage.getInstance().getTranslator(options);
Kotlin+KTX
// Create an English-German translator: val options = FirebaseTranslatorOptions.Builder() .setSourceLanguage(FirebaseTranslateLanguage.EN) .setTargetLanguage(FirebaseTranslateLanguage.DE) .build() val englishGermanTranslator = FirebaseNaturalLanguage.getInstance().getTranslator(options)
如果您不知道輸入文字的語言,可以使用 識別資訊 API。(但別忘了 而不是同時在裝置上保留太多語言模型)。
確認所需翻譯模型已下載到裝置。 確認模型可用前,請勿呼叫
translate()
。Java
FirebaseModelDownloadConditions conditions = new FirebaseModelDownloadConditions.Builder() .requireWifi() .build(); englishGermanTranslator.downloadModelIfNeeded(conditions) .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.) } }) .addOnFailureListener( new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Model couldn’t be downloaded or other internal error. // ... } });
Kotlin+KTX
englishGermanTranslator.downloadModelIfNeeded() .addOnSuccessListener { // Model downloaded successfully. Okay to start translating. // (Set a flag, unhide the translation UI, etc.) } .addOnFailureListener { exception -> // Model couldn’t be downloaded or other internal error. // ... }
語言模型大約有 30 MB 的空間,因此不需要下載 除非使用者另有指定,否則只能透過 Wi-Fi 下載下載。個人中心 並刪除不需要的模型 請參閱「明確管理翻譯模型」。
確認下載模型後,請在 將原文語言轉換為
translate()
:Java
englishGermanTranslator.translate(text) .addOnSuccessListener( new OnSuccessListener<String>() { @Override public void onSuccess(@NonNull String translatedText) { // Translation successful. } }) .addOnFailureListener( new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Error. // ... } });
Kotlin+KTX
englishGermanTranslator.translate(text) .addOnSuccessListener { translatedText -> // Translation successful. } .addOnFailureListener { exception -> // Error. // ... }
系統會以您設定的目標語言將譯文傳遞到 成功事件監聽器
明確管理翻譯模型
按照上述說明使用翻譯 API 時,ML Kit 會自動使用 視需要下載特定語言專屬的翻譯模型至裝置。個人中心 也可以明確管理 整合了 ML Kit 的翻譯模型管理 API可用的值包括 舉例來說,如果您想事先下載模型,或是刪除不需要的模型 登入裝置。
Java
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) {
// ...
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Error.
}
});
// 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.
}
});
Kotlin+KTX
val modelManager = FirebaseModelManager.getInstance()
// Get translation models stored on the device.
modelManager.getDownloadedModels(FirebaseTranslateRemoteModel::class.java)
.addOnSuccessListener { models ->
// ...
}
.addOnFailureListener {
// Error.
}
// Delete the German model if it's on the device.
val deModel = FirebaseTranslateRemoteModel.Builder(FirebaseTranslateLanguage.DE).build()
modelManager.deleteDownloadedModel(deModel)
.addOnSuccessListener {
// Model deleted.
}
.addOnFailureListener {
// Error.
}
// Download the French model.
val frModel = FirebaseTranslateRemoteModel.Builder(FirebaseTranslateLanguage.FR).build()
val conditions = FirebaseModelDownloadConditions.Builder()
.requireWifi()
.build()
modelManager.download(frModel, conditions)
.addOnSuccessListener {
// Model downloaded.
}
.addOnFailureListener {
// Error.
}