Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated Apr 29, 2024

Translate language

Amplify Android v1 is deprecated as of June 1st, 2024. No new features or bug fixes will be added. Dependencies may become outdated and potentially introduce compatibility issues.

Please use the latest version (v2) of Amplify Library for Android to get started. Refer to the upgrade guide for instructions on upgrading your application to the latest version.

Amplify libraries should be used for all new cloud connected applications. If you are currently using the AWS Mobile SDK for Android, you can access the documentation here.

Set up the backend

Run amplify add predictions, then use the following answers:

? Please select from one of the categories below
`Convert`
? What would you like to convert? (Use arrow keys)
`Translate text into a different language`
? Provide a friendly name for your resource
translate
? What is the source language? (Use arrow keys)
`US English`
? What is the target language? (Use arrow keys)
`Italian`
? Who should have access? (Use arrow keys)
`Auth and Guest users`

Run amplify push to create the resources in the cloud.

Working with the API

Translate text as configured

Open MainActivity.java and add the following to the bottom of onCreate():

Amplify.Predictions.translateText("I like to eat spaghetti",
result -> Log.i("MyAmplifyApp", result.getTranslatedText()),
error -> Log.e("MyAmplifyApp", "Translation failed", error)
);

Open MainActivity.kt and add the following to the bottom of onCreate():

Amplify.Predictions.translateText("I like to eat spaghetti",
{ Log.i("MyAmplifyApp", it.translatedText) },
{ Log.e("MyAmplifyApp", "Translation failed", it) }
)

Open MainActivity.kt and add the following to the bottom of onCreate():

val text = "I like to eat spaghetti"
try {
val result = Amplify.Predictions.translateText(text)
Log.i("MyAmplifyApp", result.translatedText)
} catch (error: PredictionsException) {
Log.e("MyAmplifyApp", "Translation failed", error)
}

Open MainActivity.java and add the following to the bottom of onCreate():

RxAmplify.Predictions.translateText("I like to eat spaghetti")
.subscribe(
result -> Log.i("MyAmplifyApp", result.getTranslatedText()),
error -> Log.e("MyAmplifyApp", "Translation failed", error)
);

As a result of running this code, you will see the translated text printed to the console.

I/MyAmplifyApp: Mi piace mangiare gli spaghetti

Override configured language

In order to override any choices you made in regards to target or source languages while adding this resource using the Amplify CLI, you can pass them in directly as parameters as shown below.

Add the LanguageType options as below:

Amplify.Predictions.translateText(
"I like to eat spaghetti", LanguageType.ENGLISH, LanguageType.RUSSIAN,
result -> Log.i("MyAmplifyApp", result.getTranslatedText()),
error -> Log.e("MyAmplifyApp", "Translation failed", error)
);
Amplify.Predictions.translateText(
"I like to eat spaghetti", LanguageType.ENGLISH, LanguageType.RUSSIAN,
{ Log.i("MyAmplifyApp", it.translatedText) },
{ Log.e("MyAmplifyApp", "Translation failed", it) }
)

Open MainActivity.kt and add the following to the bottom of onCreate():

val text = "I like to eat spaghetti"
try {
val result = Amplify.Predictions.translateText(text, ENGLISH, RUSSIAN)
Log.i("MyAmplifyApp", result.translatedText)
} catch (error: PredictionsException) {
Log.e("MyAmplifyApp", "Translation failed", error)
}
RxAmplify.Predictions.translateText("I like to eat spaghetti",
LanguageType.ENGLISH,
LanguageType.RUSSIAN)
.subscribe(
result -> Log.i("MyAmplifyApp", result.getTranslatedText()),
error -> Log.e("MyAmplifyApp", "Translation failed", error)
);

As a result of running this code, you will see the translated text printed to the console.

I/MyAmplifyApp: Мне нравится есть спагетти