Skip to content

Commit

Permalink
samples: translate: add v3 language samples (#1937)
Browse files Browse the repository at this point in the history
Co-authored-by: Les Vogel <lesv@users.noreply.github.com>
  • Loading branch information
2 people authored and Shabirmean committed Nov 17, 2022
1 parent a4b66d5 commit 8cfdc9b
Show file tree
Hide file tree
Showing 6 changed files with 410 additions and 0 deletions.
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]
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]
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]
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");
}
}
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");
}
}
Loading

0 comments on commit 8cfdc9b

Please sign in to comment.