文本翻译Text Translation

将文本从源语言翻译为目标语言,支持 49 种语言、自动语言检测、正式/非正式语气控制、术语库管理、HTML/XML 标签保留及上下文优化。Translate text from source to target language. Supports 49 languages, auto-detection, formality control, glossary management, HTML/XML tag preservation, and context enhancement.

请求端点

Endpoint

POST/v1/translate

认证

Authentication

需要 OAuth 2.0 Bearer Token。Requires OAuth 2.0 Bearer Token. 查看认证说明See Authentication

Authorization: Bearer {access_token}

请求头

Request Headers

HeaderHeaderValue必填Required说明Description
AuthorizationBearer YOUR_API_TOKENrequired认证凭据Auth credentials
Content-Typeapplication/jsonrequired请求体格式,必须为 JSONRequest body format; must be JSON
请求体大小上限 128 KiB,超过请使用文档翻译接口Request body size limit: 128 KiB. Use the Document Translation API for larger content.

请求参数

Request Parameters

参数Parameter类型Type必填Required说明Description
textstring | string[]required待翻译文本。单条传 string,批量传 string[](最多 50 条)。返回顺序与输入顺序一致。Text to translate. Single string or array (max 50). Response order matches input.
target_langstringrequired目标语言代码(ISO 639-1),如 enzhja。支持区域变体如 en-USpt-BRTarget language code (ISO 639-1), e.g. en, zh, ja. Supports regional variants.
source_langstringoptional源语言代码。省略时自动检测,响应中返回 detected_source_languageSource language code. If omitted, auto-detected; detected_source_language returned.
formalitystringoptional语气控制:default(默认)| prefer_more(倾向正式)| prefer_less(倾向非正式)。仅部分目标语言支持,参见语气控制Formality: default, prefer_more, prefer_less. Only for select target languages; see Formality.
glossary_idstringoptional术语库 ID,确保专业术语一致。须同时提供 source_langGlossary ID for consistent terminology. Requires source_lang.
contextstringoptional额外上下文信息,帮助引擎消除歧义。该文本不会被翻译,也不计入计费。Additional context to disambiguate meanings. This text is NOT translated and not billed.
split_sentencesstringoptional句子分割策略:0(不分割)| 1(按标点和换行分割,默认)| nonewlines(仅按标点)Sentence splitting: 0 (none), 1 (punctuation & newlines, default), nonewlines (punctuation only)
preserve_formattingbooleanoptional保留原文空格和换行格式,默认 false。对代码片段、诗歌建议设为 truePreserve original whitespace/formatting; default false. Set true for code snippets or poetry.
tag_handlingstringoptional标签处理:xml / html。保留输入中的标签在译文对应位置。参见标签处理Tag handling: xml / html. Preserves tags at correct positions in output. See Tag Handling.
ignore_tagsstring[]optional不翻译的标签列表,标签内文本保留原文。如 ["code","pre"]Tags whose content should NOT be translated, e.g. ["code","pre"].
show_billed_charactersbooleanoptional是否返回计费字符数,默认 falseReturn billed character count; default false.
model_typestringoptional翻译模型:quality_optimized(质量优先,默认)| latency_optimized(速度优先)Model: quality_optimized (default) or latency_optimized

快速开始

Quick Start

curl -X POST https://api.itranslator.cc/v1/translate \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "text": "人工智能正在改变世界。",
    "source_lang": "zh",
    "target_lang": "en"
  }'

批量翻译

Batch Translation

text 参数设为字符串数组,即可在一次请求中翻译多条文本。响应中的 translations 数组与输入顺序一一对应。

Set text to a string array to translate multiple texts in one request. The translations array maps 1:1 to input order.

curl -X POST https://api.itranslator.cc/v1/translate \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "text": ["你好", "谢谢", "再见"],
    "target_lang": "en"
  }'
{
  "translations": [
    { "detected_source_language": "zh", "text": "Hello" },
    { "detected_source_language": "zh", "text": "Thank you" },
    { "detected_source_language": "zh", "text": "Goodbye" }
  ],
  "billed_characters": 6
}
💡 单条文本 (string) 返回扁平结构;批量文本 (string[]) 返回 translations 数组。💡 Single text (string) returns flat structure; batch (string[]) returns translations array.

语气控制

通过 formality 参数控制译文正式程度。在不支持的语言中,prefer_more/prefer_less 会自动回退为 default,不会报错。

Control translation formality with formality. For unsupported languages, prefer_more/prefer_less silently fall back to default.

取值Value行为Behavior
default默认语气,不做干预Default tone, no intervention
prefer_more倾向正式语气(不支持时回退默认)Prefer formal tone (falls back if unsupported)
prefer_less倾向非正式语气(不支持时回退默认)Prefer informal tone (falls back if unsupported)

正式/非正式语气在以下目标语言中可用:中文、英语、日语、韩语、法语、德语、西班牙语、意大利语、葡萄牙语、荷兰语、俄语、波兰语

Formality is supported for target languages: Chinese, English, Japanese, Korean, French, German, Spanish, Italian, Portuguese, Dutch, Russian, Polish.

日语 formality 对比

Japanese Formality Comparison

语气Tone译文Translation适用场景Scenario
prefer_more資料をご確認いただけますでしょうか。商务邮件、正式文档Business email, formal docs
prefer_less資料、確認してもらえる?聊天、社交媒体Chat, social media

HTML/XML 标签处理

设置 tag_handlinghtmlxml 后,API 会识别输入中的标签结构,将标签放到译文正确位置,仅翻译标签外的文本。

