翻译器 3.0:TranslateTranslator 3.0: Translate

翻译文本。Translates text.

请求 URLRequest URL

POST 请求发送到:Send a POST request to:

https://api.translator.azure.cn/translate?api-version=3.0

请求参数Request parameters

查询字符串上传递的请求参数如下:Request parameters passed on the query string are:

必需的参数Required parameters

查询参数Query parameter 说明Description
api-versionapi-version 必需参数。Required parameter.
客户端所请求的 API 的版本。Version of the API requested by the client. 值必须是 3.0Value must be 3.0.
toto 必需参数。Required parameter.
指定输出文本的语言。Specifies the language of the output text. 目标语言必须是 translation 范围中包含的支持的语言之一。The target language must be one of the supported languages included in the translation scope. 例如,若要翻译为德语,请使用 to=deFor example, use to=de to translate to German.
可以在查询字符串中重复使用此参数,这样就可以同时翻译为多种语言。It's possible to translate to multiple languages simultaneously by repeating the parameter in the query string. 例如,若要翻译为德语和意大利语,请使用 to=de&to=itFor example, use to=de&to=it to translate to German and Italian.

可选参数Optional parameters

查询参数Query parameter 说明Description
fromfrom 可选参数。Optional parameter.
指定输入文本的语言。Specifies the language of the input text. 可以使用 translation 范围来查找支持的语言,了解哪些语言可以翻译。Find which languages are available to translate from by looking up supported languages using the translation scope. 如果未指定 from 参数,则会应用自动语言检测来确定源语言。If the from parameter is not specified, automatic language detection is applied to determine the source language.

使用动态字典功能时,必须使用 from 参数而不是自动检测。You must use the from parameter rather than autodetection when using the dynamic dictionary feature.
textTypetextType 可选参数。Optional parameter.
定义要翻译的文本是纯文本还是 HTML 文本。Defines whether the text being translated is plain text or HTML text. HTML 必须是格式正确的完整元素。Any HTML needs to be a well-formed, complete element. 可能的值为 plain(默认)或Possible values are: plain (default) or html上获取。.
categorycategory 可选参数。Optional parameter.
一个字符串,指定翻译的类别(领域)。A string specifying the category (domain) of the translation. 默认值为 generalDefault value is: general.
profanityActionprofanityAction 可选参数。Optional parameter.
指定在翻译时应如何处理不雅内容。Specifies how profanities should be treated in translations. 可能的值为 NoAction(默认)MarkedDeletedPossible values are: NoAction (default), Marked or Deleted. 若要了解处理不雅内容的方式,请参阅处理不雅内容To understand ways to treat profanity, see Profanity handling.
profanityMarkerprofanityMarker 可选参数。Optional parameter.
指定在翻译时应如何标记不雅内容。Specifies how profanities should be marked in translations. 可能的值为 Asterisk(默认)TagPossible values are: Asterisk (default) or Tag. 若要了解处理不雅内容的方式,请参阅处理不雅内容To understand ways to treat profanity, see Profanity handling.
includeAlignmentincludeAlignment 可选参数。Optional parameter.
指定是否包括从源文本到翻译文本的比对投射。Specifies whether to include alignment projection from source text to translated text. 可能的值为 truefalse(默认)。Possible values are: true or false (default).
includeSentenceLengthincludeSentenceLength 可选参数。Optional parameter.
指定是否包括输入文本和翻译文本的句子边界。Specifies whether to include sentence boundaries for the input text and the translated text. 可能的值为 truefalse(默认)。Possible values are: true or false (default).
suggestedFromsuggestedFrom 可选参数。Optional parameter.
在输入文本的语言无法确定的情况下,指定一种回退语言。Specifies a fallback language if the language of the input text can't be identified. 在省略 from 参数的情况下,应用语言自动检测功能。Language auto-detection is applied when the from parameter is omitted. 如果检测失败,则采用 suggestedFrom 语言。If detection fails, the suggestedFrom language will be assumed.
fromScriptfromScript 可选参数。Optional parameter.
指定输入文本的脚本。Specifies the script of the input text.
toScripttoScript 可选参数。Optional parameter.
指定翻译文本的脚本。Specifies the script of the translated text.
allowFallbackallowFallback 可选参数。Optional parameter.
指定当自定义系统不存在时允许服务回退到一个常规系统。Specifies that the service is allowed to fallback to a general system when a custom system does not exist. 可能的值为 true(默认)falsePossible values are: true (default) or false.

