Last active
August 12, 2017 06:37
-
-
Save feanz/6991499 to your computer and use it in GitHub Desktop.
Microsoft Translator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Configuration; | |
using System.IO; | |
using System.Net; | |
using System.Runtime.Serialization.Json; | |
using System.Text; | |
using System.Web; | |
using Think.Formica.Extensions; | |
namespace Think.Formica.Translation | |
{ | |
public class MicrosoftTranslator : ITranslator | |
{ | |
private string _clientID; | |
private string _clientSecret; | |
private string _translatorAccessUri; | |
private string _accessHeader; | |
private string _translatorUrl; | |
public MicrosoftTranslator() | |
{ | |
_translatorAccessUri = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"; | |
_translatorUrl = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text="; | |
_clientID = "FormicaTestData"; | |
_clientSecret = "aJXPEJSKO2kOcC2mMdySE9SwK2pqBdjZ8MBTAjpTF00="; | |
} | |
public string ClientID | |
{ | |
get { return _clientID ?? (_clientID = ConfigurationManager.AppSettings["Bing.ClientID"]); } | |
set { _clientID = value; } | |
} | |
public string ClientSecret | |
{ | |
get { return _clientSecret ?? (_clientSecret = ConfigurationManager.AppSettings["Bing.ClientSecret"]); } | |
set { _clientSecret = value; } | |
} | |
public string TranslatorAccessUri | |
{ | |
get { return _translatorAccessUri ?? (_translatorAccessUri = ConfigurationManager.AppSettings["Bing.AuthUrl"]); } | |
set { _translatorAccessUri = value; } | |
} | |
public string TranslatorUrl | |
{ | |
get { return _translatorUrl ?? (_translatorUrl = ConfigurationManager.AppSettings["Bing.TranslatorUrl"]); } | |
set { _translatorUrl = value; } | |
} | |
private string GetAccessTokenheader() | |
{ | |
var requestDetails = CreateTranslatorAuthUri(); | |
var webRequest = WebRequest.Create(TranslatorAccessUri); | |
webRequest.ContentType = "application/x-www-form-urlencoded"; | |
webRequest.Method = "POST"; | |
var bytes = Encoding.ASCII.GetBytes(requestDetails); | |
webRequest.ContentLength = bytes.Length; | |
using (var outputStream = webRequest.GetRequestStream()) | |
{ | |
outputStream.Write(bytes, 0, bytes.Length); | |
} | |
var webResponse = (HttpWebResponse)webRequest.GetResponse(); | |
AdmAccessToken token = null; | |
using (var stream = webResponse.GetResponseStream()) | |
{ | |
var serializer = new DataContractJsonSerializer(typeof(AdmAccessToken)); | |
if (stream != null) token = (AdmAccessToken)serializer.ReadObject(stream); | |
} | |
return (token != null) ? "Bearer " + token.access_token : null; | |
} | |
private string CreateTranslatorAuthUri() | |
{ | |
var requestDetails = string.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=http://api.microsofttranslator.com", HttpUtility.UrlEncode(ClientID), HttpUtility.UrlEncode(ClientSecret)); | |
return requestDetails; | |
} | |
public string Translate(string textTobeTranslated, string languageFrom, string languageTo) | |
{ | |
textTobeTranslated.ThrowIfNullOrEmpty("textTobeTranslated"); | |
languageFrom.ThrowIfNullOrEmpty("languageFrom"); | |
languageTo.ThrowIfNullOrEmpty("languageTo"); | |
var accessheader = _accessHeader ?? (_accessHeader = GetAccessTokenheader()); | |
var uri = CreateTranslatorUri(textTobeTranslated, languageFrom, languageTo); | |
var translationWebRequest = WebRequest.Create(uri); | |
translationWebRequest.Headers.Add("Authorization", accessheader); | |
var response = translationWebRequest.GetResponse(); | |
string translatedText = null; | |
var encode = Encoding.GetEncoding("utf-8"); | |
using (var stream = response.GetResponseStream()) | |
if (stream != null) | |
using(var translatedStream = new StreamReader(stream, encode)) | |
{ | |
var xTranslation = new System.Xml.XmlDocument(); | |
xTranslation.LoadXml(translatedStream.ReadToEnd()); | |
translatedText = xTranslation.InnerText; | |
} | |
return translatedText; | |
} | |
private string CreateTranslatorUri(string textTobeTranslated, string languageFrom, string languageTo) | |
{ | |
var uri = string.Format("{0}{1}&from={2}&to={3}", TranslatorUrl, HttpUtility.UrlEncode(textTobeTranslated), languageFrom, languageTo); | |
return uri; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AdmAccessToken | |
{ | |
public string access_token { get; set; } | |
public string token_type { get; set; } | |
public string expires_in { get; set; } | |
public string scope { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ITranslator | |
{ | |
string Translate(string originalString, string originalLanguageIso, string targetLanguageIso); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment