Class Translator
java.lang.Object
com.codename1.ai.language.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 Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classReusable owner of a native translation backend. -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanstatic booleanisSupported(LanguageOptions options) Tests whether translation is available for a backend selection.static Translator.Sessionopen(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.
-
Method Details
-
isSupported
public static boolean isSupported()- Returns:
- whether automatic on-device translation is available
-
isSupported
Tests whether translation is available for a backend selection.
This capability check creates and closes a temporary native backend. Retain a
Translator.Sessionfromopen(LanguageOptions)when the application will immediately perform repeated translations.- Parameters:
options- backend selection, ornullfor defaults- Returns:
- whether the selected backend supports translation
-
open
Opens a reusable translation session. Reuse it for repeated requests so native translation clients and downloaded model state are retained.- Parameters:
options- backend options, ornull- 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 asen-US, and select the corresponding supported base-language model (enin 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;nullis treated as emptysourceLanguage- BCP-47 source language tagtargetLanguage- BCP-47 target language tagoptions- backend options, ornull- Returns:
- asynchronous translated text
-