1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 // (c) 2008-2011 Moz (@syo68k)
4 // (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 // (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 // (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 // (c) 2011 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
8 // All rights reserved.
10 // This file is part of OpenTween.
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General Public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
28 using System.Collections.Generic;
29 using System.Net.Http;
30 using System.Net.Http.Headers;
32 using System.Threading.Tasks;
34 using System.Xml.Linq;
35 using OpenTween.Connection;
41 private static readonly List<string> LanguageTable = new List<string>
169 private static readonly string TranslateUri =
170 "https://api.datamarket.azure.com/Data.ashx/Bing/MicrosoftTranslator/v1/Translate";
172 protected HttpClient http
174 get { return this.localHttpClient ?? Networking.Http; }
176 private readonly HttpClient localHttpClient;
183 public Bing(HttpClient http)
185 this.localHttpClient = http;
189 /// Microsoft Translator API を使用した翻訳を非同期に行います
191 /// <exception cref="HttpRequestException"/>
192 public async Task<string> TranslateAsync(string text, string langFrom, string langTo)
194 var param = new Dictionary<string, string>
196 {"Text", "'" + text + "'"},
197 {"To", "'" + langTo + "'"},
201 if (langFrom != null)
202 param["From"] = "'" + langFrom + "'";
204 var uri = new Uri(TranslateUri + "?" + MyCommon.BuildQueryString(param));
205 var request = new HttpRequestMessage(HttpMethod.Get, uri);
206 request.Headers.Authorization = CreateBasicAuthHeaderValue(ApplicationSettings.AzureMarketplaceKey, ApplicationSettings.AzureMarketplaceKey);
208 using (var response = await this.http.SendAsync(request).ConfigureAwait(false))
210 response.EnsureSuccessStatusCode();
212 var xmlStr = await response.Content.ReadAsStringAsync()
213 .ConfigureAwait(false);
215 var xdoc = XDocument.Parse(xmlStr);
217 return xdoc.Root.Value;
221 public static string GetLanguageEnumFromIndex(int index)
223 return LanguageTable[index];
226 public static int GetIndexFromLanguageEnum(string lang)
228 return LanguageTable.IndexOf(lang);
231 internal static AuthenticationHeaderValue CreateBasicAuthHeaderValue(string user, string pass)
233 var paramBytes = Encoding.UTF8.GetBytes(user + ":" + pass);
234 return new AuthenticationHeaderValue("Basic", Convert.ToBase64String(paramBytes));