智能识别代码与注释,仅翻译注释、文档字符串和 Markdown 文档,保持代码逻辑完全不变。支持 20+ 编程语言,精准解析各类注释语法和文档字符串格式,可按需控制字符串字面量是否翻译,适用于开源项目国际化、代码审查、技术文档本地化等场景。Intelligently separates code from comments — translates only comments, docstrings, and Markdown docs while preserving code logic. Supports 20+ programming languages with precise parsing of comment syntax and docstring formats. Optional string literal translation for open-source i18n, code review, and technical documentation localization.
快速概览
Quick Overview
属性
Attribute
说明
Description
翻译端点
Endpoint
POST /v1/code/translate
认证
Authentication
Bearer Token(Authorization 请求头)
Bearer Token in Authorization header
请求体
Request Body
application/json(JSON 格式)
application/json (JSON format)
代码上限
Code Limit
单次最多 50,000 字符
Max 50,000 characters per request
支持语言
Supported Languages
20+ 编程语言(Python/JS/TS/Java/Go/C++/Rust 等)
20+ programming languages (Python/JS/TS/Java/Go/C++/Rust etc.)
Preserve original indentation and alignment of comments, default true
请求示例
Request Examples
curl -X POST https://api.itranslator.cc/v1/code/translate \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"code": "# This function calculates the average\n# of a list of numbers\ndef calculate_average(numbers):\n \"\"\"Return the mean.\"\"\"\n if not numbers:\n return 0\n return sum(numbers) / len(numbers)",
"lang": "py",
"target_lang": "zh",
"translate_only": "all"
}'
import requests
code = """# This function calculates the average
# of a list of numbers
def calculate_average(numbers):
"""Return the mean."""
if not numbers:
return 0
return sum(numbers) / len(numbers)"""
payload = {"code": code, "lang": "py", "target_lang": "zh", "translate_only": "all"}
resp = requests.post(
"https://api.itranslator.cc/v1/code/translate",
headers={"Authorization": f"Bearer {token}"},
json=payload
)
print(resp.json())
const code = `# This function calculates the average
# of a list of numbers
def calculate_average(numbers):
"""Return the mean."""
if not numbers:
return 0
return sum(numbers) / len(numbers)`;
const resp = await fetch("https://api.itranslator.cc/v1/code/translate", {
method: "POST",
headers: { "Authorization": `Bearer ${token}`, "Content-Type": "application/json" },
body: JSON.stringify({ code, lang: "py", target_lang: "zh", translate_only: "all" })
});
console.log(await resp.json());
String code = "# This function calculates the average\n"
+ "# of a list of numbers\n"
+ "def calculate_average(numbers):\n"
+ " \"\"\"Return the mean.\"\"\"\n"
+ " if not numbers:\n"
+ " return 0\n"
+ " return sum(numbers) / len(numbers)";
OkHttpClient client = new OkHttpClient();
String json = String.format(
"{\"code\":\"%s\",\"lang\":\"py\",\"target_lang\":\"zh\",\"translate_only\":\"all\"}",
code.replace("\"", "\\\"").replace("\n", "\\n"));
RequestBody body = RequestBody.create(json, MediaType.parse("application/json"));
Request request = new Request.Builder()
.url("https://api.itranslator.cc/v1/code/translate")
.header("Authorization", "Bearer " + token)
.post(body)
.build();
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
func main() {
code := `// This function calculates the average
// of a list of numbers
func calculateAverage(numbers []float64) float64 {
// Return the mean
if len(numbers) == 0 {
return 0
}
sum := 0.0
for _, n := range numbers {
sum += n
}
return sum / float64(len(numbers))
}`
payload, _ := json.Marshal(map[string]interface{}{
"code": code,
"lang": "go",
"target_lang": "zh",
"translate_only": "all",
"preserve_formatting": true,
})
req, _ := http.NewRequest("POST",
"https://api.itranslator.cc/v1/code/translate",
bytes.NewBuffer(payload))
req.Header.Set("Authorization", "Bearer "+os.Getenv("API_TOKEN"))
req.Header.Set("Content-Type", "application/json")
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
}
响应字段说明
Response Fields
字段
Field
类型
Type
说明
Description
translated_code
string
翻译后的完整代码(含已翻译的注释和文档字符串)
Translated full code with translated comments and docstrings
original_lang
string
检测到的源注释语言
Detected source comment language
target_lang
string
目标自然语言
Target natural language
code_intact
boolean
代码逻辑是否完整保持不变(true 表示代码部分未被修改)
Whether code logic is fully preserved (true means code was not modified)
comments_translated
integer
已翻译的注释数量
Number of comments translated
docstrings_translated
integer
已翻译的文档字符串数量
Number of docstrings translated
strings_translated
integer
已翻译的字符串字面量数量(仅 translate_strings=true 时返回)
Number of string literals translated (only when translate_strings=true)
Translate Strings Carefully: translate_strings=true translates string literals, which may affect i18n frameworks (e.g. gettext), log parsing, and regex matching. Use only when safe.
Preserve Comment Formatting: Keep preserve_formatting=true by default; ensures translated comments match original indentation and alignment for code review.
大文件分段处理:超过 50,000 字符的文件按函数或模块分段调用,避免单次请求超限。
Chunk Large Files: For files over 50,000 chars, split by function or module to avoid exceeding the limit.
CI/CD 集成:将代码翻译集成到 CI 流程,自动将多语言注释推送到不同分支,保持文档同步更新。
CI/CD Integration: Integrate code translation into CI pipelines to auto-push multi-language comments to branches, keeping docs in sync.
指定源语言:混合语言注释时显式指定 source_lang,避免自动检测误判。
Specify Source Language: Set source_lang explicitly for mixed-language comments to avoid auto-detection errors.
应用场景
Use Cases
场景
Scenario
推荐配置
Recommended Config
说明
Notes
📦 开源项目国际化
📦 Open-Source i18n
translate_only=all
translate_only=all
将开源代码的英文注释翻译为多语言,方便全球开发者阅读理解
Translate English comments to multiple languages for global developers
📖 API 文档本地化
📖 API Doc Localization
translate_only=docstrings
translate_only=docstrings
翻译 JSDoc/JavaDoc/Python docstring,配合文档生成工具输出多语言 API 文档
Translate JSDoc/JavaDoc/docstrings; output multi-language API docs with doc generators
🔍 代码审查辅助
🔍 Code Review Aid
translate_only=comments
translate_only=comments
将外文注释翻译为母语,辅助理解和审查第三方代码
Translate foreign comments to native language to aid reviewing third-party code