检索指定的术语表。
包含此代码示例的文档页面
如需查看上下文中使用的代码示例,请参阅以下文档:
代码示例
C#
在试用此示例之前,请按照《Translation 快速入门:使用客户端库》中的 C# 设置说明进行操作。如需了解详情,请参阅 Translation C# API 参考文档。
using Google.Cloud.Translate.V3;
using System;
namespace GoogleCloudSamples
{
public static class GetGlossary
{
/// <summary>
/// Retrieves a glossary.
/// </summary>
/// <param name="projectId">Your Google Cloud Project ID.</param>
/// <param name="glossaryId">Glossary ID.</param>
public static void GetGlossarySample(string projectId = "[Google Cloud Project ID]",
string glossaryId = "[YOUR_GLOSSARY_ID]")
{
TranslationServiceClient translationServiceClient = TranslationServiceClient.Create();
GetGlossaryRequest request = new GetGlossaryRequest
{
GlossaryName = new GlossaryName(projectId, "us-central1", glossaryId),
};
Glossary response = translationServiceClient.GetGlossary(request);
Console.WriteLine($"Glossary name: {response.Name}");
Console.WriteLine($"Entry count: {response.EntryCount}");
Console.WriteLine($"Input URI: {response.InputConfig.GcsSource.InputUri}");
}
}
Go
在试用此示例之前,请按照《Translation 快速入门:使用客户端库》中的 Go 设置说明进行操作。如需了解详情,请参阅 Translation Go API 参考文档。
import (
"context"
"fmt"
"io"
translate "cloud.google.com/go/translate/apiv3"
translatepb "google.golang.org/genproto/googleapis/cloud/translate/v3"
)
// getGlossary gets the specified glossary.
func getGlossary(w io.Writer, projectID string, location string, glossaryID string) error {
// projectID := "my-project-id"
// location := "us-central1"
// glossaryID := "glossary-id"
ctx := context.Background()
client, err := translate.NewTranslationClient(ctx)
if err != nil {
return fmt.Errorf("NewTranslationClient: %v", err)
}
defer client.Close()
req := &translatepb.GetGlossaryRequest{
Name: fmt.Sprintf("projects/%s/locations/%s/glossaries/%s", projectID, location, glossaryID),
}
resp, err := client.GetGlossary(ctx, req)
if err != nil {
return fmt.Errorf("GetGlossary: %v", err)
}
fmt.Fprintf(w, "Glossary name: %v\n", resp.GetName())
fmt.Fprintf(w, "Entry count: %v\n", resp.GetEntryCount())
fmt.Fprintf(w, "Input URI: %v\n", resp.GetInputConfig().GetGcsSource().GetInputUri())
return nil
}
Java
在试用此示例之前,请按照《Translation 快速入门:使用客户端库》中的 Java 设置说明进行操作。如需了解详情,请参阅 Translation Java API 参考文档。
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 a particular glossary based on the glossary ID
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());
}
}
}
Node.js
在试用此示例之前,请按照《Translation 快速入门:使用客户端库》中的 Node.js 设置说明进行操作。如需了解详情,请参阅 Translation Node.js API 参考文档。
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'global';
// const glossaryId = 'YOUR_GLOSSARY_ID';
// Imports the Google Cloud Translation library
const {TranslationServiceClient} = require('@google-cloud/translate');
// Instantiates a client
const translationClient = new TranslationServiceClient();
async function getGlossary() {
// Construct request
const request = {
parent: `projects/${projectId}/locations/${location}`,
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
};
try {
// Get glossary
const [response] = await translationClient.getGlossary(request);
console.log(`Got glossary: ${response.name}`);
} catch (error) {
console.error(error.details);
}
}
getGlossary();
PHP
在试用此示例之前,请按照《Translation 快速入门:使用客户端库》中的 PHP 设置说明进行操作。如需了解详情,请参阅 Translation PHP API 参考文档。
use Google\Cloud\Translate\V3\TranslationServiceClient;
$translationServiceClient = new TranslationServiceClient();
/** Uncomment and populate these variables in your code */
// $projectId = '[Google Cloud Project ID]';
// $glossaryId = '[Glossary ID]';
$formattedName = $translationServiceClient->glossaryName(
$projectId,
'us-central1',
$glossaryId
);
try {
$response = $translationServiceClient->getGlossary($formattedName);
printf('Glossary name: %s' . PHP_EOL, $response->getName());
printf('Entry count: %s' . PHP_EOL, $response->getEntryCount());
printf(
'Input URI: %s' . PHP_EOL,
$response->getInputConfig()
->getGcsSource()
->getInputUri()
);
} finally {
$translationServiceClient->close();
}
Python
在试用此示例之前,请按照《Translation 快速入门:使用客户端库》中的 Python 设置说明进行操作。如需了解详情,请参阅 Translation Python API 参考文档。
from google.cloud import translate_v3 as translate
def get_glossary(project_id="YOUR_PROJECT_ID", glossary_id="YOUR_GLOSSARY_ID"):
"""Get a particular glossary based on the glossary ID."""
client = translate.TranslationServiceClient()
name = client.glossary_path(project_id, "us-central1", glossary_id)
response = client.get_glossary(name=name)
print(u"Glossary name: {}".format(response.name))
print(u"Entry count: {}".format(response.entry_count))
print(u"Input URI: {}".format(response.input_config.gcs_source.input_uri))
Ruby
在试用此示例之前,请按照《Translation 快速入门:使用客户端库》中的 Ruby 设置说明进行操作。如需了解详情,请参阅 Translation Ruby API 参考文档。
require "google/cloud/translate"
# project_id = "[Google Cloud Project ID]"
# location_id = "[LOCATION ID]"
# glossary_id = "[YOUR_GLOSSARY_ID]"
client = Google::Cloud::Translate.translation_service
name = client.glossary_path project: project_id,
location: location_id,
glossary: glossary_id
response = client.get_glossary name: name
puts "Glossary name: #{response.name}"
puts "Entry count: #{response.entry_count}"
puts "Input URI: #{response.input_config.gcs_source.input_uri}"