> ## 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 的對接和使用

> Localization translation 集成指南 - XHuoAPI

Localization Translate API 的主要功能是透過輸入需要翻譯的文本來獲取翻譯後的文本，同時翻譯後的語言可以自定義，並且翻譯結果可以採用 `json` ， `markdown` 兩種主流的方法來輸出。

本文檔將詳細介紹 Translate API 的對接說明，幫助您輕鬆集成並充分利用該 API 的強大功能。透過 Translate API ，您可以輕鬆實現將輸入的文本翻譯為特定語言，並且支持特定的方式來輸出翻譯結果。

## 申請流程

要使用 Localization Translate API，需要先到 申請頁面 [Localization Translate API ](https://api.xhuoapi.ai/documents/6dcc60d0-a973-4ad2-92d8-11f4d3775532)申請相應的服務，進入頁面之後，點擊「Acquire」按鈕，如圖所示：

![申請頁面](https://cdn.xhuoapi.ai/rci31i.png)

如果您尚未登錄或註冊，會自動跳轉到[登錄頁面](https://api.xhuoapi.ai)邀請您來註冊和登錄，登錄註冊之後會自動返回當前頁面。

首次申請時會有免費額度贈送，可以免費使用該 API。

## 請求示例

我們以一個英文輸入為例，演示如何使用該 API。假設英文描述詞為：`# Title 1\n\nThis is a paragraph.\n\n## Title 2\n\nThis is another paragraph.`，接下來演示如何上傳英文描述詞並獲取中文的翻譯結果，同時以 markdown 的形式展示出來。

### 設置請求頭和請求體

**Request Headers** 包括：

* `accept`：指定接收 JSON 格式的響應結果，這裡填寫為 `application/json`。
* `authorization`：調用 API 的密鑰，申請之後可以直接下拉選擇。

**Request Body** 包括：

* `input`：上傳的所需翻譯的文本。
* `locale`：自定義翻譯的語言。
* `extension`：自定義翻譯結果的展示形式。
* `model`：翻譯採用的大模型，默認是 gpt-3.5。

設置如下圖所示：

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

### 代碼示例

可以發現，在頁面右側已經自動生成了各種語言的代碼，如圖所示：

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

部分代碼示例如下：

#### 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)
```

### 響應示例

請求成功後，API 將返回此處翻譯任務的結果信息。例如：

```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"
}
```

可以看到，結果中有一個 `data` 字段，裡面包含了翻譯後的中文，同時採用了 markdown 的形式返回，其它信息如下所示：

* `data`，此處翻譯任務的結果。
* `model`，此處翻譯任務採用的大語言模型。
* `locale`，此處翻譯任務的翻譯語言類型。

## 錯誤處理

在調用 API 時，如果遇到錯誤，API 會返回相應的錯誤代碼和信息。例如：

* `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.

### 錯誤響應示例

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

## 結論

透過本文檔，您已經了解了如何使用 Localization Translate API 輕鬆實現將輸入的文本翻譯為特定語言，並且支持特定的方式來輸出翻譯結果。希望本文檔能幫助您更好地對接和使用該 API。如有任何問題，請隨時聯繫我們的技術支持團隊。
