This API requires Android API level 21 or above. Make sure that your app's build file uses a minSdkVersion value of 21 or higher.
In your project-level build.gradle file, make sure to include Google's Maven repository in both your buildscript and allprojects sections.
Add the dependencies for the ML Kit Android libraries to your module's app-level gradle file, which is usually app/build.gradle:
dependencies {
// ...
implementation 'com.google.mlkit:translate:17.0.3'
}
To translate a string between two languages:
Create a Translator object, configuring it with the source and target languages:
// Create an English-German translator:
TranslatorOptions options =
new TranslatorOptions.Builder()
.setSourceLanguage(TranslateLanguage.ENGLISH)
.setTargetLanguage(TranslateLanguage.GERMAN)
.build();
final Translator englishGermanTranslator =
Translation.getClient(options);
If you don't know the language of the input text, you can use the Language Identification API which gives you a language tag. Then convert the tag to a TranslateLanguage using TranslateLanguage.fromLanguageTag().
Avoid keeping too many language models on the device at once.
2. Make sure the required translation model has been downloaded to the device. Don't call translate() until you know the model is available.
DownloadConditions conditions = new DownloadConditions.Builder()
.requireWifi()
.build();
englishGermanTranslator.downloadModelIfNeeded(conditions)
.addOnSuccessListener(
new OnSuccessListener
Language models are around 30MB, so don't download them unnecessarily, and only download them using Wi-Fi unless the user has specified otherwise. You should also delete unneeded models. See Explicitly manage translation models.
3. After you confirm the model has been downloaded, pass a string of text in the source language to translate():
englishGermanTranslator.translate(text)
.addOnSuccessListener(
new OnSuccessListener
The translated text, in the target language you configured, is passed to the success listener.
4. Ensure that the close() method is called when the Translator object will no longer be used.
If you are using a Translator in a Fragment or AppCompatActivity, one easy way to do that is call LifecycleOwner.getLifecycle() on the Fragment or AppCompatActivity, and then call Lifecycle.addObserver. For example:
TranslatorOptions options = ...
Translator translator = Translation.getClient(options);
getLifecycle().addObserver(translator);
... use translator ...
When you use the translation API as described above, ML Kit automatically downloads language-specific translation models to the device as required. You can also explicitly manage the translation models you want available on the device by using ML Kit's translation model management API. This can be useful if you want to download models ahead of time, or delete unneeded models from the device.
RemoteModelManager modelManager = RemoteModelManager.getInstance();
// Get translation models stored on the device.
modelManager.getDownloadedModels(TranslateRemoteModel.class)
.addOnSuccessListener(new OnSuccessListener