allowFallback=false 指定翻译只应使用针对 category(由请求指定)训练的系统。allowFallback=false specifies that the translation should only use systems trained for the category specified by the request. 如果将语言 X 翻译成语言 Y 需要通过枢轴语言 E 进行链接,那么此链中的所有系统(X->E 和 E->Y)需要进行自定义并且需要具有相同的类别。If a translation for language X to language Y requires chaining through a pivot language E, then all the systems in the chain (X->E and E->Y) will need to be custom and have the same category. 如果未通过特定类别找到任何系统,此请求会返回 400 状态代码。If no system is found with the specific category, the request will return a 400 status code. allowFallback=true 指定当自定义系统不存在时允许服务回退到一个常规系统。allowFallback=true specifies that the service is allowed to fallback to a general system when a custom system does not exist.

请求标头包括:Request headers include:

头文件Headers 说明Description
身份验证标头Authentication header(s) 必需的请求标头。Required request header.
请参阅用于身份验证的可用选项See available options for authentication.
Content-TypeContent-Type 必需的请求标头。Required request header.
指定有效负载的内容类型。Specifies the content type of the payload.
接受的值为:application/json; charset=UTF-8Accepted value is application/json; charset=UTF-8.
Content-LengthContent-Length 必需的请求标头。Required request header.
请求正文的长度。The length of the request body.
X-ClientTraceIdX-ClientTraceId 可选。Optional.
客户端生成的 GUID,用于唯一标识请求。A client-generated GUID to uniquely identify the request. 如果在查询字符串中使用名为 ClientTraceId 的查询参数包括了跟踪 ID,则可以省略此标头。You can omit this header if you include the trace ID in the query string using a query parameter named ClientTraceId.

请求正文Request body

请求的正文是一个 JSON 数组。The body of the request is a JSON array. 每个数组元素都是一个 JSON 对象,具有一个名为 Text 的字符串属性,该属性表示要翻译的字符串。Each array element is a JSON object with a string property named Text, which represents the string to translate.

[
    {"Text":"I would really like to drive your car around the block a few times."}
]

以下限制适用:The following limitations apply:

  • 数组最多可具有 100 个元素。The array can have at most 100 elements.
  • 包括空格在内,请求中包含的整个文本不能超过 5,000 个字符。The entire text included in the request cannot exceed 5,000 characters including spaces.

响应正文Response body

