Skip to content

Commit 383ac01

Browse files
nnegreylesv
authored andcommitted
samples: translate: add v3 language samples (#1937)
Co-authored-by: Les Vogel <[email protected]>
1 parent 747bbfb commit 383ac01

File tree

6 files changed

+410
-0
lines changed

6 files changed

+410
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.translate;
18+
19+
// [START translate_v3_detect_language]
20+
import com.google.cloud.translate.v3.DetectLanguageRequest;
21+
import com.google.cloud.translate.v3.DetectLanguageResponse;
22+
import com.google.cloud.translate.v3.DetectedLanguage;
23+
import com.google.cloud.translate.v3.LocationName;
24+
import com.google.cloud.translate.v3.TranslationServiceClient;
25+
26+
import java.io.IOException;
27+
28+
public class DetectLanguage {
29+
30+
public static void detectLanguage() throws IOException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String projectId = "YOUR-PROJECT-ID";
33+
String text = "your-text";
34+
35+
detectLanguage(projectId, text);
36+
}
37+
38+
// Detecting the language of a text string
39+
public static void detectLanguage(String projectId, String text) throws IOException {
40+
41+
// Initialize client that will be used to send requests. This client only needs to be created
42+
// once, and can be reused for multiple requests. After completing all of your requests, call
43+
// the "close" method on the client to safely clean up any remaining background resources.
44+
try (TranslationServiceClient client = TranslationServiceClient.create()) {
45+
// Supported Locations: `global`, [glossary location], or [model location]
46+
// Glossaries must be hosted in `us-central1`
47+
// Custom Models must use the same location as your model. (us-central1)
48+
LocationName parent = LocationName.of(projectId, "global");
49+
50+
// Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
51+
DetectLanguageRequest request =
52+
DetectLanguageRequest.newBuilder()
53+
.setParent(parent.toString())
54+
.setMimeType("text/plain")
55+
.setContent(text)
56+
.build();
57+
58+
DetectLanguageResponse response = client.detectLanguage(request);
59+
60+
// Display list of detected languages sorted by detection confidence.
61+
// The most probable language is first.
62+
for (DetectedLanguage language : response.getLanguagesList()) {
63+
// The language detected
64+
System.out.printf("Language code: %s\n", language.getLanguageCode());
65+
// Confidence of detection result for this language
66+
System.out.printf("Confidence: %s\n", language.getConfidence());
67+
}
68+
}
69+
}
70+
}
71+
// [END translate_v3_detect_language]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.translate;
18+
19+
// [START translate_v3_get_supported_languages]
20+
import com.google.cloud.translate.v3.GetSupportedLanguagesRequest;
21+
import com.google.cloud.translate.v3.LocationName;
22+
import com.google.cloud.translate.v3.SupportedLanguage;
23+
import com.google.cloud.translate.v3.SupportedLanguages;
24+
import com.google.cloud.translate.v3.TranslationServiceClient;
25+
26+
import java.io.IOException;
27+
28+
public class GetSupportedLanguages {
29+
30+
public static void getSupportedLanguages() throws IOException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String projectId = "YOUR-PROJECT-ID";
33+
getSupportedLanguages(projectId);
34+
}
35+
36+
// Getting a list of supported language codes
37+
public static void getSupportedLanguages(String projectId) throws IOException {
38+
39+
// Initialize client that will be used to send requests. This client only needs to be created
40+
// once, and can be reused for multiple requests. After completing all of your requests, call
41+
// the "close" method on the client to safely clean up any remaining background resources.
42+
try (TranslationServiceClient client = TranslationServiceClient.create()) {
43+
// Supported Locations: `global`, [glossary location], or [model location]
44+
// Glossaries must be hosted in `us-central1`
45+
// Custom Models must use the same location as your model. (us-central1)
46+
LocationName parent = LocationName.of(projectId, "global");
47+
GetSupportedLanguagesRequest request =
48+
GetSupportedLanguagesRequest.newBuilder().setParent(parent.toString()).build();
49+
50+
SupportedLanguages response = client.getSupportedLanguages(request);
51+
52+
// List language codes of supported languages
53+
for (SupportedLanguage language : response.getLanguagesList()) {
54+
System.out.printf("Language Code: %s\n", language.getLanguageCode());
55+
}
56+
}
57+
}
58+
}
59+
// [END translate_v3_get_supported_languages]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.translate;
18+
19+
// [START translate_v3_get_supported_languages_for_target]
20+
import com.google.cloud.translate.v3.GetSupportedLanguagesRequest;
21+
import com.google.cloud.translate.v3.LocationName;
22+
import com.google.cloud.translate.v3.SupportedLanguage;
23+
import com.google.cloud.translate.v3.SupportedLanguages;
24+
import com.google.cloud.translate.v3.TranslationServiceClient;
25+
26+
import java.io.IOException;
27+
28+
public class GetSupportedLanguagesForTarget {
29+
30+
public static void getSupportedLanguagesForTarget() throws IOException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String projectId = "YOUR-PROJECT-ID";
33+
// Supported Languages: https://cloud.google.com/translate/docs/languages
34+
String languageCode = "your-language-code";
35+
getSupportedLanguagesForTarget(projectId, languageCode);
36+
}
37+
38+
// Listing supported languages with target language name
39+
public static void getSupportedLanguagesForTarget(String projectId, String languageCode)
40+
throws IOException {
41+
42+
// Initialize client that will be used to send requests. This client only needs to be created
43+
// once, and can be reused for multiple requests. After completing all of your requests, call
44+
// the "close" method on the client to safely clean up any remaining background resources.
45+
try (TranslationServiceClient client = TranslationServiceClient.create()) {
46+
// Supported Locations: `global`, [glossary location], or [model location]
47+
// Glossaries must be hosted in `us-central1`
48+
// Custom Models must use the same location as your model. (us-central1)
49+
LocationName parent = LocationName.of(projectId, "global");
50+
GetSupportedLanguagesRequest request =
51+
GetSupportedLanguagesRequest.newBuilder()
52+
.setParent(parent.toString())
53+
.setDisplayLanguageCode(languageCode)
54+
.build();
55+
56+
SupportedLanguages response = client.getSupportedLanguages(request);
57+
58+
// List language codes of supported languages
59+
for (SupportedLanguage language : response.getLanguagesList()) {
60+
System.out.printf("Language Code: %s\n", language.getLanguageCode());
61+
System.out.printf("Display Name: %s\n", language.getDisplayName());
62+
}
63+
}
64+
}
65+
}
66+
// [END translate_v3_get_supported_languages_for_target]
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.translate;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.IOException;
24+
import java.io.PrintStream;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
import org.junit.runners.JUnit4;
31+
32+
/** Tests for Detect Languages sample. */
33+
@RunWith(JUnit4.class)
34+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
35+
public class DetectLanguageTests {
36+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
37+
38+
private ByteArrayOutputStream bout;
39+
private PrintStream out;
40+
41+
private static void requireEnvVar(String varName) {
42+
assertNotNull(
43+
"Environment variable '%s' is required to perform these tests.".format(varName),
44+
System.getenv(varName));
45+
}
46+
47+
@BeforeClass
48+
public static void checkRequirements() {
49+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
50+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
51+
}
52+
53+
@Before
54+
public void setUp() {
55+
bout = new ByteArrayOutputStream();
56+
out = new PrintStream(bout);
57+
System.setOut(out);
58+
}
59+
60+
@After
61+
public void tearDown() {
62+
System.setOut(null);
63+
}
64+
65+
@Test
66+
public void testDetectLanguage() throws IOException {
67+
DetectLanguage.detectLanguage(PROJECT_ID, "Hæ sæta");
68+
String got = bout.toString();
69+
assertThat(got).contains("is");
70+
}
71+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.translate;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.IOException;
24+
import java.io.PrintStream;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
import org.junit.runners.JUnit4;
31+
32+
/** Tests for Get Supported Languages For Target sample. */
33+
@RunWith(JUnit4.class)
34+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
35+
public class GetSupportedLanguagesForTargetTests {
36+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
37+
38+
private ByteArrayOutputStream bout;
39+
private PrintStream out;
40+
41+
private static void requireEnvVar(String varName) {
42+
assertNotNull(
43+
"Environment variable '%s' is required to perform these tests.".format(varName),
44+
System.getenv(varName));
45+
}
46+
47+
@BeforeClass
48+
public static void checkRequirements() {
49+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
50+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
51+
}
52+
53+
@Before
54+
public void setUp() {
55+
bout = new ByteArrayOutputStream();
56+
out = new PrintStream(bout);
57+
System.setOut(out);
58+
}
59+
60+
@After
61+
public void tearDown() {
62+
System.setOut(null);
63+
}
64+
65+
@Test
66+
public void testGetSupportedLanguages() throws IOException {
67+
GetSupportedLanguagesForTarget.getSupportedLanguagesForTarget(PROJECT_ID, "is");
68+
String got = bout.toString();
69+
assertThat(got).contains("Language Code: sq");
70+
assertThat(got).contains("Display Name: albanska");
71+
}
72+
}

0 commit comments

Comments
 (0)