- 2.73.0 (latest)
- 2.72.0
- 2.70.0
- 2.68.0
- 2.67.0
- 2.64.0
- 2.63.0
- 2.62.0
- 2.60.0
- 2.59.0
- 2.58.0
- 2.57.0
- 2.56.0
- 2.55.0
- 2.54.0
- 2.53.0
- 2.52.0
- 2.51.0
- 2.49.0
- 2.48.0
- 2.47.0
- 2.46.0
- 2.45.0
- 2.44.0
- 2.43.0
- 2.42.0
- 2.41.0
- 2.40.0
- 2.39.0
- 2.37.0
- 2.36.0
- 2.35.0
- 2.34.0
- 2.33.0
- 2.32.0
- 2.31.0
- 2.30.0
- 2.29.0
- 2.28.0
- 2.27.0
- 2.24.0
- 2.23.0
- 2.22.0
- 2.21.0
- 2.20.0
- 2.19.0
- 2.18.0
- 2.17.0
- 2.16.0
- 2.15.0
- 2.14.0
- 2.13.0
- 2.12.0
- 2.11.0
- 2.9.0
- 2.8.0
- 2.7.0
- 2.6.0
- 2.5.0
- 2.4.0
- 2.3.5
- 2.2.0
- 2.1.13
public class TranslationServiceClient implements BackgroundResource
Service Description: Provides natural language translation operations.
This class provides the ability to make remote calls to the backing service through method calls that map to API methods. Sample code to get started:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
TranslateTextRequest request =
TranslateTextRequest.newBuilder()
.addAllContents(new ArrayList<String>())
.setMimeType("mimeType-1392120434")
.setSourceLanguageCode("sourceLanguageCode1645917472")
.setTargetLanguageCode("targetLanguageCode-106414698")
.setParent(LocationName.of("[PROJECT]", "[LOCATION]").toString())
.setModel("model104069929")
.setGlossaryConfig(TranslateTextGlossaryConfig.newBuilder().build())
.putAllLabels(new HashMap<String, String>())
.build();
TranslateTextResponse response = translationServiceClient.translateText(request);
}
Note: close() needs to be called on the TranslationServiceClient object to clean up resources such as threads. In the example above, try-with-resources is used, which automatically calls close().
The surface of this class includes several types of Java methods for each of the API's methods:
- A "flattened" method. With this type of method, the fields of the request type have been converted into function parameters. It may be the case that not all fields are available as parameters, and not every API method will have a flattened method entry point.
- A "request object" method. This type of method only takes one parameter, a request object, which must be constructed before the call. Not every API method will have a request object method.
- A "callable" method. This type of method takes no parameters and returns an immutable API callable object, which can be used to initiate calls to the service.
See the individual methods for example code.
Many parameters require resource names to be formatted in a particular way. To assist with these names, this class includes a format method for each type of name, and additionally a parse method to extract the individual identifiers contained within names that are returned.
This class can be customized by passing in a custom instance of TranslationServiceSettings to create(). For example:
To customize credentials:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
TranslationServiceSettings translationServiceSettings =
TranslationServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
.build();
TranslationServiceClient translationServiceClient =
TranslationServiceClient.create(translationServiceSettings);
To customize the endpoint:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
TranslationServiceSettings translationServiceSettings =
TranslationServiceSettings.newBuilder().setEndpoint(myEndpoint).build();
TranslationServiceClient translationServiceClient =
TranslationServiceClient.create(translationServiceSettings);
To use REST (HTTP1.1/JSON) transport (instead of gRPC) for sending and receiving requests over the wire:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
TranslationServiceSettings translationServiceSettings =
TranslationServiceSettings.newHttpJsonBuilder().build();
TranslationServiceClient translationServiceClient =
TranslationServiceClient.create(translationServiceSettings);
Please refer to the GitHub repository's samples for more quickstart code snippets.
Implements
BackgroundResourceStatic Methods
create()
public static final TranslationServiceClient create()
Constructs an instance of TranslationServiceClient with default settings.
Returns | |
---|---|
Type | Description |
TranslationServiceClient |
Exceptions | |
---|---|
Type | Description |
IOException |
create(TranslationServiceSettings settings)
public static final TranslationServiceClient create(TranslationServiceSettings settings)
Constructs an instance of TranslationServiceClient, using the given settings. The channels are created based on the settings passed in, or defaults for any settings that are not set.
Parameter | |
---|---|
Name | Description |
settings |
TranslationServiceSettings |
Returns | |
---|---|
Type | Description |
TranslationServiceClient |
Exceptions | |
---|---|
Type | Description |
IOException |
create(TranslationServiceStub stub)
public static final TranslationServiceClient create(TranslationServiceStub stub)
Constructs an instance of TranslationServiceClient, using the given stub for making calls. This is for advanced usage - prefer using create(TranslationServiceSettings).
Parameter | |
---|---|
Name | Description |
stub |
TranslationServiceStub |
Returns | |
---|---|
Type | Description |
TranslationServiceClient |
Constructors
TranslationServiceClient(TranslationServiceSettings settings)
protected TranslationServiceClient(TranslationServiceSettings settings)
Constructs an instance of TranslationServiceClient, using the given settings. This is protected so that it is easy to make a subclass, but otherwise, the static factory methods should be preferred.
Parameter | |
---|---|
Name | Description |
settings |
TranslationServiceSettings |
TranslationServiceClient(TranslationServiceStub stub)
protected TranslationServiceClient(TranslationServiceStub stub)
Parameter | |
---|---|
Name | Description |
stub |
TranslationServiceStub |
Methods
awaitTermination(long duration, TimeUnit unit)
public boolean awaitTermination(long duration, TimeUnit unit)
Parameters | |
---|---|
Name | Description |
duration |
long |
unit |
TimeUnit |
Returns | |
---|---|
Type | Description |
boolean |
Exceptions | |
---|---|
Type | Description |
InterruptedException |
batchTranslateDocumentAsync(BatchTranslateDocumentRequest request)
public final OperationFuture<BatchTranslateDocumentResponse,BatchTranslateDocumentMetadata> batchTranslateDocumentAsync(BatchTranslateDocumentRequest request)
Translates a large volume of documents in asynchronous batch mode. This function provides real-time output as the inputs are being processed. If caller cancels a request, the partial results (for an input file, it's all or nothing) may still be available on the specified output location.
This call returns immediately and you can use google.longrunning.Operation.name to poll the status of the call.
Sample code:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
BatchTranslateDocumentRequest request =
BatchTranslateDocumentRequest.newBuilder()
.setParent(LocationName.of("[PROJECT]", "[LOCATION]").toString())
.setSourceLanguageCode("sourceLanguageCode1645917472")
.addAllTargetLanguageCodes(new ArrayList<String>())
.addAllInputConfigs(new ArrayList<BatchDocumentInputConfig>())
.setOutputConfig(BatchDocumentOutputConfig.newBuilder().build())
.putAllModels(new HashMap<String, String>())
.putAllGlossaries(new HashMap<String, TranslateTextGlossaryConfig>())
.putAllFormatConversions(new HashMap<String, String>())
.build();
BatchTranslateDocumentResponse response =
translationServiceClient.batchTranslateDocumentAsync(request).get();
}
Parameter | |
---|---|
Name | Description |
request |
BatchTranslateDocumentRequest The request object containing all of the parameters for the API call. |
Returns | |
---|---|
Type | Description |
OperationFuture<BatchTranslateDocumentResponse,BatchTranslateDocumentMetadata> |
batchTranslateDocumentAsync(LocationName parent, String sourceLanguageCode, List<String> targetLanguageCodes, List<BatchDocumentInputConfig> inputConfigs, BatchDocumentOutputConfig outputConfig)
public final OperationFuture<BatchTranslateDocumentResponse,BatchTranslateDocumentMetadata> batchTranslateDocumentAsync(LocationName parent, String sourceLanguageCode, List<String> targetLanguageCodes, List<BatchDocumentInputConfig> inputConfigs, BatchDocumentOutputConfig outputConfig)
Translates a large volume of documents in asynchronous batch mode. This function provides real-time output as the inputs are being processed. If caller cancels a request, the partial results (for an input file, it's all or nothing) may still be available on the specified output location.
This call returns immediately and you can use google.longrunning.Operation.name to poll the status of the call.
Sample code:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
String sourceLanguageCode = "sourceLanguageCode1645917472";
List<String> targetLanguageCodes = new ArrayList<>();
List<BatchDocumentInputConfig> inputConfigs = new ArrayList<>();
BatchDocumentOutputConfig outputConfig = BatchDocumentOutputConfig.newBuilder().build();
BatchTranslateDocumentResponse response =
translationServiceClient
.batchTranslateDocumentAsync(
parent, sourceLanguageCode, targetLanguageCodes, inputConfigs, outputConfig)
.get();
}
Parameters | |
---|---|
Name | Description |
parent |
LocationName Required. Location to make a regional call. Format: The Only AutoML Translation models or glossaries within the same region (have the same location-id) can be used, otherwise an INVALID_ARGUMENT (400) error is returned. |
sourceLanguageCode |
String Required. The BCP-47 language code of the input document if known, for example, "en-US" or "sr-Latn". Supported language codes are listed in Language Support (https://cloud.google.com/translate/docs/languages). |
targetLanguageCodes |
List<String> Required. The BCP-47 language code to use for translation of the input document. Specify up to 10 language codes here. |
inputConfigs |
List<BatchDocumentInputConfig> Required. Input configurations. The total number of files matched should be <= 100. The total content size to translate should be <= 100M Unicode codepoints. The files must use UTF-8 encoding. |
outputConfig |
BatchDocumentOutputConfig Required. Output configuration. If 2 input configs match to the same file (that is, same input path), we don't generate output for duplicate inputs. |
Returns | |
---|---|
Type | Description |
OperationFuture<BatchTranslateDocumentResponse,BatchTranslateDocumentMetadata> |
batchTranslateDocumentAsync(String parent, String sourceLanguageCode, List<String> targetLanguageCodes, List<BatchDocumentInputConfig> inputConfigs, BatchDocumentOutputConfig outputConfig)
public final OperationFuture<BatchTranslateDocumentResponse,BatchTranslateDocumentMetadata> batchTranslateDocumentAsync(String parent, String sourceLanguageCode, List<String> targetLanguageCodes, List<BatchDocumentInputConfig> inputConfigs, BatchDocumentOutputConfig outputConfig)
Translates a large volume of documents in asynchronous batch mode. This function provides real-time output as the inputs are being processed. If caller cancels a request, the partial results (for an input file, it's all or nothing) may still be available on the specified output location.
This call returns immediately and you can use google.longrunning.Operation.name to poll the status of the call.
Sample code:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
String parent = LocationName.of("[PROJECT]", "[LOCATION]").toString();
String sourceLanguageCode = "sourceLanguageCode1645917472";
List<String> targetLanguageCodes = new ArrayList<>();
List<BatchDocumentInputConfig> inputConfigs = new ArrayList<>();
BatchDocumentOutputConfig outputConfig = BatchDocumentOutputConfig.newBuilder().build();
BatchTranslateDocumentResponse response =
translationServiceClient
.batchTranslateDocumentAsync(
parent, sourceLanguageCode, targetLanguageCodes, inputConfigs, outputConfig)
.get();
}
Parameters | |
---|---|
Name | Description |
parent |
String Required. Location to make a regional call. Format: The Only AutoML Translation models or glossaries within the same region (have the same location-id) can be used, otherwise an INVALID_ARGUMENT (400) error is returned. |
sourceLanguageCode |
String Required. The BCP-47 language code of the input document if known, for example, "en-US" or "sr-Latn". Supported language codes are listed in Language Support (https://cloud.google.com/translate/docs/languages). |
targetLanguageCodes |
List<String> Required. The BCP-47 language code to use for translation of the input document. Specify up to 10 language codes here. |
inputConfigs |
List<BatchDocumentInputConfig> Required. Input configurations. The total number of files matched should be <= 100. The total content size to translate should be <= 100M Unicode codepoints. The files must use UTF-8 encoding. |
outputConfig |
BatchDocumentOutputConfig Required. Output configuration. If 2 input configs match to the same file (that is, same input path), we don't generate output for duplicate inputs. |
Returns | |
---|---|
Type | Description |
OperationFuture<BatchTranslateDocumentResponse,BatchTranslateDocumentMetadata> |
batchTranslateDocumentCallable()
public final UnaryCallable<BatchTranslateDocumentRequest,Operation> batchTranslateDocumentCallable()
Translates a large volume of documents in asynchronous batch mode. This function provides real-time output as the inputs are being processed. If caller cancels a request, the partial results (for an input file, it's all or nothing) may still be available on the specified output location.
This call returns immediately and you can use google.longrunning.Operation.name to poll the status of the call.
Sample code:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
BatchTranslateDocumentRequest request =
BatchTranslateDocumentRequest.newBuilder()
.setParent(LocationName.of("[PROJECT]", "[LOCATION]").toString())
.setSourceLanguageCode("sourceLanguageCode1645917472")
.addAllTargetLanguageCodes(new ArrayList<String>())
.addAllInputConfigs(new ArrayList<BatchDocumentInputConfig>())
.setOutputConfig(BatchDocumentOutputConfig.newBuilder().build())
.putAllModels(new HashMap<String, String>())
.putAllGlossaries(new HashMap<String, TranslateTextGlossaryConfig>())
.putAllFormatConversions(new HashMap<String, String>())
.build();
ApiFuture<Operation> future =
translationServiceClient.batchTranslateDocumentCallable().futureCall(request);
// Do something.
Operation response = future.get();
}
Returns | |
---|---|
Type | Description |
UnaryCallable<BatchTranslateDocumentRequest,Operation> |
batchTranslateDocumentOperationCallable()
public final OperationCallable<BatchTranslateDocumentRequest,BatchTranslateDocumentResponse,BatchTranslateDocumentMetadata> batchTranslateDocumentOperationCallable()
Translates a large volume of documents in asynchronous batch mode. This function provides real-time output as the inputs are being processed. If caller cancels a request, the partial results (for an input file, it's all or nothing) may still be available on the specified output location.
This call returns immediately and you can use google.longrunning.Operation.name to poll the status of the call.
Sample code:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
BatchTranslateDocumentRequest request =
BatchTranslateDocumentRequest.newBuilder()
.setParent(LocationName.of("[PROJECT]", "[LOCATION]").toString())
.setSourceLanguageCode("sourceLanguageCode1645917472")
.addAllTargetLanguageCodes(new ArrayList<String>())
.addAllInputConfigs(new ArrayList<BatchDocumentInputConfig>())
.setOutputConfig(BatchDocumentOutputConfig.newBuilder().build())
.putAllModels(new HashMap<String, String>())
.putAllGlossaries(new HashMap<String, TranslateTextGlossaryConfig>())
.putAllFormatConversions(new HashMap<String, String>())
.build();
OperationFuture<BatchTranslateDocumentResponse, BatchTranslateDocumentMetadata> future =
translationServiceClient.batchTranslateDocumentOperationCallable().futureCall(request);
// Do something.
BatchTranslateDocumentResponse response = future.get();
}
Returns | |
---|---|
Type | Description |
OperationCallable<BatchTranslateDocumentRequest,BatchTranslateDocumentResponse,BatchTranslateDocumentMetadata> |
batchTranslateTextAsync(BatchTranslateTextRequest request)
public final OperationFuture<BatchTranslateResponse,BatchTranslateMetadata> batchTranslateTextAsync(BatchTranslateTextRequest request)
Translates a large volume of text in asynchronous batch mode. This function provides real-time output as the inputs are being processed. If caller cancels a request, the partial results (for an input file, it's all or nothing) may still be available on the specified output location.
This call returns immediately and you can use google.longrunning.Operation.name to poll the status of the call.
Sample code:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
BatchTranslateTextRequest request =
BatchTranslateTextRequest.newBuilder()
.setParent(LocationName.of("[PROJECT]", "[LOCATION]").toString())
.setSourceLanguageCode("sourceLanguageCode1645917472")
.addAllTargetLanguageCodes(new ArrayList<String>())
.putAllModels(new HashMap<String, String>())
.addAllInputConfigs(new ArrayList<InputConfig>())
.setOutputConfig(OutputConfig.newBuilder().build())
.putAllGlossaries(new HashMap<String, TranslateTextGlossaryConfig>())
.putAllLabels(new HashMap<String, String>())
.build();
BatchTranslateResponse response =
translationServiceClient.batchTranslateTextAsync(request).get();
}
Parameter | |
---|---|
Name | Description |
request |
BatchTranslateTextRequest The request object containing all of the parameters for the API call. |
Returns | |
---|---|
Type | Description |
OperationFuture<BatchTranslateResponse,BatchTranslateMetadata> |
batchTranslateTextCallable()
public final UnaryCallable<BatchTranslateTextRequest,Operation> batchTranslateTextCallable()
Translates a large volume of text in asynchronous batch mode. This function provides real-time output as the inputs are being processed. If caller cancels a request, the partial results (for an input file, it's all or nothing) may still be available on the specified output location.
This call returns immediately and you can use google.longrunning.Operation.name to poll the status of the call.
Sample code:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
BatchTranslateTextRequest request =
BatchTranslateTextRequest.newBuilder()
.setParent(LocationName.of("[PROJECT]", "[LOCATION]").toString())
.setSourceLanguageCode("sourceLanguageCode1645917472")
.addAllTargetLanguageCodes(new ArrayList<String>())
.putAllModels(new HashMap<String, String>())
.addAllInputConfigs(new ArrayList<InputConfig>())
.setOutputConfig(OutputConfig.newBuilder().build())
.putAllGlossaries(new HashMap<String, TranslateTextGlossaryConfig>())
.putAllLabels(new HashMap<String, String>())
.build();
ApiFuture<Operation> future =
translationServiceClient.batchTranslateTextCallable().futureCall(request);
// Do something.
Operation response = future.get();
}
Returns | |
---|---|
Type | Description |
UnaryCallable<BatchTranslateTextRequest,Operation> |
batchTranslateTextOperationCallable()
public final OperationCallable<BatchTranslateTextRequest,BatchTranslateResponse,BatchTranslateMetadata> batchTranslateTextOperationCallable()
Translates a large volume of text in asynchronous batch mode. This function provides real-time output as the inputs are being processed. If caller cancels a request, the partial results (for an input file, it's all or nothing) may still be available on the specified output location.
This call returns immediately and you can use google.longrunning.Operation.name to poll the status of the call.
Sample code:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
BatchTranslateTextRequest request =
BatchTranslateTextRequest.newBuilder()
.setParent(LocationName.of("[PROJECT]", "[LOCATION]").toString())
.setSourceLanguageCode("sourceLanguageCode1645917472")
.addAllTargetLanguageCodes(new ArrayList<String>())
.putAllModels(new HashMap<String, String>())
.addAllInputConfigs(new ArrayList<InputConfig>())
.setOutputConfig(OutputConfig.newBuilder().build())
.putAllGlossaries(new HashMap<String, TranslateTextGlossaryConfig>())
.putAllLabels(new HashMap<String, String>())
.build();
OperationFuture<BatchTranslateResponse, BatchTranslateMetadata> future =
translationServiceClient.batchTranslateTextOperationCallable().futureCall(request);
// Do something.
BatchTranslateResponse response = future.get();
}
Returns | |
---|---|
Type | Description |
OperationCallable<BatchTranslateTextRequest,BatchTranslateResponse,BatchTranslateMetadata> |
close()
public final void close()
createGlossaryAsync(CreateGlossaryRequest request)
public final OperationFuture<Glossary,CreateGlossaryMetadata> createGlossaryAsync(CreateGlossaryRequest request)
Creates a glossary and returns the long-running operation. Returns NOT_FOUND, if the project doesn't exist.
Sample code:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
CreateGlossaryRequest request =
CreateGlossaryRequest.newBuilder()
.setParent(LocationName.of("[PROJECT]", "[LOCATION]").toString())
.setGlossary(Glossary.newBuilder().build())
.build();
Glossary response = translationServiceClient.createGlossaryAsync(request).get();
}
Parameter | |
---|---|
Name | Description |
request |
|