成功的响应是一个 JSON 数组,其中的每个结果对应于输入数组中的一个字符串。A successful response is a JSON array with one result for each string in the input array. 结果对象包括以下属性:A result object includes the following properties:

  • detectedLanguage:一个对象,它通过以下属性描述检测到的语言:detectedLanguage: An object describing the detected language through the following properties:

    • language:一个字符串,表示检测到的语言的代码。language: A string representing the code of the detected language.

    • score:一个浮点值,表示结果的置信度。score: A float value indicating the confidence in the result. 分数介于 0 和 1 之间,较低的分数表示较低的置信度。The score is between zero and one and a low score indicates a low confidence.

    当请求了语言自动检测时,detectedLanguage 属性仅存在于结果对象中。The detectedLanguage property is only present in the result object when language auto-detection is requested.

  • translations:翻译结果的数组。translations: An array of translation results. 数组的大小与通过 to 查询参数指定的目标语言的数目匹配。The size of the array matches the number of target languages specified through the to query parameter. 数组中的每个元素包括:Each element in the array includes:

    • to:一个字符串,表示目标语言的语言代码。to: A string representing the language code of the target language.

    • text:一个字符串,提供翻译的文本。text: A string giving the translated text.

    • transliteration:一个对象,在 toScript 参数指定的脚本中提供翻译的文本。transliteration: An object giving the translated text in the script specified by the toScript parameter.

      • script:一个字符串,指定目标脚本。script: A string specifying the target script.

      • text:一个字符串,在目标脚本中提供翻译的文本。text: A string giving the translated text in the target script.

    如果不进行音译,则不包括 transliteration 对象。The transliteration object is not included if transliteration does not take place.

    • alignment:一个对象,包含的名为 proj 的单个字符串属性可以将输入文本映射到翻译文本。alignment: An object with a single string property named proj, which maps input text to translated text. 只有在请求参数 includeAlignmenttrue 时,才提供比对信息。The alignment information is only provided when the request parameter includeAlignment is true. 将以 [[SourceTextStartIndex]:[SourceTextEndIndex]–[TgtTextStartIndex]:[TgtTextEndIndex]] 格式的字符串值返回比对内容。Alignment is returned as a string value of the following format: [[SourceTextStartIndex]:[SourceTextEndIndex]–[TgtTextStartIndex]:[TgtTextEndIndex]]. 冒号分隔开始和结束索引,连字符分隔语言,空格分隔单词。The colon separates start and end index, the dash separates the languages, and space separates the words. 一个单词可能与另一种语言中的 0 个、1 个或多个单词比对,而比对的词可能是不连续的。One word may align with zero, one, or multiple words in the other language, and the aligned words may be non-contiguous. 当没有可用的比对信息时,Alignment 元素会为空。When no alignment information is available, the alignment element will be empty. 请参阅获取比对信息,了解示例和限制。See Obtain alignment information for an example and restrictions.

    • sentLen:一个对象,返回输入和输出文本中的句子边界。sentLen: An object returning sentence boundaries in the input and output texts.

      • srcSentLen:一个整数数组,表示输入文本中句子的长度。srcSentLen: An integer array representing the lengths of the sentences in the input text. 数组的长度是句子的数量,而各个值是每个句子的长度。The length of the array is the number of sentences, and the values are the length of each sentence.

      • transSentLen:一个整数数组,表示翻译文本中句子的长度。transSentLen: An integer array representing the lengths of the sentences in the translated text. 数组的长度是句子的数量,而各个值是每个句子的长度。The length of the array is the number of sentences, and the values are the length of each sentence.

    只有在请求参数 includeSentenceLengthtrue 时,才包括句子边界。Sentence boundaries are only included when the request parameter includeSentenceLength is true.

  • sourceText:一个对象,包含的名为 text 的单个字符串属性在源语言的默认脚本中提供输入文本。sourceText: An object with a single string property named text, which gives the input text in the default script of the source language. sourceText 属性存在的前提是,表述输入时采用的脚本不是该语言的通用脚本。sourceText property is present only when the input is expressed in a script that's not the usual script for the language. 例如,如果输入是采用拉丁语脚本编写的阿拉伯语,则 sourceText.text 就是转换为阿拉伯脚本的同一阿拉伯文本。For example, if the input were Arabic written in Latin script, then sourceText.text would be the same Arabic text converted into Arab script.

示例部分提供了 JSON 响应的示例。Example of JSON responses are provided in the examples section.

响应标头Response headers

头文件Headers 说明Description
X-RequestIdX-RequestId 服务生成的用于标识请求的值。Value generated by the service to identify the request. 它用于故障排除目的。It is used for troubleshooting purposes.
X-MT-SystemX-MT-System 请求翻译时,对于每种“目标”语言,指定用于翻译的系统类型。Specifies the system type that was used for translation for each ‘to’ language requested for translation. 此值是以逗号分隔的字符串列表。The value is a comma-separated list of strings. 每个字符串指示一个类型:Each string indicates a type:
  • 自定义 - 请求包括一个自定义系统。在翻译期间至少使用了一个自定义系统。Custom - Request includes a custom system and at least one custom system was used during translation.
  • 团队 - 所有其他请求Team - All other requests

响应状态代码Response status codes

