> ## Documentation Index
> Fetch the complete documentation index at: https://docs.xhuoapi.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Localization Translate API Integration and Usage

> Localization translation 集成指南 - XHuoAPI

The main function of the Localization Translate API is to obtain translated text by inputting the text that needs to be translated, while the target language for translation can be customized, and the translation results can be output in two mainstream formats: `json` and `markdown`.

This document will provide detailed instructions on integrating the Translate API, helping you easily integrate and fully utilize the powerful features of this API. With the Translate API, you can easily translate the input text into a specific language and support specific methods for outputting the translation results.

## Application Process

To use the Localization Translate API, you need to first apply for the corresponding service on the application page [Localization Translate API](https://api.xhuoapi.ai/documents/6dcc60d0-a973-4ad2-92d8-11f4d3775532). After entering the page, click the "Acquire" button, as shown in the image:

![Application Page](https://cdn.xhuoapi.ai/rci31i.png)

If you are not logged in or registered, you will be automatically redirected to the [login page](https://api.xhuoapi.ai) inviting you to register and log in. After logging in or registering, you will be automatically returned to the current page.

There is a free quota available for first-time applicants, allowing you to use the API for free.

## Request Example

We will take an English input as an example to demonstrate how to use the API. Suppose the English description is: `# Title 1\n\nThis is a paragraph.\n\n## Title 2\n\nThis is another paragraph.`, and we will demonstrate how to upload the English description and obtain the Chinese translation result, while displaying it in markdown format.

### Setting Request Headers and Request Body

**Request Headers** include:

* `accept`: Specifies that the response result should be in JSON format, set to `application/json`.
* `authorization`: The key to call the API, which can be selected directly after application.

**Request Body** includes:

* `input`: The text that needs to be translated.
* `locale`: The language for the translation.
* `extension`: The format for displaying the translation result.
* `model`: The large model used for translation, defaulting to gpt-3.5.

Set as shown in the image below:

<p>
  <img src="https://cdn.xhuoapi.ai/d4uqst.png" width="500" className="m-auto" />
</p>

### Code Example

You can see that various language codes have been automatically generated on the right side of the page, as shown in the image:

<p>
  <img src="https://cdn.xhuoapi.ai/tn5nno.png" width="500" className="m-auto" />
</p>

Some code examples are as follows:

#### CURL

```bash theme={null}
curl -X POST 'https://api.xhuoapi.ai/v1/localization/translate' \
-H 'accept: application/json' \
-H 'authorization: Bearer {token}' \
-H 'content-type: application/json' \
-d '{
  "input": "# Title 1\n\nThis is a paragraph.\n\n## Title 2\n\nThis is another paragraph.",
  "locale": "zh-CN",
  "extension": "md"
}'
```

#### Python

```python theme={null}
import requests

url = "https://api.xhuoapi.ai/v1/localization/translate"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "input": "# Title 1\n\nThis is a paragraph.\n\n## Title 2\n\nThis is another paragraph.",
    "locale": "zh-CN",
    "extension": "md"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)
```

### Response Example

Upon a successful request, the API will return the result information of the translation task. For example:

```json theme={null}
{
  "data": "# 标题 1\n\n这是一个段落。\n\n## 标题 2\n\n这是另一个段落。",
  "usage": {
    "prompt_tokens": 172,
    "completion_tokens": 25
  },
  "model": "gpt-3.5-turbo-16k",
  "locale": "zh-CN"
}
```

You can see that the result contains a `data` field, which includes the translated Chinese text, and is returned in markdown format. Other information is as follows:

* `data`: The result of the translation task.
* `model`: The large language model used for the translation task.
* `locale`: The type of translation language for the translation task.

## Error Handling

When calling the API, if an error occurs, the API will return the corresponding error code and message. For example:

* `400 token_mismatched`: Bad request, possibly due to missing or invalid parameters.
* `400 api_not_implemented`: Bad request, possibly due to missing or invalid parameters.
* `401 invalid_token`: Unauthorized, invalid or missing authorization token.
* `429 too_many_requests`: Too many requests, you have exceeded the rate limit.
* `500 api_error`: Internal server error, something went wrong on the server.

### Error Response Example

```json theme={null}
{
  "success": false,
  "error": {
    "code": "api_error",
    "message": "fetch failed"
  },
  "trace_id": "2cf86e86-22a4-46e1-ac2f-032c0f2a4e89"
}
```

## Conclusion

Through this document, you have learned how to easily use the Localization Translate API to translate input text into a specific language and support specific methods for outputting translation results. We hope this document helps you better integrate and use the API. If you have any questions, please feel free to contact our technical support team.
