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; } } }
查看全文
相关阅读:
day40_jQuery学习笔记_01
jQuery选择什么版本 1.x? 2.x? 3.x?
6个关于dd命令备份Linux系统的例子
快速掌握grep命令及正则表达式
Linux下删除乱码或特殊字符文件
在 Linux 中永久修改 USB 设备权限
CentOS 7 中 hostnamectl 的使用
申请红帽企业版Linux开发者订阅
CentOS6 下rsync服务器配置
Centos6下DRBD的安装配置
原文地址:https://www.cnblogs.com/zhangqs008/p/2341093.html
最新文章
Maven如何手动添加jar包到本地Maven仓库
eclipse+tomcat7解决项目中文乱码的一个思路
如何设置EditPlus的默认编码utf-8方式
Windows server 2008下开启telnet功能
怎么修改tomcat默认访问首页
Java中对List集合的排序
解决eclipse+tomcat7的中文乱码的一个方法
[Java]算术表达式求值之二(中序表达式转后序表达式方案,支持小数)
[Java]使用正则表达式实现分词
[Java]算术表达式求值之一(中序表达式转后序表达式方案)
热门文章
[Java]将算术表达式(中序表达式Infix)转成后续表达式Postfix
[Java]分解算术表达式二
[Java]分解算术表达式一
转贴:MySQL的explain分析sql语句
万变的Web,不变的CRUD
转自B站 真希望我在20岁就懂得的10个人生道理 主讲:王魄
评【TED】陆克文:中美注定要冲突吗?
拆一个全新的罗技M220无线鼠标
浏览器调试jQuery代码演示动图
【强烈推荐】优启时代系统维护盘的安装步骤
Copyright © 2011-2022 走看看