下面是请求可能返回的 HTTP 状态代码。The following are the possible HTTP status codes that a request returns.

状态代码Status Code 说明Description
200200 成功。Success.
400400 查询参数之一缺失或无效。One of the query parameters is missing or not valid. 请更正请求参数,然后重试。Correct request parameters before retrying.
401401 无法对请求进行身份验证。The request could not be authenticated. 请确保凭据已指定且有效。Check that credentials are specified and valid.
403403 请求未经授权。The request is not authorized. 请检查详细错误消息。Check the details error message. 这通常表示试用订阅提供的所有可用翻译已用完。This often indicates that all free translations provided with a trial subscription have been used up.
408408 无法满足请求,因为缺少资源。The request could not be fulfilled because a resource is missing. 请检查详细错误消息。Check the details error message. 使用自定义 category 时,这通常指示自定义翻译系统尚不可用于处理请求。When using a custom category, this often indicates that the custom translation system is not yet available to serve requests. 应在等待一段时间(例如 1 分钟)后重试此请求。The request should be retried after a waiting period (e.g. 1 minute).
429429 由于客户端已超出请求限制,服务器拒绝了请求。The server rejected the request because the client has exceeded request limits.
500500 发生了意外错误。An unexpected error occurred. 如果错误持续存在,请报告相关信息:发生故障的日期和时间、响应标头 X-RequestId 中的请求标识符、请求标头 X-ClientTraceId 中的客户端标识符。If the error persists, report it with: date and time of the failure, request identifier from response header X-RequestId, and client identifier from request header X-ClientTraceId.
503503 服务器暂不可用。Server temporarily unavailable. 重试请求。Retry the request. 如果错误持续存在,请报告相关信息:发生故障的日期和时间、响应标头 X-RequestId 中的请求标识符、请求标头 X-ClientTraceId 中的客户端标识符。If the error persists, report it with: date and time of the failure, request identifier from response header X-RequestId, and client identifier from request header X-ClientTraceId.

如果发生错误,请求也将返回 JSON 错误响应。If an error occurs, the request will also return a JSON error response. 错误代码是一个 6 位数字,包括 3 位数的 HTTP 状态代码,后接用于进一步将错误分类的 3 位数。The error code is a 6-digit number combining the 3-digit HTTP status code followed by a 3-digit number to further categorize the error. 常见错误代码可在 v3 翻译器参考页上找到。Common error codes can be found on the v3 Translator reference page.

示例Examples

翻译单个输入Translate a single input

以下示例演示了如何将单个句子从英文翻译为简体中文。This example shows how to translate a single sentence from English to Simplified Chinese.

curl -X POST "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from=en&to=zh-Hans" -H "Ocp-Apim-Subscription-Key: <client-secret>" -H "Content-Type: application/json; charset=UTF-8" -d "[{'Text':'Hello, what is your name?'}]"

响应正文为:The response body is:

[
    {
        "translations":[
            {"text":"你好, 你叫什么名字?","to":"zh-Hans"}
        ]
    }
]

translations 数组包括一个元素,该元素提供输入中一段文本的翻译。The translations array includes one element, which provides the translation of the single piece of text in the input.

使用语言自动检测功能翻译单个输入Translate a single input with language auto-detection

以下示例演示了如何将单个句子从英文翻译为简体中文。This example shows how to translate a single sentence from English to Simplified Chinese. 请求未指定输入语言,The request does not specify the input language. 而是使用自动检测源语言的功能。Auto-detection of the source language is used instead.

curl -X POST "https://api.translator.azure.cn/translate?api-version=3.0&to=zh-Hans" -H "Ocp-Apim-Subscription-Key: <client-secret>" -H "Ocp-Apim-Subscription-Region: your-region" -H "Content-Type: application/json" -d "[{'Text':'Hello, what is your name?'}]"

响应正文为:The response body is:

[
    {
        "detectedLanguage": {"language": "en", "score": 1.0},
        "translations":[
            {"text": "你好, 你叫什么名字?", "to": "zh-Hans"}
        ]
    }
]

