TranslationServiceClient 类 (3.12.0)

TranslationServiceClient(*, credentials: typing.Optional[google.auth.credentials.Credentials] = None, transport: typing.Optional[typing.Union[str, google.cloud.translate_v3.services.translation_service.transports.base.TranslationServiceTransport]] = None, client_options: typing.Optional[typing.Union[google.api_core.client_options.ClientOptions, dict]] = None, client_info: google.api_core.gapic_v1.client_info.ClientInfo = <google.api_core.gapic_v1.client_info.ClientInfo object>)

提供自然语言翻译操作。

属性

transport

返回客户端实例使用的传输方式。

返回结果
类型说明
TranslationServiceTransport客户端实例使用的传输。

方法

TranslationServiceClient

TranslationServiceClient(*, credentials: typing.Optional[google.auth.credentials.Credentials] = None, transport: typing.Optional[typing.Union[str, google.cloud.translate_v3.services.translation_service.transports.base.TranslationServiceTransport]] = None, client_options: typing.Optional[typing.Union[google.api_core.client_options.ClientOptions, dict]] = None, client_info: google.api_core.gapic_v1.client_info.ClientInfo = <google.api_core.gapic_v1.client_info.ClientInfo object>)

实例化翻译服务客户端。

参数
名称说明
credentials Optional[google.auth.credentials.Credentials]

要附加到请求的授权凭据。这些凭据用于向服务标识应用;如果未指定任何凭据,客户端将尝试从环境中确定凭据。

transport Union[str, TranslationServiceTransport]

要使用的传输方式。如果设置为 None,系统会自动选择传输方式。

client_options Optional[Union[google.api_core.client_options.ClientOptions, dict]]

客户端的自定义选项。如果提供了 transport 实例,则不会生效。(1) 您可以使用 api_endpoint 属性替换客户端提供的默认端点。您还可以使用 GOOGLE_API_USE_MTLS_ENDPOINT 环境变量来替换端点:“always”(始终使用默认 mTLS 端点)、“never”(始终使用默认常规端点)和“auto”(如果存在客户端证书,则自动切换到默认 mTLS 端点;这是默认值)。不过,如果提供了 api_endpoint 属性,则该属性优先。(2) 如果 GOOGLE_API_USE_CLIENT_CERTIFICATE 环境变量为“true”,则可以使用 client_cert_source 属性为双向 TLS 传输提供客户端证书。如果未提供,系统将使用默认的 SSL 客户端证书(如果有)。如果 GOOGLE_API_USE_CLIENT_CERTIFICATE 为“false”或未设置,则不会使用任何客户端证书。

client_info google.api_core.gapic_v1.client_info.ClientInfo

用于随 API 请求一起发送用户代理字符串的客户端信息。如果值为 None,则使用默认信息。一般来说,只有在开发自己的客户端库时才需要设置此属性。

例外情况
类型说明
google.auth.exceptions.MutualTLSChannelError如果因任何原因导致双向 TLS 传输创建失败。

__exit__

__exit__(type, value, traceback)

释放底层传输的资源。

batch_translate_document

batch_translate_document(
    request: typing.Optional[
        typing.Union[
            google.cloud.translate_v3.types.translation_service.BatchTranslateDocumentRequest,
            dict,
        ]
    ] = None,
    *,
    parent: typing.Optional[str] = None,
    source_language_code: typing.Optional[str] = None,
    target_language_codes: typing.Optional[typing.MutableSequence[str]] = None,
    input_configs: typing.Optional[
        typing.MutableSequence[
            google.cloud.translate_v3.types.translation_service.BatchDocumentInputConfig
        ]
    ] = None,
    output_config: typing.Optional[
        google.cloud.translate_v3.types.translation_service.BatchDocumentOutputConfig
    ] = None,
    retry: typing.Union[
        google.api_core.retry.Retry, google.api_core.gapic_v1.method._MethodDefault
    ] = _MethodDefault._DEFAULT_VALUE,
    timeout: typing.Union[float, object] = _MethodDefault._DEFAULT_VALUE,
    metadata: typing.Sequence[typing.Tuple[str, str]] = ()
) -> google.api_core.operation.Operation

以异步批处理模式翻译大量文档。此函数会在处理输入内容时提供实时输出。如果调用方取消请求,指定输出位置可能仍会提供部分结果(对于输入文件,要么全部提供,要么全部不提供)。 此调用会立即返回,您可以使用 google.longrunning.Operation.name 轮询调用的状态。

