-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: translate: add v3 language samples (#1937)
Co-authored-by: Les Vogel <[email protected]>
- Loading branch information
1 parent
76262f6
commit 401adbb
Showing
6 changed files
with
410 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
translate/snippets/src/main/java/com/example/translate/DetectLanguage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
59 changes: 59 additions & 0 deletions
59
translate/snippets/src/main/java/com/example/translate/GetSupportedLanguages.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
66 changes: 66 additions & 0 deletions
66
translate/snippets/src/main/java/com/example/translate/GetSupportedLanguagesForTarget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
71 changes: 71 additions & 0 deletions
71
translate/snippets/src/test/java/com/example/translate/DetectLanguageTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...ate/snippets/src/test/java/com/example/translate/GetSupportedLanguagesForTargetTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} |
Oops, something went wrong.