响应类似于前一示例中的响应。The response is similar to the response from the previous example. 由于请求了语言自动检测,因此响应还包括针对输入文本检测到的语言的信息。Since language auto-detection was requested, the response also includes information about the language detected for the input text.

通过音译进行翻译Translate with transliteration

让我们添加音译,对上一示例进行扩展。Let's extend the previous example by adding transliteration. 以下请求要求提供以拉丁字母拼写的中文翻译。The following request asks for a Chinese translation written in Latin script.

curl -X POST "https://api.translator.azure.cn/translate?api-version=3.0&to=zh-Hans&toScript=Latn" -H "Ocp-Apim-Subscription-Key: <client-secret>" -H "Ocp-Apim-Subscription-Region: your-region" -H "Content-Type: application/json; charset=UTF-8" -d "[{'Text':'Hello, what is your name?'}]"

响应正文为:The response body is:

[
    {
        "detectedLanguage":{"language":"en","score":1.0},
        "translations":[
            {
                "text":"你好, 你叫什么名字?",
                "transliteration":{"script":"Latn", "text":"nǐ hǎo , nǐ jiào shén me míng zì ?"},
                "to":"zh-Hans"
            }
        ]
    }
]

翻译结果现在包括一个 transliteration 属性,该属性提供使用拉丁字符的翻译文本。The translation result now includes a transliteration property, which gives the translated text using Latin characters.

翻译多行文本Translate multiple pieces of text

一次翻译多个字符串时,只需在请求正文中指定一个字符串数组即可。Translating multiple strings at once is simply a matter of specifying an array of strings in the request body.

curl -X POST "https://api.translator.azure.cn/translate?api-version=3.0&from=en&to=zh-Hans" -H "Ocp-Apim-Subscription-Key: <client-secret>" -H "Ocp-Apim-Subscription-Region: your-region" -H "Content-Type: application/json; charset=UTF-8" -d "[{'Text':'Hello, what is your name?'}, {'Text':'I am fine, thank you.'}]"

响应正文为:The response body is:

[
    {
        "translations":[
            {"text":"你好, 你叫什么名字?","to":"zh-Hans"}
        ]
    },            
    {
        "translations":[
            {"text":"我很好,谢谢你。","to":"zh-Hans"}
        ]
    }
]

翻译为多种语言Translate to multiple languages

以下示例演示如何在一个请求中将同一输入翻译为多种语言。This example shows how to translate the same input to several languages in one request.

curl -X POST "https://api.translator.azure.cn/translate?api-version=3.0&from=en&to=zh-Hans&to=de" -H "Ocp-Apim-Subscription-Key: <client-secret>" -H "Ocp-Apim-Subscription-Region: your-region" -H "Content-Type: application/json; charset=UTF-8" -d "[{'Text':'Hello, what is your name?'}]"

响应正文为:The response body is:

[
    {
        "translations":[
            {"text":"你好, 你叫什么名字?","to":"zh-Hans"},
            {"text":"Hallo, was ist dein Name?","to":"de"}
        ]
    }
]

处理不雅内容Handle profanity

通常,翻译工具服务在翻译中会保留源中存在的不雅内容。Normally the Translator service will retain profanity that is present in the source in the translation. 不雅程度和使词语不雅的语境在不同的文化之间有所不同,因此,目标语言中的不雅程度可能会放大或缩小。The degree of profanity and the context that makes words profane differ between cultures, and as a result the degree of profanity in the target language may be amplified or reduced.

如果无论源文本中是否存在不雅内容,你都希望避免在翻译中出现不雅内容,可以使用不雅内容筛选选项。If you want to avoid getting profanity in the translation, regardless of the presence of profanity in the source text, you can use the profanity filtering option. 使用该选项可以选择是要看到不雅内容被删除、标有相应标记(允许你添加自己的后处理操作)还是不被执行任何操作。The option allows you to choose whether you want to see profanity deleted, whether you want to mark profanities with appropriate tags (giving you the option to add your own post-processing), or you want no action taken. ProfanityAction 来说,接受的值为 DeletedMarkedNoAction(默认)。The accepted values of ProfanityAction are Deleted, Marked and NoAction (default).

