Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion translate/cloud-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ See https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/The-Google
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>3.0.0</version>
<version>3.3.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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<String> 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 Glossary
public static void createGlossary(
String projectId, String glossaryId, List<String> 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();

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();

OperationFuture<Glossary, CreateGlossaryMetadata> 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]
Original file line number Diff line number Diff line change
@@ -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 Glossary */
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();

OperationFuture<DeleteGlossaryResponse, DeleteGlossaryMetadata> 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]
Original file line number Diff line number Diff line change
@@ -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_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 Glossary
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]
Original file line number Diff line number Diff line change
@@ -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_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 Glossaries
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]
Original file line number Diff line number Diff line change
@@ -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;

@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<String> 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);
}
}
Loading