With tag_handling set to html or xml, the API recognizes tag structures, places tags correctly in the translation, and only translates text outside tags.

HTML 翻译

HTML Translation

curl -X POST https://api.itranslator.cc/v1/translate \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "text": "<p>Welcome to <strong>iTranslator</strong></p>",
    "source_lang": "en",
    "target_lang": "zh",
    "tag_handling": "html"
  }'
{
  "translated_text": "<p>欢迎使用<strong>iTranslator</strong></p>",
  "source_lang": "en",
  "target_lang": "zh",
  "char_count": 15
}

忽略指定标签

Ignore Specific Tags

ignore_tags 指定不翻译的标签,标签内文本保留原文,适用于代码块、变量名等场景。

ignore_tags specifies tags whose content should not be translated. Useful for code blocks, variable names, etc.

curl -X POST https://api.itranslator.cc/v1/translate \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "text": "<p>Run <code>npm install</code> to start.</p>",
    "source_lang": "en",
    "target_lang": "zh",
    "tag_handling": "html",
    "ignore_tags": ["code"]
  }'
{
  "translated_text": "<p>运行 <code>npm install</code> 开始。</p>",
  "source_lang": "en",
  "target_lang": "zh",
  "char_count": 15
}

上下文增强

Context Enhancement

context 参数传入额外上下文,帮助消除歧义、选择更准确的译法。适合短文本、一词多义的场景。

The context parameter provides additional context to disambiguate meanings and improve translation accuracy. Ideal for short texts or polysemous terms.

curl -X POST https://api.itranslator.cc/v1/translate \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "text": "bank",
    "source_lang": "en",
    "target_lang": "zh",
    "context": "The river flows past the bank where people sit and fish."
  }'
{
  "translated_text": "河岸",
  "detected_source_language": "en",
  "target_lang": "zh",
  "char_count": 4
}
💡 没有上下文时 "bank" 可能被译为"银行"。context 本身不会被翻译,也不消耗配额。💡 Without context, "bank" might be translated as "银行" (financial institution). context itself is not translated and costs no quota.

响应字段

Response Fields

字段Field类型Type说明Description
translated_textstring翻译结果(单条文本时返回)Translated text (single-text input)
translationsobject[]翻译结果数组(批量文本时返回),每项含 textdetected_source_languageTranslation array (batch input); each item has text and detected_source_language
source_langstring源语言代码(指定时原样返回)Source language code (returned as-is if specified)
detected_source_languagestring自动检测的源语言代码(未指定 source_lang 时返回)Auto-detected source language (returned when source_lang omitted)
target_langstring目标语言代码Target language code
char_countinteger输入文本字符数(不含空格和标点)Input character count (excl. spaces and punctuation)
billed_charactersinteger计费字符数(需 show_billed_characters=trueBilled characters (requires show_billed_characters=true)

响应示例

Response Examples

基础翻译(指定源语言)

Basic (source specified)

{
  "translated_text": "Artificial intelligence is changing the world.",
  "source_lang": "zh",
  "target_lang": "en",
  "char_count": 10
}

自动检测源语言

Auto-detect Source

{
  "translated_text": "Guten Morgen",
  "detected_source_language": "en",
  "target_lang": "de",
  "char_count": 12
}

术语库 + 语气控制

Glossary + Formality

{
  "translated_text": "当社は、お客様情報の保護に細心の注意を払っております。",
  "source_lang": "zh",
  "target_lang": "ja",
  "char_count": 14,
  "formality": "prefer_more",
  "glossary_id": "a3f8c2e1-4d56-7890-abcd-ef1234567890"
}

错误码

Error Codes

HTTP Code错误码Code说明Description
2000成功Success
400INVALID_PARAM缺少必填参数(texttarget_lang)或格式错误Missing required param (text or target_lang) or invalid format
400TEXT_TOO_LONG文本超过字符上限Text exceeds character limit
400UNSUPPORTED_LANG源语言或目标语言不支持Source or target language not supported
400GLOSSARY_REQUIRES_SOURCEglossary_id 要求同时提供 source_langglossary_id requires source_lang
401UNAUTHORIZEDToken 无效或已过期Invalid or expired token
403FORBIDDEN无权限访问Access denied
413BODY_TOO_LARGE请求体超过 128 KiBRequest body exceeds 128 KiB
429RATE_LIMITED请求频率超限Rate limit exceeded
456QUOTA_EXHAUSTED翻译配额用尽Translation quota exhausted
500INTERNAL_ERROR服务器内部错误Internal server error

使用说明

Notes

📌 最佳实践📌 Best Practices
  • 自定源语言比自动检测更快,也避免歧义语言被误判。
  • Specifying source_lang is faster than auto-detection and avoids misidentification.
  • 批量翻译将多条文本合并为一次请求可减少网络开销,提高吞吐量。
  • Batch translation reduces network overhead and improves throughput.
  • 翻译含 HTML/XML 的内容时启用 tag_handling,避免标签被错误翻译或丢失。
  • Enable tag_handling when translating HTML/XML to prevent tag corruption.
  • 生产环境建议在服务端调用 API,避免将 Token 暴露给客户端。
  • In production, call the API server-side to avoid exposing your Token to the client.
  • 翻译计费基于输入文本字符数,空格和标点不计入。
  • Billing is based on input character count; spaces and punctuation are excluded.
  • 自动检测语言(省略 source_lang)会增加约 50ms 延迟。
  • Auto-detection (omitting source_lang) adds approximately 50ms latency.