ProfanityActionProfanityAction 操作Action
NoAction 这是默认行为。This is the default behavior. 不雅内容会从源传递到目标。Profanity will pass from source to target.

示例源(日语):彼はジャッカスです。 Example Source (Japanese): 彼はジャッカスです。
示例翻译(中文):他是一个笨蛋。 Example Translation (English): He is a jackass.
Deleted 不雅词语会从输出中删除,不进行替换。Profane words will be removed from the output without replacement.

示例源(日语):彼はジャッカスです。 Example Source (Japanese): 彼はジャッカスです。
示例翻译(中文):他是一个。 Example Translation (English): He is a.
Marked 不雅词语会在输出中使用标记进行替换。Profane words are replaced by a marker in the output. 标记取决于 ProfanityMarker 参数。The marker depends on the ProfanityMarker parameter.

如果 ProfanityMarker=Asterisk,不雅词语会被替换为 \*\*\*For ProfanityMarker=Asterisk, profane words are replaced with \*\*\*:
示例源(日语):彼はジャッカスです。 Example Source (Japanese): 彼はジャッカスです。
示例翻译(中文):他是一个 \*\*\*。 Example Translation (English): He is a \*\*\*.

如果 ProfanityMarker=Tag,则不雅词语会被括在 XML 标记 <profanity> 和 </profanity> 中:For ProfanityMarker=Tag, profane words are surrounded by XML tags <profanity> and </profanity>:
示例源(日语):彼はジャッカスです。 Example Source (Japanese): 彼はジャッカスです。
示例翻译(中文):他是一个 <profanity>笨蛋</profanity>。 Example Translation (English): He is a <profanity>jackass</profanity>.

例如:For example:

curl -X POST "https://api.translator.azure.cn/translate?api-version=3.0&from=en&to=de&profanityAction=Marked" -H "Ocp-Apim-Subscription-Key: <client-secret>" -H "Ocp-Apim-Subscription-Region: your-region" -H "Content-Type: application/json; charset=UTF-8" -d "[{'Text':'This is a freaking good idea.'}]"

这样会返回:This returns:

[
    {
        "translations":[
            {"text":"Das ist eine *** gute Idee.","to":"de"}
        ]
    }
]

与以下示例比较:Compare with:

curl -X POST "https://api.translator.azure.cn/translate?api-version=3.0&from=en&to=de&profanityAction=Marked&profanityMarker=Tag" -H "Ocp-Apim-Subscription-Key: <client-secret>" -H "Ocp-Apim-Subscription-Region: your-region" -H "Content-Type: application/json; charset=UTF-8" -d "[{'Text':'This is a freaking good idea.'}]"

上一个请求返回:That last request returns:

[
    {
        "translations":[
            {"text":"Das ist eine <profanity>verdammt</profanity> gute Idee.","to":"de"}
        ]
    }
]

翻译带标记的内容并确定翻译的内容Translate content with markup and decide what's translated

通常需要翻译包含标记的内容,例如 HTML 页中的内容或 XML 文档中的内容。It's common to translate content which includes markup such as content from an HTML page or content from an XML document. 在翻译包含标记的内容时包括查询参数 textType=htmlInclude query parameter textType=html when translating content with tags. 另外,有时候需从翻译中排除特定的内容。In addition, it's sometimes useful to exclude specific content from translation. 可以使用属性 class=notranslate 指定应保留不译的内容。You can use the attribute class=notranslate to specify content that should remain in its original language. 在以下示例中,第一个 div 元素中的内容不会翻译,第二个 div 元素中的内容则会翻译。In the following example, the content inside the first div element will not be translated, while the content in the second div element will be translated.

<div class="notranslate">This will not be translated.</div>
<div>This will be translated. </div>

下面是用于演示的示例请求。Here is a sample request to illustrate.

