public final class Translator

  1. Object
  2. Translator
Translates text on device with lazily installed language-pair models. The first request for a pair may take longer while ML Kit downloads the model; download failures are reported through the returned resource.

Nested types

class Translator.SessionReusable owner of a native translation backend.

Methods

public static boolean isSupported()
public static boolean isSupported(LanguageOptions options)Tests whether translation is available for a backend selection.
public static Translator.Session open(LanguageOptions options)Opens a reusable translation session.
public static AsyncResource<String> translate(String text, String sourceLanguage, String targetLanguage, LanguageOptions options)Option values are copied before asynchronous backend work begins.

Inherited methods

Method details

isSupported

public static boolean isSupported()

Returns

whether automatic on-device translation is available

isSupported

public static boolean isSupported(LanguageOptions options)

Tests whether translation is available for a backend selection.

This capability check creates and closes a temporary native backend. Retain a Session from open(LanguageOptions) when the application will immediately perform repeated translations.

Parameters

options LanguageOptions
backend selection, or null for defaults

Returns

whether the selected backend supports translation

open

public static Translator.Session open(LanguageOptions options)
Opens a reusable translation session. Reuse it for repeated requests so native translation clients and downloaded model state are retained.

Parameters

options LanguageOptions
backend options, or null

Returns

a reusable session that owns one native language backend

Throws

UnsupportedOperationException
if the selected backend is absent

translate

public static AsyncResource<String> translate(String text, String sourceLanguage, String targetLanguage, LanguageOptions options)
Option values are copied before asynchronous backend work begins. Current ML Kit backends accept a BCP-47 tag with an optional script or region, such as en-US, and select the corresponding supported base-language model (en in this example). The asynchronous resource fails when either tag does not identify a supported model. Cancelling the returned resource suppresses late result callbacks; the temporary backend is released after its pending native work settles.

Parameters

text String
source text; null is treated as empty
sourceLanguage String
BCP-47 source language tag
targetLanguage String
BCP-47 target language tag
options LanguageOptions
backend options, or null

Returns

asynchronous translated text