Client for Cloud Translation API#

class google.cloud.translate_v3beta1.TranslationServiceClient(transport=None, channel=None, credentials=None, client_config=None, client_info=None)[source]#

Provides natural language translation operations.

Constructor.

Parameters:
  • (Union[TranslationServiceGrpcTransport, (transport) – Callable[[~.Credentials, type], ~.TranslationServiceGrpcTransport]): A transport instance, responsible for actually making the API calls. The default transport uses the gRPC protocol. This argument may also be a callable which returns a transport instance. Callables will be sent the credentials as the first argument and the default transport class as the second argument.
  • channel (grpc.Channel) – DEPRECATED. A Channel instance through which to make calls. This argument is mutually exclusive with credentials; providing both will raise an exception.
  • credentials (google.auth.credentials.Credentials) – The authorization credentials to attach to requests. These credentials identify this application to the service. If none are specified, the client will attempt to ascertain the credentials from the environment. This argument is mutually exclusive with providing a transport instance to transport; doing so will raise an exception.
  • client_config (dict) – DEPRECATED. A dictionary of call options for each method. If not specified, the default configuration is used.
  • client_info (google.api_core.gapic_v1.client_info.ClientInfo) – The client info used to send a user-agent string along with API requests. If None, then default info will be used. Generally, you only need to set this if you’re developing your own client library.
batch_translate_text(source_language_code, target_language_codes, input_configs, output_config, parent=None, models=None, glossaries=None, retry=<object object>, timeout=<object object>, metadata=None)[source]#

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.

Example

>>> from google.cloud import translate_v3beta1
>>>
>>> client = translate_v3beta1.TranslationServiceClient()
>>>
>>> # TODO: Initialize `source_language_code`:
>>> source_language_code = ''
>>>
>>> # TODO: Initialize `target_language_codes`:
>>> target_language_codes = []
>>>
>>> # TODO: Initialize `input_configs`:
>>> input_configs = []
>>>
>>> # TODO: Initialize `output_config`:
>>> output_config = {}
>>>
>>> response = client.batch_translate_text(source_language_code, target_language_codes, input_configs, output_config)
>>>
>>> def callback(operation_future):
...     # Handle result.
...     result = operation_future.result()
>>>
>>> response.add_done_callback(callback)
>>>
>>> # Handle metadata.
>>> metadata = response.metadata()
Parameters:
  • source_language_code (str) – Required. Source language code.
  • target_language_codes (list[str]) – Required. Specify up to 10 language codes here.
  • input_configs (list[Union[dict, InputConfig]]) –

    Required. Input configurations. The total number of files matched should be <= 1000. The total content size should be <= 100M Unicode codepoints. The files must use UTF-8 encoding.

    If a dict is provided, it must be of the same form as the protobuf message InputConfig

  • output_config (Union[dict, OutputConfig]) –

    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.

    If a dict is provided, it must be of the same form as the protobuf message OutputConfig

  • parent (str) –

    Optional. Only used when making regionalized call. Format: projects/{project-id}/locations/{location-id}.

    Only custom models/glossaries within the same location-id can be used. Otherwise 400 is returned.

  • models (dict[str -> str]) –

    Optional. The models to use for translation. Map’s key is target language code. Map’s value is model name. Value can be a built-in general model, or a custom model built by AutoML.

    The value format depends on model type: 1. Custom models: projects/{project-id}/locations/{location-id}/models/{model-id}. 2. General (built-in) models: projects/{project-id}/locations/{location-id}/models/general/nmt projects/{project-id}/locations/{location-id}/models/general/base

    If the map is empty or a specific model is not requested for a language pair, then default google model is used.

  • glossaries (dict[str -> Union[dict, TranslateTextGlossaryConfig]]) –

    Optional. Glossaries to be applied for translation. It’s keyed by target language code.

    If a dict is provided, it must be of the same form as the protobuf message TranslateTextGlossaryConfig

  • retry (Optional[google.api_core.retry.Retry]) – A retry object used to retry requests. If None is specified, requests will not be retried.
  • timeout (Optional[float]) – The amount of time, in seconds, to wait for the request to complete. Note that if retry is specified, the timeout applies to each individual attempt.
  • metadata (Optional[Sequence[Tuple[str, str]]]) – Additional metadata that is provided to the method.
Returns:

A _OperationFuture instance.

Raises:
create_glossary(parent, glossary, retry=<object object>, timeout=<object object>, metadata=None)[source]#

Creates a glossary and returns the long-running operation. Returns NOT_FOUND, if the project doesn’t exist.

Example

>>> from google.cloud import translate_v3beta1
>>>
>>> client = translate_v3beta1.TranslationServiceClient()
>>>
>>> parent = client.location_path('[PROJECT]', '[LOCATION]')
>>>
>>> # TODO: Initialize `glossary`:
>>> glossary = {}
>>>
>>> response = client.create_glossary(parent, glossary)
>>>
>>> def callback(operation_future):
...     # Handle result.
...     result = operation_future.result()
>>>
>>> response.add_done_callback(callback)
>>>
>>> # Handle metadata.
>>> metadata = response.metadata()
Parameters:
  • parent (str) – Required. The project name.
  • glossary (Union[dict, Glossary]) –

    Required. The glossary to create.

    If a dict is provided, it must be of the same form as the protobuf message Glossary

  • retry (Optional[google.api_core.retry.Retry]) – A retry object used to retry requests. If None is specified, requests will not be retried.
  • timeout (Optional[float]) – The amount of time, in seconds, to wait for the request to complete. Note that if retry is specified, the timeout applies to each individual attempt.
  • metadata (Optional[Sequence[Tuple[str, str]]]) – Additional metadata that is provided to the method.
Returns:

A _OperationFuture instance.

Raises:
delete_glossary(name, retry=<object object>, timeout=<object object>, metadata=None)[source]#

Deletes a glossary, or cancels glossary construction if the glossary isn’t created yet. Returns NOT_FOUND, if the glossary doesn’t exist.

Example

>>> from google.cloud import translate_v3beta1
>>>
>>> client = translate_v3beta1.TranslationServiceClient()
>>>
>>> name = client.glossary_path('[PROJECT]', '[LOCATION]', '[GLOSSARY]')
>>>
>>> response = client.delete_glossary(name)
>>>
>>> def callback(operation_future):
...     # Handle result.
...     result = operation_future.result()
>>>
>>> response.add_done_callback(callback)
>>>
>>> # Handle metadata.
>>> metadata = response.metadata()
Parameters:
  • name (str) – Required. The name of the glossary to delete.
  • retry (Optional[google.api_core.retry.Retry]) – A retry object used to retry requests. If None is specified, requests will not be retried.
  • timeout (Optional[float]) – The amount of time, in seconds, to wait for the request to complete. Note that if retry is specified, the timeout applies to each individual attempt.
  • metadata (Optional[Sequence[Tuple[str, str]]]) – Additional metadata that is provided to the method.
Returns:

A _OperationFuture instance.

Raises:
detect_language(parent=None, model=None, content=None, mime_type=None, retry=<object object>, timeout=<object object>, metadata=None)[source]#

Detects the language of text within a request.

Example

>>> from google.cloud import translate_v3beta1
>>>
>>> client = translate_v3beta1.TranslationServiceClient()
>>>
>>> response = client.detect_language()
Parameters:
  • parent (str) –

    Optional. Only used when making regionalized call. Format: projects/{project-id}/locations/{location-id}.

    Only custom model within the same location-id can be used. Otherwise 400 is returned.

  • model (str) – Optional. The language detection model to be used. projects/{project-id}/locations/{location-id}/models/language-detection/{model-id} If not specified, default will be used.
  • content (str) – The content of the input stored as a string.
  • mime_type (str) – Optional. The format of the source text, for example, “text/html”, “text/plain”. If left blank, the MIME type is assumed to be “text/html”.
  • retry (Optional[google.api_core.retry.Retry]) – A retry object used to retry requests. If None is specified, requests will not be retried.
  • timeout (Optional[float]) – The amount of time, in seconds, to wait for the request to complete. Note that if retry is specified, the timeout applies to each individual attempt.
  • metadata (Optional[Sequence[Tuple[str, str]]]) – Additional metadata that is provided to the method.
Returns:

A DetectLanguageResponse instance.

Raises:
classmethod from_service_account_file(filename, *args, **kwargs)[source]#

Creates an instance of this client using the provided credentials file.

Parameters:
  • filename (str) – The path to the service account private key json file.
  • args – Additional arguments to pass to the constructor.
  • kwargs – Additional arguments to pass to the constructor.
Returns:

The constructed client.

Return type:

TranslationServiceClient

classmethod from_service_account_json(filename, *args, **kwargs)#

Creates an instance of this client using the provided credentials file.

Parameters:
  • filename (str) – The path to the service account private key json file.
  • args – Additional arguments to pass to the constructor.
  • kwargs – Additional arguments to pass to the constructor.
Returns:

The constructed client.

Return type:

TranslationServiceClient

get_glossary(name, retry=<object object>, timeout=<object object>, metadata=None)[source]#

Gets a glossary. Returns NOT_FOUND, if the glossary doesn’t exist.

Example

>>> from google.cloud import translate_v3beta1
>>>
>>> client = translate_v3beta1.TranslationServiceClient()
>>>
>>> name = client.glossary_path('[PROJECT]', '[LOCATION]', '[GLOSSARY]')
>>>
>>> response = client.get_glossary(name)
Parameters:
  • name (str) – Required. The name of the glossary to retrieve.
  • retry (Optional[google.api_core.retry.Retry]) – A retry object used to retry requests. If None is specified, requests will not be retried.
  • timeout (Optional[float]) – The amount of time, in seconds, to wait for the request to complete. Note that if retry is specified, the timeout applies to each individual attempt.
  • metadata (Optional[Sequence[Tuple[str, str]]]) – Additional metadata that is provided to the method.
Returns:

A Glossary instance.

Raises:
get_supported_languages(parent=None, display_language_code=None, model=None, retry=<object object>, timeout=<object object>, metadata=None)[source]#

Returns a list of supported languages for translation.

Example

>>> from google.cloud import translate_v3beta1
>>>
>>> client = translate_v3beta1.TranslationServiceClient()
>>>
>>> response = client.get_supported_languages()
Parameters:
  • parent (str) –

    Optional. Used for making regionalized calls. Format: projects/{project-id}/locations/{location-id}. For global calls, use projects/{project-id}/locations/global. If missing, the call is treated as a global call.

    Only custom model within the same location-id can be used. Otherwise 400 is returned.

  • display_language_code (str) – Optional. The language to use to return localized, human readable names of supported languages. If missing, default language is ENGLISH.
  • model (str) – Optional. Get supported languages of this model. The format depends on model type: 1. Custom models: projects/{project-id}/locations/{location-id}/models/{model-id}. 2. General (built-in) models: projects/{project-id}/locations/{location-id}/models/general/nmt projects/{project-id}/locations/{location-id}/models/general/base Returns languages supported by the specified model. If missing, we get supported languages of Google general NMT model.
  • retry (Optional[google.api_core.retry.Retry]) – A retry object used to retry requests. If None is specified, requests will not be retried.
  • timeout (Optional[float]) – The amount of time, in seconds, to wait for the request to complete. Note that if retry is specified, the timeout applies to each individual attempt.
  • metadata (Optional[Sequence[Tuple[str, str]]]) – Additional metadata that is provided to the method.
Returns:

A SupportedLanguages instance.

Raises:
classmethod glossary_path(project, location, glossary)[source]#

Return a fully-qualified glossary string.

list_glossaries(parent=None, page_size=None, filter_=None, retry=<object object>, timeout=<object object>, metadata=None)[source]#

Lists glossaries in a project. Returns NOT_FOUND, if the project doesn’t exist.

Example

>>> from google.cloud import translate_v3beta1
>>>
>>> client = translate_v3beta1.TranslationServiceClient()
>>>
>>> # Iterate over all results
>>> for element in client.list_glossaries():
...     # process element
...     pass
>>>
>>>
>>> # Alternatively:
>>>
>>> # Iterate over results one page at a time
>>> for page in client.list_glossaries().pages:
...     for element in page:
...         # process element
...         pass
Parameters:
  • parent (str) – Required. The name of the project from which to list all of the glossaries.
  • page_size (int) – The maximum number of resources contained in the underlying API response. If page streaming is performed per- resource, this parameter does not affect the return value. If page streaming is performed per-page, this determines the maximum number of resources in a page.
  • filter (str) – Optional. Filter specifying constraints of a list operation. For example, tags.glossary_name="products*". If missing, no filtering is performed.
  • retry (Optional[google.api_core.retry.Retry]) – A retry object used to retry requests. If None is specified, requests will not be retried.
  • timeout (Optional[float]) – The amount of time, in seconds, to wait for the request to complete. Note that if retry is specified, the timeout applies to each individual attempt.
  • metadata (Optional[Sequence[Tuple[str, str]]]) – Additional metadata that is provided to the method.
Returns:

A PageIterator instance. By default, this is an iterable of Glossary instances. This object can also be configured to iterate over the pages of the response through the options parameter.

Raises:
classmethod location_path(project, location)[source]#

Return a fully-qualified location string.

translate_text(contents, target_language_code, mime_type=None, source_language_code=None, parent=None, model=None, glossary_config=None, retry=<object object>, timeout=<object object>, metadata=None)[source]#

Translates input text and returns translated text.

Example

>>> from google.cloud import translate_v3beta1
>>>
>>> client = translate_v3beta1.TranslationServiceClient()
>>>
>>> # TODO: Initialize `contents`:
>>> contents = []
>>>
>>> # TODO: Initialize `target_language_code`:
>>> target_language_code = ''
>>>
>>> response = client.translate_text(contents, target_language_code)
Parameters:
  • contents (list[str]) – Required. The content of the input in string format. We recommend the total contents to be less than 30k codepoints. Please use BatchTranslateText for larger text.
  • target_language_code (str) – Required. The BCP-47 language code to use for translation of the input text, set to one of the language codes listed in Language Support.
  • mime_type (str) – Optional. The format of the source text, for example, “text/html”, “text/plain”. If left blank, the MIME type is assumed to be “text/html”.
  • source_language_code (str) – Optional. The BCP-47 language code of the input text if known, for example, “en-US” or “sr-Latn”. Supported language codes are listed in Language Support. If the source language isn’t specified, the API attempts to identify the source language automatically and returns the the source language within the response.
  • parent (str) –

    Optional. Only used when making regionalized call. Format: projects/{project-id}/locations/{location-id}.

    Only custom model/glossary within the same location-id can be used. Otherwise 400 is returned.

  • model (str) –

    Optional. The model type requested for this translation.

    The format depends on model type:

    1. Custom models: projects/{project-id}/locations/{location-id}/models/{model-id}.
    2. General (built-in) models: projects/{project-id}/locations/{location-id}/models/general/nmt projects/{project-id}/locations/{location-id}/models/general/base

    For global (non-regionalized) requests, use {location-id} ‘global’. For example, projects/{project-id}/locations/global/models/general/nmt

    If missing, the system decides which google base model to use.

  • glossary_config (Union[dict, TranslateTextGlossaryConfig]) –

    Optional. Glossary to be applied. The glossary needs to be in the same region as the model, otherwise an INVALID_ARGUMENT error is returned.

    If a dict is provided, it must be of the same form as the protobuf message TranslateTextGlossaryConfig

  • retry (Optional[google.api_core.retry.Retry]) – A retry object used to retry requests. If None is specified, requests will not be retried.
  • timeout (Optional[float]) – The amount of time, in seconds, to wait for the request to complete. Note that if retry is specified, the timeout applies to each individual attempt.
  • metadata (Optional[Sequence[Tuple[str, str]]]) – Additional metadata that is provided to the method.
Returns:

A TranslateTextResponse instance.

Raises: