asp.net use Google API to do translation _ practical skills

Source: Internet
Author: User

Description: Google provides a set of API can give us very convenient to achieve language translation functions, for us (Chinese), commonly used in Chinese and English translation. Good English can look at Google's documents:http://code.google.com/apis/ajaxlanguage/documentation/reference.html

This article was originally written in:http://blog.moozi.net/archives/2008/10/16/ The-realization-of-web-service-of-full-text-translations-based-on-google-ajax-language-api.aspx

Need to use to Json.NET in http://www.codeplex.com/Json download.

What is serialization? You can look at the article in front of me. 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 under. Net. What's the situation? Collect Chinese articles and translate them into English, or write a C/s software. Below I will mainly paste out the code, concrete can see miracle write article.
It needs to use:
encodeURIComponent
The value is passed.
Main code:
Copy Code code as follows:

<summary>
REPLACES special translated characters for line-wrapping, 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 get Google translated content
</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" > What needs 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 "error occurred";
}
}
Else
{
return str;
}

}
catch (Exception ex)
{
Return ex. message;
}
}

deserialization:
Copy Code code 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;
}
}

<summary>
Content of the translation
</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.