Amazon Translate
Developer Guide

Translating Text Using the AWS Mobile SDK for Android

You can use Amazon Translate in an Android application to translate text.

To configure the example

  1. Set up the AWS Mobile SDK for Android. For instructions, see Android: Setup Options for the SDK in the AWS Mobile Developer Guide

  2. Create an IAM user with the minimum required permissions to run this example. For information about creating an IAM user, see Creating an IAM User in Your AWS Account in the AWS Identity and Access Management User Guide. For the required permissions policies, see Using Identity-Based Policies (IAM Policies) for Amazon Translate. After you create the user, download the credentials or record the access key and secret access key.

  3. Create a new project with Android Studio.

  4. Add the following to the dependencies section of your build.gradle file.

    dependencies { implementation 'com.amazonaws:aws-android-sdk-translate:2.6.20' }
  5. Add the following permissions to the AndroidManifest.xml file.

    <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  6. Copy the source code into your project

  7. Change the access key and secret access key to the keys that you recorded in step one.


Use the following code to create the example.

package com.amazonaws.amazontranslatetester; import; import android.util.Log; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.handlers.AsyncHandler; import; import; import; public class MainActivity extends Activity { private static final String LOG_TAG = MainActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { AWSCredentials awsCredentials = new AWSCredentials() { @Override public String getAWSAccessKeyId() { return "access key"; } @Override public String getAWSSecretKey() { return "secret key"; } }; AmazonTranslateAsyncClient translateAsyncClient = new AmazonTranslateAsyncClient(awsCredentials); TranslateTextRequest translateTextRequest = new TranslateTextRequest() .withText("Hello, world") .withSourceLanguageCode("en") .withTargetLanguageCode("es"); translateAsyncClient.translateTextAsync(translateTextRequest, new AsyncHandler<TranslateTextRequest, TranslateTextResult>() { @Override public void onError(Exception e) { Log.e(LOG_TAG, "Error occurred in translating the text: " + e.getLocalizedMessage()); } @Override public void onSuccess(TranslateTextRequest request, TranslateTextResult translateTextResult) { Log.d(LOG_TAG, "Original Text: " + request.getText()); Log.d(LOG_TAG, "Translated Text: " + translateTextResult.getTranslatedText()); } }); } }

On this page: