您可以使用 ML Kit 在不同語言之間翻譯文字。ML Kit 目前支援59 種語言之間的翻譯。
事前準備
- 如果您尚未將 Firebase 新增至 Android 專案,請新增 Firebase。
- 將 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
// 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
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
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
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.
}