您可以使用机器学习套件在语言之间翻译文本。机器学习套件 可以在超过 50 种语言。
<ph type="x-smartling-placeholder">试试看
- 您可以试用示例应用, 请查看此 API 的用法示例。
准备工作
<ph type="x-smartling-placeholder">- 在项目级
build.gradle
文件中,请务必包含buildscript
和 Google 的 Maven 制品库allprojects
个版块。 - 将 Android 版机器学习套件库的依赖项添加到模块的
应用级 Gradle 文件,通常为
app/build.gradle
:dependencies { // ... implementation 'com.google.mlkit:translate:17.0.3' }
翻译文本字符串
如需在两种语言之间翻译字符串,请执行以下操作:
创建
Translator
对象,使用源语言和目标语言对其进行配置:Kotlin
// Create an English-German translator: val options = TranslatorOptions.Builder() .setSourceLanguage(TranslateLanguage.ENGLISH) .setTargetLanguage(TranslateLanguage.GERMAN) .build() val englishGermanTranslator = Translation.getClient(options)
Java
// Create an English-German translator: TranslatorOptions options = new TranslatorOptions.Builder() .setSourceLanguage(TranslateLanguage.ENGLISH) .setTargetLanguage(TranslateLanguage.GERMAN) .build(); final Translator englishGermanTranslator = Translation.getClient(options);
如果您不知道输入文本的语言,则可以使用语言 Identification API 会给出一个语言标记。然后将该代码转换为
TranslateLanguage
使用TranslateLanguage.fromLanguageTag()
。避免同时在设备上保留过多语言模型。
确保已将所需的翻译模型下载到设备上。 确信模型可用之后,再调用
translate()
。Kotlin
var conditions = DownloadConditions.Builder() .requireWifi() .build() englishGermanTranslator.downloadModelIfNeeded(conditions) .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. // ... }
Java
DownloadConditions conditions = new DownloadConditions.Builder() .requireWifi() .build(); englishGermanTranslator.downloadModelIfNeeded(conditions) .addOnSuccessListener( new OnSuccessListener
() { @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. // ... } }); 语言模型大约为 30MB,因此不要不必要地下载它们, 除非用户另行指定,否则只能使用 Wi-Fi 下载应用。您 也应删除不需要的模型。 请参阅显式管理翻译模型。
确认模型已下载后,将文本字符串传递给 源语言
translate()
:Kotlin
englishGermanTranslator.translate(text) .addOnSuccessListener { translatedText -> // Translation successful. } .addOnFailureListener { exception -> // Error. // ... }
Java
englishGermanTranslator.translate(text) .addOnSuccessListener( new OnSuccessListener
() { @Override public void onSuccess(@NonNull String translatedText) { // Translation successful. } }) .addOnFailureListener( new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Error. // ... } }); 采用您配置的目标语言的译文即会传递到 成功监听器。
确保
close()
方法时,系统会调用Translator
对象。如果你在 Fragment 或 AppCompatActivity 中使用翻译器 方法是调用LifecycleOwner.getLifecycle() 然后调用 Lifecycle.addObserver。 例如:
Kotlin
val options = ... val translator = Translation.getClient(options) getLifecycle().addObserver(translator)
Java
TranslatorOptions options = ... Translator translator = Translation.getClient(options); getLifecycle().addObserver(translator); ... use translator ...
请注意,此方法假定代码位于实现 LifecycleOwner(例如 Fragment 或 AppCompatActivity)。
显式管理翻译模型
当您按上述说明使用 Translation API 时,机器学习套件会自动 根据需要将特定语言的翻译模型下载到设备。您 还可以明确管理您希望在集群上提供的 使用机器学习套件的翻译模型管理 API 构建机器学习模型。可以是 如果您想提前下载模型或删除不需要的模型 。
Kotlin
val modelManager = RemoteModelManager.getInstance() // Get translation models stored on the device. modelManager.getDownloadedModels(TranslateRemoteModel::class.java) .addOnSuccessListener { models -> // ... } .addOnFailureListener { // Error. } // Delete the German model if it's on the device. val germanModel = TranslateRemoteModel.Builder(TranslateLanguage.GERMAN).build() modelManager.deleteDownloadedModel(germanModel) .addOnSuccessListener { // Model deleted. } .addOnFailureListener { // Error. } // Download the French model. val frenchModel = TranslateRemoteModel.Builder(TranslateLanguage.FRENCH).build() val conditions = DownloadConditions.Builder() .requireWifi() .build() modelManager.download(frenchModel, conditions) .addOnSuccessListener { // Model downloaded. } .addOnFailureListener { // Error. }
Java
RemoteModelManager modelManager = RemoteModelManager.getInstance(); // Get translation models stored on the device. modelManager.getDownloadedModels(TranslateRemoteModel.class) .addOnSuccessListener(new OnSuccessListener>() { @Override public void onSuccess(Set models) { // ... } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Error. } }); // Delete the German model if it's on the device. TranslateRemoteModel germanModel = new TranslateRemoteModel.Builder(TranslateLanguage.GERMAN).build(); modelManager.deleteDownloadedModel(germanModel) .addOnSuccessListener(new OnSuccessListener () { @Override public void onSuccess(Void v) { // Model deleted. } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Error. } }); // Download the French model. TranslateRemoteModel frenchModel = new TranslateRemoteModel.Builder(TranslateLanguage.FRENCH).build(); DownloadConditions conditions = new DownloadConditions.Builder() .requireWifi() .build(); modelManager.download(frenchModel, conditions) .addOnSuccessListener(new OnSuccessListener () { @Override public void onSuccess(Void v) { // Model downloaded. } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Error. } });