# 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://googleapis.dev/python/google-api-core/latest/client_options.html
from google.cloud import translate_v3

def sample_batch_translate_document():
    # Create a client
    client = translate_v3.TranslationServiceClient()

    # Initialize request argument(s)
    input_configs = translate_v3.BatchDocumentInputConfig()
    input_configs.gcs_source.input_uri = "input_uri_value"

    output_config = translate_v3.BatchDocumentOutputConfig()
    output_config.gcs_destination.output_uri_prefix = "output_uri_prefix_value"

    request = translate_v3.BatchTranslateDocumentRequest(
        parent="parent_value",
        source_language_code="source_language_code_value",
        target_language_codes=['target_language_codes_value1', 'target_language_codes_value2'],
        input_configs=input_configs,
        output_config=output_config,
    )

    # Make the request
    operation = client.batch_translate_document(request=request)

    print("Waiting for operation to complete...")

    response = operation.result()

    # Handle the response
    print(response)
参数
名称说明
request Union[google.cloud.translate_v3.types.BatchTranslateDocumentRequest, dict]

请求对象。BatchTranslateDocument 请求。

parent str

必需。进行区域调用的位置。格式:projects/{project-number-or-id}/locations/{location-id}。批量翻译不支持 global 位置。只能使用同一区域(具有相同位置 ID)内的 AutoML 翻译模型或词汇表,否则系统会返回 INVALID_ARGUMENT (400) 错误。这对应于 request 实例上的 parent 字段;如果提供了 request,则不应设置此字段。

source_language_code str

必需。输入文档的 ISO-639 语言代码(如果已知),例如“en-US”或“sr-Latn”。Language Support https://cloud.google.com/translate/docs/languages__ 中列出了支持的语言代码。这对应于 request 实例上的 source_language_code 字段;如果提供了 request,则不应设置此字段。

target_language_codes MutableSequence[str]

必需。用于翻译输入文档的 ISO-639 语言代码。您可以在此处指定最多 10 个语言代码。这对应于 request 实例上的 target_language_codes 字段;如果提供了 request,则不应设置此字段。

input_configs MutableSequence[google.cloud.translate_v3.types.BatchDocumentInputConfig]

必需。输入配置。匹配的文件总数应不超过 100。要翻译的总内容大小应小于等于 100M Unicode 代码点。文件必须使用 UTF-8 编码。这对应于 request 实例上的 input_configs 字段;如果提供了 request,则不应设置此字段。

output_config google.cloud.translate_v3.types.BatchDocumentOutputConfig

必需。输出配置。如果 2 个输入配置匹配到同一文件(即输入路径相同),则我们不会为重复的输入生成输出。这对应于 request 实例上的 output_config 字段;如果提供了 request,则不应设置此字段。

retry google.api_core.retry.Retry

指定应重试哪些错误(如果有)。

timeout float

相应请求的超时时间。

metadata Sequence[Tuple[str, str]]

应随请求一起作为元数据发送的字符串。

返回结果
类型说明
google.api_core.operation.Operation表示长时间运行的操作的对象。如果至少有一个文档成功翻译,则操作的结果类型为 BatchTranslateDocumentResponse,并存储在 BatchTranslateDocument 返回的 google.longrunning.Operation.response][google.longrunning.Operation.response] 字段中。

batch_translate_text

batch_translate_text(
    request: typing.Optional[
        typing.Union[
            google.cloud.translate_v3.types.translation_service.BatchTranslateTextRequest,
            dict,
        ]
    ] = None,
    *,
    retry: typing.Union[
        google.api_core.retry.Retry, google.api_core.gapic_v1.method._MethodDefault
    ] = _MethodDefault._DEFAULT_VALUE,
    timeout: typing.Union[float, object] = _MethodDefault._DEFAULT_VALUE,
    metadata: typing.Sequence[typing.Tuple[str, str]] = ()
) -> google.api_core.operation.Operation

以异步批处理模式翻译大量文本。此函数会在处理输入内容时提供实时输出。如果调用方取消请求,指定输出位置可能仍会提供部分结果(对于输入文件,要么全部提供,要么全部不提供)。 此调用会立即返回,您可以使用 google.longrunning.Operation.name 轮询调用的状态。

