Skip to content

translate_document async

translate_document(
    document: Document,
    source_language_code: LanguageCodeString,
    target_language_code: LanguageCodeString,
    *,
    config_overrides: Optional[AsyncTranslateClientConfig] = None,
    terminology_names: Optional[ResourceNameList] = None,
    settings: Optional[TranslationSettings] = None,
) -> TranslateDocumentResponse

Translates the input document from the source language to the target language. This synchronous operation supports text, HTML, or Word documents as the input document. TranslateDocument supports translations from English to any supported language, and from any supported language to English. Therefore, specify either the source language code or the target language code as “en” (English).

If you set the Formality parameter, the request will fail if the target language does not support formality. For a list of target languages that support formality, see Setting formality.

Parameters:

  • document (Document) –

    The content and content type for the document to be translated. The document size must not exceed 100 KB.

  • terminology_names (Optional[ResourceNameList], default: None ) –

    The name of a terminology list file to add to the translation job. This file provides source terms and the desired translation for each term. A terminology list can contain a maximum of 256 terms. You can use one custom terminology resource in your translation request.

    Use the ListTerminologies operation to get the available terminology lists.

    For more information about custom terminology lists, see Custom terminology.

  • source_language_code (LanguageCodeString) –

    The language code for the language of the source text. For a list of supported language codes, see Supported languages.

    To have Amazon Translate determine the source language of your text, you can specify auto in the SourceLanguageCode field. If you specify auto, Amazon Translate will call Amazon Comprehend to determine the source language.

    If you specify auto, you must send the TranslateDocument request in a region that supports Amazon Comprehend. Otherwise, the request returns an error indicating that autodetect is not supported.

  • target_language_code (LanguageCodeString) –

    The language code requested for the translated document. For a list of supported language codes, see Supported languages.

  • settings (Optional[TranslationSettings], default: None ) –

    Settings to configure your translation output. You can configure the following options:

    • Brevity: not supported.

    • Formality: sets the formality level of the output text.

    • Profanity: masks profane words and phrases in your translation output.

Raises:

  • InternalServerException –

    An internal server error occurred. Retry your request.

  • InvalidRequestException –

    The request that you made is not valid. Check your request to determine why it's not valid and then retry the request.

  • LimitExceededException –

    The specified limit has been exceeded. Review your request and retry it with a quantity below the stated limit.

  • ResourceNotFoundException –

    The resource you are looking for has not been found. Review the resource you're looking for and see if a different resource will accomplish your needs before retrying the revised request.

  • ServiceUnavailableException –

    The Amazon Translate service is temporarily unavailable. Wait a bit and then retry your request.

  • TooManyRequestsException –

    You have made too many requests within a short period of time. Wait for a short time and then try your request again.

  • UnsupportedLanguagePairException –

    Amazon Translate does not support translation from the language of the source text into the requested target language. For more information, see Supported languages.

  • UnknownServiceError –

    The service returned an error code this client does not model.

