diff --git a/.github/workflows/samples.yaml b/.github/workflows/samples.yaml
new file mode 100644
index 00000000..a1d50073
--- /dev/null
+++ b/.github/workflows/samples.yaml
@@ -0,0 +1,14 @@
+on:
+ pull_request:
+name: samples
+jobs:
+ lint:
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v2
+ - uses: actions/setup-java@v1
+ with:
+ java-version: 8
+ - name: Run checkstyle
+ run: mvn -P lint --quiet --batch-mode checkstyle:check
+ working-directory: samples/snippets
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 85212ae9..033f5cd3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,12 @@
# Changelog
+### [1.95.2](https://www.github.com/googleapis/java-translate/compare/v1.95.1...v1.95.2) (2020-08-21)
+
+
+### Dependencies
+
+* update dependency com.google.apis:google-api-services-translate to v2-rev20170525-1.30.10 ([#245](https://www.github.com/googleapis/java-translate/issues/245)) ([ac1d2fd](https://www.github.com/googleapis/java-translate/commit/ac1d2fdb1be8d4a04ba00bab3389e067c9e3061d))
+
### [1.95.1](https://www.github.com/googleapis/java-translate/compare/v1.95.0...v1.95.1) (2020-08-10)
diff --git a/README.md b/README.md
index b68194af..753d774d 100644
--- a/README.md
+++ b/README.md
@@ -17,7 +17,7 @@ If you are using Maven with [BOM][libraries-bom], add this to your pom.xml file
com.google.cloud
libraries-bom
- 8.1.0
+ 9.1.0
pom
import
@@ -29,6 +29,7 @@ If you are using Maven with [BOM][libraries-bom], add this to your pom.xml file
com.google.cloud
google-cloud-translate
+
```
@@ -38,7 +39,7 @@ If you are using Maven without BOM, add this to your dependencies:
com.google.cloud
google-cloud-translate
- 1.95.0
+ 1.95.1
```
@@ -47,11 +48,11 @@ If you are using Maven without BOM, add this to your dependencies:
If you are using Gradle, add this to your dependencies
```Groovy
-compile 'com.google.cloud:google-cloud-translate:1.95.1'
+compile 'com.google.cloud:google-cloud-translate:1.95.2'
```
If you are using SBT, add this to your dependencies
```Scala
-libraryDependencies += "com.google.cloud" % "google-cloud-translate" % "1.95.1"
+libraryDependencies += "com.google.cloud" % "google-cloud-translate" % "1.95.2"
```
[//]: # ({x-version-update-end})
@@ -163,6 +164,31 @@ Default Credentials or a valid API key are available.
+## Samples
+
+Samples are in the [`samples/`](https://github.com/googleapis/java-translate/tree/master/samples) directory. The samples' `README.md`
+has instructions for running the samples.
+
+| Sample | Source Code | Try it |
+| --------------------------- | --------------------------------- | ------ |
+| Batch Translate Text | [source code](https://github.com/googleapis/java-translate/blob/master/samples/snippets/src/main/java/com/example/translate/BatchTranslateText.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-translate&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/translate/BatchTranslateText.java) |
+| Batch Translate Text With Glossary | [source code](https://github.com/googleapis/java-translate/blob/master/samples/snippets/src/main/java/com/example/translate/BatchTranslateTextWithGlossary.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-translate&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/translate/BatchTranslateTextWithGlossary.java) |
+| Batch Translate Text With Glossary And Model | [source code](https://github.com/googleapis/java-translate/blob/master/samples/snippets/src/main/java/com/example/translate/BatchTranslateTextWithGlossaryAndModel.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-translate&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/translate/BatchTranslateTextWithGlossaryAndModel.java) |
+| Batch Translate Text With Model | [source code](https://github.com/googleapis/java-translate/blob/master/samples/snippets/src/main/java/com/example/translate/BatchTranslateTextWithModel.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-translate&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/translate/BatchTranslateTextWithModel.java) |
+| Create Glossary | [source code](https://github.com/googleapis/java-translate/blob/master/samples/snippets/src/main/java/com/example/translate/CreateGlossary.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-translate&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/translate/CreateGlossary.java) |
+| Delete Glossary | [source code](https://github.com/googleapis/java-translate/blob/master/samples/snippets/src/main/java/com/example/translate/DeleteGlossary.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-translate&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/translate/DeleteGlossary.java) |
+| Detect Language | [source code](https://github.com/googleapis/java-translate/blob/master/samples/snippets/src/main/java/com/example/translate/DetectLanguage.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-translate&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/translate/DetectLanguage.java) |
+| Get Glossary | [source code](https://github.com/googleapis/java-translate/blob/master/samples/snippets/src/main/java/com/example/translate/GetGlossary.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-translate&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/translate/GetGlossary.java) |
+| Get Supported Languages | [source code](https://github.com/googleapis/java-translate/blob/master/samples/snippets/src/main/java/com/example/translate/GetSupportedLanguages.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-translate&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/translate/GetSupportedLanguages.java) |
+| Get Supported Languages For Target | [source code](https://github.com/googleapis/java-translate/blob/master/samples/snippets/src/main/java/com/example/translate/GetSupportedLanguagesForTarget.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-translate&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/translate/GetSupportedLanguagesForTarget.java) |
+| List Glossaries | [source code](https://github.com/googleapis/java-translate/blob/master/samples/snippets/src/main/java/com/example/translate/ListGlossaries.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-translate&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/translate/ListGlossaries.java) |
+| Quickstart Sample | [source code](https://github.com/googleapis/java-translate/blob/master/samples/snippets/src/main/java/com/example/translate/QuickstartSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-translate&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/translate/QuickstartSample.java) |
+| Translate Text | [source code](https://github.com/googleapis/java-translate/blob/master/samples/snippets/src/main/java/com/example/translate/TranslateText.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-translate&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/translate/TranslateText.java) |
+| Translate Text With Glossary | [source code](https://github.com/googleapis/java-translate/blob/master/samples/snippets/src/main/java/com/example/translate/TranslateTextWithGlossary.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-translate&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/translate/TranslateTextWithGlossary.java) |
+| Translate Text With Glossary And Model | [source code](https://github.com/googleapis/java-translate/blob/master/samples/snippets/src/main/java/com/example/translate/TranslateTextWithGlossaryAndModel.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-translate&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/translate/TranslateTextWithGlossaryAndModel.java) |
+| Translate Text With Model | [source code](https://github.com/googleapis/java-translate/blob/master/samples/snippets/src/main/java/com/example/translate/TranslateTextWithModel.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-translate&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/translate/TranslateTextWithModel.java) |
+
+
## Troubleshooting
diff --git a/google-cloud-translate-bom/pom.xml b/google-cloud-translate-bom/pom.xml
index 852a2168..498c18b0 100644
--- a/google-cloud-translate-bom/pom.xml
+++ b/google-cloud-translate-bom/pom.xml
@@ -3,7 +3,7 @@
4.0.0
com.google.cloud
google-cloud-translate-bom
- 1.95.1
+ 1.95.2
pom
com.google.cloud
@@ -63,27 +63,27 @@
com.google.api.grpc
grpc-google-cloud-translate-v3beta1
- 0.78.1
+ 0.78.2
com.google.api.grpc
grpc-google-cloud-translate-v3
- 1.1.1
+ 1.1.2
com.google.api.grpc
proto-google-cloud-translate-v3beta1
- 0.78.1
+ 0.78.2
com.google.api.grpc
proto-google-cloud-translate-v3
- 1.1.1
+ 1.1.2
com.google.cloud
google-cloud-translate
- 1.95.1
+ 1.95.2
diff --git a/google-cloud-translate/pom.xml b/google-cloud-translate/pom.xml
index 07b2481d..c1343197 100644
--- a/google-cloud-translate/pom.xml
+++ b/google-cloud-translate/pom.xml
@@ -3,7 +3,7 @@
4.0.0
com.google.cloud
google-cloud-translate
- 1.95.1
+ 1.95.2
jar
Google Cloud Translate
https://github.com/googleapis/java-translate
@@ -11,7 +11,7 @@
com.google.cloud
google-cloud-translate-parent
- 1.95.1
+ 1.95.2
google-cloud-translate
diff --git a/grpc-google-cloud-translate-v3/pom.xml b/grpc-google-cloud-translate-v3/pom.xml
index 9e96b36d..63b7ec73 100644
--- a/grpc-google-cloud-translate-v3/pom.xml
+++ b/grpc-google-cloud-translate-v3/pom.xml
@@ -4,13 +4,13 @@
4.0.0
com.google.api.grpc
grpc-google-cloud-translate-v3
- 1.1.1
+ 1.1.2
grpc-google-cloud-translate-v3
GRPC library for grpc-google-cloud-translate-v3
com.google.cloud
google-cloud-translate-parent
- 1.95.1
+ 1.95.2
diff --git a/grpc-google-cloud-translate-v3beta1/pom.xml b/grpc-google-cloud-translate-v3beta1/pom.xml
index 9fe6effd..e0568fab 100644
--- a/grpc-google-cloud-translate-v3beta1/pom.xml
+++ b/grpc-google-cloud-translate-v3beta1/pom.xml
@@ -4,13 +4,13 @@
4.0.0
com.google.api.grpc
grpc-google-cloud-translate-v3beta1
- 0.78.1
+ 0.78.2
grpc-google-cloud-translate-v3beta1
GRPC library for grpc-google-cloud-translate-v3beta1
com.google.cloud
google-cloud-translate-parent
- 1.95.1
+ 1.95.2
diff --git a/pom.xml b/pom.xml
index e3dda410..ce1c0bee 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,7 +4,7 @@
com.google.cloud
google-cloud-translate-parent
pom
- 1.95.1
+ 1.95.2
Google Cloud Translate Parent
https://github.com/googleapis/java-translate
@@ -70,27 +70,27 @@
com.google.api.grpc
proto-google-cloud-translate-v3beta1
- 0.78.1
+ 0.78.2
com.google.api.grpc
proto-google-cloud-translate-v3
- 1.1.1
+ 1.1.2
com.google.api.grpc
grpc-google-cloud-translate-v3beta1
- 0.78.1
+ 0.78.2
com.google.api.grpc
grpc-google-cloud-translate-v3
- 1.1.1
+ 1.1.2
com.google.cloud
google-cloud-translate
- 1.95.1
+ 1.95.2
@@ -103,7 +103,7 @@
com.google.apis
google-api-services-translate
- v2-rev20170525-1.30.9
+ v2-rev20170525-1.30.10
diff --git a/proto-google-cloud-translate-v3/pom.xml b/proto-google-cloud-translate-v3/pom.xml
index 9194e263..5c580b8e 100644
--- a/proto-google-cloud-translate-v3/pom.xml
+++ b/proto-google-cloud-translate-v3/pom.xml
@@ -4,13 +4,13 @@
4.0.0
com.google.api.grpc
proto-google-cloud-translate-v3
- 1.1.1
+ 1.1.2
proto-google-cloud-translate-v3
PROTO library for proto-google-cloud-translate-v3
com.google.cloud
google-cloud-translate-parent
- 1.95.1
+ 1.95.2
diff --git a/proto-google-cloud-translate-v3beta1/pom.xml b/proto-google-cloud-translate-v3beta1/pom.xml
index 82ed4302..7924f2aa 100644
--- a/proto-google-cloud-translate-v3beta1/pom.xml
+++ b/proto-google-cloud-translate-v3beta1/pom.xml
@@ -4,13 +4,13 @@
4.0.0
com.google.api.grpc
proto-google-cloud-translate-v3beta1
- 0.78.1
+ 0.78.2
proto-google-cloud-translate-v3beta1
PROTO library for proto-google-cloud-translate-v3beta1
com.google.cloud
google-cloud-translate-parent
- 1.95.1
+ 1.95.2
diff --git a/samples/install-without-bom/pom.xml b/samples/install-without-bom/pom.xml
index be6c3cc9..95749ee1 100644
--- a/samples/install-without-bom/pom.xml
+++ b/samples/install-without-bom/pom.xml
@@ -29,10 +29,14 @@
com.google.cloud
google-cloud-translate
- 1.95.0
+ 1.95.1
-
+
+ com.google.cloud
+ google-cloud-storage
+ 1.111.2
+
junit
junit
diff --git a/samples/snapshot/pom.xml b/samples/snapshot/pom.xml
index d64343ec..31d5447f 100644
--- a/samples/snapshot/pom.xml
+++ b/samples/snapshot/pom.xml
@@ -23,12 +23,18 @@
UTF-8
-
+
com.google.cloud
google-cloud-translate
- 1.95.0
+ 1.95.2
+
+
+
+ com.google.cloud
+ google-cloud-storage
+ 1.111.2
@@ -44,7 +50,6 @@
test
-
diff --git a/samples/snippets/pom.xml b/samples/snippets/pom.xml
index 5016563a..896632c2 100644
--- a/samples/snippets/pom.xml
+++ b/samples/snippets/pom.xml
@@ -30,7 +30,7 @@
com.google.cloud
libraries-bom
- 8.1.0
+ 9.1.0
pom
import
@@ -42,8 +42,11 @@
com.google.cloud
google-cloud-translate
-
-
+
+
+ com.google.cloud
+ google-cloud-storage
+
junit
junit
@@ -56,5 +59,7 @@
1.0.1
test
+
+
diff --git a/samples/snippets/src/main/java/com/example/translate/BatchTranslateText.java b/samples/snippets/src/main/java/com/example/translate/BatchTranslateText.java
new file mode 100644
index 00000000..118cf6bf
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/translate/BatchTranslateText.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright 2020 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.translate;
+
+// [START translate_v3_batch_translate_text]
+import com.google.api.gax.longrunning.OperationFuture;
+import com.google.cloud.translate.v3.BatchTranslateMetadata;
+import com.google.cloud.translate.v3.BatchTranslateResponse;
+import com.google.cloud.translate.v3.BatchTranslateTextRequest;
+import com.google.cloud.translate.v3.GcsDestination;
+import com.google.cloud.translate.v3.GcsSource;
+import com.google.cloud.translate.v3.InputConfig;
+import com.google.cloud.translate.v3.LocationName;
+import com.google.cloud.translate.v3.OutputConfig;
+import com.google.cloud.translate.v3.TranslationServiceClient;
+import java.io.IOException;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+public class BatchTranslateText {
+
+ public static void batchTranslateText()
+ throws InterruptedException, ExecutionException, IOException, 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/";
+ batchTranslateText(projectId, sourceLanguage, targetLanguage, inputUri, outputUri);
+ }
+
+ // Batch translate text
+ public static void batchTranslateText(
+ String projectId,
+ String sourceLanguage,
+ String targetLanguage,
+ String inputUri,
+ String outputUri)
+ throws IOException, ExecutionException, InterruptedException, TimeoutException {
+
+ // Initialize client that will be used to send requests. This client only needs to be created
+ // once, and can be reused for multiple requests. After completing all of your requests, call
+ // the "close" method on the client to safely clean up any remaining background resources.
+ try (TranslationServiceClient client = TranslationServiceClient.create()) {
+ // Supported Locations: `us-central1`
+ LocationName parent = LocationName.of(projectId, "us-central1");
+
+ GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build();
+ // Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
+ InputConfig inputConfig =
+ InputConfig.newBuilder().setGcsSource(gcsSource).setMimeType("text/plain").build();
+
+ GcsDestination gcsDestination =
+ GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
+ OutputConfig outputConfig =
+ OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
+
+ BatchTranslateTextRequest request =
+ BatchTranslateTextRequest.newBuilder()
+ .setParent(parent.toString())
+ .setSourceLanguageCode(sourceLanguage)
+ .addTargetLanguageCodes(targetLanguage)
+ .addInputConfigs(inputConfig)
+ .setOutputConfig(outputConfig)
+ .build();
+
+ OperationFuture future =
+ client.batchTranslateTextAsync(request);
+
+ System.out.println("Waiting for operation to complete...");
+ BatchTranslateResponse response = future.get(180, TimeUnit.SECONDS);
+ System.out.printf("Total Characters: %s\n", response.getTotalCharacters());
+ System.out.printf("Translated Characters: %s\n", response.getTranslatedCharacters());
+ }
+ }
+}
+// [END translate_v3_batch_translate_text]
diff --git a/samples/snippets/src/main/java/com/example/translate/BatchTranslateTextWithGlossary.java b/samples/snippets/src/main/java/com/example/translate/BatchTranslateTextWithGlossary.java
new file mode 100644
index 00000000..a839d500
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/translate/BatchTranslateTextWithGlossary.java
@@ -0,0 +1,113 @@
+/*
+ * Copyright 2020 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.translate;
+
+// [START translate_v3_batch_translate_text_with_glossary]
+import com.google.api.gax.longrunning.OperationFuture;
+import com.google.cloud.translate.v3.BatchTranslateMetadata;
+import com.google.cloud.translate.v3.BatchTranslateResponse;
+import com.google.cloud.translate.v3.BatchTranslateTextRequest;
+import com.google.cloud.translate.v3.GcsDestination;
+import com.google.cloud.translate.v3.GcsSource;
+import com.google.cloud.translate.v3.GlossaryName;
+import com.google.cloud.translate.v3.InputConfig;
+import com.google.cloud.translate.v3.LocationName;
+import com.google.cloud.translate.v3.OutputConfig;
+import com.google.cloud.translate.v3.TranslateTextGlossaryConfig;
+import com.google.cloud.translate.v3.TranslationServiceClient;
+import java.io.IOException;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+public class BatchTranslateTextWithGlossary {
+
+ public static void batchTranslateTextWithGlossary()
+ throws InterruptedException, ExecutionException, IOException, 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/";
+ String glossaryId = "your-glossary-display-name";
+ batchTranslateTextWithGlossary(
+ projectId, sourceLanguage, targetLanguage, inputUri, outputUri, glossaryId);
+ }
+
+ // Batch Translate Text with a Glossary.
+ public static void batchTranslateTextWithGlossary(
+ String projectId,
+ String sourceLanguage,
+ String targetLanguage,
+ String inputUri,
+ String outputUri,
+ String glossaryId)
+ throws IOException, ExecutionException, InterruptedException, TimeoutException {
+
+ // Initialize client that will be used to send requests. This client only needs to be created
+ // once, and can be reused for multiple requests. After completing all of your requests, call
+ // the "close" method on the client to safely clean up any remaining background resources.
+ try (TranslationServiceClient client = TranslationServiceClient.create()) {
+ // Supported Locations: `global`, [glossary location], or [model location]
+ // Glossaries must be hosted in `us-central1`
+ // Custom Models must use the same location as your model. (us-central1)
+ String location = "us-central1";
+ LocationName parent = LocationName.of(projectId, location);
+
+ // Configure the source of the file from a GCS bucket
+ GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build();
+ // Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
+ InputConfig inputConfig =
+ InputConfig.newBuilder().setGcsSource(gcsSource).setMimeType("text/plain").build();
+
+ // Configure where to store the output in a GCS bucket
+ GcsDestination gcsDestination =
+ GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
+ OutputConfig outputConfig =
+ OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
+
+ // Configure the glossary used in the request
+ GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId);
+ TranslateTextGlossaryConfig glossaryConfig =
+ TranslateTextGlossaryConfig.newBuilder().setGlossary(glossaryName.toString()).build();
+
+ // Build the request that will be sent to the API
+ BatchTranslateTextRequest request =
+ BatchTranslateTextRequest.newBuilder()
+ .setParent(parent.toString())
+ .setSourceLanguageCode(sourceLanguage)
+ .addTargetLanguageCodes(targetLanguage)
+ .addInputConfigs(inputConfig)
+ .setOutputConfig(outputConfig)
+ .putGlossaries(targetLanguage, glossaryConfig)
+ .build();
+
+ // Start an asynchronous request
+ OperationFuture future =
+ client.batchTranslateTextAsync(request);
+
+ System.out.println("Waiting for operation to complete...");
+ BatchTranslateResponse response = future.get(180, TimeUnit.SECONDS);
+ // Display the translation for each input text provided
+ System.out.printf("Total Characters: %s\n", response.getTotalCharacters());
+ System.out.printf("Translated Characters: %s\n", response.getTranslatedCharacters());
+ }
+ }
+}
+// [END translate_v3_batch_translate_text_with_glossary]
diff --git a/samples/snippets/src/main/java/com/example/translate/BatchTranslateTextWithGlossaryAndModel.java b/samples/snippets/src/main/java/com/example/translate/BatchTranslateTextWithGlossaryAndModel.java
new file mode 100644
index 00000000..92615eb1
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/translate/BatchTranslateTextWithGlossaryAndModel.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright 2020 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.translate;
+
+// [START translate_v3_batch_translate_text_with_glossary_and_model]
+import com.google.api.gax.longrunning.OperationFuture;
+import com.google.cloud.translate.v3.BatchTranslateMetadata;
+import com.google.cloud.translate.v3.BatchTranslateResponse;
+import com.google.cloud.translate.v3.BatchTranslateTextRequest;
+import com.google.cloud.translate.v3.GcsDestination;
+import com.google.cloud.translate.v3.GcsSource;
+import com.google.cloud.translate.v3.GlossaryName;
+import com.google.cloud.translate.v3.InputConfig;
+import com.google.cloud.translate.v3.LocationName;
+import com.google.cloud.translate.v3.OutputConfig;
+import com.google.cloud.translate.v3.TranslateTextGlossaryConfig;
+import com.google.cloud.translate.v3.TranslationServiceClient;
+import java.io.IOException;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+public class BatchTranslateTextWithGlossaryAndModel {
+
+ public static void batchTranslateTextWithGlossaryAndModel()
+ throws InterruptedException, ExecutionException, IOException, 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/";
+ String glossaryId = "your-glossary-display-name";
+ String modelId = "YOUR-MODEL-ID";
+ batchTranslateTextWithGlossaryAndModel(
+ projectId, sourceLanguage, targetLanguage, inputUri, outputUri, glossaryId, modelId);
+ }
+
+ // Batch translate text with Model and Glossary
+ public static void batchTranslateTextWithGlossaryAndModel(
+ String projectId,
+ String sourceLanguage,
+ String targetLanguage,
+ String inputUri,
+ String outputUri,
+ String glossaryId,
+ String modelId)
+ throws IOException, ExecutionException, InterruptedException, TimeoutException {
+
+ // Initialize client that will be used to send requests. This client only needs to be created
+ // once, and can be reused for multiple requests. After completing all of your requests, call
+ // the "close" method on the client to safely clean up any remaining background resources.
+ try (TranslationServiceClient client = TranslationServiceClient.create()) {
+ // Supported Locations: `global`, [glossary location], or [model location]
+ // Glossaries must be hosted in `us-central1`
+ // Custom Models must use the same location as your model. (us-central1)
+ String location = "us-central1";
+ LocationName parent = LocationName.of(projectId, location);
+
+ // Configure the source of the file from a GCS bucket
+ GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build();
+ // Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
+ InputConfig inputConfig =
+ InputConfig.newBuilder().setGcsSource(gcsSource).setMimeType("text/plain").build();
+
+ // Configure where to store the output in a GCS bucket
+ GcsDestination gcsDestination =
+ GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
+ OutputConfig outputConfig =
+ OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
+
+ // Configure the glossary used in the request
+ GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId);
+ TranslateTextGlossaryConfig glossaryConfig =
+ TranslateTextGlossaryConfig.newBuilder().setGlossary(glossaryName.toString()).build();
+
+ // Configure the model used in the request
+ String modelPath =
+ String.format("projects/%s/locations/%s/models/%s", projectId, location, modelId);
+
+ // Build the request that will be sent to the API
+ BatchTranslateTextRequest request =
+ BatchTranslateTextRequest.newBuilder()
+ .setParent(parent.toString())
+ .setSourceLanguageCode(sourceLanguage)
+ .addTargetLanguageCodes(targetLanguage)
+ .addInputConfigs(inputConfig)
+ .setOutputConfig(outputConfig)
+ .putGlossaries(targetLanguage, glossaryConfig)
+ .putModels(targetLanguage, modelPath)
+ .build();
+
+ // Start an asynchronous request
+ OperationFuture future =
+ client.batchTranslateTextAsync(request);
+
+ System.out.println("Waiting for operation to complete...");
+ BatchTranslateResponse response = future.get(180, TimeUnit.SECONDS);
+ // Display the translation for each input text provided
+ System.out.printf("Total Characters: %s\n", response.getTotalCharacters());
+ System.out.printf("Translated Characters: %s\n", response.getTranslatedCharacters());
+ }
+ }
+}
+// [END translate_v3_batch_translate_text_with_glossary_and_model]
diff --git a/samples/snippets/src/main/java/com/example/translate/BatchTranslateTextWithModel.java b/samples/snippets/src/main/java/com/example/translate/BatchTranslateTextWithModel.java
new file mode 100644
index 00000000..acce7147
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/translate/BatchTranslateTextWithModel.java
@@ -0,0 +1,110 @@
+/*
+ * Copyright 2020 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.translate;
+
+// [START translate_v3_batch_translate_text_with_model]
+import com.google.api.gax.longrunning.OperationFuture;
+import com.google.cloud.translate.v3.BatchTranslateMetadata;
+import com.google.cloud.translate.v3.BatchTranslateResponse;
+import com.google.cloud.translate.v3.BatchTranslateTextRequest;
+import com.google.cloud.translate.v3.GcsDestination;
+import com.google.cloud.translate.v3.GcsSource;
+import com.google.cloud.translate.v3.InputConfig;
+import com.google.cloud.translate.v3.LocationName;
+import com.google.cloud.translate.v3.OutputConfig;
+import com.google.cloud.translate.v3.TranslationServiceClient;
+import java.io.IOException;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+public class BatchTranslateTextWithModel {
+
+ public static void batchTranslateTextWithModel()
+ throws InterruptedException, ExecutionException, IOException, 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/";
+ String modelId = "YOUR-MODEL-ID";
+ batchTranslateTextWithModel(
+ projectId, sourceLanguage, targetLanguage, inputUri, outputUri, modelId);
+ }
+
+ // Batch translate text using AutoML Translation model
+ public static void batchTranslateTextWithModel(
+ String projectId,
+ String sourceLanguage,
+ String targetLanguage,
+ String inputUri,
+ String outputUri,
+ String modelId)
+ throws IOException, ExecutionException, InterruptedException, TimeoutException {
+
+ // Initialize client that will be used to send requests. This client only needs to be created
+ // once, and can be reused for multiple requests. After completing all of your requests, call
+ // the "close" method on the client to safely clean up any remaining background resources.
+ try (TranslationServiceClient client = TranslationServiceClient.create()) {
+ // Supported Locations: `global`, [glossary location], or [model location]
+ // Glossaries must be hosted in `us-central1`
+ // Custom Models must use the same location as your model. (us-central1)
+ String location = "us-central1";
+ LocationName parent = LocationName.of(projectId, location);
+
+ // Configure the source of the file from a GCS bucket
+ GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build();
+ // Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
+ InputConfig inputConfig =
+ InputConfig.newBuilder().setGcsSource(gcsSource).setMimeType("text/plain").build();
+
+ // Configure where to store the output in a GCS bucket
+ GcsDestination gcsDestination =
+ GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
+ OutputConfig outputConfig =
+ OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
+
+ // Configure the model used in the request
+ String modelPath =
+ String.format("projects/%s/locations/%s/models/%s", projectId, location, modelId);
+
+ // Build the request that will be sent to the API
+ BatchTranslateTextRequest request =
+ BatchTranslateTextRequest.newBuilder()
+ .setParent(parent.toString())
+ .setSourceLanguageCode(sourceLanguage)
+ .addTargetLanguageCodes(targetLanguage)
+ .addInputConfigs(inputConfig)
+ .setOutputConfig(outputConfig)
+ .putModels(targetLanguage, modelPath)
+ .build();
+
+ // Start an asynchronous request
+ OperationFuture future =
+ client.batchTranslateTextAsync(request);
+
+ System.out.println("Waiting for operation to complete...");
+ BatchTranslateResponse response = future.get(180, TimeUnit.SECONDS);
+ // Display the translation for each input text provided
+ System.out.printf("Total Characters: %s\n", response.getTotalCharacters());
+ System.out.printf("Translated Characters: %s\n", response.getTranslatedCharacters());
+ }
+ }
+}
+// [END translate_v3_batch_translate_text_with_model]
diff --git a/samples/snippets/src/main/java/com/example/translate/CreateGlossary.java b/samples/snippets/src/main/java/com/example/translate/CreateGlossary.java
new file mode 100644
index 00000000..ab9f7de4
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/translate/CreateGlossary.java
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2020 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.translate;
+
+// [START translate_v3_create_glossary]
+import com.google.api.gax.longrunning.OperationFuture;
+import com.google.cloud.translate.v3.CreateGlossaryMetadata;
+import com.google.cloud.translate.v3.CreateGlossaryRequest;
+import com.google.cloud.translate.v3.GcsSource;
+import com.google.cloud.translate.v3.Glossary;
+import com.google.cloud.translate.v3.GlossaryInputConfig;
+import com.google.cloud.translate.v3.GlossaryName;
+import com.google.cloud.translate.v3.LocationName;
+import com.google.cloud.translate.v3.TranslationServiceClient;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+
+public class CreateGlossary {
+
+ public static void createGlossary() throws InterruptedException, ExecutionException, IOException {
+ // TODO(developer): Replace these variables before running the sample.
+ String projectId = "YOUR-PROJECT-ID";
+ String glossaryId = "your-glossary-display-name";
+ List languageCodes = new ArrayList<>();
+ languageCodes.add("your-language-code");
+ String inputUri = "gs://your-gcs-bucket/path/to/input/file.txt";
+ createGlossary(projectId, glossaryId, languageCodes, inputUri);
+ }
+
+ // Create a equivalent term sets glossary
+ // https://cloud.google.com/translate/docs/advanced/glossary#format-glossary
+ public static void createGlossary(
+ String projectId, String glossaryId, List languageCodes, String inputUri)
+ throws IOException, ExecutionException, InterruptedException {
+
+ // Initialize client that will be used to send requests. This client only needs to be created
+ // once, and can be reused for multiple requests. After completing all of your requests, call
+ // the "close" method on the client to safely clean up any remaining background resources.
+ try (TranslationServiceClient client = TranslationServiceClient.create()) {
+ // Supported Locations: `global`, [glossary location], or [model location]
+ // Glossaries must be hosted in `us-central1`
+ // Custom Models must use the same location as your model. (us-central1)
+ String location = "us-central1";
+ LocationName parent = LocationName.of(projectId, location);
+ GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId);
+
+ // Supported Languages: https://cloud.google.com/translate/docs/languages
+ Glossary.LanguageCodesSet languageCodesSet =
+ Glossary.LanguageCodesSet.newBuilder().addAllLanguageCodes(languageCodes).build();
+
+ // Configure the source of the file from a GCS bucket
+ GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build();
+ GlossaryInputConfig inputConfig =
+ GlossaryInputConfig.newBuilder().setGcsSource(gcsSource).build();
+
+ Glossary glossary =
+ Glossary.newBuilder()
+ .setName(glossaryName.toString())
+ .setLanguageCodesSet(languageCodesSet)
+ .setInputConfig(inputConfig)
+ .build();
+
+ CreateGlossaryRequest request =
+ CreateGlossaryRequest.newBuilder()
+ .setParent(parent.toString())
+ .setGlossary(glossary)
+ .build();
+
+ // Start an asynchronous request
+ OperationFuture future =
+ client.createGlossaryAsync(request);
+
+ System.out.println("Waiting for operation to complete...");
+ Glossary response = future.get();
+ System.out.println("Created Glossary.");
+ System.out.printf("Glossary name: %s\n", response.getName());
+ System.out.printf("Entry count: %s\n", response.getEntryCount());
+ System.out.printf("Input URI: %s\n", response.getInputConfig().getGcsSource().getInputUri());
+ }
+ }
+}
+// [END translate_v3_create_glossary]
diff --git a/samples/snippets/src/main/java/com/example/translate/DeleteGlossary.java b/samples/snippets/src/main/java/com/example/translate/DeleteGlossary.java
new file mode 100644
index 00000000..78266cdb
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/translate/DeleteGlossary.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2020 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.translate;
+
+// [START translate_v3_delete_glossary]
+import com.google.api.gax.longrunning.OperationFuture;
+import com.google.cloud.translate.v3.DeleteGlossaryMetadata;
+import com.google.cloud.translate.v3.DeleteGlossaryRequest;
+import com.google.cloud.translate.v3.DeleteGlossaryResponse;
+import com.google.cloud.translate.v3.GlossaryName;
+import com.google.cloud.translate.v3.TranslationServiceClient;
+import java.io.IOException;
+import java.util.concurrent.ExecutionException;
+
+public class DeleteGlossary {
+
+ public static void deleteGlossary() throws InterruptedException, ExecutionException, IOException {
+ // TODO(developer): Replace these variables before running the sample.
+ String projectId = "YOUR-PROJECT-ID";
+ String glossaryId = "your-glossary-display-name";
+ deleteGlossary(projectId, glossaryId);
+ }
+
+ // Delete a specific glossary based on the glossary ID
+ public static void deleteGlossary(String projectId, String glossaryId)
+ throws InterruptedException, ExecutionException, IOException {
+
+ // Initialize client that will be used to send requests. This client only needs to be created
+ // once, and can be reused for multiple requests. After completing all of your requests, call
+ // the "close" method on the client to safely clean up any remaining background resources.
+ try (TranslationServiceClient client = TranslationServiceClient.create()) {
+ // Supported Locations: `global`, [glossary location], or [model location]
+ // Glossaries must be hosted in `us-central1`
+ // Custom Models must use the same location as your model. (us-central1)
+ GlossaryName glossaryName = GlossaryName.of(projectId, "us-central1", glossaryId);
+ DeleteGlossaryRequest request =
+ DeleteGlossaryRequest.newBuilder().setName(glossaryName.toString()).build();
+
+ // Start an asynchronous request
+ OperationFuture future =
+ client.deleteGlossaryAsync(request);
+
+ System.out.println("Waiting for operation to complete...");
+ DeleteGlossaryResponse response = future.get();
+ System.out.format("Deleted Glossary: %s\n", response.getName());
+ }
+ }
+}
+// [END translate_v3_delete_glossary]
diff --git a/samples/snippets/src/main/java/com/example/translate/DetectLanguage.java b/samples/snippets/src/main/java/com/example/translate/DetectLanguage.java
new file mode 100644
index 00000000..a1b4c5fb
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/translate/DetectLanguage.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2020 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.translate;
+
+// [START translate_v3_detect_language]
+import com.google.cloud.translate.v3.DetectLanguageRequest;
+import com.google.cloud.translate.v3.DetectLanguageResponse;
+import com.google.cloud.translate.v3.DetectedLanguage;
+import com.google.cloud.translate.v3.LocationName;
+import com.google.cloud.translate.v3.TranslationServiceClient;
+import java.io.IOException;
+
+public class DetectLanguage {
+
+ public static void detectLanguage() throws IOException {
+ // TODO(developer): Replace these variables before running the sample.
+ String projectId = "YOUR-PROJECT-ID";
+ String text = "your-text";
+
+ detectLanguage(projectId, text);
+ }
+
+ // Detecting the language of a text string
+ public static void detectLanguage(String projectId, String text) throws IOException {
+
+ // Initialize client that will be used to send requests. This client only needs to be created
+ // once, and can be reused for multiple requests. After completing all of your requests, call
+ // the "close" method on the client to safely clean up any remaining background resources.
+ try (TranslationServiceClient client = TranslationServiceClient.create()) {
+ // Supported Locations: `global`, [glossary location], or [model location]
+ // Glossaries must be hosted in `us-central1`
+ // Custom Models must use the same location as your model. (us-central1)
+ LocationName parent = LocationName.of(projectId, "global");
+
+ // Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
+ DetectLanguageRequest request =
+ DetectLanguageRequest.newBuilder()
+ .setParent(parent.toString())
+ .setMimeType("text/plain")
+ .setContent(text)
+ .build();
+
+ DetectLanguageResponse response = client.detectLanguage(request);
+
+ // Display list of detected languages sorted by detection confidence.
+ // The most probable language is first.
+ for (DetectedLanguage language : response.getLanguagesList()) {
+ // The language detected
+ System.out.printf("Language code: %s\n", language.getLanguageCode());
+ // Confidence of detection result for this language
+ System.out.printf("Confidence: %s\n", language.getConfidence());
+ }
+ }
+ }
+}
+// [END translate_v3_detect_language]
diff --git a/samples/snippets/src/main/java/com/example/translate/GetGlossary.java b/samples/snippets/src/main/java/com/example/translate/GetGlossary.java
new file mode 100644
index 00000000..c3e8283b
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/translate/GetGlossary.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2020 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.translate;
+
+// [START translate_v3_get_glossary]
+import com.google.cloud.translate.v3.GetGlossaryRequest;
+import com.google.cloud.translate.v3.Glossary;
+import com.google.cloud.translate.v3.GlossaryName;
+import com.google.cloud.translate.v3.TranslationServiceClient;
+import java.io.IOException;
+
+public class GetGlossary {
+
+ public static void getGlossary() throws IOException {
+ // TODO(developer): Replace these variables before running the sample.
+ String projectId = "YOUR-PROJECT-ID";
+ String glossaryId = "your-glossary-display-name";
+ getGlossary(projectId, glossaryId);
+ }
+
+ // Get a particular glossary based on the glossary ID
+ public static void getGlossary(String projectId, String glossaryId) throws IOException {
+ // Initialize client that will be used to send requests. This client only needs to be created
+ // once, and can be reused for multiple requests. After completing all of your requests, call
+ // the "close" method on the client to safely clean up any remaining background resources.
+ try (TranslationServiceClient client = TranslationServiceClient.create()) {
+ // Supported Locations: `global`, [glossary location], or [model location]
+ // Glossaries must be hosted in `us-central1`
+ // Custom Models must use the same location as your model. (us-central1)
+ GlossaryName glossaryName = GlossaryName.of(projectId, "us-central1", glossaryId);
+ GetGlossaryRequest request =
+ GetGlossaryRequest.newBuilder().setName(glossaryName.toString()).build();
+
+ Glossary response = client.getGlossary(request);
+
+ System.out.printf("Glossary name: %s\n", response.getName());
+ System.out.printf("Entry count: %s\n", response.getEntryCount());
+ System.out.printf("Input URI: %s\n", response.getInputConfig().getGcsSource().getInputUri());
+ }
+ }
+}
+// [END translate_v3_get_glossary]
diff --git a/samples/snippets/src/main/java/com/example/translate/GetSupportedLanguages.java b/samples/snippets/src/main/java/com/example/translate/GetSupportedLanguages.java
new file mode 100644
index 00000000..08eb5d46
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/translate/GetSupportedLanguages.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2020 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.translate;
+
+// [START translate_v3_get_supported_languages]
+import com.google.cloud.translate.v3.GetSupportedLanguagesRequest;
+import com.google.cloud.translate.v3.LocationName;
+import com.google.cloud.translate.v3.SupportedLanguage;
+import com.google.cloud.translate.v3.SupportedLanguages;
+import com.google.cloud.translate.v3.TranslationServiceClient;
+import java.io.IOException;
+
+public class GetSupportedLanguages {
+
+ public static void getSupportedLanguages() throws IOException {
+ // TODO(developer): Replace these variables before running the sample.
+ String projectId = "YOUR-PROJECT-ID";
+ getSupportedLanguages(projectId);
+ }
+
+ // Getting a list of supported language codes
+ public static void getSupportedLanguages(String projectId) throws IOException {
+
+ // Initialize client that will be used to send requests. This client only needs to be created
+ // once, and can be reused for multiple requests. After completing all of your requests, call
+ // the "close" method on the client to safely clean up any remaining background resources.
+ try (TranslationServiceClient client = TranslationServiceClient.create()) {
+ // Supported Locations: `global`, [glossary location], or [model location]
+ // Glossaries must be hosted in `us-central1`
+ // Custom Models must use the same location as your model. (us-central1)
+ LocationName parent = LocationName.of(projectId, "global");
+ GetSupportedLanguagesRequest request =
+ GetSupportedLanguagesRequest.newBuilder().setParent(parent.toString()).build();
+
+ SupportedLanguages response = client.getSupportedLanguages(request);
+
+ // List language codes of supported languages
+ for (SupportedLanguage language : response.getLanguagesList()) {
+ System.out.printf("Language Code: %s\n", language.getLanguageCode());
+ }
+ }
+ }
+}
+// [END translate_v3_get_supported_languages]
diff --git a/samples/snippets/src/main/java/com/example/translate/GetSupportedLanguagesForTarget.java b/samples/snippets/src/main/java/com/example/translate/GetSupportedLanguagesForTarget.java
new file mode 100644
index 00000000..d3de319a
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/translate/GetSupportedLanguagesForTarget.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2020 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.translate;
+
+// [START translate_v3_get_supported_languages_for_target]
+import com.google.cloud.translate.v3.GetSupportedLanguagesRequest;
+import com.google.cloud.translate.v3.LocationName;
+import com.google.cloud.translate.v3.SupportedLanguage;
+import com.google.cloud.translate.v3.SupportedLanguages;
+import com.google.cloud.translate.v3.TranslationServiceClient;
+import java.io.IOException;
+
+public class GetSupportedLanguagesForTarget {
+
+ public static void getSupportedLanguagesForTarget() throws IOException {
+ // TODO(developer): Replace these variables before running the sample.
+ String projectId = "YOUR-PROJECT-ID";
+ // Supported Languages: https://cloud.google.com/translate/docs/languages
+ String languageCode = "your-language-code";
+ getSupportedLanguagesForTarget(projectId, languageCode);
+ }
+
+ // Listing supported languages with target language name
+ public static void getSupportedLanguagesForTarget(String projectId, String languageCode)
+ throws IOException {
+
+ // Initialize client that will be used to send requests. This client only needs to be created
+ // once, and can be reused for multiple requests. After completing all of your requests, call
+ // the "close" method on the client to safely clean up any remaining background resources.
+ try (TranslationServiceClient client = TranslationServiceClient.create()) {
+ // Supported Locations: `global`, [glossary location], or [model location]
+ // Glossaries must be hosted in `us-central1`
+ // Custom Models must use the same location as your model. (us-central1)
+ LocationName parent = LocationName.of(projectId, "global");
+ GetSupportedLanguagesRequest request =
+ GetSupportedLanguagesRequest.newBuilder()
+ .setParent(parent.toString())
+ .setDisplayLanguageCode(languageCode)
+ .build();
+
+ SupportedLanguages response = client.getSupportedLanguages(request);
+
+ // List language codes of supported languages
+ for (SupportedLanguage language : response.getLanguagesList()) {
+ System.out.printf("Language Code: %s\n", language.getLanguageCode());
+ System.out.printf("Display Name: %s\n", language.getDisplayName());
+ }
+ }
+ }
+}
+// [END translate_v3_get_supported_languages_for_target]
diff --git a/samples/snippets/src/main/java/com/example/translate/ListGlossaries.java b/samples/snippets/src/main/java/com/example/translate/ListGlossaries.java
new file mode 100644
index 00000000..129d58ec
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/translate/ListGlossaries.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2020 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.translate;
+
+// [START translate_v3_list_glossary]
+import com.google.cloud.translate.v3.Glossary;
+import com.google.cloud.translate.v3.ListGlossariesRequest;
+import com.google.cloud.translate.v3.LocationName;
+import com.google.cloud.translate.v3.TranslationServiceClient;
+import java.io.IOException;
+
+public class ListGlossaries {
+
+ public static void listGlossaries() throws IOException {
+ // TODO(developer): Replace these variables before running the sample.
+ String projectId = "YOUR-PROJECT-ID";
+ listGlossaries(projectId);
+ }
+
+ // List all the glossaries in a specified location
+ public static void listGlossaries(String projectId) throws IOException {
+ // Initialize client that will be used to send requests. This client only needs to be created
+ // once, and can be reused for multiple requests. After completing all of your requests, call
+ // the "close" method on the client to safely clean up any remaining background resources.
+ try (TranslationServiceClient client = TranslationServiceClient.create()) {
+ // Supported Locations: `global`, [glossary location], or [model location]
+ // Glossaries must be hosted in `us-central1`
+ // Custom Models must use the same location as your model. (us-central1)
+ LocationName parent = LocationName.of(projectId, "us-central1");
+ ListGlossariesRequest request =
+ ListGlossariesRequest.newBuilder().setParent(parent.toString()).build();
+
+ for (Glossary responseItem : client.listGlossaries(request).iterateAll()) {
+ System.out.printf("Glossary name: %s\n", responseItem.getName());
+ System.out.printf("Entry count: %s\n", responseItem.getEntryCount());
+ System.out.printf(
+ "Input URI: %s\n", responseItem.getInputConfig().getGcsSource().getInputUri());
+ }
+ }
+ }
+}
+// [END translate_v3_list_glossary]
diff --git a/samples/snippets/src/main/java/com/example/translate/QuickstartSample.java b/samples/snippets/src/main/java/com/example/translate/QuickstartSample.java
new file mode 100644
index 00000000..d946c13a
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/translate/QuickstartSample.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * 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.translate;
+
+// [START translate_quickstart]
+// Imports the Google Cloud client library
+import com.google.cloud.translate.Translate;
+import com.google.cloud.translate.Translate.TranslateOption;
+import com.google.cloud.translate.TranslateOptions;
+import com.google.cloud.translate.Translation;
+
+public class QuickstartSample {
+ public static void main(String... args) throws Exception {
+ // Instantiates a client
+ Translate translate = TranslateOptions.getDefaultInstance().getService();
+
+ // The text to translate
+ String text = "Hello, world!";
+
+ // Translates some text into Russian
+ Translation translation =
+ translate.translate(
+ text, TranslateOption.sourceLanguage("en"), TranslateOption.targetLanguage("ru"));
+
+ System.out.printf("Text: %s%n", text);
+ System.out.printf("Translation: %s%n", translation.getTranslatedText());
+ }
+}
+// [END translate_quickstart]
diff --git a/samples/snippets/src/main/java/com/example/translate/TranslateText.java b/samples/snippets/src/main/java/com/example/translate/TranslateText.java
new file mode 100644
index 00000000..a36b54a6
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/translate/TranslateText.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2020 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.translate;
+
+// [START translate_v3_translate_text]
+import com.google.cloud.translate.v3.LocationName;
+import com.google.cloud.translate.v3.TranslateTextRequest;
+import com.google.cloud.translate.v3.TranslateTextResponse;
+import com.google.cloud.translate.v3.Translation;
+import com.google.cloud.translate.v3.TranslationServiceClient;
+import java.io.IOException;
+
+public class TranslateText {
+
+ public static void translateText() throws IOException {
+ // TODO(developer): Replace these variables before running the sample.
+ String projectId = "YOUR-PROJECT-ID";
+ // Supported Languages: https://cloud.google.com/translate/docs/languages
+ String targetLanguage = "your-target-language";
+ String text = "your-text";
+ translateText(projectId, targetLanguage, text);
+ }
+
+ // Translating Text
+ public static void translateText(String projectId, String targetLanguage, String text)
+ throws IOException {
+
+ // Initialize client that will be used to send requests. This client only needs to be created
+ // once, and can be reused for multiple requests. After completing all of your requests, call
+ // the "close" method on the client to safely clean up any remaining background resources.
+ try (TranslationServiceClient client = TranslationServiceClient.create()) {
+ // Supported Locations: `global`, [glossary location], or [model location]
+ // Glossaries must be hosted in `us-central1`
+ // Custom Models must use the same location as your model. (us-central1)
+ LocationName parent = LocationName.of(projectId, "global");
+
+ // Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
+ TranslateTextRequest request =
+ TranslateTextRequest.newBuilder()
+ .setParent(parent.toString())
+ .setMimeType("text/plain")
+ .setTargetLanguageCode(targetLanguage)
+ .addContents(text)
+ .build();
+
+ TranslateTextResponse response = client.translateText(request);
+
+ // Display the translation for each input text provided
+ for (Translation translation : response.getTranslationsList()) {
+ System.out.printf("Translated text: %s\n", translation.getTranslatedText());
+ }
+ }
+ }
+}
+// [END translate_v3_translate_text]
diff --git a/samples/snippets/src/main/java/com/example/translate/TranslateTextWithGlossary.java b/samples/snippets/src/main/java/com/example/translate/TranslateTextWithGlossary.java
new file mode 100644
index 00000000..78c35aa6
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/translate/TranslateTextWithGlossary.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2020 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.translate;
+
+// [START translate_v3_translate_text_with_glossary]
+import com.google.cloud.translate.v3.GlossaryName;
+import com.google.cloud.translate.v3.LocationName;
+import com.google.cloud.translate.v3.TranslateTextGlossaryConfig;
+import com.google.cloud.translate.v3.TranslateTextRequest;
+import com.google.cloud.translate.v3.TranslateTextResponse;
+import com.google.cloud.translate.v3.Translation;
+import com.google.cloud.translate.v3.TranslationServiceClient;
+import java.io.IOException;
+
+public class TranslateTextWithGlossary {
+
+ public static void translateTextWithGlossary() throws IOException {
+ // 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 text = "your-text";
+ String glossaryId = "your-glossary-display-name";
+ translateTextWithGlossary(projectId, sourceLanguage, targetLanguage, text, glossaryId);
+ }
+
+ // Translates a given text using a glossary.
+ public static void translateTextWithGlossary(
+ String projectId,
+ String sourceLanguage,
+ String targetLanguage,
+ String text,
+ String glossaryId)
+ throws IOException {
+
+ // Initialize client that will be used to send requests. This client only needs to be created
+ // once, and can be reused for multiple requests. After completing all of your requests, call
+ // the "close" method on the client to safely clean up any remaining background resources.
+ try (TranslationServiceClient client = TranslationServiceClient.create()) {
+ // Supported Locations: `global`, [glossary location], or [model location]
+ // Glossaries must be hosted in `us-central1`
+ // Custom Models must use the same location as your model. (us-central1)
+ String location = "us-central1";
+ LocationName parent = LocationName.of(projectId, location);
+
+ GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId);
+ TranslateTextGlossaryConfig glossaryConfig =
+ TranslateTextGlossaryConfig.newBuilder().setGlossary(glossaryName.toString()).build();
+
+ // Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
+ TranslateTextRequest request =
+ TranslateTextRequest.newBuilder()
+ .setParent(parent.toString())
+ .setMimeType("text/plain")
+ .setSourceLanguageCode(sourceLanguage)
+ .setTargetLanguageCode(targetLanguage)
+ .addContents(text)
+ .setGlossaryConfig(glossaryConfig)
+ .build();
+
+ TranslateTextResponse response = client.translateText(request);
+
+ // Display the translation for each input text provided
+ for (Translation translation : response.getGlossaryTranslationsList()) {
+ System.out.printf("Translated text: %s\n", translation.getTranslatedText());
+ }
+ }
+ }
+}
+// [END translate_v3_translate_text_with_glossary]
diff --git a/samples/snippets/src/main/java/com/example/translate/TranslateTextWithGlossaryAndModel.java b/samples/snippets/src/main/java/com/example/translate/TranslateTextWithGlossaryAndModel.java
new file mode 100644
index 00000000..a383689d
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/translate/TranslateTextWithGlossaryAndModel.java
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2020 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.translate;
+
+// [START translate_v3_translate_text_with_glossary_and_model]
+import com.google.cloud.translate.v3.GlossaryName;
+import com.google.cloud.translate.v3.LocationName;
+import com.google.cloud.translate.v3.TranslateTextGlossaryConfig;
+import com.google.cloud.translate.v3.TranslateTextRequest;
+import com.google.cloud.translate.v3.TranslateTextResponse;
+import com.google.cloud.translate.v3.Translation;
+import com.google.cloud.translate.v3.TranslationServiceClient;
+import java.io.IOException;
+
+public class TranslateTextWithGlossaryAndModel {
+
+ public static void translateTextWithGlossaryAndModel() throws IOException {
+ // 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 text = "your-text";
+ String glossaryId = "your-glossary-display-name";
+ String modelId = "YOUR-MODEL-ID";
+ translateTextWithGlossaryAndModel(
+ projectId, sourceLanguage, targetLanguage, text, glossaryId, modelId);
+ }
+
+ // Translating Text with Glossary and Model
+ public static void translateTextWithGlossaryAndModel(
+ String projectId,
+ String sourceLanguage,
+ String targetLanguage,
+ String text,
+ String glossaryId,
+ String modelId)
+ throws IOException {
+
+ // Initialize client that will be used to send requests. This client only needs to be created
+ // once, and can be reused for multiple requests. After completing all of your requests, call
+ // the "close" method on the client to safely clean up any remaining background resources.
+ try (TranslationServiceClient client = TranslationServiceClient.create()) {
+ // Supported Locations: `global`, [glossary location], or [model location]
+ // Glossaries must be hosted in `us-central1`
+ // Custom Models must use the same location as your model. (us-central1)
+ String location = "us-central1";
+ LocationName parent = LocationName.of(projectId, location);
+
+ GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId);
+ TranslateTextGlossaryConfig glossaryConfig =
+ TranslateTextGlossaryConfig.newBuilder().setGlossary(glossaryName.toString()).build();
+
+ String modelPath =
+ String.format("projects/%s/locations/%s/models/%s", projectId, location, modelId);
+
+ // Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
+ TranslateTextRequest request =
+ TranslateTextRequest.newBuilder()
+ .setParent(parent.toString())
+ .setMimeType("text/plain")
+ .setSourceLanguageCode(sourceLanguage)
+ .setTargetLanguageCode(targetLanguage)
+ .addContents(text)
+ .setGlossaryConfig(glossaryConfig)
+ .setModel(modelPath)
+ .build();
+
+ TranslateTextResponse response = client.translateText(request);
+
+ // Display the translation for each input text provided
+ for (Translation translation : response.getGlossaryTranslationsList()) {
+ System.out.printf("Translated text: %s\n", translation.getTranslatedText());
+ }
+ }
+ }
+}
+// [END translate_v3_translate_text_with_glossary_and_model]
diff --git a/samples/snippets/src/main/java/com/example/translate/TranslateTextWithModel.java b/samples/snippets/src/main/java/com/example/translate/TranslateTextWithModel.java
new file mode 100644
index 00000000..9d81c797
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/translate/TranslateTextWithModel.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2020 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.translate;
+
+// [START translate_v3_translate_text_with_model]
+import com.google.cloud.translate.v3.LocationName;
+import com.google.cloud.translate.v3.TranslateTextRequest;
+import com.google.cloud.translate.v3.TranslateTextResponse;
+import com.google.cloud.translate.v3.Translation;
+import com.google.cloud.translate.v3.TranslationServiceClient;
+import java.io.IOException;
+
+public class TranslateTextWithModel {
+
+ public static void translateTextWithModel() throws IOException {
+ // 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 text = "your-text";
+ String modelId = "YOUR-MODEL-ID";
+ translateTextWithModel(projectId, sourceLanguage, targetLanguage, text, modelId);
+ }
+
+ // Translating Text with Model
+ public static void translateTextWithModel(
+ String projectId, String sourceLanguage, String targetLanguage, String text, String modelId)
+ throws IOException {
+
+ // Initialize client that will be used to send requests. This client only needs to be created
+ // once, and can be reused for multiple requests. After completing all of your requests, call
+ // the "close" method on the client to safely clean up any remaining background resources.
+ try (TranslationServiceClient client = TranslationServiceClient.create()) {
+ // Supported Locations: `global`, [glossary location], or [model location]
+ // Glossaries must be hosted in `us-central1`
+ // Custom Models must use the same location as your model. (us-central1)
+ String location = "us-central1";
+ LocationName parent = LocationName.of(projectId, location);
+ String modelPath =
+ String.format("projects/%s/locations/%s/models/%s", projectId, location, modelId);
+
+ // Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
+ TranslateTextRequest request =
+ TranslateTextRequest.newBuilder()
+ .setParent(parent.toString())
+ .setMimeType("text/plain")
+ .setSourceLanguageCode(sourceLanguage)
+ .setTargetLanguageCode(targetLanguage)
+ .addContents(text)
+ .setModel(modelPath)
+ .build();
+
+ TranslateTextResponse response = client.translateText(request);
+
+ // Display the translation for each input text provided
+ for (Translation translation : response.getTranslationsList()) {
+ System.out.printf("Translated text: %s\n", translation.getTranslatedText());
+ }
+ }
+ }
+}
+// [END translate_v3_translate_text_with_model]
diff --git a/samples/snippets/src/test/java/com/example/translate/BatchTranslateTextTests.java b/samples/snippets/src/test/java/com/example/translate/BatchTranslateTextTests.java
new file mode 100644
index 00000000..2b41f00d
--- /dev/null
+++ b/samples/snippets/src/test/java/com/example/translate/BatchTranslateTextTests.java
@@ -0,0 +1,110 @@
+/*
+ * Copyright 2020 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.translate;
+
+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 Text sample. */
+@RunWith(JUnit4.class)
+@SuppressWarnings("checkstyle:abbreviationaswordinname")
+public class BatchTranslateTextTests {
+ private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
+ private static final String INPUT_URI = "gs://cloud-samples-data/translation/text.txt";
+ private static final String PREFIX = "BATCH_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 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(
+ "Environment variable '%s' is required to perform these tests.".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);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() {
+ cleanUpBucket();
+ System.setOut(null);
+ }
+
+ @Test
+ public void testBatchTranslateText()
+ throws InterruptedException, ExecutionException, IOException, TimeoutException {
+ BatchTranslateText.batchTranslateText(PROJECT_ID, "en", "es", INPUT_URI, OUTPUT_URI);
+ String got = bout.toString();
+ assertThat(got).contains("Total Characters: 13");
+ }
+}
diff --git a/samples/snippets/src/test/java/com/example/translate/BatchTranslateTextWithGlossaryAndModelTests.java b/samples/snippets/src/test/java/com/example/translate/BatchTranslateTextWithGlossaryAndModelTests.java
new file mode 100644
index 00000000..b88419d9
--- /dev/null
+++ b/samples/snippets/src/test/java/com/example/translate/BatchTranslateTextWithGlossaryAndModelTests.java
@@ -0,0 +1,129 @@
+/*
+ * Copyright 2020 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.translate;
+
+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.ArrayList;
+import java.util.List;
+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 Text With Glossary sample. */
+@RunWith(JUnit4.class)
+@SuppressWarnings("checkstyle:abbreviationaswordinname")
+public class BatchTranslateTextWithGlossaryAndModelTests {
+ private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
+ private static final String INPUT_URI =
+ "gs://cloud-samples-data/translation/text_with_custom_model_and_glossary.txt";
+ private static final String GLOSSARY_ID =
+ String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
+ private static final String MODEL_ID = "TRL2188848820815848149";
+ private static final String GLOSSARY_INPUT_URI =
+ "gs://cloud-samples-data/translation/glossary_ja.csv";
+ private static final String PREFIX = "BATCH_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 static final 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(
+ "Environment variable '%s' is required to perform these tests.".format(varName),
+ System.getenv(varName));
+ }
+
+ @BeforeClass
+ public static void checkRequirements() {
+ requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
+ requireEnvVar("GOOGLE_CLOUD_PROJECT");
+ }
+
+ @Before
+ public void setUp() throws InterruptedException, ExecutionException, IOException {
+ // Create a glossary that can be used in the test
+ PrintStream temp = new PrintStream(new ByteArrayOutputStream());
+ System.setOut(temp);
+ List languageCodes = new ArrayList<>();
+ languageCodes.add("en");
+ languageCodes.add("ja");
+ CreateGlossary.createGlossary(PROJECT_ID, GLOSSARY_ID, languageCodes, GLOSSARY_INPUT_URI);
+
+ bout = new ByteArrayOutputStream();
+ out = new PrintStream(bout);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() throws InterruptedException, ExecutionException, IOException {
+ cleanUpBucket();
+ // Delete the created glossary
+ DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
+ System.setOut(null);
+ }
+
+ @Test
+ public void testBatchTranslateTextWithGlossaryAndModel()
+ throws InterruptedException, ExecutionException, IOException, TimeoutException {
+ BatchTranslateTextWithGlossaryAndModel.batchTranslateTextWithGlossaryAndModel(
+ PROJECT_ID, "en", "ja", INPUT_URI, OUTPUT_URI, GLOSSARY_ID, MODEL_ID);
+ String got = bout.toString();
+ assertThat(got).contains("Total Characters: 25");
+ }
+}
diff --git a/samples/snippets/src/test/java/com/example/translate/BatchTranslateTextWithGlossaryTests.java b/samples/snippets/src/test/java/com/example/translate/BatchTranslateTextWithGlossaryTests.java
new file mode 100644
index 00000000..92258d8e
--- /dev/null
+++ b/samples/snippets/src/test/java/com/example/translate/BatchTranslateTextWithGlossaryTests.java
@@ -0,0 +1,129 @@
+/*
+ * Copyright 2020 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.translate;
+
+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.ArrayList;
+import java.util.List;
+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 Text With Glossary and Model sample. */
+@RunWith(JUnit4.class)
+@SuppressWarnings("checkstyle:abbreviationaswordinname")
+public class BatchTranslateTextWithGlossaryTests {
+ private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
+ private static final String INPUT_URI =
+ "gs://cloud-samples-data/translation/text_with_glossary.txt";
+ private static final String GLOSSARY_INPUT_URI =
+ "gs://cloud-samples-data/translation/glossary_ja.csv";
+ private static final String GLOSSARY_ID =
+ String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
+ private static final String PREFIX = "BATCH_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 static final 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(
+ "Environment variable '%s' is required to perform these tests.".format(varName),
+ System.getenv(varName));
+ }
+
+ @BeforeClass
+ public static void checkRequirements() {
+ requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
+ requireEnvVar("GOOGLE_CLOUD_PROJECT");
+ }
+
+ @Before
+ public void setUp() throws InterruptedException, ExecutionException, IOException {
+ // Create a glossary that can be used in the test
+ PrintStream temp = new PrintStream(new ByteArrayOutputStream());
+ System.setOut(temp);
+ List languageCodes = new ArrayList<>();
+ languageCodes.add("en");
+ languageCodes.add("ja");
+ CreateGlossary.createGlossary(PROJECT_ID, GLOSSARY_ID, languageCodes, GLOSSARY_INPUT_URI);
+
+ bout = new ByteArrayOutputStream();
+ out = new PrintStream(bout);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() throws InterruptedException, ExecutionException, IOException {
+ // Clean up
+ cleanUpBucket();
+ // Delete the created glossary
+ DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
+ System.setOut(null);
+ }
+
+ @Test
+ public void testBatchTranslateTextWithGlossary()
+ throws InterruptedException, ExecutionException, IOException, TimeoutException {
+ BatchTranslateTextWithGlossary.batchTranslateTextWithGlossary(
+ PROJECT_ID, "en", "ja", INPUT_URI, OUTPUT_URI, GLOSSARY_ID);
+ String got = bout.toString();
+ assertThat(got).contains("Total Characters: 9");
+ }
+}
diff --git a/samples/snippets/src/test/java/com/example/translate/BatchTranslateTextWithModelTests.java b/samples/snippets/src/test/java/com/example/translate/BatchTranslateTextWithModelTests.java
new file mode 100644
index 00000000..db7ef195
--- /dev/null
+++ b/samples/snippets/src/test/java/com/example/translate/BatchTranslateTextWithModelTests.java
@@ -0,0 +1,113 @@
+/*
+ * Copyright 2020 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.translate;
+
+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 Text With Model sample. */
+@RunWith(JUnit4.class)
+@SuppressWarnings("checkstyle:abbreviationaswordinname")
+public class BatchTranslateTextWithModelTests {
+ private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
+ private static final String INPUT_URI =
+ "gs://cloud-samples-data/translation/custom_model_text.txt";
+ private static final String MODEL_ID = "TRL2188848820815848149";
+ private static final String PREFIX = "BATCH_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 static final 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(
+ "Environment variable '%s' is required to perform these tests.".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);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() {
+ cleanUpBucket();
+ System.setOut(null);
+ }
+
+ @Test
+ public void testBatchTranslateTextWithModel()
+ throws InterruptedException, ExecutionException, IOException, TimeoutException {
+ BatchTranslateTextWithModel.batchTranslateTextWithModel(
+ PROJECT_ID, "en", "ja", INPUT_URI, OUTPUT_URI, MODEL_ID);
+ String got = bout.toString();
+ assertThat(got).contains("Total Characters: 15");
+ }
+}
diff --git a/samples/snippets/src/test/java/com/example/translate/CreateGlossaryTests.java b/samples/snippets/src/test/java/com/example/translate/CreateGlossaryTests.java
new file mode 100644
index 00000000..258dd378
--- /dev/null
+++ b/samples/snippets/src/test/java/com/example/translate/CreateGlossaryTests.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2020 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.translate;
+
+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 java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.ExecutionException;
+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;
+
+@RunWith(JUnit4.class)
+@SuppressWarnings("checkstyle:abbreviationaswordinname")
+public class CreateGlossaryTests {
+
+ private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
+ private static final String GLOSSARY_INPUT_URI =
+ "gs://cloud-samples-data/translation/glossary_ja.csv";
+ private static final String GLOSSARY_ID =
+ String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
+
+ private ByteArrayOutputStream bout;
+ private PrintStream out;
+
+ private static void requireEnvVar(String varName) {
+ assertNotNull(
+ "Environment variable '%s' is required to perform these tests.".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);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() throws InterruptedException, ExecutionException, IOException {
+ // Delete the created glossary
+ DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
+
+ System.setOut(null);
+ }
+
+ @Test
+ public void testCreateGlossary() throws InterruptedException, ExecutionException, IOException {
+ List languageCodes = new ArrayList<>();
+ languageCodes.add("en");
+ languageCodes.add("ja");
+ CreateGlossary.createGlossary(PROJECT_ID, GLOSSARY_ID, languageCodes, GLOSSARY_INPUT_URI);
+
+ String got = bout.toString();
+ assertThat(got).contains("Created");
+ assertThat(got).contains(GLOSSARY_ID);
+ assertThat(got).contains(GLOSSARY_INPUT_URI);
+ }
+}
diff --git a/samples/snippets/src/test/java/com/example/translate/DeleteGlossaryTests.java b/samples/snippets/src/test/java/com/example/translate/DeleteGlossaryTests.java
new file mode 100644
index 00000000..800a3236
--- /dev/null
+++ b/samples/snippets/src/test/java/com/example/translate/DeleteGlossaryTests.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2020 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.translate;
+
+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 java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.ExecutionException;
+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 Create and Delete Glossary samples. */
+@RunWith(JUnit4.class)
+@SuppressWarnings("checkstyle:abbreviationaswordinname")
+public class DeleteGlossaryTests {
+
+ private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
+ private static final String GLOSSARY_INPUT_URI =
+ "gs://cloud-samples-data/translation/glossary_ja.csv";
+ private static final String GLOSSARY_ID =
+ String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
+
+ private ByteArrayOutputStream bout;
+ private PrintStream out;
+
+ private static void requireEnvVar(String varName) {
+ assertNotNull(
+ "Environment variable '%s' is required to perform these tests.".format(varName),
+ System.getenv(varName));
+ }
+
+ @BeforeClass
+ public static void checkRequirements() {
+ requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
+ requireEnvVar("GOOGLE_CLOUD_PROJECT");
+ }
+
+ @Before
+ public void setUp() throws InterruptedException, ExecutionException, IOException {
+ // Create a glossary to be deleted
+ PrintStream temp = new PrintStream(new ByteArrayOutputStream());
+ System.setOut(temp);
+ List languageCodes = new ArrayList<>();
+ languageCodes.add("en");
+ languageCodes.add("ja");
+ CreateGlossary.createGlossary(PROJECT_ID, GLOSSARY_ID, languageCodes, GLOSSARY_INPUT_URI);
+
+ bout = new ByteArrayOutputStream();
+ out = new PrintStream(bout);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() {
+ System.setOut(null);
+ }
+
+ @Test
+ public void testDeleteGlossary() throws InterruptedException, ExecutionException, IOException {
+ DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
+ String got = bout.toString();
+ assertThat(got).contains("us-central1");
+ assertThat(got).contains(GLOSSARY_ID);
+ }
+}
diff --git a/samples/snippets/src/test/java/com/example/translate/DetectLanguageTests.java b/samples/snippets/src/test/java/com/example/translate/DetectLanguageTests.java
new file mode 100644
index 00000000..c93b7a11
--- /dev/null
+++ b/samples/snippets/src/test/java/com/example/translate/DetectLanguageTests.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2020 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.translate;
+
+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 Detect Languages sample. */
+@RunWith(JUnit4.class)
+@SuppressWarnings("checkstyle:abbreviationaswordinname")
+public class DetectLanguageTests {
+ private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
+
+ private ByteArrayOutputStream bout;
+ private PrintStream out;
+
+ private static void requireEnvVar(String varName) {
+ assertNotNull(
+ "Environment variable '%s' is required to perform these tests.".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);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() {
+ System.setOut(null);
+ }
+
+ @Test
+ public void testDetectLanguage() throws IOException {
+ DetectLanguage.detectLanguage(PROJECT_ID, "Hæ sæta");
+ String got = bout.toString();
+ assertThat(got).contains("is");
+ }
+}
diff --git a/samples/snippets/src/test/java/com/example/translate/GetGlossaryTests.java b/samples/snippets/src/test/java/com/example/translate/GetGlossaryTests.java
new file mode 100644
index 00000000..b8ce2cdb
--- /dev/null
+++ b/samples/snippets/src/test/java/com/example/translate/GetGlossaryTests.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2020 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.translate;
+
+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 java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.ExecutionException;
+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 Get Glossary sample. */
+@RunWith(JUnit4.class)
+@SuppressWarnings("checkstyle:abbreviationaswordinname")
+public class GetGlossaryTests {
+ private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
+ private static final String GLOSSARY_INPUT_URI =
+ "gs://cloud-samples-data/translation/glossary_ja.csv";
+ private static final String GLOSSARY_ID =
+ String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
+
+ private ByteArrayOutputStream bout;
+ private PrintStream out;
+
+ private static void requireEnvVar(String varName) {
+ assertNotNull(
+ "Environment variable '%s' is required to perform these tests.".format(varName),
+ System.getenv(varName));
+ }
+
+ @BeforeClass
+ public static void checkRequirements() {
+ requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
+ requireEnvVar("GOOGLE_CLOUD_PROJECT");
+ }
+
+ @Before
+ public void setUp() throws InterruptedException, ExecutionException, IOException {
+ // Create a glossary that can be used in the test
+ PrintStream temp = new PrintStream(new ByteArrayOutputStream());
+ System.setOut(temp);
+ List languageCodes = new ArrayList<>();
+ languageCodes.add("en");
+ languageCodes.add("ja");
+ CreateGlossary.createGlossary(PROJECT_ID, GLOSSARY_ID, languageCodes, GLOSSARY_INPUT_URI);
+
+ bout = new ByteArrayOutputStream();
+ out = new PrintStream(bout);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() throws InterruptedException, ExecutionException, IOException {
+ // Delete the created glossary
+ DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
+ System.setOut(null);
+ }
+
+ @Test
+ public void testGetGlossary() throws IOException {
+ GetGlossary.getGlossary(PROJECT_ID, GLOSSARY_ID);
+ String got = bout.toString();
+ assertThat(got).contains(GLOSSARY_ID);
+ assertThat(got).contains(GLOSSARY_INPUT_URI);
+ }
+}
diff --git a/samples/snippets/src/test/java/com/example/translate/GetSupportedLanguagesForTargetTests.java b/samples/snippets/src/test/java/com/example/translate/GetSupportedLanguagesForTargetTests.java
new file mode 100644
index 00000000..1381c0af
--- /dev/null
+++ b/samples/snippets/src/test/java/com/example/translate/GetSupportedLanguagesForTargetTests.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2020 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.translate;
+
+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 Get Supported Languages For Target sample. */
+@RunWith(JUnit4.class)
+@SuppressWarnings("checkstyle:abbreviationaswordinname")
+public class GetSupportedLanguagesForTargetTests {
+ private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
+
+ private ByteArrayOutputStream bout;
+ private PrintStream out;
+
+ private static void requireEnvVar(String varName) {
+ assertNotNull(
+ "Environment variable '%s' is required to perform these tests.".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);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() {
+ System.setOut(null);
+ }
+
+ @Test
+ public void testGetSupportedLanguages() throws IOException {
+ GetSupportedLanguagesForTarget.getSupportedLanguagesForTarget(PROJECT_ID, "is");
+ String got = bout.toString();
+ assertThat(got).contains("Language Code: sq");
+ assertThat(got).contains("Display Name: albanska");
+ }
+}
diff --git a/samples/snippets/src/test/java/com/example/translate/GetSupportedLanguagesTests.java b/samples/snippets/src/test/java/com/example/translate/GetSupportedLanguagesTests.java
new file mode 100644
index 00000000..da63a936
--- /dev/null
+++ b/samples/snippets/src/test/java/com/example/translate/GetSupportedLanguagesTests.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2020 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.translate;
+
+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 Get Supported Languages sample. */
+@RunWith(JUnit4.class)
+@SuppressWarnings("checkstyle:abbreviationaswordinname")
+public class GetSupportedLanguagesTests {
+ private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
+
+ private ByteArrayOutputStream bout;
+ private PrintStream out;
+
+ private static void requireEnvVar(String varName) {
+ assertNotNull(
+ "Environment variable '%s' is required to perform these tests.".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);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() {
+ System.setOut(null);
+ }
+
+ @Test
+ public void testGetSupportedLanguages() throws IOException {
+ GetSupportedLanguages.getSupportedLanguages(PROJECT_ID);
+ String got = bout.toString();
+ assertThat(got).contains("zh-CN");
+ }
+}
diff --git a/samples/snippets/src/test/java/com/example/translate/ListGlossariesTests.java b/samples/snippets/src/test/java/com/example/translate/ListGlossariesTests.java
new file mode 100644
index 00000000..55776b32
--- /dev/null
+++ b/samples/snippets/src/test/java/com/example/translate/ListGlossariesTests.java
@@ -0,0 +1,91 @@
+/*
+ * Copyright 2020 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.translate;
+
+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 java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.ExecutionException;
+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 List Glossaries sample. */
+@RunWith(JUnit4.class)
+@SuppressWarnings("checkstyle:abbreviationaswordinname")
+public class ListGlossariesTests {
+
+ private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
+ private static final String GLOSSARY_INPUT_URI =
+ "gs://cloud-samples-data/translation/glossary_ja.csv";
+ private static final String GLOSSARY_ID =
+ String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
+
+ private ByteArrayOutputStream bout;
+ private PrintStream out;
+
+ private static void requireEnvVar(String varName) {
+ assertNotNull(
+ "Environment variable '%s' is required to perform these tests.".format(varName),
+ System.getenv(varName));
+ }
+
+ @BeforeClass
+ public static void checkRequirements() {
+ requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
+ requireEnvVar("GOOGLE_CLOUD_PROJECT");
+ }
+
+ @Before
+ public void setUp() throws InterruptedException, ExecutionException, IOException {
+ // Create a glossary that can be used in the test
+ PrintStream temp = new PrintStream(new ByteArrayOutputStream());
+ System.setOut(temp);
+ List languageCodes = new ArrayList<>();
+ languageCodes.add("en");
+ languageCodes.add("ja");
+ CreateGlossary.createGlossary(PROJECT_ID, GLOSSARY_ID, languageCodes, GLOSSARY_INPUT_URI);
+
+ bout = new ByteArrayOutputStream();
+ out = new PrintStream(bout);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() throws InterruptedException, ExecutionException, IOException {
+ // Delete the created glossary
+ DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
+ System.setOut(null);
+ }
+
+ @Test
+ public void testListGlossaries() throws IOException {
+ ListGlossaries.listGlossaries(PROJECT_ID);
+ String got = bout.toString();
+ assertThat(got).contains(GLOSSARY_ID);
+ assertThat(got).contains(GLOSSARY_INPUT_URI);
+ }
+}
diff --git a/samples/snippets/src/test/java/com/example/translate/QuickstartSampleIT.java b/samples/snippets/src/test/java/com/example/translate/QuickstartSampleIT.java
new file mode 100644
index 00000000..a9a5fb8a
--- /dev/null
+++ b/samples/snippets/src/test/java/com/example/translate/QuickstartSampleIT.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * 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.translate;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Tests for quickstart sample. */
+@RunWith(JUnit4.class)
+@SuppressWarnings("checkstyle:abbreviationaswordinname")
+public class QuickstartSampleIT {
+ private ByteArrayOutputStream bout;
+ private PrintStream out;
+
+ @Before
+ public void setUp() {
+ bout = new ByteArrayOutputStream();
+ out = new PrintStream(bout);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() {
+ System.setOut(null);
+ }
+
+ @Test
+ public void testQuickstart() throws Exception {
+ // Act
+ QuickstartSample.main();
+
+ // Assert
+ String got = bout.toString();
+ assertThat(got).contains("Text: Hello, world!");
+ assertThat(got).contains("Translation: ");
+ }
+}
diff --git a/samples/snippets/src/test/java/com/example/translate/TranslateTextTests.java b/samples/snippets/src/test/java/com/example/translate/TranslateTextTests.java
new file mode 100644
index 00000000..0c6ddbd8
--- /dev/null
+++ b/samples/snippets/src/test/java/com/example/translate/TranslateTextTests.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2020 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.translate;
+
+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 Text sample. */
+@RunWith(JUnit4.class)
+@SuppressWarnings("checkstyle:abbreviationaswordinname")
+public class TranslateTextTests {
+ private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
+
+ private ByteArrayOutputStream bout;
+ private PrintStream out;
+
+ private static void requireEnvVar(String varName) {
+ assertNotNull(
+ "Environment variable '%s' is required to perform these tests.".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);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() {
+ System.setOut(null);
+ }
+
+ @Test
+ public void testTranslateText() throws IOException {
+ TranslateText.translateText(PROJECT_ID, "es", "Hello world");
+ String got = bout.toString();
+ assertThat(got).contains("Hola Mundo");
+ }
+}
diff --git a/samples/snippets/src/test/java/com/example/translate/TranslateTextWithGlossaryAndModelTests.java b/samples/snippets/src/test/java/com/example/translate/TranslateTextWithGlossaryAndModelTests.java
new file mode 100644
index 00000000..80d269fc
--- /dev/null
+++ b/samples/snippets/src/test/java/com/example/translate/TranslateTextWithGlossaryAndModelTests.java
@@ -0,0 +1,93 @@
+/*
+ * Copyright 2020 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.translate;
+
+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 java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.ExecutionException;
+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 Text With Glossary and Model sample. */
+@RunWith(JUnit4.class)
+@SuppressWarnings("checkstyle:abbreviationaswordinname")
+public class TranslateTextWithGlossaryAndModelTests {
+ private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
+ private static final String MODEL_ID = "TRL2188848820815848149";
+ private static final String GLOSSARY_INPUT_URI =
+ "gs://cloud-samples-data/translation/glossary_ja.csv";
+ private static final String GLOSSARY_ID =
+ String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
+
+ private ByteArrayOutputStream bout;
+ private PrintStream out;
+
+ private static void requireEnvVar(String varName) {
+ assertNotNull(
+ "Environment variable '%s' is required to perform these tests.".format(varName),
+ System.getenv(varName));
+ }
+
+ @BeforeClass
+ public static void checkRequirements() {
+ requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
+ requireEnvVar("GOOGLE_CLOUD_PROJECT");
+ }
+
+ @Before
+ public void setUp() throws InterruptedException, ExecutionException, IOException {
+ // Create a glossary that can be used in the test
+ PrintStream temp = new PrintStream(new ByteArrayOutputStream());
+ System.setOut(temp);
+ List languageCodes = new ArrayList<>();
+ languageCodes.add("en");
+ languageCodes.add("ja");
+ CreateGlossary.createGlossary(PROJECT_ID, GLOSSARY_ID, languageCodes, GLOSSARY_INPUT_URI);
+
+ bout = new ByteArrayOutputStream();
+ out = new PrintStream(bout);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() throws InterruptedException, ExecutionException, IOException {
+ // Clean up
+ // Delete the created glossary
+ DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
+ System.setOut(null);
+ }
+
+ @Test
+ public void testTranslateTextWithGlossaryAndModel() throws IOException {
+ TranslateTextWithGlossaryAndModel.translateTextWithGlossaryAndModel(
+ PROJECT_ID, "en", "ja", "That' il do it. deception", GLOSSARY_ID, MODEL_ID);
+ String got = bout.toString();
+ assertThat(got).contains("それはそうだ"); // custom model
+ assertThat(got).contains("欺く"); // glossary
+ }
+}
diff --git a/samples/snippets/src/test/java/com/example/translate/TranslateTextWithGlossaryTests.java b/samples/snippets/src/test/java/com/example/translate/TranslateTextWithGlossaryTests.java
new file mode 100644
index 00000000..3b2be959
--- /dev/null
+++ b/samples/snippets/src/test/java/com/example/translate/TranslateTextWithGlossaryTests.java
@@ -0,0 +1,91 @@
+/*
+ * Copyright 2020 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.translate;
+
+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 java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.ExecutionException;
+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 Text With Glossary sample. */
+@RunWith(JUnit4.class)
+@SuppressWarnings("checkstyle:abbreviationaswordinname")
+public class TranslateTextWithGlossaryTests {
+ private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
+ private static final String GLOSSARY_INPUT_URI =
+ "gs://cloud-samples-data/translation/glossary_ja.csv";
+ private static final String GLOSSARY_ID =
+ String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
+
+ private ByteArrayOutputStream bout;
+ private PrintStream out;
+
+ private static void requireEnvVar(String varName) {
+ assertNotNull(
+ "Environment variable '%s' is required to perform these tests.".format(varName),
+ System.getenv(varName));
+ }
+
+ @BeforeClass
+ public static void checkRequirements() {
+ requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
+ requireEnvVar("GOOGLE_CLOUD_PROJECT");
+ }
+
+ @Before
+ public void setUp() throws InterruptedException, ExecutionException, IOException {
+ // Create a glossary that can be used in the test
+ PrintStream temp = new PrintStream(new ByteArrayOutputStream());
+ System.setOut(temp);
+ List languageCodes = new ArrayList<>();
+ languageCodes.add("en");
+ languageCodes.add("ja");
+ CreateGlossary.createGlossary(PROJECT_ID, GLOSSARY_ID, languageCodes, GLOSSARY_INPUT_URI);
+
+ bout = new ByteArrayOutputStream();
+ out = new PrintStream(bout);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() throws InterruptedException, ExecutionException, IOException {
+ // Clean up
+ // Delete the created glossary
+ DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
+ System.setOut(null);
+ }
+
+ @Test
+ public void testTranslateTextWithGlossary() throws IOException {
+ TranslateTextWithGlossary.translateTextWithGlossary(
+ PROJECT_ID, "en", "ja", "account", GLOSSARY_ID);
+ String got = bout.toString();
+ assertThat(got).contains("アカウント");
+ }
+}
diff --git a/samples/snippets/src/test/java/com/example/translate/TranslateTextWithModelTests.java b/samples/snippets/src/test/java/com/example/translate/TranslateTextWithModelTests.java
new file mode 100644
index 00000000..0864cefc
--- /dev/null
+++ b/samples/snippets/src/test/java/com/example/translate/TranslateTextWithModelTests.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2020 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.translate;
+
+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 Batch Translate Text With Model sample. */
+@RunWith(JUnit4.class)
+@SuppressWarnings("checkstyle:abbreviationaswordinname")
+public class TranslateTextWithModelTests {
+ private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
+ private static final String MODEL_ID = "TRL2188848820815848149";
+
+ private ByteArrayOutputStream bout;
+ private PrintStream out;
+
+ private static void requireEnvVar(String varName) {
+ assertNotNull(
+ "Environment variable '%s' is required to perform these tests.".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);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() {
+ System.setOut(null);
+ }
+
+ @Test
+ public void testTranslateTextWithModel() throws IOException {
+ TranslateTextWithModel.translateTextWithModel(
+ PROJECT_ID, "en", "ja", "That' il do it. deception", MODEL_ID);
+ String got = bout.toString();
+ assertThat(got).contains("それはそうだ");
+ }
+}
diff --git a/synth.metadata b/synth.metadata
index 64a22af9..22739d43 100644
--- a/synth.metadata
+++ b/synth.metadata
@@ -4,7 +4,7 @@
"git": {
"name": ".",
"remote": "https://github.com/googleapis/java-translate.git",
- "sha": "f59565262faf8893962d6bb673d1dc5ece87f15a"
+ "sha": "81170cddef7b6ad31bf6ae743ffcd464b2941254"
}
},
{
@@ -27,7 +27,7 @@
"git": {
"name": "synthtool",
"remote": "https://github.com/googleapis/synthtool.git",
- "sha": "f8823dec98277a9516f2fb6fae9f58b3a59a23e1"
+ "sha": "9602086c6c5b05db77950c7f7495a2a3868f3537"
}
}
],
@@ -60,6 +60,7 @@
".github/release-please.yml",
".github/trusted-contribution.yml",
".github/workflows/ci.yaml",
+ ".github/workflows/samples.yaml",
".kokoro/build.bat",
".kokoro/build.sh",
".kokoro/coerce_logs.sh",
diff --git a/versions.txt b/versions.txt
index db43a5d5..5db64842 100644
--- a/versions.txt
+++ b/versions.txt
@@ -1,8 +1,8 @@
# Format:
# module:released-version:current-version
-proto-google-cloud-translate-v3beta1:0.78.1:0.78.1
-proto-google-cloud-translate-v3:1.1.1:1.1.1
-grpc-google-cloud-translate-v3beta1:0.78.1:0.78.1
-grpc-google-cloud-translate-v3:1.1.1:1.1.1
-google-cloud-translate:1.95.1:1.95.1
\ No newline at end of file
+proto-google-cloud-translate-v3beta1:0.78.2:0.78.2
+proto-google-cloud-translate-v3:1.1.2:1.1.2
+grpc-google-cloud-translate-v3beta1:0.78.2:0.78.2
+grpc-google-cloud-translate-v3:1.1.2:1.1.2
+google-cloud-translate:1.95.2:1.95.2
\ No newline at end of file