# 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://googleapis.dev/python/google-api-core/latest/client_options.html
from google.cloud import translate_v3

def sample_batch_translate_text():
    # Create a client
    client = translate_v3.TranslationServiceClient()

    # Initialize request argument(s)
    input_configs = translate_v3.InputConfig()
    input_configs.gcs_source.input_uri = "input_uri_value"

    output_config = translate_v3.OutputConfig()
    output_config.gcs_destination.output_uri_prefix = "output_uri_prefix_value"

    request = translate_v3.BatchTranslateTextRequest(
        parent="parent_value",
        source_language_code="source_language_code_value",
        target_language_codes=['target_language_codes_value1', 'target_language_codes_value2'],
        input_configs=input_configs,
        output_config=output_config,
    )

    # Make the request
    operation = client.batch_translate_text(request=request)

    print("Waiting for operation to complete...")

    response = operation.result()

    # Handle the response
    print(response)
参数
名称说明
request Union[google.cloud.translate_v3.types.BatchTranslateTextRequest, dict]

请求对象。批量翻译请求。

retry google.api_core.retry.Retry

指定应重试哪些错误(如果有)。

timeout float

相应请求的超时时间。

metadata Sequence[Tuple[str, str]]

应随请求一起作为元数据发送的字符串。

返回结果
类型说明
google.api_core.operation.Operation表示长时间运行的操作的对象。如果至少有一句话成功翻译,则操作的结果类型为 BatchTranslateResponse,存储在 BatchTranslateText 返回的 google.longrunning.Operation.response][google.longrunning.Operation.response] 字段中。

create_glossary

create_glossary(
    request: typing.Optional[
        typing.Union[
            google.cloud.translate_v3.types.translation_service.CreateGlossaryRequest,
            dict,
        ]
    ] = None,
    *,
    parent: typing.Optional[str] = None,
    glossary: typing.Optional[
        google.cloud.translate_v3.types.translation_service.Glossary
    ] = None,
    retry: typing.Union[
        google.api_core.retry.Retry, google.api_core.gapic_v1.method._MethodDefault
    ] = _MethodDefault._DEFAULT_VALUE,
    timeout: typing.Union[float, object] = _MethodDefault._DEFAULT_VALUE,
    metadata: typing.Sequence[typing.Tuple[str, str]] = ()
) -> google.api_core.operation.Operation

创建术语表并返回长时间运行的操作。 如果项目不存在,则返回 NOT_FOUND。

# 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://googleapis.dev/python/google-api-core/latest/client_options.html
from google.cloud import translate_v3

def sample_create_glossary():
    # Create a client
    client = translate_v3.TranslationServiceClient()

    # Initialize request argument(s)
    glossary = translate_v3.Glossary()
    glossary.name = "name_value"

    request = translate_v3.CreateGlossaryRequest(
        parent="parent_value",
        glossary=glossary,
    )

    # Make the request
    operation = client.create_glossary(request=request)

    print("Waiting for operation to complete...")

    response = operation.result()

    # Handle the response
    print(response)
参数
名称说明
request Union[google.cloud.translate_v3.types.CreateGlossaryRequest, dict]

请求对象。CreateGlossary 的请求消息。

parent str

必需。项目名称。这对应于 request 实例上的 parent 字段;如果提供了 request,则不应设置此字段。

glossary google.cloud.translate_v3.types.Glossary

必需。要创建的术语库。这对应于 request 实例上的 glossary 字段;如果提供了 request,则不应设置此字段。

retry google.api_core.retry.Retry

指定应重试哪些错误(如果有)。

timeout float

相应请求的超时时间。

metadata Sequence[Tuple[str, str]]

应随请求一起作为元数据发送的字符串。

返回结果
类型说明
google.api_core.operation.Operation表示长时间运行的操作的对象。相应操作的结果类型将为 Glossary,表示根据用户提供的数据构建的词汇表。

delete_glossary

delete_glossary(
    request: typing.Optional[
        typing.Union[
            google.cloud.translate_v3.types.translation_service.DeleteGlossaryRequest,
            dict,
        ]
    ] = None,
    *,
    name: typing.Optional[str] = None,
    retry: typing.Union[
        google.api_core.retry.Retry, google.api_core.gapic_v1.method._MethodDefault
    ] = _MethodDefault._DEFAULT_VALUE,
    timeout: typing.Union[float, object] = _MethodDefault._DEFAULT_VALUE,
    metadata: typing.Sequence[typing.Tuple[str, str]] = ()
) -> google.api_core.operation.Operation

