Page updated Jan 16, 2024

Translate language

Set up the backend

Run amplify add predictions, then use the following answers:

1? Please select from one of the categories below
2 `Convert`
3? What would you like to convert? (Use arrow keys)
4 `Translate text into a different language`
5? Provide a friendly name for your resource
6 translate
7? What is the source language? (Use arrow keys)
8 `US English`
9? What is the target language? (Use arrow keys)
10 `Italian`
11? Who should have access? (Use arrow keys)
12 `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():

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

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

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

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

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

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

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

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

1I/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:

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

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

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

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

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