curl -X POST "https://api.translator.azure.cn/translate?api-version=3.0&from=en&to=zh-Hans&textType=html" -H "Ocp-Apim-Subscription-Key: <client-secret>" -H "Ocp-Apim-Subscription-Region: your-region" -H "Content-Type: application/json; charset=UTF-8" -d "[{'Text':'<div class=\"notranslate\">This will not be translated.</div><div>This will be translated.</div>'}]"

响应为:The response is:

[
    {
        "translations":[
            {"text":"<div class=\"notranslate\">This will not be translated.</div><div>这将被翻译。</div>","to":"zh-Hans"}
        ]
    }
]

获取比对信息Obtain alignment information

对齐将作为以下格式的字符串值返回给源的每个词。Alignment is returned as a string value of the following format for every word of the source. 每个词的信息由一个空格分隔,其中包括非空格分隔的语言(脚本),比如中文:The information for each word is separated by a space, including for non-space-separated languages (scripts) like Chinese:

[[SourceTextStartIndex]:[SourceTextEndIndex]–[TgtTextStartIndex]:[TgtTextEndIndex]] *[[SourceTextStartIndex]:[SourceTextEndIndex]–[TgtTextStartIndex]:[TgtTextEndIndex]] *

对齐字符串示例:“0:0-7:10 1:2-11:20 3:4-0:3 3:4-4:6 5:5-21:21”。Example alignment string: "0:0-7:10 1:2-11:20 3:4-0:3 3:4-4:6 5:5-21:21".

换而言之,冒号分隔开始和结束索引,连字符分隔语言,空格分隔词。In other words, the colon separates start and end index, the dash separates the languages, and space separates the words. 一个单词可能与另一种语言中的 0 个、1 个或多个单词比对,而比对的词可能是不连续的。One word may align with zero, one, or multiple words in the other language, and the aligned words may be non-contiguous. 当没有可用的对齐信息时,Alignment 元素将为空。When no alignment information is available, the Alignment element will be empty. 在这种情况下,该方法不会返回任何错误。The method returns no error in that case.

若要接收比对信息,请在查询字符串中指定 includeAlignment=trueTo receive alignment information, specify includeAlignment=true on the query string.

curl -X POST "https://api.translator.azure.cn/translate?api-version=3.0&from=en&to=fr&includeAlignment=true" -H "Ocp-Apim-Subscription-Key: <client-secret>" -H "Ocp-Apim-Subscription-Region: your-region" -H "Content-Type: application/json; charset=UTF-8" -d "[{'Text':'The answer lies in machine translation.'}]"

响应为:The response is:

[
    {
        "translations":[
            {
                "text":"La réponse se trouve dans la traduction automatique.",
                "to":"fr",
                "alignment":{"proj":"0:2-0:1 4:9-3:9 11:14-11:19 16:17-21:24 19:25-40:50 27:37-29:38 38:38-51:51"}
            }
        ]
    }
]

比对信息以 0:2-0:1 开头,这意味着源文本中的头三个字符 (The) 映射到翻译文本中的头两个字符 (La)。The alignment information starts with 0:2-0:1, which means that the first three characters in the source text (The) map to the first two characters in the translated text (La).

限制Limitations

获取对齐信息是一项实验性功能,我们已启用此功能,以使用可能的短语映射来创建原型研究和体验。Obtaining alignment information is an experimental feature that we have enabled for prototyping research and experiences with potential phrase mappings. 我们可能会选择在将来停止支持此功能。We may choose to stop supporting this in the future. 下面是一些不支持对齐的显著限制:Here are some of the notable restrictions where alignments are not supported:

  • 对齐不适用于 HTML 格式的文本(即 textType=html)Alignment is not available for text in HTML format i.e., textType=html
  • 仅针对一部分语言对返回比对内容:Alignment is only returned for a subset of the language pairs:
    • 从英语到除繁体中文、粤语(繁体)或塞尔维亚语(西里尔文)外的任何其他语言,以及从此类其他语言到英语。English to/from any other language except Chinese Traditional, Cantonese (Traditional) or Serbian (Cyrillic).
    • 从日语到韩语或从韩语到日语。from Japanese to Korean or from Korean to Japanese.
    • 从日语到简体中文以及从简体中文到日语。from Japanese to Chinese Simplified and Chinese Simplified to Japanese.
    • 从简体中文到繁体中文以及从繁体中文到简体中文。from Chinese Simplified to Chinese Traditional and Chinese Traditional to Chinese Simplified.
  • 如果句子是预录翻译,则不会收到比对内容。You will not receive alignment if the sentence is a canned translation. 预录翻译示例有“This is a test”、“I love you”,以及其他高频句子。Example of a canned translation is "This is a test", "I love you" and other high frequency sentences.
  • 当应用任何方法来防止翻译时,对齐功能不可用,如此文所述Alignment is not available when you apply any of the approaches to prevent translation as described here

