zoukankan
html css js c++ java
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; } } }
查看全文
相关阅读:
Shiro安全框架之集成 Web(下)
Shiro安全框架之集成 Web(中)
Shiro安全框架之集成 Web(上)
01背包
巴什博弈
斐波那契博弈
一. 至少转最多
平面分割类问题
求凸包(安德鲁算法)
GCD和exGCD
原文地址:https://www.cnblogs.com/zhangqs008/p/2341093.html
最新文章
Linux中的shell
adeng朝花夕拾
Nginx
cobbler之详细配置
typecho博客出404页面修改方法
zabbix之2安装编译/基本功能实现
zabbix之3触发器/action及模板
zabbix之1监控概念
llinux之sudo配置
python之5关于_name_和_main_的具体含义
热门文章
awk之7 常用函数的解析
deepin 2014 静态IP无法保存,临时方法
deepin 2014 安装后 ,grub出错
CentOS常用命令之基本命令
Java爬虫技术之Log4j 日志学习笔记
Java爬虫技术之Executor多线程学习笔记
Java爬虫技术之Ehcache缓存学习笔记
Java爬虫技术之HttpClient学习笔记
Shiro安全框架之整合Spring框架
Shiro安全框架之加密工具
Copyright © 2011-2022 走看看