Package org.mozilla.geckoview
Class TranslationsController.RuntimeTranslation
Object
org.mozilla.geckoview.TranslationsController.RuntimeTranslation
- Enclosing class:
- TranslationsController
Runtime translation coordinates runtime messaging between the translations toolkit actor and
GeckoView.
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
Nested ClassesModifier 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
FieldsModifier 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
Constructors -
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<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: 1.static GeckoResult<Void>
setLanguageSettings
(String languageCode, String languageSetting) Sets the language state for a given language.
-
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. App languages 2. Web requested languages 3. 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.Bug 1854691 will add an option for deleting translations model "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 required, will return 0.
Will be implemented in bug 1854691.
- 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.
-