Translator

public interface Translator implements Closeable LifecycleObserver

A Translation client for translating simple sentences from one language to another.

An Translator is created via getClient(TranslatorOptions).

Example:

Translator translator = Translation.getClient(options);
 

This class can be used from any thread.

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

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

public abstract Task<String> translate (String input)

Translates the given input from the source language into the target language. Source and target languages are provided in the options object that was used to get the instance.

You should check whether the model has been downloaded by calling isModelDownloaded(RemoteModel), passing models for the source and target language. If the model has not been downloaded yet, the returned Task will fail with an MlKitException with error code NOT_FOUND.

Parameters
input a string in the source language
Returns
  • the translated string in the target language