代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
Translater-语言翻译类
作者:
汉妤
/ 发布于
2016/1/4
/
1263
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; } } }
试试其它关键字
Translater
语言翻译
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
汉妤
贡献的其它代码
(
20
)
.
去掉字符串String中的换行或空格
.
直接取出 post 请求中的 json、得请求体参数、查看 po
.
判断一个日期 是否为 今天、昨天
.
查看linux是32还是64位
.
C#获取oracle服务器名
.
创建分区表
.
内层CSS3盒阴影
.
支持带x身份证号码验证函数
.
复制DataTable数据到新DataTable
.
CollectionHelper-网页采集辅助类
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3