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 ;}
}
}
}