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>
/// Summary of googletranslate
/// </Summary>
Public class googletranslate
{
Public googletranslate ()
{
//
// Todo: add the constructor logic here
//
}
/// <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 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>
/// 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>
/// Use webrequest to obtain translated content from Google
/// </Summary>
/// <Param name = "strtranslatestring"> content to be translated </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>
/// Deserialization of Translation results
/// </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
/// </Summary>
Public class translatedtext
{
Private string translatedtext;
Public String translatedtext
{
Get {return translatedtext ;}
Set {translatedtext = value ;}
}
}
}
}
---- Call
Protected void btnsumit_click (Object sender, eventargs E)
{
Googletranslate TRAN = new googletranslate ();
Textbox2.text = Tran. multi‑agetranslate (textbox1.text, "ZH-CN", "en ");
}