删除术语表,如果术语表尚未创建,则取消术语表构建。如果词库不存在,则返回 NOT_FOUND。

# 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://googleapis.dev/python/google-api-core/latest/client_options.html
from google.cloud import translate_v3

def sample_delete_glossary():
    # Create a client
    client = translate_v3.TranslationServiceClient()

    # Initialize request argument(s)
    request = translate_v3.DeleteGlossaryRequest(
        name="name_value",
    )

    # Make the request
    operation = client.delete_glossary(request=request)

    print("Waiting for operation to complete...")

    response = operation.result()

    # Handle the response
    print(response)
参数
名称说明
request Union[google.cloud.translate_v3.types.DeleteGlossaryRequest, dict]

请求对象。针对 DeleteGlossary 的请求消息。

name str

必需。要删除的词库的名称。这对应于 request 实例上的 name 字段;如果提供了 request,则不应设置此字段。

retry google.api_core.retry.Retry

指定应重试哪些错误(如果有)。

timeout float

相应请求的超时时间。

metadata Sequence[Tuple[str, str]]

应随请求一起作为元数据发送的字符串。

返回结果
类型说明
google.api_core.operation.Operation表示长时间运行的操作的对象。相应操作的结果类型将为 DeleteGlossaryResponse,存储在 DeleteGlossary 返回的 google.longrunning.Operation.response][google.longrunning.Operation.response] 字段中。

detect_language

detect_language(
    request: typing.Optional[
        typing.Union[
            google.cloud.translate_v3.types.translation_service.DetectLanguageRequest,
            dict,
        ]
    ] = None,
    *,
    parent: typing.Optional[str] = None,
    model: typing.Optional[str] = None,
    mime_type: typing.Optional[str] = None,
    content: typing.Optional[str] = None,
    retry: typing.Union[
        google.api_core.retry.Retry, google.api_core.gapic_v1.method._MethodDefault
    ] = _MethodDefault._DEFAULT_VALUE,
    timeout: typing.Union[float, object] = _MethodDefault._DEFAULT_VALUE,
    metadata: typing.Sequence[typing.Tuple[str, str]] = ()
) -> google.cloud.translate_v3.types.translation_service.DetectLanguageResponse

检测请求文本使用的语言。

# 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://googleapis.dev/python/google-api-core/latest/client_options.html
from google.cloud import translate_v3

def sample_detect_language():
    # Create a client
    client = translate_v3.TranslationServiceClient()

    # Initialize request argument(s)
    request = translate_v3.DetectLanguageRequest(
        content="content_value",
        parent="parent_value",
    )

    # Make the request
    response = client.detect_language(request=request)

    # Handle the response
    print(response)
参数
名称说明
request Union[google.cloud.translate_v3.types.DetectLanguageRequest, dict]

请求对象。语言检测的请求消息。

parent str

必需。项目或位置以进行呼叫。必须引用调用者的项目。格式:projects/{project-number-or-id}/locations/{location-id}projects/{project-number-or-id}。对于全局调用,请使用 projects/{project-number-or-id}/locations/globalprojects/{project-number-or-id}。只能使用同一区域(具有相同位置 ID)内的模型。否则,系统会返回 INVALID_ARGUMENT (400) 错误。这对应于 request 实例上的 parent 字段;如果提供了 request,则不应设置此字段。

model str

可选。要使用的语言检测模型。格式:projects/{project-number-or-id}/locations/{location-id}/models/language-detection/{model-id} 目前仅支持一种语言检测模型:projects/{project-number-or-id}/locations/{location-id}/models/language-detection/default。如果未指定,则使用默认模型。这对应于 request 实例上的 model 字段;如果提供了 request,则不应设置此字段。

mime_type str

可选。源文本的格式,例如“text/html”“text/plain”。如果留空,MIME 类型将默认为“text/html”。这对应于 request 实例上的 mime_type 字段;如果提供了 request,则不应设置此字段。

content str

以字符串形式存储的输入内容。这对应于 request 实例上的 content 字段;如果提供了 request,则不应设置此字段。

retry google.api_core.retry.Retry

指定应重试哪些错误(如果有)。

timeout float

相应请求的超时时间。

metadata Sequence[Tuple[str, str]]

应随请求一起作为元数据发送的字符串。

