[How to]Bing API - Translation Service
在Bing的服務中,也提供了翻譯的功能。
像你在畫面中輸入「translate I love you」,它就會翻譯出西班牙文、法文、德文...等的版本。
當然你也可以限定翻譯的語言,透過「translate I love you to chinese(japanese)」,就可以指定語言翻譯(目前限定在英文版的Bing才有效)。
那如果您要實做這個功能,Bing API也有提供這個Translation SourceType的API。
同樣地,如果您有看過第一篇 [How to]Bing API開發初體驗 ,就知道使用Bing API的時候,會需要設定一些必要的參數,以及一些選擇性的參數。
必要參數
The following parameters are required for all requests:
- SearchRequest.AppId Property (Bing, Version 2.0)
- SearchRequest.Query Property (Bing, Version 2.0)
- SearchRequest.Sources Property (Bing, Version 2.0)
選擇性參數
The following optional parameters are applicable to this SourceType.
- TranslationRequest.SourceLanguage Property (Bing, Version 2.2)
- TranslationRequest.TargetLanguage Property (Bing, Version 2.2)
回覆的內容
The following response fields are common to all source types:
- SearchResponse.Version Property (Bing, Version 2.0)
- Query.AlterationOverrideQuery Property (Bing, Version 2.0)
- Query.SearchTerms Property (Bing, Version 2.0)
The following response fields are specific to the Translation SourceType:(下列兩個欄位只有使用Translation API才會出現)
- TranslationResponse.Results Property (Bing, Version 2.2)
- TranslationResult.TranslatedTerm Property (Bing, Version 2.2)
程式我們參考範例程式,並略為修改:
1: public partial class _Default : System.Web.UI.Page
2: {
3:
4: const string AppId = "APPID";
5:
6: protected void Page_Load(object sender, EventArgs e)
7: {
8:
9: }
10: protected void Button1_Click(object sender, EventArgs e)
11: {
12: //查詢TextBox1的輸入字串
13: HttpWebRequest request = BuildRequest(TextBox1.Text.Trim());
14:
15: try
16: {
17: // 傳送request, 接受response
18: HttpWebResponse response = (HttpWebResponse)request.GetResponse();
19: DisplayResponse(response);
20:
21: }
22: catch (WebException ex)
23: {
24: // 處理exception
25: Response.Write(ex.Message);
26:
27: }
28: }
29:
30: static HttpWebRequest BuildRequest(string qString)
31: {
32: string requestString = "http://api.search.live.net/xml.aspx?"
33:
34: // Common request fields (required)
35: + "AppId=" + AppId
36: + "&Query=" + qString
37: + "&Sources=Translation"
38:
39: // Common request fields (optional)
40: + "&Version=2.2"
41: + "&Market=en-us"
42: + "&Options=EnableHighlighting"
43:
44: // SourceType-specific request fields (required)
45: + "&Translation.SourceLanguage=en"
46: + "&Translation.TargetLanguage=zh-CHT";
47:
48: // 建立和初始化request
49: HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(
50: requestString);
51:
52: return request;
53: }
54:
55: void DisplayResponse(HttpWebResponse response)
56: {
57: // Load the response into an XmlDocument.
58: XmlDocument document = new XmlDocument();
59: document.Load(response.GetResponseStream());
60:
61: // Add the default namespace to the namespace manager.
62: XmlNamespaceManager nsmgr = new XmlNamespaceManager(
63: document.NameTable);
64: nsmgr.AddNamespace(
65: "api",
66: "http://schemas.microsoft.com/LiveSearch/2008/04/XML/element");
67:
68: XmlNodeList errors = document.DocumentElement.SelectNodes(
69: "./api:Errors/api:Error",
70: nsmgr);
71:
72: if (errors.Count > 0)
73: {
74: // There are errors in the response. Display error details.
75: DisplayErrors(errors);
76: }
77: else
78: {
79: // There were no errors in the response. Display the
80: // Translation results.
81: DisplayResults(document.DocumentElement, nsmgr);
82: }
83: }
84:
85: void DisplayResults(XmlNode root, XmlNamespaceManager nsmgr)
86: {
87: string version = root.SelectSingleNode("./@Version", nsmgr).InnerText;
88: string searchTerms = root.SelectSingleNode(
89: "./api:Query/api:SearchTerms",
90: nsmgr).InnerText;
91:
92: // Display the results header.
93: Response.Write("Bing API Version " + version +"<br>");
94: Response.Write("Translation results for " + searchTerms + "<br>");
95:
96:
97: // Add the Translation SourceType namespace to the namespace manager.
98: nsmgr.AddNamespace(
99: "tra",
100: "http://schemas.microsoft.com/LiveSearch/2008/04/XML/translation");
101:
102: XmlNodeList results = root.SelectNodes(
103: "./tra:Translation/tra:Results/tra:TranslationResult",
104: nsmgr);
105:
106: // Display the Translation results.
107: foreach (XmlNode result in results)
108: {
109: DisplayTextWithHighlighting(
110: result.SelectSingleNode("./tra:TranslatedTerm", nsmgr).InnerText);
111: }
112: }
113:
114: void DisplayTextWithHighlighting(string text)
115: {
116: // Write text to the standard output stream, changing the console
117: // foreground color as highlighting characters are encountered.
118: foreach (char c in text.ToCharArray())
119: {
120:
121: Response.Write(c);
122:
123: }
124: }
125:
126: void DisplayErrors(XmlNodeList errors)
127: {
128: // Iterate over the list of errors and display error details.
129: Response.Write("Errors:");
130: foreach (XmlNode error in errors)
131: {
132: foreach (XmlNode detail in error.ChildNodes)
133: {
134: Response.Write(detail.Name + ": " + detail.InnerText + "</br>");
135: }
136: }
137: }
138: }
執行結果:
我們再略作修改,把目標語言指定為DropDownList所選的值:
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="zh-tw">繁體中文</asp:ListItem>
<asp:ListItem Value="zh-chs">簡體中文</asp:ListItem>
<asp:ListItem Value="en">英文</asp:ListItem>
<asp:ListItem Value="ja">日文</asp:ListItem>
<asp:ListItem Value="fr">法文</asp:ListItem>
<asp:ListItem Value="es">西班牙語</asp:ListItem>
</asp:DropDownList>
顯示結果:
接下來,我們再來玩一個介面變換:
一開始的畫面如下:
變換成日文:
法文:
繁體中文:
怎樣? 是不是很好玩呢? 來玩玩看吧..
如果您有微軟技術開發的問題,可以到MSDN Forum發問。
如果您有微軟IT管理的問題,可以到TechNet Forum發問喔。