ASP. NET implements Sino-English Conversion

Source: Internet
Author: User

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 ("I Love You ");
}
}
}

Using System. Net;

Public class GoogleTranslate
{


/// <Summary>
/// Use WebRequest to obtain translated content from Google
/// </Summary>
/// <Param name = "strTranslateString"> content to be translated, asp.net </param>
/// <Param name = "strRequestLanguage"> original language </param>
/// <Param name = "strResultLanguage"> Target Language </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>
/// Translation
/// </Summary>
/// <Param name = "strTranslateString"> content to be translated </param>
/// <Param name = "strRequestLanguage"> original language </param>
/// <Param name = "strResultLanguage"> Target Language </param>
/// <Returns> </returns>
Public string multi‑agetranslate (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>
/// Translation
/// By default, English is translated into simplified Chinese
/// </Summary>
/// <Param name = "TranslateString"> content to be translated </param>
/// <Returns> </returns>
Public string TranslateEnglishToChinese (string strTranslateString)
{
Return multi‑agetranslate (strTranslateString, "en", "zh-CN ");
}


/// <Summary>
/// Translation
/// Translate Simplified Chinese into English www.52mvc.com by default
/// </Summary>
/// <Param name = "TranslateString"> content to be translated </param>
/// <Returns> </returns>
Public string TranslateChineseToEnglish (string strTranslateString)
{
Return multi‑agetranslate (strTranslateString, "zh-CN", "en ");
}


}

 


/// <Summary>
/// Deserialization of the Translation
/// </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>
/// Translation content
/// </Summary>
Public class TranslatedText
{
Private string translatedtext;
Public string translatedText
{
Get {return translatedtext ;}
Set {translatedtext = value ;}
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.