|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.translate; |
| 18 | + |
| 19 | +// [START translate_v3_batch_translate_text] |
| 20 | +import com.google.api.gax.longrunning.OperationFuture; |
| 21 | +import com.google.cloud.translate.v3.BatchTranslateMetadata; |
| 22 | +import com.google.cloud.translate.v3.BatchTranslateResponse; |
| 23 | +import com.google.cloud.translate.v3.BatchTranslateTextRequest; |
| 24 | +import com.google.cloud.translate.v3.GcsDestination; |
| 25 | +import com.google.cloud.translate.v3.GcsSource; |
| 26 | +import com.google.cloud.translate.v3.InputConfig; |
| 27 | +import com.google.cloud.translate.v3.LocationName; |
| 28 | +import com.google.cloud.translate.v3.OutputConfig; |
| 29 | +import com.google.cloud.translate.v3.TranslationServiceClient; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | +import java.util.concurrent.ExecutionException; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | +import java.util.concurrent.TimeoutException; |
| 35 | + |
| 36 | +public class BatchTranslateText { |
| 37 | + |
| 38 | + public static void batchTranslateText() |
| 39 | + throws InterruptedException, ExecutionException, IOException, TimeoutException { |
| 40 | + // TODO(developer): Replace these variables before running the sample. |
| 41 | + String projectId = "YOUR-PROJECT-ID"; |
| 42 | + // Supported Languages: https://cloud.google.com/translate/docs/languages |
| 43 | + String sourceLanguage = "your-source-language"; |
| 44 | + String targetLanguage = "your-target-language"; |
| 45 | + String inputUri = "gs://your-gcs-bucket/path/to/input/file.txt"; |
| 46 | + String outputUri = "gs://your-gcs-bucket/path/to/results/"; |
| 47 | + batchTranslateText(projectId, sourceLanguage, targetLanguage, inputUri, outputUri); |
| 48 | + } |
| 49 | + |
| 50 | + // Batch translate text |
| 51 | + public static void batchTranslateText( |
| 52 | + String projectId, |
| 53 | + String sourceLanguage, |
| 54 | + String targetLanguage, |
| 55 | + String inputUri, |
| 56 | + String outputUri) |
| 57 | + throws IOException, ExecutionException, InterruptedException, TimeoutException { |
| 58 | + |
| 59 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 60 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 61 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 62 | + try (TranslationServiceClient client = TranslationServiceClient.create()) { |
| 63 | + // Supported Locations: `us-central1` |
| 64 | + LocationName parent = LocationName.of(projectId, "us-central1"); |
| 65 | + |
| 66 | + GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build(); |
| 67 | + // Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats |
| 68 | + InputConfig inputConfig = |
| 69 | + InputConfig.newBuilder().setGcsSource(gcsSource).setMimeType("text/plain").build(); |
| 70 | + |
| 71 | + GcsDestination gcsDestination = |
| 72 | + GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build(); |
| 73 | + OutputConfig outputConfig = |
| 74 | + OutputConfig.newBuilder().setGcsDestination(gcsDestination).build(); |
| 75 | + |
| 76 | + BatchTranslateTextRequest request = |
| 77 | + BatchTranslateTextRequest.newBuilder() |
| 78 | + .setParent(parent.toString()) |
| 79 | + .setSourceLanguageCode(sourceLanguage) |
| 80 | + .addTargetLanguageCodes(targetLanguage) |
| 81 | + .addInputConfigs(inputConfig) |
| 82 | + .setOutputConfig(outputConfig) |
| 83 | + .build(); |
| 84 | + |
| 85 | + OperationFuture<BatchTranslateResponse, BatchTranslateMetadata> future = |
| 86 | + client.batchTranslateTextAsync(request); |
| 87 | + |
| 88 | + System.out.println("Waiting for operation to complete..."); |
| 89 | + BatchTranslateResponse response = future.get(120, TimeUnit.SECONDS); |
| 90 | + System.out.printf("Total Characters: %s\n", response.getTotalCharacters()); |
| 91 | + System.out.printf("Translated Characters: %s\n", response.getTranslatedCharacters()); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | +// [END translate_v3_batch_translate_text] |
0 commit comments