Asp.net uses Google APIs for translation

Source: Internet
Author: User

Note: Google provides a set of APIS for us to conveniently implement language translation. For us (Chinese), Chinese and English translations are commonly used. Good English can look at Google Documents: http://code.google.com/apis/ajaxlanguage/documentation/reference.html#LangNameArray

This articleArticleOriginal: http://blog.moozi.net/archives/2008/10/16/the-realization-of-web-service-of-full-text-translations-based-on-google-ajax-language-api.aspx

JSON. Net needs to be used for download at http://www.codeplex.com/json.

What is serialization? Let's take a look at my previous article. Http://www.sosuo8.com/article/show.asp? Id = 2470

In fact, Google has provided a very convenient translation function in JS, but in some cases we need to operate in. net. What is the situation? Collect Chinese articles and translate them into English, or write a C/S software. Below I will postCodeFor details, refer to the articles written by Mu Zi.
It needs to use:
Encodeuricomponent
Value.
Main Code:Copy codeThe Code is as follows: // <summary>
/// Replace the translated special characters for line breaks, etc.
/// </Summary>
/// <Param name = "str"> </param>
/// <Returns> </returns>
Private string transtohtml (string Str)
{
String temp;
Temp = Str. Replace ("003c", "<");
Temp = temp. Replace ("003e", "> ");
Return temp;
}

/// <Summary>
/// Use webrequest to obtain translated content from Google
/// </Summary>
/// <Param name = "str"> </param>
/// <Param name = "strrequestlan"> </param>
/// <Param name = "strresultlan"> </param>
/// <Returns> </returns>
Private string getgoogletransjsonstring (string STR, string strrequestlan, string strresultlan)
{
Try
{
Webrequest request = httpwebrequest. Create ("http://ajax.googleapis.com/ajax/services/language/translate? V = 1.0 & Q = "+ STR +" & langpair = "+ strrequestlan +" % 7C "+ strresultlan );
Request. Credentials = credentialcache. defaultcredentials;
Httpwebresponse response = (httpwebresponse) request. getresponse ();
Stream stream = response. getresponsestream ();
Streamreader reader = new streamreader (Stream );
String responsefromserver = reader. readtoend ();
Reader. Close ();
Stream. Close ();
Response. Close ();
Return responsefromserver;
}
Catch (exception ex)
{
Return ex. message;
}
}

/// <Summary>
/// Translation
/// </Summary>
/// <Param name = "str"> content to be translated </param>
/// <Param name = "strreuest"> original </param>
/// <Param name = "strresult"> translation </param>
/// <Returns> </returns>
Public String translate (string STR, string strreuestlan, string strresultlan)
{
Try
{
If (! String. isnullorempty (STR ))
{
Translatestring transtr = (translatestring) newtonsoft. JSON. javascriptconvert. deserializeobject (getgoogletransjsonstring (STR, strreuestlan, strresultlan), typeof (translatestring ));
If (transtr. responsestatus = 200)
{
Return transtr. responsedata. translatedtext;
}
Else
{
Return "An error occurred ";
}
}
Else
{
Return STR;
}

}
Catch (exception ex)
{
Return ex. message;
}
}

Deserialization: Copy codeThe Code is as follows: using system;

/// <Summary>
/// Deserialization
/// </Summary>
/// {"Responsedata": {"translatedtext": "ahuinan"}, "responsedetails": NULL, "responsestatus": 200}
[Serializable]
Public class translatestring
{
Private translatedtext _ responsedata;
Private string _ responsedetails;
Private int _ responsestauts;

Public translatedtext responsedata
{
Get
{
Return _ responsedata;
}
Set
{
_ Responsedata = value;
}
}

Public String responsedetails
{
Get
{
Return _ responsedetails;
}
Set
{
_ Responsedetails = value;
}
}

Public int responsestatus
{
Get
{
Return _ responsestauts;
}
Set
{
_ Responsestauts = value;
}
}

///


// translated content
///
public class translatedtext
{
private string _ translatedtext;
Public String translatedtext
{< br> Get
{< br> return _ translatedtext;
}< br> set
{< BR >_translatedtext = value;
}< BR >}

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.