Text Translation

Introduction

This API is used to translate text, which is to convert text from the source language to text in the target language.

The text translation API belongs to MT APIs. Currently, MT is in commercial use. Therefore, calling MT APIs will incur fees. You can learn about the pricing details in NLP Price Calculator.

For details about text translation endpoints, see Endpoints.

URI

  • URI format
    POST /v1/{project_id}/machine-translation/text-translation
  • Parameter description
    Table 1 URI parameters

    Parameter

    Mandatory

    Description

    project_id

    Yes

    Project ID. For details about how to obtain the project ID, see Obtaining a Project ID.

Request

Table 2 describes the request parameters.
Table 2 Request parameters

Parameter

Type

Mandatory

Description

text

String

Yes

It is encoded using UTF-8. The text length cannot exceed 2,000 characters. Chinese characters, letters, and punctuation marks are all characters.

from

String

Yes

Source language to be translated. For details about specific values, see the following table.

to

String

Yes

Target language that is translated into. For details about its values, see the following table.

scene

String

No

The default value is common. Currently, only common scenarios are supported.

Table 3 Supported languages

Language

Description

zh

Chinese

en

English

ja

Japanese

ru

Russian

auto

The system automatically detects the input language and translates it to the target language. You need to specify the target language.

Response

Table 4 describes the response parameters.
Table 4 Response parameters

Parameter

Type

Description

src_text

String

Translation of the source text when the API call succeeds. It is encoded using UTF-8.

This parameter is not included when the API fails to be called.

translated_text

String

Calling result when the API call succeeds. It is encoded using UTF-8

This parameter is not included when the API fails to be called.

from

String

Source language when the API call succeeds (if the source language is set to auto, the language detection result is displayed). It is encoded using UTF-8.

This parameter is not included when the API fails to be called.

to

String

Target language when the API call succeeds. It is encoded using UTF-8.

This parameter is not included when the API fails to be called.

error_code

String

Error code when the API call fails. For details, see Error Code.

The parameter is not included when the API call succeeds.

error_msg

String

Error message returned when the API fails to be called.

The parameter is not included when the API call succeeds.

Examples

  • Example request
    POST https://{endpoint}/v1/{project_id}/machine-translation/text-translation
       
    Request Header:  
        Content-Type:application/json
        X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG...   
    
    Request Body:
        {
            "text": "Text to translate",
            "from": "zh",
            "to": "en",
            "scene":"common"
        }  
  • Successful example response
    {
        "src_text": "Text to translate",
        "translated_text": "Welcome to use machine translation services",
        "from": "zh",
        "to": "en"
    }
  • Failed example response
    {
        "error_code": "NLP.0101",
        "error_msg": "Authentication failed. Verify the token."
    }

Status Code

For details about status codes, see Status Code.

Error Code

For details about error codes, see Error Code.