use of com.google.cloud.translate.v3beta1.BatchDocumentInputConfig in project java-translate by googleapis.
the class BatchTranslateDocument method batchTranslateDocument.
// Batch translate document
public static void batchTranslateDocument(String projectId, String sourceLanguage, String targetLanguage, String inputUri, String outputUri, int timeout) throws IOException, ExecutionException, InterruptedException, TimeoutException {
// refer to https://github.com/googleapis/java-translate/issues/613
TranslationServiceSettings.Builder translationServiceSettingsBuilder = TranslationServiceSettings.newBuilder();
TimedRetryAlgorithm timedRetryAlgorithm = OperationTimedPollAlgorithm.create(RetrySettings.newBuilder().setTotalTimeout(Duration.ofSeconds(1000)).build());
translationServiceSettingsBuilder.batchTranslateDocumentOperationSettings().setPollingAlgorithm(timedRetryAlgorithm);
TranslationServiceSettings translationServiceSettings = translationServiceSettingsBuilder.build();
// up any remaining background resources.
try (TranslationServiceClient client = TranslationServiceClient.create(translationServiceSettings)) {
// The ``global`` location is not supported for batch translation
LocationName parent = LocationName.of(projectId, "us-central1");
// Google Cloud Storage location for the source input. This can be a single file
// (for example, ``gs://translation-test/input.docx``) or a wildcard
// (for example, ``gs://translation-test/*``).
// Supported file types: https://cloud.google.com/translate/docs/supported-formats
GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build();
BatchDocumentInputConfig batchDocumentInputConfig = BatchDocumentInputConfig.newBuilder().setGcsSource(gcsSource).build();
GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
BatchDocumentOutputConfig batchDocumentOutputConfig = BatchDocumentOutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
BatchTranslateDocumentRequest request = BatchTranslateDocumentRequest.newBuilder().setParent(parent.toString()).setSourceLanguageCode(sourceLanguage).addTargetLanguageCodes(targetLanguage).addInputConfigs(batchDocumentInputConfig).setOutputConfig(batchDocumentOutputConfig).build();
OperationFuture<BatchTranslateDocumentResponse, BatchTranslateDocumentMetadata> future = client.batchTranslateDocumentAsync(request);
System.out.println("Waiting for operation to complete...");
// random number between timeout
long randomNumber = ThreadLocalRandom.current().nextInt(timeout, timeout + 100);
BatchTranslateDocumentResponse response = future.get(randomNumber, TimeUnit.SECONDS);
System.out.println("Total Pages: " + response.getTotalPages());
}
}
Aggregations