# Translator

public interface **Translator** extends [Closeable](https://developer.android.com/reference/java/io/Closeable.html), [LifecycleObserver](https://developer.android.com/reference/androidx/lifecycle/LifecycleObserver.html) A `https://developers.google.com/android/reference/com/google/mlkit/nl/translate/Translation`
client for translating simple sentences from one language to another.

An `https://developers.google.com/android/reference/com/google/mlkit/nl/translate/Translator` is
created via `https://developers.google.com/android/reference/com/google/mlkit/nl/translate/Translation#getClient(com.google.mlkit.nl.translate.TranslatorOptions)`.

Example:

     Translator translator = Translation.getClient(options); 

This class can be used from any thread.

### Public Method Summary

|---|---|
| abstract void | [close](https://developers.google.com/android/reference/com/google/mlkit/nl/translate/Translator#close())() Closes the translator and releases its resources. |
| abstract Task\<[Void](https://developer.android.com/reference/java/lang/Void.html)\> | [downloadModelIfNeeded](https://developers.google.com/android/reference/com/google/mlkit/nl/translate/Translator#downloadModelIfNeeded())() Downloads the model files required for translation, if they are not already present. |
| abstract Task\<[Void](https://developer.android.com/reference/java/lang/Void.html)\> | [downloadModelIfNeeded](https://developers.google.com/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. |
| abstract Task\<[String](https://developer.android.com/reference/java/lang/String.html)\> | [translate](https://developers.google.com/android/reference/com/google/mlkit/nl/translate/Translator#translate(java.lang.String))([String](https://developer.android.com/reference/java/lang/String.html) input) Translates the given `input` from the source language into the target language. |

### Inherited Method Summary

From interface java.io.Closeable

|---|---|
| abstract void | close() |

From interface java.lang.AutoCloseable

|---|---|
| abstract void | close() |

## Public Methods

#### public abstract void **close** ()

Closes the translator and releases its resources.

#### public abstract Task\<[Void](https://developer.android.com/reference/java/lang/Void.html)\> **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 `https://developers.google.com/android/reference/com/google/android/gms/tasks/Task` that will be completed when the required files have been downloaded

#### public abstract Task\<[Void](https://developer.android.com/reference/java/lang/Void.html)\> **downloadModelIfNeeded** (DownloadConditions conditions)

Downloads the model files required for translation, if they are not already present,
when the given `conditions` are met.

##### Returns

- a `https://developers.google.com/android/reference/com/google/android/gms/tasks/Task` that will be completed when the required files have been downloaded

#### public abstract Task\<[String](https://developer.android.com/reference/java/lang/String.html)\>
**translate** ([String](https://developer.android.com/reference/java/lang/String.html) input)

Translates the given `input` from the source language into the target
language. Source and target languages are provided in the `https://developers.google.com/android/reference/com/google/mlkit/nl/translate/TranslatorOptions`
object that was used to get the instance.

You should check whether the model has been downloaded by calling `https://developers.google.com/android/reference/com/google/mlkit/common/model/RemoteModelManager#isModelDownloaded(com.google.mlkit.common.model.RemoteModel)`, passing models for the
source and target language. If the model has not been downloaded yet, the returned
`https://developers.google.com/android/reference/com/google/android/gms/tasks/Task` will fail
with an `https://developers.google.com/android/reference/com/google/mlkit/common/MlKitException`
with error code `https://developers.google.com/android/reference/com/google/mlkit/common/MlKitException#NOT_FOUND`.

##### Parameters

| input | a string in the source language |
|---|---|

##### Returns

- the translated string in the target language