Udostępnij przez


TextTranslationClient Class

Text translation is a cloud-based REST API feature of the Translator service that uses neural machine translation technology to enable quick and accurate source-to-target text translation in real time across all supported languages.

The following methods are supported by the Text Translation feature:

Languages. Returns a list of languages supported by Translate, Transliterate, and Dictionary Lookup operations.

Translate. Renders single source-language text to multiple target-language texts with a single request.

Transliterate. Converts characters or letters of a source language to the corresponding characters or letters of a target language.

Detect. Returns the source code language code and a boolean variable denoting whether the detected language is supported for text translation and transliteration.

Dictionary lookup. Returns equivalent words for the source term in the target language.

Dictionary example Returns grammatical structure and context examples for the source term and target term pair.

Combinations of endpoint and credential values: str + AzureKeyCredential - used custom domain translator endpoint str + AzureKeyCredential + Region - used for global translator endpoint str + TokenCredential - used for regional endpoint with token authentication str + None - used with text translation on-prem container None + AzureKeyCredential - used for global translator endpoint with global Translator resource None + TokenCredential - general translator endpoint with token authentication None + TokenCredential + Region - general translator endpoint with regional Translator resource

Constructor

TextTranslationClient(*, credential: TokenCredential, region: str | None = None, endpoint: str | None = None, resource_id: str | None = None, audience: str | None = None, api_version: str = '2025-10-01-preview', **kwargs)

Keyword-Only Parameters

Name Description
endpoint
str

Supported Text Translation endpoints (protocol and hostname, for example: https://api.cognitive.microsofttranslator.com). If not provided, global translator endpoint will be used.

credential
Union[<xref:AzureKeyCredential>, <xref:TokenCredential>]

Credential used to authenticate with the Translator service

region
str

Used for National Clouds.

resource_id
str

Used with both a TokenCredential combined with a region.

audience
str

Scopes of the credentials.

api_version
str

Default value is "2025-10-01-preview". Note that overriding this default value may result in unsupported behavior.

Methods

close
get_supported_languages

Gets the set of languages currently supported by other operations of the Translator.

Gets the set of languages currently supported by other operations of the Translator.

send_request

Runs the network request through the client's chained policies.


>>> from azure.core.rest import HttpRequest
>>> request = HttpRequest("GET", "https://www.example.org/")
<HttpRequest [GET], url: 'https://www.example.org/'>
>>> response = client.send_request(request)
<HttpResponse: 200 OK>

For more information on this code flow, see https://aka.ms/azsdk/dpcodegen/python/send_request

translate
transliterate

close

close() -> None

get_supported_languages

Gets the set of languages currently supported by other operations of the Translator.

Gets the set of languages currently supported by other operations of the Translator.

get_supported_languages(*, client_trace_id: str | None = None, scope: str | None = None, accept_language: str | None = None, etag: str | None = None, match_condition: MatchConditions | None = None, **kwargs: Any) -> GetSupportedLanguagesResult

Keyword-Only Parameters

Name Description
client_trace_id
str

A client-generated GUID to uniquely identify the request. Default value is None.

Default value: None
scope
str

A comma-separated list of names defining the group of languages to return. Allowed group names are: translation, transliteration and dictionary. If no scope is given, then all groups are returned, which is equivalent to passing scope=translation,transliteration,dictionary. To decide which set of supported languages is appropriate for your scenario, see the description of the <xref:response-body>. Default value is None.

Default value: None
accept_language
str

The language to use for user interface strings. Some of the fields in the response are names of languages or names of regions. Use this parameter to define the language in which these names are returned. The language is specified by providing a well-formed BCP 47 language tag. For instance, use the value fr to request names in French or use the value zh-Hant to request names in Chinese Traditional. Names are provided in the English language when a target language is not specified or when localization is not available. Default value is None.

Default value: None
etag
str

check if resource is changed. Set None to skip checking etag. Default value is None.

Default value: None
match_condition

The match condition to use upon the etag. Default value is None.

Default value: None

Returns

Type Description

GetSupportedLanguagesResult. The GetSupportedLanguagesResult is compatible with MutableMapping

Exceptions

Type Description

send_request

Runs the network request through the client's chained policies.


>>> from azure.core.rest import HttpRequest
>>> request = HttpRequest("GET", "https://www.example.org/")
<HttpRequest [GET], url: 'https://www.example.org/'>
>>> response = client.send_request(request)
<HttpResponse: 200 OK>

For more information on this code flow, see https://aka.ms/azsdk/dpcodegen/python/send_request

send_request(request: HttpRequest, *, stream: bool = False, **kwargs: Any) -> HttpResponse

Parameters

Name Description
request
Required

The network request you want to make. Required.

Keyword-Only Parameters

Name Description
stream

Whether the response payload will be streamed. Defaults to False.

Default value: False

Returns

Type Description

The response of your network call. Does not do error handling on your response.

translate

translate(body: List[str] | List[TranslateInputItem] | MutableMapping[str, Any] | IO[bytes], *, to_language: List[str] | None = None, from_language: str | None = None, client_trace_id: str | None = None, content_type: str = 'application/json', **kwargs: Any) -> List[TranslatedTextItem]

Parameters

Name Description
body
Required

Keyword-Only Parameters

Name Description
to_language
Default value: None
from_language
Default value: None
client_trace_id
Default value: None
content_type
Default value: application/json

transliterate

transliterate(body: List[str] | List[InputTextItem] | MutableMapping[str, Any] | IO[bytes], *, language: str, from_script: str, to_script: str, client_trace_id: str | None = None, content_type: str = 'application/json', **kwargs: Any) -> List[TransliteratedText]

Parameters

Name Description
body
Required

Keyword-Only Parameters

Name Description
language
Required
from_script
Required
to_script
Required
client_trace_id
Default value: None
content_type
Default value: application/json