Sample code for 30+ languages & platforms
Java

Amazon Translate Text

See more AWS Translate Examples

Demonstrates how to use the AWS Translate service to translate text from one language to another.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.AuthAws;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Rest;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Rest rest = new Rest();

        AuthAws authAws = new AuthAws();
        authAws.setAccessKey("AWS_ACCESS_KEY");
        authAws.setSecretKey("AWS_SECRET_KEY");
        authAws.setRegion("us-west-2");
        authAws.setServiceName("translate");
        rest.setAuthAws(authAws);

        // URL: https://translate.us-west-2.amazonaws.com/
        boolean bTls = true;
        int port = 443;
        boolean bAutoReconnect = true;
        try {
            rest.connect("translate.us-west-2.amazonaws.com", port, bTls, bAutoReconnect);
        } catch (ChilkatException e) {
            System.out.println("ConnectFailReason: " + rest.getConnectFailReason());
            System.out.println(e.getLastErrorText());
            return;
        }

        // Translate text from English to German
        JsonObject json = new JsonObject();
        json.updateString("SourceLanguageCode", "en");
        json.updateString("TargetLanguageCode", "de");
        json.updateString("Text", "This is the text to be translated");

        rest.addHeader("Content-Type", "application/x-amz-json-1.1");
        rest.addHeader("X-Amz-Target", "AWSShineFrontendService_20170701.TranslateText");

        StringBuilder sbRequestBody = new StringBuilder();
        json.emitSb(sbRequestBody);
        StringBuilder sbResponseBody = new StringBuilder();
        try {
            rest.fullRequestSb("POST", "/", sbRequestBody, sbResponseBody);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        int respStatusCode = rest.getResponseStatusCode();
        if (respStatusCode >= 400) {
            System.out.println("Response Status Code = " + respStatusCode);
            System.out.println("Response Header:");
            System.out.println(rest.getResponseHeader());
            System.out.println("Response Body:");
            System.out.println(sbResponseBody.getAsString());
            return;
        }

        JsonObject jsonResponse = new JsonObject();
        jsonResponse.loadSb(sbResponseBody);
        jsonResponse.setEmitCompact(false);

        System.out.println(jsonResponse.emit());

        // {
        //   "SourceLanguageCode": "en",
        //   "TargetLanguageCode": "de",
        //   "TranslatedText": "Dies ist der zu �bersetzende Text"
        // }

        System.out.println(jsonResponse.stringOf("TranslatedText"));
    }
}