• Google API的全文翻译 asp.net,简单调用实现,源代码(转)


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Net;
    using System.Text;
    using System.IO;
    using System.Collections;
    using System.Web.Services;
    using System.Web.Services.Protocols;

    /// <summary>
    ///GoogleTranslate 的摘要说明
    /// </summary>
    public class GoogleTranslate
    {
        public GoogleTranslate()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 翻译
        /// 默认地将英文翻译成简体中文
        /// </summary>
        /// <param name="TranslateString">需要翻译的内容</param>
        /// <returns></returns>

        public string TranslateEnglishToChinese(string strTranslateString)
        {
            return MultiLanguageTranslate(strTranslateString, "en", "zh-CN");
        }

        /// <summary>
        /// 翻译
        /// 默认地将简体中文翻译成英文
        /// </summary>
        /// <param name="TranslateString">需要翻译的内容</param>
        /// <returns></returns>

        public string TranslateChineseToEnglish(string strTranslateString)
        {
            return MultiLanguageTranslate(strTranslateString, "zh-CN", "en");
        }

        /// <summary>
        /// 翻译
        /// </summary>
        /// <param name="strTranslateString">需要翻译的内容</param>
        /// <param name="strRequestLanguage">原文语种</param>
        /// <param name="strResultLanguage">译文语种</param>
        /// <returns></returns>

        public string MultiLanguageTranslate(string strTranslateString, string strRequestLanguage, string strResultLanguage)
        {
            try
            {
                if (!string.IsNullOrEmpty(strTranslateString))
                {
                    TranslateString transtring = (TranslateString)Newtonsoft.Json.JavaScriptConvert.DeserializeObject(
                           GetGoogleTranslateJSONString(strTranslateString, strRequestLanguage, strResultLanguage),
                           typeof(TranslateString));
                    if (transtring.responseStatus == 200)
                        return transtring.responseData.translatedText;
                    else
                        return "There was an error.";
                }
                else
                {
                    return strTranslateString;
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        /// <summary>
        /// 使用WebRequest获取Google翻译后的内容
        /// </summary>
        /// <param name="strTranslateString">需要翻译的内容</param>
        /// <param name="strRequestLanguage">原文语种</param>
        /// <param name="strResultLanguage">译文语种</param>
        /// <returns></returns>
        private string GetGoogleTranslateJSONString(string strTranslateString,
            string strRequestLanguage, string strResultLanguage)
        {
            WebRequest request = HttpWebRequest.Create("
    http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q="
                + strTranslateString + "&langpair=" + strRequestLanguage + "%7C" + strResultLanguage + "");
            request.Credentials = CredentialCache.DefaultCredentials;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
            reader.Close();
            dataStream.Close();
            response.Close();
            return responseFromServer;
        }

        /// <summary>
        /// 翻译结果的反系列化类
        /// </summary>
        public class TranslateString
        {
            private TranslatedText responsedata;
            public TranslatedText responseData
            {
                get { return responsedata; }
                set { responsedata = value; }
            }

            private string responsedetails;
            public string responseDetails
            {
                get { return responsedetails; }
                set { responsedetails = value; }
            }

            private int responsestatus;
            public int responseStatus
            {
                get { return responsestatus; }
                set { responsestatus = value; }
            }

            /// <summary>
            /// 译文
            /// </summary>
            public class TranslatedText
            {
                private string translatedtext;
                public string translatedText
                {
                    get { return translatedtext; }
                    set { translatedtext = value; }
                }
            }
        }
    }

    ----调用

        protected void btnSutmit_Click(object sender, EventArgs e)
        {
            GoogleTranslate tran = new GoogleTranslate();
            TextBox2.Text = tran.MultiLanguageTranslate(TextBox1.Text, "zh-CN","en");
        }

  • 相关阅读:
    Java文件操作大全
    struts2整合spring3整合成功但是spring无法注入
    简单理解xFire webservices
    分享几个常用过滤器
    mysql导sql脚本
    开源Jbpm4.4+ssh2+oracle10实现表单、流程自定义开发流程业务
    关于搭建FTP服务器
    chrome标签同步神器插件集装箱!
    记录java接口自动化模板优化
    java之封装日期工具类DateUtils
  • 原文地址:https://www.cnblogs.com/yanpo/p/2262519.html
Copyright © 2020-2023  润新知