Class TranslationsController.RuntimeTranslation
- Enclosing class:
- TranslationsController
Performs translations actions that are not dependent on the page. Typical usage is for setting preferences, managing downloads, and getting information on language models available.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Information about a language model.static @interface
The runtime language settings a given language may have that dictates the app's translation offering behavior.static class
Options for managing the translation language models.static @interface
Operations toolkit can perform on the language models.static @interface
Operation type for toolkit to operate on.static class
Language translation options. -
Field Summary
Modifier and TypeFieldDescriptionstatic final String
The all type indicates that the operation should be performed on all model filesstatic final String
The translations engine should always expect this language to be translated and automatically translate on page load.static final String
The cache type indicates that the operation should be performed on model files that do not make up a suit.static final String
The delete operation is for deleting models.static final String
The download operation is for downloading models.static final String
The language type indicates the operation should be performed only on the specified language.static final String
The translations engine should never offer to translate this language.static final String
The translations engine should offer this language to be translated. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic GeckoResult<Long>
checkPairDownloadSize
(String fromLanguage, String toLanguage) When `translate()` is called on a given pair, then the system will downloaded the necessary models to complete the translation.static GeckoResult<Long>
Convenience method forcheckPairDownloadSize(String, String)
.static GeckoResult<String>
getLanguageSetting
(String languageCode) Returns the given language setting for the corresponding language.static GeckoResult<Map<String,
String>> Creates a map of known language codes with their corresponding language setting.static GeckoResult<List<String>>
Gets the list of sites that have a never translate site preference set.static GeckoResult<Boolean>
Checks if the device can use the supplied model binary files for translations.Creates a list of all of the available language models, their size for a full download, and download state.List languages that can be translated to and from.static GeckoResult<Void>
Manage the language model or models.static GeckoResult<List<String>>
Returns the preferred languages of the user in the following order:static GeckoResult<Void>
setLanguageSettings
(String languageCode, String languageSetting) Sets the language state for a given language.static GeckoResult<Void>
setNeverTranslateSpecifiedSite
(Boolean neverTranslate, String origin) Sets whether the specified site should be translated or not.
-
Field Details
-
DOWNLOAD
The download operation is for downloading models.- See Also:
-
DELETE
The delete operation is for deleting models.- See Also:
-
LANGUAGE
The language type indicates the operation should be performed only on the specified language.- See Also:
-
CACHE
The cache type indicates that the operation should be performed on model files that do not make up a suit.- See Also:
-
ALL
The all type indicates that the operation should be performed on all model files- See Also:
-
ALWAYS
The translations engine should always expect this language to be translated and automatically translate on page load.- See Also:
-
OFFER
The translations engine should offer this language to be translated. This is the default state, i.e., no user selection was made.- See Also:
-
NEVER
The translations engine should never offer to translate this language.- See Also:
-
-
Constructor Details
-
RuntimeTranslation
public RuntimeTranslation()
-
-
Method Details
-
isTranslationsEngineSupported
Checks if the device can use the supplied model binary files for translations.Use to check if translations are ever possible.
- Returns:
- true if translations are supported on the device, or false if not.
-
preferredLanguages
Returns the preferred languages of the user in the following order:1. Most recent target languages
2. Web requested languages
3. App languages
4. OS language
- Returns:
- a GeckoResult with a user's preferred language(s) or null or an exception
-
manageLanguageModel
@AnyThread @NonNull public static GeckoResult<Void> manageLanguageModel(@NonNull TranslationsController.RuntimeTranslation.ModelManagementOptions options) Manage the language model or models. Options are to download or delete a BCP 47 language or all or cache.- Parameters:
options
- contain language, operation, and operation level to perform on the model- Returns:
- the request proceeded as expected or an exception.
-
listSupportedLanguages
@AnyThread @NonNull public static GeckoResult<TranslationsController.RuntimeTranslation.TranslationSupport> listSupportedLanguages()List languages that can be translated to and from. Use is populating language selection.- Returns:
- a GeckoResult with a TranslationSupport object with "to" and "from" languages or an exception.
-
checkPairDownloadSize
@AnyThread @NonNull public static GeckoResult<Long> checkPairDownloadSize(@NonNull String fromLanguage, @NonNull String toLanguage) When `translate()` is called on a given pair, then the system will downloaded the necessary models to complete the translation. This method is to check the exact size of those downloads. Typical case is informing the user of the download size for users in a low-data mode.If no download is detected, it will return 0. Note, if the model is not present, this will also result in a value of 0 bytes.
- Parameters:
fromLanguage
- from BCP 47 codetoLanguage
- from BCP 47 code- Returns:
- The size of the file size in bytes. If no download is required, will return 0.
-
checkPairDownloadSize
@AnyThread @NonNull public static GeckoResult<Long> checkPairDownloadSize(@NonNull TranslationsController.SessionTranslation.TranslationPair pair) Convenience method forcheckPairDownloadSize(String, String)
.- Parameters:
pair
- language pair that will be used by `translate()`- Returns:
- The size of the necessary file size in bytes. If no download is required, will return 0.
-
listModelDownloadStates
@AnyThread @NonNull public static GeckoResult<List<TranslationsController.RuntimeTranslation.LanguageModel>> listModelDownloadStates()Creates a list of all of the available language models, their size for a full download, and download state. Expected use is for displaying model state for user management.- Returns:
- A GeckoResult with a list of the available language model's and their states or an exception.
-
getLanguageSetting
@AnyThread @NonNull public static GeckoResult<String> getLanguageSetting(@NonNull String languageCode) Returns the given language setting for the corresponding language.- Parameters:
languageCode
- The BCP 47 language portion of the code to check the settings for. For example, es, en, de, etc.- Returns:
- The
TranslationsController.RuntimeTranslation.LanguageSetting
string for the language.
-
getLanguageSettings
Creates a map of known language codes with their corresponding language setting.- Returns:
- A GeckoResult with a map of each BCP 47 language portion of the code (key) and its
corresponding
TranslationsController.RuntimeTranslation.LanguageSetting
string (value).
-
setLanguageSettings
@AnyThread @NonNull public static GeckoResult<Void> setLanguageSettings(@NonNull String languageCode, @NonNull String languageSetting) Sets the language state for a given language.- Parameters:
languageCode
- - The specified BCP 47 language portion of the code to update. For example, es, en, de, etc.languageSetting
- - The specified setting for a given language.- Returns:
- A GeckoResult that will return void if successful or else will complete exceptionally.
-
getNeverTranslateSiteList
Gets the list of sites that have a never translate site preference set. Should be used for retrieving a list for global preference setting outside of a specific site.Recommend using:
TranslationsController.SessionTranslation.getNeverTranslateSiteSetting()
to query the current session's site's never translate preferences.- Returns:
- A list of display ready site URIs to set preferences for.
-
setNeverTranslateSpecifiedSite
@AnyThread @NonNull public static GeckoResult<Void> setNeverTranslateSpecifiedSite(@NonNull Boolean neverTranslate, @NonNull String origin) Sets whether the specified site should be translated or not. This function should be used for global updates to the never translate list.Please use:
TranslationsController.SessionTranslation.setNeverTranslateSiteSetting(Boolean)
when the session is currently on the site to adjust the permissions for.- Parameters:
origin
- A site origin URI that will have the specified never translate permission set. Recommend using URI values returned fromgetNeverTranslateSiteList()
and using the session to set a given site to ensure proper scope when possible.neverTranslate
- Should be set to true if the site should never be translated or false if it should be translated.- Returns:
- Void if the operation to set the value completed or exceptionally if an issue occurred.
-