Google API full-text translation Asp.net, simple call implementation, source code

Source: Internet
Author: User

 

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

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.