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; import; import; import; 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 // 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.