diff --git a/translate/snippets/resources/fake_invoice.pdf b/translate/snippets/resources/fake_invoice.pdf new file mode 100644 index 00000000000..b9107fba129 Binary files /dev/null and b/translate/snippets/resources/fake_invoice.pdf differ diff --git a/translate/snippets/src/main/java/com/example/translatev3beta1/BatchTranslateDocument.java b/translate/snippets/src/main/java/com/example/translatev3beta1/BatchTranslateDocument.java new file mode 100644 index 00000000000..13e449d153a --- /dev/null +++ b/translate/snippets/src/main/java/com/example/translatev3beta1/BatchTranslateDocument.java @@ -0,0 +1,123 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.translatev3beta1; + +// [START translate_v3beta1_batch_translate_document] + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.api.gax.longrunning.OperationTimedPollAlgorithm; +import com.google.api.gax.retrying.RetrySettings; +import com.google.api.gax.retrying.TimedRetryAlgorithm; +import com.google.cloud.translate.v3beta1.BatchDocumentInputConfig; +import com.google.cloud.translate.v3beta1.BatchDocumentOutputConfig; +import com.google.cloud.translate.v3beta1.BatchTranslateDocumentMetadata; +import com.google.cloud.translate.v3beta1.BatchTranslateDocumentRequest; +import com.google.cloud.translate.v3beta1.BatchTranslateDocumentResponse; +import com.google.cloud.translate.v3beta1.GcsDestination; +import com.google.cloud.translate.v3beta1.GcsSource; +import com.google.cloud.translate.v3beta1.LocationName; +import com.google.cloud.translate.v3beta1.TranslationServiceClient; +import com.google.cloud.translate.v3beta1.TranslationServiceSettings; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.threeten.bp.Duration; + +public class BatchTranslateDocument { + + public static void batchTranslateDocument() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR-PROJECT-ID"; + // Supported Languages: https://cloud.google.com/translate/docs/languages + String sourceLanguage = "your-source-language"; + String targetLanguage = "your-target-language"; + String inputUri = "gs://your-gcs-bucket/path/to/input/file.txt"; + String outputUri = "gs://your-gcs-bucket/path/to/results/"; + int timeout = 400; // timeout in seconds + batchTranslateDocument(projectId, sourceLanguage, targetLanguage, inputUri, outputUri, timeout); + } + + // 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(); + + // Initialize client that sends requests. This client can be reused for multiple requests. After + // completing all of your requests, call the "close" method on the client to clean + // 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 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()); + } + } +} +// [END translate_v3beta1_batch_translate_document] diff --git a/translate/snippets/src/main/java/com/example/translatev3beta1/TranslateDocument.java b/translate/snippets/src/main/java/com/example/translatev3beta1/TranslateDocument.java new file mode 100644 index 00000000000..7a342a3802e --- /dev/null +++ b/translate/snippets/src/main/java/com/example/translatev3beta1/TranslateDocument.java @@ -0,0 +1,76 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.translatev3beta1; + +// [START translate_v3beta1_translate_document] + +import com.google.cloud.translate.v3beta1.DocumentInputConfig; +import com.google.cloud.translate.v3beta1.LocationName; +import com.google.cloud.translate.v3beta1.TranslateDocumentRequest; +import com.google.cloud.translate.v3beta1.TranslateDocumentResponse; +import com.google.cloud.translate.v3beta1.TranslationServiceClient; +import com.google.protobuf.ByteString; +import java.io.FileInputStream; +import java.io.IOException; + +public class TranslateDocument { + + public static void translateDocument() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR-PROJECT-ID"; + String filePath = "your-text"; + translateDocument(projectId, filePath); + } + + // Translating Document + public static void translateDocument(String projectId, String filePath) throws IOException { + // Initialize client that sends requests. This client can be reused for multiple requests. After + // completing all of your requests, call the "close" method on the client to clean + // up any remaining background resources. + try (TranslationServiceClient client = TranslationServiceClient.create()) { + // The ``global`` location is not supported for batch translation + LocationName parent = LocationName.of(projectId, "us-central1"); + + // Supported file types: https://cloud.google.com/translate/docs/supported-formats + ByteString content = ByteString.readFrom(new FileInputStream(filePath)); + + DocumentInputConfig documentInputConfig = + DocumentInputConfig.newBuilder() + .setContent(content) + .setMimeType("application/pdf") + .build(); + + TranslateDocumentRequest request = + TranslateDocumentRequest.newBuilder() + .setParent(parent.toString()) + .setTargetLanguageCode("fr-FR") + .setDocumentInputConfig(documentInputConfig) + .build(); + + TranslateDocumentResponse response = client.translateDocument(request); + + // To view translated document, write `response.document_translation.byte_stream_outputs` + // to file. If not provided in the TranslationRequest, the translated file will only be + // returned through a byte-stream and its output mime type will be the same as the input + // file's mime type + System.out.println( + "Response: Detected Language Code - " + + response.getDocumentTranslation().getDetectedLanguageCode()); + } + } +} +// [END translate_v3beta1_translate_document] diff --git a/translate/snippets/src/test/java/com/example/translatev3beta1/BatchTranslateDocumentTests.java b/translate/snippets/src/test/java/com/example/translatev3beta1/BatchTranslateDocumentTests.java new file mode 100644 index 00000000000..588ae493691 --- /dev/null +++ b/translate/snippets/src/test/java/com/example/translatev3beta1/BatchTranslateDocumentTests.java @@ -0,0 +1,118 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.translatev3beta1; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.api.gax.paging.Page; +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for Batch Translate Document sample. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class BatchTranslateDocumentTests { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String INPUT_URI = + "gs://java-docs-samples-testing/translation/invoice2.docx"; + private static final String PREFIX = "BATCH_DOC_TRANSLATION_OUTPUT_"; + private static final String OUTPUT_URI = + String.format("gs://%s/%s%s/", PROJECT_ID, PREFIX, UUID.randomUUID()); + + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void cleanUpBucket() { + Storage storage = StorageOptions.getDefaultInstance().getService(); + Page blobs = + storage.list( + PROJECT_ID, + Storage.BlobListOption.currentDirectory(), + Storage.BlobListOption.prefix(PREFIX)); + + deleteDirectory(storage, blobs); + } + + private static void deleteDirectory(Storage storage, Page blobs) { + for (Blob blob : blobs.iterateAll()) { + System.out.println(blob.getBlobId()); + if (!blob.delete()) { + Page subBlobs = + storage.list( + PROJECT_ID, + Storage.BlobListOption.currentDirectory(), + Storage.BlobListOption.prefix(blob.getName())); + + deleteDirectory(storage, subBlobs); + } + } + } + + private static void requireEnvVar(String varName) { + assertNotNull(String.format(varName), System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + + // clean up bucket before the use to prevent concurrency issue. + cleanUpBucket(); + } + + @After + public void tearDown() { + cleanUpBucket(); + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testBatchTranslateDocument() + throws InterruptedException, ExecutionException, IOException, TimeoutException { + BatchTranslateDocument.batchTranslateDocument( + PROJECT_ID, "en-US", "fr-FR", INPUT_URI, OUTPUT_URI, 1000); + + String got = bout.toString(); + assertThat(got).contains("Total Pages:"); + } +} diff --git a/translate/snippets/src/test/java/com/example/translatev3beta1/TranslateDocumentTests.java b/translate/snippets/src/test/java/com/example/translatev3beta1/TranslateDocumentTests.java new file mode 100644 index 00000000000..02780eee0d5 --- /dev/null +++ b/translate/snippets/src/test/java/com/example/translatev3beta1/TranslateDocumentTests.java @@ -0,0 +1,73 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.translatev3beta1; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for Translate Document sample. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class TranslateDocumentTests { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull(String.format(varName), System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testTranslateText() throws IOException { + TranslateDocument.translateDocument(PROJECT_ID, "resources/fake_invoice.pdf"); + String got = bout.toString(); + assertThat(got).contains("Response: Detected Language Code"); + } +}