Translater

Wesley13
• 阅读 442
using System;
using System.Collections.Generic;

using System.Text;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using System.Web;
using System.Text.RegularExpressions;

namespace Framework
{
    /// <summary>
    /// 语言类型
    /// </summary>
    public class LanguageType
    {
        /// <summary>
        /// 中文
        /// </summary>
        public static string Chinese = "zh-cn";
        /// <summary>
        /// 英文
        /// </summary>
        public static string English = "en";
    }
    /// <summary>
    /// 翻译方式类型
    /// </summary>
    public class TranslationType
    {
        /// <summary>
        /// Google
        /// </summary>
        public static string Google = "GoogleTanslater";
        /// <summary>
        /// Bing
        /// </summary>
        public static string Bing = "MircsoftTanslater";
    }
    /// <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_PostMethod(sourceText, fromLanguage, toLanguage);//"zh-cn", "en";
                    break;
            }
            return translateStr;
        }
        #region Google 翻译: Get方式获取翻译
        /// <summary>
        /// Google 翻译: Get方式获取翻译
        /// </summary>
        /// <param name="sourceText"></param>
        /// <param name="fromType"></param>
        /// <param name="toType"></param>
        /// <returns></returns>
        private static string GoogleTranslater_GetMethod(string sourceText, string fromType, string toType)
        {
            string result;
            string langPair = fromType.ToLower() == "zh-cn" ? "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;
        }
        #endregion
        #region Google 翻译: Post方式获取翻译
        /// <summary>
        /// Google 翻译: Post方式获取翻译
        /// </summary>
        /// <param name="sourceText"></param>
        /// <param name="fromType"></param>
        /// <param name="toType"></param>
        /// <returns></returns>
        private static string GoogleTranslater_PostMethod(string sourceText, string fromType, string toType)
        {
            string fromLan = fromType.ToLower() == "zh-cn" ? "zh" : "en";
            string toLan = toType.ToLower() == "zh-cn" ? "zh" : "en";
            HttpWebRequest requestScore = (HttpWebRequest)WebRequest.Create("http://translate.google.com/translate_t#");
            StringBuilder postContent = new StringBuilder();
            Encoding myEncoding = Encoding.UTF8;
            postContent.Append(HttpUtility.UrlEncode("hl", myEncoding));
            postContent.Append("=");
            postContent.Append(HttpUtility.UrlEncode("en", myEncoding));
            postContent.Append("&");
            postContent.Append(HttpUtility.UrlEncode("ie", myEncoding));
            postContent.Append("=");
            postContent.Append(HttpUtility.UrlEncode("UTF-8", myEncoding));
            postContent.Append("&");
            postContent.Append(HttpUtility.UrlEncode("sl", myEncoding));
            postContent.Append("=");
            postContent.Append(HttpUtility.UrlEncode(fromLan, myEncoding));
            postContent.Append("&");
            postContent.Append(HttpUtility.UrlEncode("text", myEncoding));
            postContent.Append("=");
            postContent.Append(HttpUtility.UrlEncode(sourceText, myEncoding));
            postContent.Append("&");
            postContent.Append(HttpUtility.UrlEncode("tl", myEncoding));
            postContent.Append("=");
            postContent.Append(HttpUtility.UrlEncode(toLan, myEncoding));

            byte[] data = Encoding.ASCII.GetBytes(postContent.ToString());
            requestScore.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            requestScore.Method = "Post";
            //requestScore.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
            requestScore.ContentLength = data.Length;
            requestScore.KeepAlive = true;
            requestScore.Timeout = (6 * 60 * 1000);
            requestScore.ProtocolVersion = HttpVersion.Version10;

            Stream stream = requestScore.GetRequestStream();
            stream.Write(data, 0, data.Length);
            stream.Close();
            string content = string.Empty;
            try
            {
                System.Net.ServicePointManager.Expect100Continue = false;
                HttpWebResponse responseSorce = (HttpWebResponse)requestScore.GetResponse();
                StreamReader reader = new StreamReader(responseSorce.GetResponseStream());
                content = reader.ReadToEnd();
                responseSorce.Close();
                reader.Dispose();
                stream.Dispose();
            }
            catch (WebException ex)
            {
                HttpWebResponse responseSorce = (HttpWebResponse)ex.Response;//得到请求网站的详细错误提示
                StreamReader reader = new StreamReader(responseSorce.GetResponseStream());
                content = reader.ReadToEnd();
                responseSorce.Close();
                reader.Dispose();
                stream.Dispose();
            }
            finally
            {
                requestScore.Abort();
            }
            string reg = @"<(?<HtmlTag>[\w]+)[^>]*\s[iI][dD]=(?<Quote>[""']?)result_box(?(Quote)\k<Quote>)[""']?[^>]*>((?<Nested><\k<HtmlTag>[^>]*>)|</\k<HtmlTag>>(?<-Nested>)|.*?)*</\k<HtmlTag>>";
            //string reg = @"<(span) id=result_box [^>]*>.*?</\1>";//匹配出翻译内容
            Regex r = new Regex(reg);
            MatchCollection mcItem = r.Matches(content);
            string result = ConvertHtmlToText(mcItem[0].Value);
            return result;
        }
        /// <summary>
        /// 将HTML转换为纯文本
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string ConvertHtmlToText(string source)
        {
            // 代码的实现的思路是:
            //a、先将html文本中的所有空格、换行符去掉(因为html中的空格和换行是被忽略的)
            //b、将<head>标记中的所有内容去掉
            //c、将<script>标记中的所有内容去掉
            //d、将<style>标记中的所有内容去掉
            //e、将td换成空格,tr,li,br,p 等标记换成换行符
            //f、去掉所有以“<>”符号为头尾的标记去掉。
            //g、转换&,&nbps;等转义字符换成相应的符号
            //h、去掉多余的空格和空行
            string result;
            //remove line breaks,tabs
            result = source.Replace("\r", " ");
            result = result.Replace("\n", " ");
            result = result.Replace("\t", " ");
            //remove the header
            result = Regex.Replace(result, "(<head>).*(</head>)", string.Empty, RegexOptions.IgnoreCase);
            result = Regex.Replace(result, @"<( )*script([^>])*>", "<script>", RegexOptions.IgnoreCase);
            result = Regex.Replace(result, @"(<script>).*(</script>)", string.Empty, RegexOptions.IgnoreCase);
            //remove all styles
            result = Regex.Replace(result, @"<( )*style([^>])*>", "<style>", RegexOptions.IgnoreCase); //clearing attributes
            result = Regex.Replace(result, "(<style>).*(</style>)", string.Empty, RegexOptions.IgnoreCase);
            //insert tabs in spaces of <td> tags
            result = Regex.Replace(result, @"<( )*td([^>])*>", " ", RegexOptions.IgnoreCase);
            //insert line breaks in places of <br> and <li> tags
            result = Regex.Replace(result, @"<( )*br( )*>", "\r", RegexOptions.IgnoreCase);
            result = Regex.Replace(result, @"<( )*li( )*>", "\r", RegexOptions.IgnoreCase);
            //insert line paragraphs in places of <tr> and <p> tags
            result = Regex.Replace(result, @"<( )*tr([^>])*>", "\r\r", RegexOptions.IgnoreCase);
            result = Regex.Replace(result, @"<( )*p([^>])*>", "\r\r", RegexOptions.IgnoreCase);
            //remove anything thats enclosed inside < >
            result = Regex.Replace(result, @"<[^>]*>", string.Empty, RegexOptions.IgnoreCase);
            //replace special characters:
            result = Regex.Replace(result, @"&", "&", RegexOptions.IgnoreCase);
            result = Regex.Replace(result, @" ", " ", RegexOptions.IgnoreCase);
            result = Regex.Replace(result, @"<", "<", RegexOptions.IgnoreCase);
            result = Regex.Replace(result, @">", ">", RegexOptions.IgnoreCase);
            result = Regex.Replace(result, @"&(.{2,6});", string.Empty, RegexOptions.IgnoreCase);
            //remove extra line breaks and tabs
            result = Regex.Replace(result, @" ( )+", " ");
            result = Regex.Replace(result, "(\r)( )+(\r)", "\r\r");
            result = Regex.Replace(result, @"(\r\r)+", "\r\n");
            return result;
        }
        #endregion
        #region 微软翻译
        /// <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;
        }
        #endregion
    }
    /// <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; }
    }
}
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Easter79 Easter79
2年前
swap空间的增减方法
(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
Java获得今日零时零分零秒的时间(Date型)
publicDatezeroTime()throwsParseException{    DatetimenewDate();    SimpleDateFormatsimpnewSimpleDateFormat("yyyyMMdd00:00:00");    SimpleDateFormatsimp2newS
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这