Skip to content

translate_text

translate_text(
    text: BoundedLengthString,
    source_language_code: LanguageCodeString,
    target_language_code: LanguageCodeString,
    *,
    config_overrides: Optional[TranslateClientConfig] = None,
    terminology_names: Optional[ResourceNameList] = None,
    settings: Optional[TranslationSettings] = None,
) -> TranslateTextResponse

Translates input text from the source language to the target language. For a list of available languages and language codes, see Supported languages.

Parameters:

  • text (BoundedLengthString) –

    The text to translate. The text string can be a maximum of 10,000 bytes long. Depending on your character set, this may be fewer than 10,000 characters.

  • 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 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 TranslateText 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 language of the target text. For a list of language codes, see Supported languages.

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

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

    • Brevity: reduces the length of the translated output for most translations.

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

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

Raises:

  • DetectedLanguageLowConfidenceException –

    The confidence that Amazon Comprehend accurately detected the source language is low. If a low confidence level is acceptable for your application, you can use the language in the exception to call Amazon Translate again. For more information, see the DetectDominantLanguage operation in the Amazon Comprehend Developer Guide.

  • 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.

  • 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.

  • TextSizeLimitExceededException –

    The size of the text you submitted exceeds the size limit. Reduce the size of the text or use a smaller document 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/translate.py
def translate_text(
    self,
    text: "capo_translate.types.bounded_length_string.BoundedLengthString",
    source_language_code: "capo_translate.types.language_code_string.LanguageCodeString",
    target_language_code: "capo_translate.types.language_code_string.LanguageCodeString",
    *,
    config_overrides: Optional[TranslateClientConfig] = 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_text_response.TranslateTextResponse":
    """<p>Translates input text from the source language to the target language. For a list of available languages and language codes, see <a href="https://docs.aws.amazon.com/translate/latest/dg/what-is-languages.html">Supported languages</a>.</p>

    Args:
        text: <p>The text to translate. The text string can be a maximum of 10,000 bytes long. Depending on your character set, this may be fewer than 10,000 characters.</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 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>TranslateText</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 language of the target text. For a list of 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: reduces the length of the translated output for most translations.</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.detected_language_low_confidence_exception.DetectedLanguageLowConfidenceException: <p>The confidence that Amazon Comprehend accurately detected the source language is low. If a low confidence level is acceptable for your application, you can use the language in the exception to call Amazon Translate again. For more information, see the <a href="https://docs.aws.amazon.com/comprehend/latest/dg/API_DetectDominantLanguage.html">DetectDominantLanguage</a> operation in the <i>Amazon Comprehend Developer Guide</i>. </p>
        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.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.text_size_limit_exceeded_exception.TextSizeLimitExceededException: <p> The size of the text you submitted exceeds the size limit. Reduce the size of the text or use a smaller document 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.
    """

    def _handler(
        req: "OperationRequest[capo_translate.types.translate_text_request.TranslateTextRequest]",
    ) -> OperationResponse[
        "capo_translate.types.translate_text_response.TranslateTextResponse"
    ]:
        import capo_translate._operations.aws_shine_frontend_service_20170701.translate_text

        output, http_response = (
            capo_translate._operations.aws_shine_frontend_service_20170701.translate_text.translate_text(
                req.options, req.input
            )
        )
        return OperationResponse(output=output, response=http_response)

    interceptors_, options_ = self.operation_options(
        config_overrides, OperationLogging("TranslateText")
    )
    input_: capo_translate.types.translate_text_request.TranslateTextRequest = {
        "text": text,
        "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 = execute_pipeline(
        OperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    response.response.close()
    return response.output