Search in sources :

Example 1 with TranslateClient

use of software.amazon.awssdk.services.translate.TranslateClient 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 TranslateClient

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

the class DescribeTextTranslationJob method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    <id> \n\n" + "Where:\n" + "    id - a translation job ID value. You can obtain this value from the BatchTranslation example.\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String id = args[0];
    Region region = Region.US_WEST_2;
    TranslateClient translateClient = TranslateClient.builder().region(region).build();
    describeTextTranslationJob(translateClient, id);
    translateClient.close();
}
Also used : TranslateClient(software.amazon.awssdk.services.translate.TranslateClient) Region(software.amazon.awssdk.regions.Region)

Example 3 with TranslateClient

use of software.amazon.awssdk.services.translate.TranslateClient 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 4 with TranslateClient

use of software.amazon.awssdk.services.translate.TranslateClient 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 5 with TranslateClient

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

the class TranslateText method main.

public static void main(String[] args) {
    Region region = Region.US_WEST_2;
    TranslateClient translateClient = TranslateClient.builder().credentialsProvider(EnvironmentVariableCredentialsProvider.create()).region(region).build();
    textTranslate(translateClient);
    translateClient.close();
}
Also used : TranslateClient(software.amazon.awssdk.services.translate.TranslateClient) Region(software.amazon.awssdk.regions.Region)

Aggregations

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