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 "";
}
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 "";
}
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);
}
}
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 "";
}
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);
}
}
Aggregations