Translate text with ML Kit on Android
Stay organized with collections
Save and categorize content based on your preferences.
Page Summary
-
ML Kit can translate text between more than 50 languages using on-demand dynamic model downloads.
-
You need to create a
Translatorobject, download the necessary language model, and then use thetranslate()method to translate text. -
Explicit model management allows you to pre-download, delete, and control language models on the device for optimized performance.
-
It's recommended to download models over Wi-Fi and delete unused models to save storage space and bandwidth.
-
Ensure the
close()method is called when theTranslatorobject is no longer needed, potentially using LifecycleObserver for easier management in Fragments or Activities.
You can use ML Kit to translate text between languages. ML Kit can translation between more than 50 languages.
Try it out
- Play around with the sample app to see an example usage of this API.
Before you begin
- In your project-level
build.gradlefile, make sure to include Google's Maven repository in both yourbuildscriptandallprojectssections. - 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' }
Translate a string of text
To translate a string between two languages:
Create a
Translatorobject, configuring it with the source and target languages:Kotlin
// Create an English-German translator: valoptions=TranslatorOptions.Builder() .setSourceLanguage(TranslateLanguage.ENGLISH) .setTargetLanguage(TranslateLanguage.GERMAN) .build() valenglishGermanTranslator=Translation.getClient(options)
Java
// Create an English-German translator: TranslatorOptionsoptions= newTranslatorOptions.Builder() .setSourceLanguage(TranslateLanguage.ENGLISH) .setTargetLanguage(TranslateLanguage.GERMAN) .build(); finalTranslatorenglishGermanTranslator= 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
TranslateLanguageusingTranslateLanguage.fromLanguageTag().Avoid keeping too many language models on the device at once.
Make sure the required translation model has been downloaded to the device. Don't call
translate()until you know the model is available.Kotlin
varconditions=DownloadConditions.Builder() .requireWifi() .build() englishGermanTranslator.downloadModelIfNeeded(conditions) .addOnSuccessListener{ // Model downloaded successfully. Okay to start translating. // (Set a flag, unhide the translation UI, etc.) } .addOnFailureListener{exception-> // Model couldn’t be downloaded or other internal error. // ... }
Java
DownloadConditionsconditions=newDownloadConditions.Builder() .requireWifi() .build(); englishGermanTranslator.downloadModelIfNeeded(conditions) .addOnSuccessListener( newOnSuccessListener
(){ @Override publicvoidonSuccess(Voidv){ // Model downloaded successfully. Okay to start translating. // (Set a flag, unhide the translation UI, etc.) } }) .addOnFailureListener( newOnFailureListener(){ @Override publicvoidonFailure(@NonNullExceptione){ // Model couldn’t be downloaded or other internal error. // ... } }); 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.
After you confirm the model has been downloaded, pass a string of text in the source language to
translate():Kotlin
englishGermanTranslator.translate(text) .addOnSuccessListener{translatedText-> // Translation successful. } .addOnFailureListener{exception-> // Error. // ... }
Java
englishGermanTranslator.translate(text) .addOnSuccessListener( newOnSuccessListener
(){ @Override publicvoidonSuccess(@NonNullStringtranslatedText){ // Translation successful. } }) .addOnFailureListener( newOnFailureListener(){ @Override publicvoidonFailure(@NonNullExceptione){ // Error. // ... } }); The translated text, in the target language you configured, is passed to the success listener.
Ensure that the
close()method is called when theTranslatorobject 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:
Kotlin
valoptions=... valtranslator=Translation.getClient(options) getLifecycle().addObserver(translator)
Java
TranslatorOptionsoptions=... Translatortranslator=Translation.getClient(options); getLifecycle().addObserver(translator); ...usetranslator...
Note that this assumes that the code is inside of a class that implements LifecycleOwner (e.g. a Fragment or AppCompatActivity).
Explicitly manage translation models
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.
Kotlin
valmodelManager=RemoteModelManager.getInstance() // Get translation models stored on the device. modelManager.getDownloadedModels(TranslateRemoteModel::class.java) .addOnSuccessListener{models-> // ... } .addOnFailureListener{ // Error. } // Delete the German model if it's on the device. valgermanModel=TranslateRemoteModel.Builder(TranslateLanguage.GERMAN).build() modelManager.deleteDownloadedModel(germanModel) .addOnSuccessListener{ // Model deleted. } .addOnFailureListener{ // Error. } // Download the French model. valfrenchModel=TranslateRemoteModel.Builder(TranslateLanguage.FRENCH).build() valconditions=DownloadConditions.Builder() .requireWifi() .build() modelManager.download(frenchModel,conditions) .addOnSuccessListener{ // Model downloaded. } .addOnFailureListener{ // Error. }
Java
RemoteModelManagermodelManager=RemoteModelManager.getInstance(); // Get translation models stored on the device. modelManager.getDownloadedModels(TranslateRemoteModel.class) .addOnSuccessListener(newOnSuccessListener<Set>(){ @Override publicvoidonSuccess(Setmodels){ // ... } }) .addOnFailureListener(newOnFailureListener(){ @Override publicvoidonFailure(@NonNullExceptione){ // Error. } }); // Delete the German model if it's on the device. TranslateRemoteModelgermanModel= newTranslateRemoteModel.Builder(TranslateLanguage.GERMAN).build(); modelManager.deleteDownloadedModel(germanModel) .addOnSuccessListener(newOnSuccessListener(){ @Override publicvoidonSuccess(Voidv){ // Model deleted. } }) .addOnFailureListener(newOnFailureListener(){ @Override publicvoidonFailure(@NonNullExceptione){ // Error. } }); // Download the French model. TranslateRemoteModelfrenchModel= newTranslateRemoteModel.Builder(TranslateLanguage.FRENCH).build(); DownloadConditionsconditions=newDownloadConditions.Builder() .requireWifi() .build(); modelManager.download(frenchModel,conditions) .addOnSuccessListener(newOnSuccessListener (){ @Override publicvoidonSuccess(Voidv){ // Model downloaded. } }) .addOnFailureListener(newOnFailureListener(){ @Override publicvoidonFailure(@NonNullExceptione){ // Error. } });
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026年09月01日 UTC.