From a4ef5e58940dd99c358c100aee716164bba74950 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Thu, 9 Jan 2020 11:09:45 -0700 Subject: [PATCH] translate: add v3 language samples --- .../com/example/translate/DetectLanguage.java | 71 ++++++++++++++++++ .../translate/GetSupportedLanguages.java | 59 +++++++++++++++ .../GetSupportedLanguagesForTarget.java | 66 +++++++++++++++++ .../translate/DetectLanguageTests.java | 71 ++++++++++++++++++ .../GetSupportedLanguagesForTargetTests.java | 72 +++++++++++++++++++ .../translate/GetSupportedLanguagesTests.java | 71 ++++++++++++++++++ 6 files changed, 410 insertions(+) create mode 100644 translate/cloud-client/src/main/java/com/example/translate/DetectLanguage.java create mode 100644 translate/cloud-client/src/main/java/com/example/translate/GetSupportedLanguages.java create mode 100644 translate/cloud-client/src/main/java/com/example/translate/GetSupportedLanguagesForTarget.java create mode 100644 translate/cloud-client/src/test/java/com/example/translate/DetectLanguageTests.java create mode 100644 translate/cloud-client/src/test/java/com/example/translate/GetSupportedLanguagesForTargetTests.java create mode 100644 translate/cloud-client/src/test/java/com/example/translate/GetSupportedLanguagesTests.java diff --git a/translate/cloud-client/src/main/java/com/example/translate/DetectLanguage.java b/translate/cloud-client/src/main/java/com/example/translate/DetectLanguage.java new file mode 100644 index 00000000000..945dad8f07e --- /dev/null +++ b/translate/cloud-client/src/main/java/com/example/translate/DetectLanguage.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; + +// [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/translate/cloud-client/src/main/java/com/example/translate/GetSupportedLanguages.java b/translate/cloud-client/src/main/java/com/example/translate/GetSupportedLanguages.java new file mode 100644 index 00000000000..32963c406bf --- /dev/null +++ b/translate/cloud-client/src/main/java/com/example/translate/GetSupportedLanguages.java @@ -0,0 +1,59 @@ +/* + * 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/translate/cloud-client/src/main/java/com/example/translate/GetSupportedLanguagesForTarget.java b/translate/cloud-client/src/main/java/com/example/translate/GetSupportedLanguagesForTarget.java new file mode 100644 index 00000000000..1001ae7a551 --- /dev/null +++ b/translate/cloud-client/src/main/java/com/example/translate/GetSupportedLanguagesForTarget.java @@ -0,0 +1,66 @@ +/* + * 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/translate/cloud-client/src/test/java/com/example/translate/DetectLanguageTests.java b/translate/cloud-client/src/test/java/com/example/translate/DetectLanguageTests.java new file mode 100644 index 00000000000..c93b7a11f57 --- /dev/null +++ b/translate/cloud-client/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/translate/cloud-client/src/test/java/com/example/translate/GetSupportedLanguagesForTargetTests.java b/translate/cloud-client/src/test/java/com/example/translate/GetSupportedLanguagesForTargetTests.java new file mode 100644 index 00000000000..1381c0af96c --- /dev/null +++ b/translate/cloud-client/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/translate/cloud-client/src/test/java/com/example/translate/GetSupportedLanguagesTests.java b/translate/cloud-client/src/test/java/com/example/translate/GetSupportedLanguagesTests.java new file mode 100644 index 00000000000..da63a936967 --- /dev/null +++ b/translate/cloud-client/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"); + } +}