会员
周边
捐助
新闻
博问
闪存
赞助商
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
清山博客
慎言其余,则寡尤;多见阙殆,慎行其余,则寡悔。言寡尤,行寡悔。
博客园
首页
新随笔
联系
订阅
管理
Translater-语言翻译类
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO; using Newtonsoft.Json; using System.Web; namespace Framework { /// <summary> /// 语言翻译类 /// </summary> public class Translater { /// <summary> /// 翻译方法 中文:"zh-cn", 英文:"en" type:MircsoftTanslater,GoogleTanslater /// </summary> /// <param name="sourceText">翻译原文</param> /// <param name="fromLanguage">原始语言</param> /// <param name="toLanguage">目标语言</param> /// <param name="type">翻译API</param> /// <returns>译文</returns> public static string Translate(string sourceText, string fromLanguage, string toLanguage, string type = "MircsoftTanslater") { string translateStr = string.Empty; switch (type) { case "MircsoftTanslater": translateStr = MircsoftTanslater(sourceText, fromLanguage, toLanguage);//"zh-cn", "en"; break; case "GoogleTanslater": translateStr = GoogleTranslater(sourceText, fromLanguage + "|" + toLanguage);//"zh-cn|en"; break; } return translateStr; } /// <summary> /// Google 翻译: Get方式获取翻译 /// </summary> /// <param name="sourceText"></param> /// <param name="langPair"></param> /// <returns></returns> private static string GoogleTranslater(string sourceText, string langPair) { string result; langPair = langPair.ToLower() == "zh" ? "zh|en" : "en|zh"; string url = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&langpair=" + HttpUtility.UrlEncode(langPair) + "&q=" + HttpUtility.UrlEncode(sourceText); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.Referer = "http://www.my-ajax-site.com"; try { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8")); string responseStr = reader.ReadToEnd(); ResponseResult readConfig = (ResponseResult)JavaScriptConvert.DeserializeObject(responseStr, typeof(ResponseResult)); if (readConfig.responseStatus == "200") { result = readConfig.responseData.translatedText; } else { result = readConfig.responseStatus; } } catch (Exception Ex) { result = "err:" + Ex.Message; } return result; } /// <summary> /// 微软翻译API : 语言类型:"zh-cn", "en" /// </summary> /// <param name="orgStr">翻译原文</param> /// <param name="fromType">原文语言类型</param> /// <param name="toType">目标语言类型</param> /// <returns></returns> public static string MircsoftTanslater(string orgStr, string fromType, string toType) { string content = string.Empty; string appId = "56E164FED4017D272E06AD7E16778536251CA5CB"; string text = orgStr;// "Translate this for me"; string from = fromType;// "en"; string to = toType;// "zh-cn"; string uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=" + appId + "&text=" + System.Web.HttpUtility.UrlEncode(text) + "&from=" + from + "&to=" + to; HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri); WebResponse response = null; try { response = httpWebRequest.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); content = reader.ReadToEnd();//"<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">Hello, China</string>" content = content.Replace("<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">", ""); content = content.Replace("</string>", ""); response.Close(); reader.Dispose(); } catch (WebException e) { content = ProcessWebException(e, "Failed to translate"); } finally { if (response != null) { response.Close(); response = null; } } return content; } private static string ProcessWebException(WebException e, string message) { string result = string.Empty; result = string.Format("{0}: {1}", message, e.ToString()); // Obtain detailed error information string strResponse = string.Empty; using (HttpWebResponse response = (HttpWebResponse)e.Response) { using (Stream responseStream = response.GetResponseStream()) { using (StreamReader sr = new StreamReader(responseStream, System.Text.Encoding.ASCII)) { strResponse = sr.ReadToEnd(); } } } result = string.Format("Http status code={0}, error message={1}", e.Status, strResponse); return result; } } /// <summary> /// 翻译返回类 /// </summary> public class ResponseResult { public ResponseData responseData { get; set; } public string responseDetails { get; set; } public string responseStatus { get; set; } } /// <summary> /// /// </summary> public class ResponseData { public string translatedText { get; set; } } }
posted @
2011-12-15 13:14
清山博客
阅读(
513
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告