Source code in docs_src/capo_translate/_services/async_translate.py
async def translate_document(
    self,
    document: "capo_translate.types.document.Document",
    source_language_code: "capo_translate.types.language_code_string.LanguageCodeString",
    target_language_code: "capo_translate.types.language_code_string.LanguageCodeString",
    *,
    config_overrides: Optional[AsyncTranslateClientConfig] = None,
    terminology_names: Optional[
        "capo_translate.types.resource_name_list.ResourceNameList"
    ] = None,
    settings: Optional[
        "capo_translate.types.translation_settings.TranslationSettings"
    ] = None,
) -> "capo_translate.types.translate_document_response.TranslateDocumentResponse":
    """<p>Translates the input document from the source language to the target language. This synchronous operation supports text, HTML, or Word documents as the input document. <code>TranslateDocument</code> supports translations from English to any supported language, and from any supported language to English. Therefore, specify either the source language code or the target language code as “en” (English). </p> <p> If you set the <code>Formality</code> parameter, the request will fail if the target language does not support formality. For a list of target languages that support formality, see <a href="https://docs.aws.amazon.com/translate/latest/dg/customizing-translations-formality.html">Setting formality</a>. </p>

    Args:
        document: <p>The content and content type for the document to be translated. The document size must not exceed 100 KB.</p>
        terminology_names: <p>The name of a terminology list file to add to the translation job. This file provides source terms and the desired translation for each term. A terminology list can contain a maximum of 256 terms. You can use one custom terminology resource in your translation request.</p> <p>Use the <a>ListTerminologies</a> operation to get the available terminology lists.</p> <p>For more information about custom terminology lists, see <a href="https://docs.aws.amazon.com/translate/latest/dg/how-custom-terminology.html">Custom terminology</a>.</p>
        source_language_code: <p>The language code for the language of the source text. For a list of supported language codes, see <a href="https://docs.aws.amazon.com/translate/latest/dg/what-is-languages.html">Supported languages</a>.</p> <p>To have Amazon Translate determine the source language of your text, you can specify <code>auto</code> in the <code>SourceLanguageCode</code> field. If you specify <code>auto</code>, Amazon Translate will call <a href="https://docs.aws.amazon.com/comprehend/latest/dg/comprehend-general.html">Amazon Comprehend</a> to determine the source language.</p> <note> <p>If you specify <code>auto</code>, you must send the <code>TranslateDocument</code> request in a region that supports Amazon Comprehend. Otherwise, the request returns an error indicating that autodetect is not supported. </p> </note>
        target_language_code: <p>The language code requested for the translated document. For a list of supported language codes, see <a href="https://docs.aws.amazon.com/translate/latest/dg/what-is-languages.html">Supported languages</a>.</p>
        settings: <p>Settings to configure your translation output. You can configure the following options:</p> <ul> <li> <p>Brevity: not supported.</p> </li> <li> <p>Formality: sets the formality level of the output text.</p> </li> <li> <p>Profanity: masks profane words and phrases in your translation output.</p> </li> </ul>

    Raises:
        capo_translate.errors.internal_server_exception.InternalServerException: <p>An internal server error occurred. Retry your request.</p>
        capo_translate.errors.invalid_request_exception.InvalidRequestException: <p> The request that you made is not valid. Check your request to determine why it's not valid and then retry the request. </p>
        capo_translate.errors.limit_exceeded_exception.LimitExceededException: <p>The specified limit has been exceeded. Review your request and retry it with a quantity below the stated limit.</p>
        capo_translate.errors.resource_not_found_exception.ResourceNotFoundException: <p>The resource you are looking for has not been found. Review the resource you're looking for and see if a different resource will accomplish your needs before retrying the revised request.</p>
        capo_translate.errors.service_unavailable_exception.ServiceUnavailableException: <p>The Amazon Translate service is temporarily unavailable. Wait a bit and then retry your request.</p>
        capo_translate.errors.too_many_requests_exception.TooManyRequestsException: <p> You have made too many requests within a short period of time. Wait for a short time and then try your request again.</p>
        capo_translate.errors.unsupported_language_pair_exception.UnsupportedLanguagePairException: <p>Amazon Translate does not support translation from the language of the source text into the requested target language. For more information, see <a href="https://docs.aws.amazon.com/translate/latest/dg/what-is-languages.html">Supported languages</a>. </p>
        capo_translate.errors.UnknownServiceError: The service returned an error code this client does not model.
    """

    async def _handler(
        req: "AsyncOperationRequest[capo_translate.types.translate_document_request.TranslateDocumentRequest]",
    ) -> AsyncOperationResponse[
        "capo_translate.types.translate_document_response.TranslateDocumentResponse"
    ]:
        import capo_translate._operations.aws_shine_frontend_service_20170701.translate_document

        (
            output,
            http_response,
        ) = await capo_translate._operations.aws_shine_frontend_service_20170701.translate_document.async_translate_document(
            req.options, req.input
        )
        return AsyncOperationResponse(output=output, response=http_response)

    def _redact_input(
        input_: "capo_translate.types.translate_document_request.TranslateDocumentRequest",
    ) -> dict[str, Any]:
        import capo_translate.types.translate_document_request

        return capo_translate.types.translate_document_request.redact_sensitive(
            input_
        )

    def _redact_output(
        output: "capo_translate.types.translate_document_response.TranslateDocumentResponse",
    ) -> dict[str, Any]:
        import capo_translate.types.translate_document_response

        return capo_translate.types.translate_document_response.redact_sensitive(
            output
        )

    interceptors_, options_ = self.operation_options(
        config_overrides,
        OperationLogging(
            "TranslateDocument",
            redact_input=_redact_input,
            redact_output=_redact_output,
        ),
    )
    input_: capo_translate.types.translate_document_request.TranslateDocumentRequest = {
        "document": document,
        "source_language_code": source_language_code,
        "target_language_code": target_language_code,
    }
    if terminology_names is not None:
        input_["terminology_names"] = terminology_names
    if settings is not None:
        input_["settings"] = settings

    response = await aexecute_pipeline(
        AsyncOperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    await response.response.aclose()
    return response.output