Translator
Stay organized with collections
Save and categorize content based on your preferences.
Public Method Summary
abstract void |
close()
Closes the translator and releases its resources.
|
abstract Task<Void>
|
downloadModelIfNeeded()
Downloads the model files required for translation, if they are not already
present.
|
abstract Task<Void>
|
downloadModelIfNeeded(DownloadConditions conditions)
Downloads the model files required for translation, if they are not already
present, when the given conditions are met.
|
abstract Task<String>
|
translate( String input)
Translates the given input from the source language into the
target language.
|
Inherited Method Summary
From interface java.io.Closeable
From interface java.lang.AutoCloseable
Public Methods
public abstract void close ()
Closes the translator and releases its resources.
public abstract Task<Void> downloadModelIfNeeded ()
Downloads the model files required for translation, if they are not already present.
The download will be triggered as soon as there is a network connection, with no other
conditions.
Returns
- a
Task
that will
be completed when the required files have been downloaded
public abstract Task<Void> downloadModelIfNeeded (DownloadConditions conditions)
Downloads the model files required for translation, if they are not already present,
when the given conditions
are met.
Returns
- a
Task
that will
be completed when the required files have been downloaded
Parameters
input |
a string in the source language |
Returns
- the translated string in the target language
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-10-31 UTC.
[null,null,["Last updated 2024-10-31 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eTranslator\u003c/code\u003e interface facilitates translating text between languages using Machine Learning Kit.\u003c/p\u003e\n"],["\u003cp\u003eYou create a \u003ccode\u003eTranslator\u003c/code\u003e instance using \u003ccode\u003eTranslation.getClient(TranslatorOptions)\u003c/code\u003e with specified source and target languages.\u003c/p\u003e\n"],["\u003cp\u003eBefore translating, ensure the necessary language models are downloaded using \u003ccode\u003edownloadModelIfNeeded()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003etranslate()\u003c/code\u003e method performs the translation, returning a \u003ccode\u003eTask\u003c/code\u003e that resolves to the translated string.\u003c/p\u003e\n"],["\u003cp\u003eRemember to close the \u003ccode\u003eTranslator\u003c/code\u003e using \u003ccode\u003eclose()\u003c/code\u003e when finished to release resources.\u003c/p\u003e\n"]]],[],null,["# Translator\n\npublic interface **Translator** extends [Closeable](//developer.android.com/reference/java/io/Closeable.html), [LifecycleObserver](//developer.android.com/reference/androidx/lifecycle/LifecycleObserver.html) \nA [Translation](/android/reference/com/google/mlkit/nl/translate/Translation)\nclient for translating simple sentences from one language to another.\n\nAn [Translator](/android/reference/com/google/mlkit/nl/translate/Translator) is\ncreated via [Translation.getClient(TranslatorOptions)](/android/reference/com/google/mlkit/nl/translate/Translation#getClient(com.google.mlkit.nl.translate.TranslatorOptions)).\n\nExample: \n\n Translator translator = Translation.getClient(options); \n\nThis class can be used from any thread. \n\n### Public Method Summary\n\n|------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract void | [close](/android/reference/com/google/mlkit/nl/translate/Translator#close())() Closes the translator and releases its resources. |\n| abstract Task\\\u003c[Void](//developer.android.com/reference/java/lang/Void.html)\\\u003e | [downloadModelIfNeeded](/android/reference/com/google/mlkit/nl/translate/Translator#downloadModelIfNeeded())() Downloads the model files required for translation, if they are not already present. |\n| abstract Task\\\u003c[Void](//developer.android.com/reference/java/lang/Void.html)\\\u003e | [downloadModelIfNeeded](/android/reference/com/google/mlkit/nl/translate/Translator#downloadModelIfNeeded(com.google.mlkit.common.model.DownloadConditions))(DownloadConditions conditions) Downloads the model files required for translation, if they are not already present, when the given `conditions` are met. |\n| abstract Task\\\u003c[String](//developer.android.com/reference/java/lang/String.html)\\\u003e | [translate](/android/reference/com/google/mlkit/nl/translate/Translator#translate(java.lang.String))([String](//developer.android.com/reference/java/lang/String.html) input) Translates the given `input` from the source language into the target language. |\n\n### Inherited Method Summary\n\nFrom interface java.io.Closeable \n\n|---------------|---------|\n| abstract void | close() |\n\nFrom interface java.lang.AutoCloseable \n\n|---------------|---------|\n| abstract void | close() |\n\nPublic Methods\n--------------\n\n#### public abstract void **close** ()\n\nCloses the translator and releases its resources. \n\n#### public abstract Task\\\u003c[Void](//developer.android.com/reference/java/lang/Void.html)\\\u003e **downloadModelIfNeeded** ()\n\nDownloads the model files required for translation, if they are not already present.\nThe download will be triggered as soon as there is a network connection, with no other\nconditions. \n\n##### Returns\n\n- a [Task](/android/reference/com/google/android/gms/tasks/Task) that will be completed when the required files have been downloaded \n\n#### public abstract Task\\\u003c[Void](//developer.android.com/reference/java/lang/Void.html)\\\u003e **downloadModelIfNeeded** (DownloadConditions conditions)\n\nDownloads the model files required for translation, if they are not already present,\nwhen the given `conditions` are met. \n\n##### Returns\n\n- a [Task](/android/reference/com/google/android/gms/tasks/Task) that will be completed when the required files have been downloaded \n\n#### public abstract Task\\\u003c[String](//developer.android.com/reference/java/lang/String.html)\\\u003e\n**translate** ([String](//developer.android.com/reference/java/lang/String.html) input)\n\nTranslates the given `input` from the source language into the target\nlanguage. Source and target languages are provided in the [options](/android/reference/com/google/mlkit/nl/translate/TranslatorOptions)\nobject that was used to get the instance.\n\nYou should check whether the model has been downloaded by calling [RemoteModelManager.isModelDownloaded(RemoteModel)](/android/reference/com/google/mlkit/common/model/RemoteModelManager#isModelDownloaded(com.google.mlkit.common.model.RemoteModel)), passing models for the\nsource and target language. If the model has not been downloaded yet, the returned\n[Task](/android/reference/com/google/android/gms/tasks/Task) will fail\nwith an [MlKitException](/android/reference/com/google/mlkit/common/MlKitException)\nwith error code [MlKitException.NOT_FOUND](/android/reference/com/google/mlkit/common/MlKitException#NOT_FOUND). \n\n##### Parameters\n\n| input | a string in the source language |\n|-------|---------------------------------|\n\n##### Returns\n\n- the translated string in the target language"]]