csharp 微软翻译

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 微软翻译相关的知识,希望对你有一定的参考价值。

public interface ITranslator
{
    string Translate(string originalString, string originalLanguageIso, string targetLanguageIso);
}
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; }
}
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;
		}
	}
}

以上是关于csharp 微软翻译的主要内容,如果未能解决你的问题,请参考以下文章

MSIL解析一(转)

csharp Tranform翻译

csharp 团结/变换/翻译

csharp 服务器翻译

csharp 服务器翻译

缺失Assembly-CSharp.dll怎么办!