参数
名称说明
filename str

服务账号私钥 JSON 文件的路径。

返回结果
类型说明
TranslationServiceClient构建的客户端。

get_glossary

get_glossary(
    request: typing.Optional[
        typing.Union[
            google.cloud.translate_v3.types.translation_service.GetGlossaryRequest, dict
        ]
    ] = None,
    *,
    name: typing.Optional[str] = None,
    retry: typing.Union[
        google.api_core.retry.Retry, google.api_core.gapic_v1.method._MethodDefault
    ] = _MethodDefault._DEFAULT_VALUE,
    timeout: typing.Union[float, object] = _MethodDefault._DEFAULT_VALUE,
    metadata: typing.Sequence[typing.Tuple[str, str]] = ()
) -> google.cloud.translate_v3.types.translation_service.Glossary

获取术语表。如果词库不存在,则返回 NOT_FOUND。

# 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://googleapis.dev/python/google-api-core/latest/client_options.html
from google.cloud import translate_v3

def sample_get_glossary():
    # Create a client
    client = translate_v3.TranslationServiceClient()

    # Initialize request argument(s)
    request = translate_v3.GetGlossaryRequest(
        name="name_value",
    )

    # Make the request
    response = client.get_glossary(request=request)

    # Handle the response
    print(response)
参数
名称说明
request Union[google.cloud.translate_v3.types.GetGlossaryRequest, dict]

请求对象。针对 GetGlossary 的请求消息。

name str

必需。要检索的词库的名称。这对应于 request 实例上的 name 字段;如果提供了 request,则不应设置此字段。

retry google.api_core.retry.Retry

指定应重试哪些错误(如果有)。

timeout float

相应请求的超时时间。

metadata Sequence[Tuple[str, str]]

应随请求一起作为元数据发送的字符串。

例外情况
类型说明
google.auth.exceptions.MutualTLSChannelError如果发生任何错误。
返回结果
类型说明
Tuple[str, Callable[[], Tuple[bytes, bytes]]]返回要使用的 API 端点和客户端证书来源。

get_supported_languages

get_supported_languages(
    request: typing.Optional[
        typing.Union[
            google.cloud.translate_v3.types.translation_service.GetSupportedLanguagesRequest,
            dict,
        ]
    ] = None,
    *,
    parent: typing.Optional[str] = None,
    model: typing.Optional[str] = None,
    display_language_code: typing.Optional[str] = None,
    retry: typing.Union[
        google.api_core.retry.Retry, google.api_core.gapic_v1.method._MethodDefault
    ] = _MethodDefault._DEFAULT_VALUE,
    timeout: typing.Union[float, object] = _MethodDefault._DEFAULT_VALUE,
    metadata: typing.Sequence[typing.Tuple[str, str]] = ()
) -> google.cloud.translate_v3.types.translation_service.SupportedLanguages

返回支持的翻译语言列表。

# 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://googleapis.dev/python/google-api-core/latest/client_options.html
from google.cloud import translate_v3

def sample_get_supported_languages():
    # Create a client
    client = translate_v3.TranslationServiceClient()

    # Initialize request argument(s)
    request = translate_v3.GetSupportedLanguagesRequest(
        parent="parent_value",
    )

    # Make the request
    response = client.get_supported_languages(request=request)

    # Handle the response
    print(response)
参数
名称说明
request Union[google.cloud.translate_v3.types.GetSupportedLanguagesRequest, dict]

请求对象。发现受支持语言的请求消息。

parent str

必需。项目或位置以进行呼叫。必须引用调用者的项目。格式:projects/{project-number-or-id}projects/{project-number-or-id}/locations/{location-id}。对于全局调用,请使用 projects/{project-number-or-id}/locations/globalprojects/{project-number-or-id}。对于 AutoML 模型,必须使用非全球位置。只能使用同一区域(具有相同位置 ID)内的模型,否则会返回 INVALID_ARGUMENT (400) 错误。这对应于 request 实例上的 parent 字段;如果提供了 request,则不应设置此字段。

model str

可选。获取相应模型支持的语言。格式取决于模型类型:- AutoML Translation 模型:projects/{project-number-or-id}/locations/{location-id}/models/{model-id} - 常规(内置)模型:projects/{project-number-or-id}/locations/{location-id}/models/general/nmt,返回指定模型支持的语言。如果缺少,我们将获取 Google 通用 NMT 模型支持的语言。这对应于 request 实例上的 model 字段;如果提供了 request,则不应设置此字段。

display_language_code str

可选。用于返回受支持语言的本地化、可读名称的语言。如果缺少此参数,则不会在响应中返回显示名称。这对应于 request 实例上的 display_language_code 字段;如果提供了 request,则不应设置此字段。

retry google.api_core.retry.Retry

指定应重试哪些错误(如果有)。

timeout float

相应请求的超时时间。

metadata Sequence[Tuple[str, str]]

应随请求一起作为元数据发送的字符串。

返回结果
类型说明
google.cloud.translate_v3.types.SupportedLanguages发现受支持语言的响应消息。

glossary_path

glossary_path(project: str, location: str, glossary: str) -> str

返回完全限定的术语字符串。

list_glossaries

list_glossaries(
    request: typing.Optional[
        typing.Union[
            google.cloud.translate_v3.types.translation_service.ListGlossariesRequest,
            dict,
        ]
    ] = None,
    *,
    parent: typing.Optional[str] = None,
    retry: typing.Union[
        google.api_core.retry.Retry, google.api_core.gapic_v1.method._MethodDefault
    ] = _MethodDefault._DEFAULT_VALUE,
    timeout: typing.Union[float, object] = _MethodDefault._DEFAULT_VALUE,
    metadata: typing.Sequence[typing.Tuple[str, str]] = ()
) -> google.cloud.translate_v3.services.translation_service.pagers.ListGlossariesPager

列出项目中的术语表。如果项目不存在,则返回 NOT_FOUND。

# 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://googleapis.dev/python/google-api-core/latest/client_options.html
from google.cloud import translate_v3

def sample_list_glossaries():
    # Create a client
    client = translate_v3.TranslationServiceClient()

    # Initialize request argument(s)
    request = translate_v3.ListGlossariesRequest(
        parent="parent_value",
    )

    # Make the request
    page_result = client.list_glossaries(request=request)

    # Handle the response
    for response in page_result:
        print(response)
参数
名称说明
request Union[google.cloud.translate_v3.types.ListGlossariesRequest, dict]

请求对象。ListGlossaries 的请求消息。

parent str

必需。要列出所有术语表的项目的名称。这对应于 request 实例上的 parent 字段;如果提供了 request,则不应设置此字段。

retry google.api_core.retry.Retry

指定应重试哪些错误(如果有)。

timeout float

相应请求的超时时间。

metadata Sequence[Tuple[str, str]]

应随请求一起作为元数据发送的字符串。

返回结果
类型说明
google.cloud.translate_v3.services.translation_service.pagers.ListGlossariesPager针对 ListGlossaries 的响应消息。迭代此对象将生成结果并自动解析其他页面。

parse_glossary_path

parse_glossary_path(path: str) -> typing.Dict[str, str]

将词汇表路径解析为组成部分。

translate_document

translate_document(
    request: typing.Optional[
        typing.Union[
            google.cloud.translate_v3.types.translation_service.TranslateDocumentRequest,
            dict,
        ]
    ] = None,
    *,
    retry: typing.Union[
        google.api_core.retry.Retry, google.api_core.gapic_v1.method._MethodDefault
    ] = _MethodDefault._DEFAULT_VALUE,
    timeout: typing.Union[float, object] = _MethodDefault._DEFAULT_VALUE,
    metadata: typing.Sequence[typing.Tuple[str, str]] = ()
) -> google.cloud.translate_v3.types.translation_service.TranslateDocumentResponse

以同步模式翻译文档。

# 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://googleapis.dev/python/google-api-core/latest/client_options.html
from google.cloud import translate_v3

def sample_translate_document():
    # Create a client
    client = translate_v3.TranslationServiceClient()

    # Initialize request argument(s)
    document_input_config = translate_v3.DocumentInputConfig()
    document_input_config.content = b'content_blob'

    request = translate_v3.TranslateDocumentRequest(
        parent="parent_value",
        target_language_code="target_language_code_value",
        document_input_config=document_input_config,
    )

    # Make the request
    response = client.translate_document(request=request)

    # Handle the response
    print(response)
参数
名称说明
request Union[google.cloud.translate_v3.types.TranslateDocumentRequest, dict]

请求对象。文档翻译请求。

retry google.api_core.retry.Retry

指定应重试哪些错误(如果有)。

