Search in sources :

Example 1 with TranslateException

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

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

the class BatchTranslation method translateDocuments.

// snippet-start:[translate.java2._batch.main]
public static String translateDocuments(TranslateClient translateClient, String s3Uri, String s3UriOut, String jobName, String dataAccessRoleArn) {
    try {
        InputDataConfig dataConfig = InputDataConfig.builder().s3Uri(s3Uri).contentType("text/plain").build();
        OutputDataConfig outputDataConfig = OutputDataConfig.builder().s3Uri(s3UriOut).build();
        StartTextTranslationJobRequest textTranslationJobRequest = StartTextTranslationJobRequest.builder().jobName(jobName).dataAccessRoleArn(dataAccessRoleArn).inputDataConfig(dataConfig).outputDataConfig(outputDataConfig).sourceLanguageCode("en").targetLanguageCodes("fr").build();
        StartTextTranslationJobResponse textTranslationJobResponse = translateClient.startTextTranslationJob(textTranslationJobRequest);
        // Keep checking until job is done
        boolean jobDone = false;
        String jobStatus = "";
        String jobId = textTranslationJobResponse.jobId();
        DescribeTextTranslationJobRequest jobRequest = DescribeTextTranslationJobRequest.builder().jobId(jobId).build();
        while (!jobDone) {
            // Check status on each loop
            DescribeTextTranslationJobResponse response = translateClient.describeTextTranslationJob(jobRequest);
            jobStatus = response.textTranslationJobProperties().jobStatusAsString();
            System.out.println(jobStatus);
            if (jobStatus.contains("COMPLETED"))
                jobDone = true;
            else {
                System.out.print(".");
                Thread.sleep(sleepTime * 1000);
            }
        }
        return textTranslationJobResponse.jobId();
    } catch (TranslateException | InterruptedException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    return "";
}
Also used : StartTextTranslationJobResponse(software.amazon.awssdk.services.translate.model.StartTextTranslationJobResponse) DescribeTextTranslationJobRequest(software.amazon.awssdk.services.translate.model.DescribeTextTranslationJobRequest) OutputDataConfig(software.amazon.awssdk.services.translate.model.OutputDataConfig) TranslateException(software.amazon.awssdk.services.translate.model.TranslateException) DescribeTextTranslationJobResponse(software.amazon.awssdk.services.translate.model.DescribeTextTranslationJobResponse) InputDataConfig(software.amazon.awssdk.services.translate.model.InputDataConfig) StartTextTranslationJobRequest(software.amazon.awssdk.services.translate.model.StartTextTranslationJobRequest)

Example 3 with TranslateException

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

the class DescribeTextTranslationJob method describeTextTranslationJob.

// snippet-start:[translate.java2._describe_jobs.main]
public static void describeTextTranslationJob(TranslateClient translateClient, String id) {
    try {
        DescribeTextTranslationJobRequest textTranslationJobRequest = DescribeTextTranslationJobRequest.builder().jobId(id).build();
        DescribeTextTranslationJobResponse jobResponse = translateClient.describeTextTranslationJob(textTranslationJobRequest);
        System.out.println("The job status is " + jobResponse.textTranslationJobProperties().jobStatus());
        System.out.println("The source language is " + jobResponse.textTranslationJobProperties().sourceLanguageCode());
        System.out.println("The target language is " + jobResponse.textTranslationJobProperties().targetLanguageCodes());
    } catch (TranslateException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : DescribeTextTranslationJobRequest(software.amazon.awssdk.services.translate.model.DescribeTextTranslationJobRequest) TranslateException(software.amazon.awssdk.services.translate.model.TranslateException) DescribeTextTranslationJobResponse(software.amazon.awssdk.services.translate.model.DescribeTextTranslationJobResponse)

Example 4 with TranslateException

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

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

Aggregations

TranslateException (software.amazon.awssdk.services.translate.model.TranslateException)8 TranslateTextRequest (software.amazon.awssdk.services.translate.model.TranslateTextRequest)5 TranslateTextResponse (software.amazon.awssdk.services.translate.model.TranslateTextResponse)5 Region (software.amazon.awssdk.regions.Region)4 TranslateClient (software.amazon.awssdk.services.translate.TranslateClient)4 DescribeTextTranslationJobRequest (software.amazon.awssdk.services.translate.model.DescribeTextTranslationJobRequest)2 DescribeTextTranslationJobResponse (software.amazon.awssdk.services.translate.model.DescribeTextTranslationJobResponse)2 InputDataConfig (software.amazon.awssdk.services.translate.model.InputDataConfig)1 ListTextTranslationJobsRequest (software.amazon.awssdk.services.translate.model.ListTextTranslationJobsRequest)1 ListTextTranslationJobsResponse (software.amazon.awssdk.services.translate.model.ListTextTranslationJobsResponse)1 OutputDataConfig (software.amazon.awssdk.services.translate.model.OutputDataConfig)1 StartTextTranslationJobRequest (software.amazon.awssdk.services.translate.model.StartTextTranslationJobRequest)1 StartTextTranslationJobResponse (software.amazon.awssdk.services.translate.model.StartTextTranslationJobResponse)1 TextTranslationJobProperties (software.amazon.awssdk.services.translate.model.TextTranslationJobProperties)1