当前位置:编程学习 > asp >>

ASP.NET 实现中英转换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace WebApplication3
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GoogleTranslate g = new GoogleTranslate();
            var name=g.TranslateChineseToEnglish("我爱你");
        }
    }
}

using System.Net;

public class GoogleTranslate
{


    /// <summary>
    /// 使用WebRequest获取Google翻译后的内容
    /// </summary>
    /// <param name="strTranslateString">需要翻译的内容 asp.net </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>
    /// <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>
    /// 翻

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,