Menu
Amazon Translate
Developer Guide

Translating Text Using the AWS SDK for Java

The following example demonstrates using the TranslateText operation in Java. To run this example, you need the AWS SDK for Java. For instructions for installing the SDK for Java, see Set up the AWS SDK for Java.

import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.client.builder.AwsClientBuilder; import com.amazonaws.services.translate.AmazonTranslate; import com.amazonaws.services.translate.AmazonTranslateClient; import com.amazonaws.services.translate.model.TranslateTextRequest; import com.amazonaws.services.translate.model.TranslateTextResult; public class App { private static final String REGION = "region"; public static void main( String[] args ) { // Create credentials using a provider chain. For more information, see // https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html AWSCredentialsProvider awsCreds = DefaultAWSCredentialsProviderChain.getInstance(); AmazonTranslate translate = AmazonTranslateClient.standard() .withCredentials(new AWSStaticCredentialsProvider(awsCreds)) .withRegion(REGION) .build(); TranslateTextRequest request = new TranslateTextRequest() .withText("Hello, world") .withSourceLanguageCode("en") .withTargetLanguageCode("es"); TranslateTextResult result = translate.translateText(request); System.out.println(result.getTranslatedText()); } }

You can change the source and target languages subject to the following constraints:

  • If the source language is English, you can translate the source text to any of the other supported languages. For a list of supported languages, see How Amazon Translate Works.

  • If the source language is not English, the target language must be English.