2.在Azure Marketplace上,订阅微软翻译API (Microsoft Translator),地址:https://datamarket.azure.com/dataset/bing/microsofttranslator
3.在Azure Marketplace上,注册应用程序:https://datamarket.azure.com/developer/applications/,如下:
4.应用程序注册完成,获得客户端ID,客户端密钥后,就可以获取对应的访问令牌(token)了哦。
(1)获取token需要的参数:
client_id:值为注册应用程序的客户端ID
client_secret:值为注册应用程序的客户端密钥
scope:值为http://api.microsofttranslator.com
grant_type:client_credentials
(2)Token请求后,返回的结果包含以下信息:
access_token:访问微软翻译API的访问令牌
token_type:访问令牌的格式
expires_in:访问令牌是有效期(单位,秒)。
scope:此令牌的有效域
5.获取了访问令牌后,就可以操作微软翻译API了哦,嘻嘻。
二.bing translator api 常用的方法:
(1)检测文件的语言:
Method: get
url: http://api.microsofttranslator.com/V2/Http.svc/Detect
parameters: appId----访问令牌,值为"Bearer"+ " " + access token;
text----需要检测语言的内容
(2)传递参数languageCodes,通过地区语言获取本地化名称
Method: post
url:http://api.microsofttranslator.com/V1/Http.svc/GetLanguageNames
parameters: appId----访问令牌,值为"Bearer"+ " " + access token;
locale----地区语言代码
languageCodes----语言代码
(3)翻译单个文本
Method: get
url: http://api.microsofttranslator.com/V2/Http.svc/Translate
parameters: appId----访问令牌,值为"Bearer"+ " " + access token;
text----需要翻译的文本
from----源语言
to----目标语言
contentType----翻译的文本格式。支持的格式包括“text / plain的”和“text/html”
category----翻译域的类别,默认为:"general"
(4)翻译多个文件
Method: post
url:http://api.microsofttranslator.com/V2/Http.svc/TranslateArray
parameters: appId----访问令牌,值为"Bearer"+ " " + access token;
texts----一个数组,包含翻译文本
from----源语言
to----目标语言
Optiona----翻译的选项元素,具体格式如下
请求格式:
XMLCode
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <TranslateArrayRequest><AppId/> <From>en</From> <Options> <Categoryxmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2"/> <ContentTypexmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2">text/plain</ContentType> <ReservedFlagsxmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2"/> <Statexmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2"/> <Urixmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2"/> <Userxmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2"/> </Options> <Texts> <stringxmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">Theanswerliesinmachinetranslation.</string> <stringxmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">This is atest.</string> </Texts> <To>zh-CHS</To> </TranslateArrayRequest> |
请求结果:
XMLCode2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <ArrayOfTranslateArrayResponsexmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2"xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <TranslateArrayResponse> <From>en</From> <OriginalTextSentenceLengthsxmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"> <a:int>39</a:int> </OriginalTextSentenceLengths> <State/> <TranslatedText>在计算机翻译中的答案。</TranslatedText> <TranslatedTextSentenceLengthsxmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"> <a:int>11</a:int> </TranslatedTextSentenceLengths> </TranslateArrayResponse> <TranslateArrayResponse> <From>en</From> <OriginalTextSentenceLengthsxmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"> <a:int>15</a:int> </OriginalTextSentenceLengths> <State/> <TranslatedText>这是个测试。</TranslatedText> <TranslatedTextSentenceLengthsxmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"> <a:int>6</a:int> </TranslatedTextSentenceLengths> </TranslateArrayResponse> </ArrayOfTranslateArrayResponse> |
(5)翻译的相关代码标识:http://msdn.microsoft.com/en-us/library/hh456380.aspx