# Class Translator

java.lang.Object
com.codename1.ai.language.Translator

public final class Translator extends Object
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 Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static final class`
  `Translator.Session`
  Reusable owner of a native translation backend.

- ## Method Summary

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

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## 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 `Translator.Session` from `open(LanguageOptions)` when the application will immediately perform repeated translations.

    Parameters:
      `options` - 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` - 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` - source text; `null` is treated as empty
      `sourceLanguage` - BCP-47 source language tag
      `targetLanguage` - BCP-47 target language tag
      `options` - backend options, or `null`
    Returns:
      asynchronous translated text