获取句子边界Obtain sentence boundaries

若要接收源文本和翻译文本中句子长度的相关信息,请在查询字符串中指定 includeSentenceLength=trueTo receive information about sentence length in the source text and translated text, specify includeSentenceLength=true on the query string.

curl -X POST "https://api.translator.azure.cn/translate?api-version=3.0&from=en&to=fr&includeSentenceLength=true" -H "Ocp-Apim-Subscription-Key: <client-secret>" -H "Ocp-Apim-Subscription-Region: your-region" -H "Content-Type: application/json" -d "[{'Text':'The answer lies in machine translation. The best machine translation technology cannot always provide translations tailored to a site or users like a human. Simply copy and paste a code snippet anywhere.'}]"

响应为:The response is:

[
    {
        "translations":[
            {
                "text":"La réponse se trouve dans la traduction automatique. La meilleure technologie de traduction automatique ne peut pas toujours fournir des traductions adaptées à un site ou des utilisateurs comme un être humain. Il suffit de copier et coller un extrait de code n’importe où.",
                "to":"fr",
                "sentLen":{"srcSentLen":[40,117,46],"transSentLen":[53,157,62]}
            }
        ]
    }
]

使用动态词典进行翻译Translate with dynamic dictionary

若已知道要应用于某个单词或短语的翻译,可以在请求中将其作为标记提供。If you already know the translation you want to apply to a word or a phrase, you can supply it as markup within the request. 动态字典仅适用于专有名词,例如个人姓名和产品名称。The dynamic dictionary is only safe for proper nouns such as personal names and product names.

要提供的标记使用以下语法。The markup to supply uses the following syntax.

<mstrans:dictionary translation="translation of phrase">phrase</mstrans:dictionary>

例如,考虑英语句子“The word wordomatic is a dictionary entry.”。For example, consider the English sentence "The word wordomatic is a dictionary entry." 若要在翻译中保留单词 wordomatic,请发送请求:To preserve the word wordomatic in the translation, send the request:

curl -X POST "https://api.translator.azure.cn/translate?api-version=3.0&from=en&to=de" -H "Ocp-Apim-Subscription-Key: <client-secret>" -H "Ocp-Apim-Subscription-Region: your-region" -H "Content-Type: application/json; charset=UTF-8" -d "[{'Text':'The word <mstrans:dictionary translation=\"wordomatic\">word or phrase</mstrans:dictionary> is a dictionary entry.'}]"

结果为:The result is:

[
    {
        "translations":[
            {"text":"Das Wort \"wordomatic\" ist ein Wörterbucheintrag.","to":"de"}
        ]
    }
]

不管使用 textType=text 还是使用 textType=html,此功能都以相同的方式工作。This feature works the same way with textType=text or with textType=html. 应尽量少使用此功能。The feature should be used sparingly. 对翻译进行自定义时,一个合适且要好得多的方法是使用自定义翻译工具。The appropriate and far better way of customizing translation is by using Custom Translator. 自定义翻译工具能够充分利用上下文和统计概率。Custom Translator makes full use of context and statistical probabilities. 如果你必须或者有能力创建在上下文中显示你的工作或短语的训练数据,则会得到好得多的结果。If you have or can afford to create training data that shows your work or phrase in context, you get much better results.