public final class Translator
- Object
- 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.Session | Reusable owner of a native translation backend. |
Methods
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
optionsLanguageOptions- backend selection, or
nullfor 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
optionsLanguageOptions- 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
textString- source text;
nullis treated as empty sourceLanguageString- BCP-47 source language tag
targetLanguageString- BCP-47 target language tag
optionsLanguageOptions- backend options, or
null
Returns
asynchronous translated text