Search in sources :

Example 1 with TranslateTextResponse

use of software.amazon.awssdk.services.translate.model.TranslateTextResponse in project aws-doc-sdk-examples by awsdocs.

the class RetrieveDataRDS method translateText.

private String translateText(String text, String lang) {
    Region region = Region.US_WEST_2;
    TranslateClient translateClient = TranslateClient.builder().credentialsProvider(EnvironmentVariableCredentialsProvider.create()).region(region).build();
    String transValue = "";
    try {
        if (lang.compareTo("French") == 0) {
            TranslateTextRequest textRequest = TranslateTextRequest.builder().sourceLanguageCode("en").targetLanguageCode("fr").text(text).build();
            TranslateTextResponse textResponse = translateClient.translateText(textRequest);
            transValue = textResponse.translatedText();
        } else if (lang.compareTo("Russian") == 0) {
            TranslateTextRequest textRequest = TranslateTextRequest.builder().sourceLanguageCode("en").targetLanguageCode("ru").text(text).build();
            TranslateTextResponse textResponse = translateClient.translateText(textRequest);
            transValue = textResponse.translatedText();
        } else if (lang.compareTo("Japanese") == 0) {
            TranslateTextRequest textRequest = TranslateTextRequest.builder().sourceLanguageCode("en").targetLanguageCode("ja").text(text).build();
            TranslateTextResponse textResponse = translateClient.translateText(textRequest);
            transValue = textResponse.translatedText();
        } else if (lang.compareTo("Spanish") == 0) {
            TranslateTextRequest textRequest = TranslateTextRequest.builder().sourceLanguageCode("en").targetLanguageCode("es").text(text).build();
            TranslateTextResponse textResponse = translateClient.translateText(textRequest);
            transValue = textResponse.translatedText();
        } else {
            TranslateTextRequest textRequest = TranslateTextRequest.builder().sourceLanguageCode("en").targetLanguageCode("zh").text(text).build();
            TranslateTextResponse textResponse = translateClient.translateText(textRequest);
            transValue = textResponse.translatedText();
        }
        return transValue;
    } catch (TranslateException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    return "";
}
Also used : TranslateClient(software.amazon.awssdk.services.translate.TranslateClient) TranslateException(software.amazon.awssdk.services.translate.model.TranslateException) TranslateTextRequest(software.amazon.awssdk.services.translate.model.TranslateTextRequest) Region(software.amazon.awssdk.regions.Region) TranslateTextResponse(software.amazon.awssdk.services.translate.model.TranslateTextResponse)

Example 2 with TranslateTextResponse

use of software.amazon.awssdk.services.translate.model.TranslateTextResponse in project aws-doc-sdk-examples by awsdocs.

the class RedshiftService method translateText.

private String translateText(String text, String lang) {
    Region region = Region.US_WEST_2;
    TranslateClient translateClient = TranslateClient.builder().credentialsProvider(EnvironmentVariableCredentialsProvider.create()).region(region).build();
    String transValue = "";
    try {
        if (lang.compareTo("French") == 0) {
            TranslateTextRequest textRequest = TranslateTextRequest.builder().sourceLanguageCode("en").targetLanguageCode("fr").text(text).build();
            TranslateTextResponse textResponse = translateClient.translateText(textRequest);
            transValue = textResponse.translatedText();
        } else if (lang.compareTo("Russian") == 0) {
            TranslateTextRequest textRequest = TranslateTextRequest.builder().sourceLanguageCode("en").targetLanguageCode("ru").text(text).build();
            TranslateTextResponse textResponse = translateClient.translateText(textRequest);
            transValue = textResponse.translatedText();
        } else if (lang.compareTo("Japanese") == 0) {
            TranslateTextRequest textRequest = TranslateTextRequest.builder().sourceLanguageCode("en").targetLanguageCode("ja").text(text).build();
            TranslateTextResponse textResponse = translateClient.translateText(textRequest);
            transValue = textResponse.translatedText();
        } else if (lang.compareTo("Spanish") == 0) {
            TranslateTextRequest textRequest = TranslateTextRequest.builder().sourceLanguageCode("en").targetLanguageCode("es").text(text).build();
            TranslateTextResponse textResponse = translateClient.translateText(textRequest);
            transValue = textResponse.translatedText();
        } else {
            TranslateTextRequest textRequest = TranslateTextRequest.builder().sourceLanguageCode("en").targetLanguageCode("zh").text(text).build();
            TranslateTextResponse textResponse = translateClient.translateText(textRequest);
            transValue = textResponse.translatedText();
        }
        return transValue;
    } catch (TranslateException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    return "";
}
Also used : TranslateClient(software.amazon.awssdk.services.translate.TranslateClient) TranslateException(software.amazon.awssdk.services.translate.model.TranslateException) TranslateTextRequest(software.amazon.awssdk.services.translate.model.TranslateTextRequest) Region(software.amazon.awssdk.regions.Region) TranslateTextResponse(software.amazon.awssdk.services.translate.model.TranslateTextResponse)

Example 3 with TranslateTextResponse

use of software.amazon.awssdk.services.translate.model.TranslateTextResponse in project aws-doc-sdk-examples by awsdocs.

the class SnsService method pubTopic.

public String pubTopic(String message, String lang) {
    try {
        String body;
        Region region = Region.US_WEST_2;
        TranslateClient translateClient = TranslateClient.builder().credentialsProvider(EnvironmentVariableCredentialsProvider.create()).region(region).build();
        if (lang.compareTo("English") == 0) {
            body = message;
        } else if (lang.compareTo("French") == 0) {
            TranslateTextRequest textRequest = TranslateTextRequest.builder().sourceLanguageCode("en").targetLanguageCode("fr").text(message).build();
            TranslateTextResponse textResponse = translateClient.translateText(textRequest);
            body = textResponse.translatedText();
        } else {
            TranslateTextRequest textRequest = TranslateTextRequest.builder().sourceLanguageCode("en").targetLanguageCode("es").text(message).build();
            TranslateTextResponse textResponse = translateClient.translateText(textRequest);
            body = textResponse.translatedText();
        }
        SnsClient snsClient = getSnsClient();
        PublishRequest request = PublishRequest.builder().message(body).topicArn(topicArn).build();
        PublishResponse result = snsClient.publish(request);
        return " Message sent in " + lang + ". Status was " + result.sdkHttpResponse().statusCode();
    } catch (SnsException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return "Error - msg not sent";
}
Also used : TranslateClient(software.amazon.awssdk.services.translate.TranslateClient) SnsClient(software.amazon.awssdk.services.sns.SnsClient) TranslateTextRequest(software.amazon.awssdk.services.translate.model.TranslateTextRequest) Region(software.amazon.awssdk.regions.Region) TranslateTextResponse(software.amazon.awssdk.services.translate.model.TranslateTextResponse)

Example 4 with TranslateTextResponse

use of software.amazon.awssdk.services.translate.model.TranslateTextResponse in project aws-doc-sdk-examples by awsdocs.

the class TranslateText method textTranslate.

// snippet-start:[translate.java2._text.main]
public static void textTranslate(TranslateClient translateClient) {
    try {
        TranslateTextRequest textRequest = TranslateTextRequest.builder().sourceLanguageCode("en").targetLanguageCode("fr").text("Its a sunny day today").build();
        TranslateTextResponse textResponse = translateClient.translateText(textRequest);
        System.out.println(textResponse.translatedText());
    } catch (TranslateException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : TranslateException(software.amazon.awssdk.services.translate.model.TranslateException) TranslateTextRequest(software.amazon.awssdk.services.translate.model.TranslateTextRequest) TranslateTextResponse(software.amazon.awssdk.services.translate.model.TranslateTextResponse)

Example 5 with TranslateTextResponse

use of software.amazon.awssdk.services.translate.model.TranslateTextResponse in project aws-doc-sdk-examples by awsdocs.

the class LexService method textTranslateToEn.

public String textTranslateToEn(String lanCode, String text) {
    Region region = Region.US_EAST_1;
    TranslateClient translateClient = TranslateClient.builder().credentialsProvider(EnvironmentVariableCredentialsProvider.create()).region(region).build();
    try {
        TranslateTextRequest textRequest = TranslateTextRequest.builder().sourceLanguageCode(lanCode).targetLanguageCode("en").text(text).build();
        TranslateTextResponse textResponse = translateClient.translateText(textRequest);
        return textResponse.translatedText();
    } catch (TranslateException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    return "";
}
Also used : TranslateClient(software.amazon.awssdk.services.translate.TranslateClient) TranslateException(software.amazon.awssdk.services.translate.model.TranslateException) TranslateTextRequest(software.amazon.awssdk.services.translate.model.TranslateTextRequest) Region(software.amazon.awssdk.regions.Region) TranslateTextResponse(software.amazon.awssdk.services.translate.model.TranslateTextResponse)

Aggregations

TranslateTextRequest (software.amazon.awssdk.services.translate.model.TranslateTextRequest)6 TranslateTextResponse (software.amazon.awssdk.services.translate.model.TranslateTextResponse)6 Region (software.amazon.awssdk.regions.Region)5 TranslateClient (software.amazon.awssdk.services.translate.TranslateClient)5 TranslateException (software.amazon.awssdk.services.translate.model.TranslateException)5 SnsClient (software.amazon.awssdk.services.sns.SnsClient)1