timeout float

相应请求的超时时间。

metadata Sequence[Tuple[str, str]]

应随请求一起作为元数据发送的字符串。

返回结果
类型说明
google.cloud.translate_v3.types.TranslateDocumentResponse翻译后的文档响应消息。

translate_text

translate_text(
    request: typing.Optional[
        typing.Union[
            google.cloud.translate_v3.types.translation_service.TranslateTextRequest,
            dict,
        ]
    ] = None,
    *,
    parent: typing.Optional[str] = None,
    target_language_code: typing.Optional[str] = None,
    contents: typing.Optional[typing.MutableSequence[str]] = None,
    model: typing.Optional[str] = None,
    mime_type: typing.Optional[str] = None,
    source_language_code: typing.Optional[str] = None,
    retry: typing.Union[
        google.api_core.retry.Retry, google.api_core.gapic_v1.method._MethodDefault
    ] = _MethodDefault._DEFAULT_VALUE,
    timeout: typing.Union[float, object] = _MethodDefault._DEFAULT_VALUE,
    metadata: typing.Sequence[typing.Tuple[str, str]] = ()
) -> google.cloud.translate_v3.types.translation_service.TranslateTextResponse

翻译输入文本并返回翻译后的文本。

# 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://googleapis.dev/python/google-api-core/latest/client_options.html
from google.cloud import translate_v3

def sample_translate_text():
    # Create a client
    client = translate_v3.TranslationServiceClient()

    # Initialize request argument(s)
    request = translate_v3.TranslateTextRequest(
        contents=['contents_value1', 'contents_value2'],
        target_language_code="target_language_code_value",
        parent="parent_value",
    )

    # Make the request
    response = client.translate_text(request=request)

    # Handle the response
    print(response)
参数
名称说明
request Union[google.cloud.translate_v3.types.TranslateTextRequest, dict]

请求对象。同步翻译的请求消息。

parent str

必需。项目或位置以进行呼叫。必须引用调用者的项目。格式:projects/{project-number-or-id}projects/{project-number-or-id}/locations/{location-id}。对于全局调用,请使用 projects/{project-number-or-id}/locations/globalprojects/{project-number-or-id}。对于使用 AutoML 模型或自定义术语库的请求,必须指定非全局位置。模型和词汇表必须位于同一区域(具有相同的位置 ID),否则系统会返回 INVALID_ARGUMENT (400) 错误。这对应于 request 实例上的 parent 字段;如果提供了 request,则不应设置此字段。

target_language_code str

必需。用于翻译输入文本的 ISO-639 语言代码,设置为“语言支持”中列出的语言代码之一。这对应于 request 实例上的 target_language_code 字段;如果提供了 request,则不应设置此字段。

contents MutableSequence[str]

必需。输入的内容(字符串格式)。 我们建议总内容少于 30,000 个码点。此字段的最大长度为 1024。对于较大的文本,请使用 BatchTranslateText。这对应于 request 实例上的 contents 字段;如果提供了 request,则不应设置此字段。

model str

可选。相应翻译所需的 model 类型。格式取决于模型类型:- AutoML Translation 模型:projects/{project-number-or-id}/locations/{location-id}/models/{model-id} - 通用(内置)模型:projects/{project-number-or-id}/locations/{location-id}/models/general/nmt,对于全球(非区域化)请求,请使用 location-id global。例如,projects/{project-number-or-id}/locations/global/models/general/nmt。如果未提供,则使用默认的 Google 模型 (NMT)。这对应于 request 实例上的 model 字段;如果提供了 request,则不应设置此字段。

mime_type str

可选。源文本的格式,例如“text/html”“text/plain”。如果留空,MIME 类型将默认为“text/html”。这对应于 request 实例上的 mime_type 字段;如果提供了 request,则不应设置此字段。

source_language_code str

可选。输入文本的 ISO-639 语言代码(如果已知),例如“en-US”或“sr-Latn”。如需查看支持的语言代码,请参阅“语言支持”。如果未指定源语言,API 会尝试自动识别源语言,并在响应中返回源语言。这对应于 request 实例上的 source_language_code 字段;如果提供了 request,则不应设置此字段。

retry google.api_core.retry.Retry

指定应重试哪些错误(如果有)。

timeout float

相应请求的超时时间。

metadata Sequence[Tuple[str, str]]

应随请求一起作为元数据发送的字符串。