Full-text translation web service implementation based on Google Ajax language API

Source: Internet
Author: User
Full-text translation web service implementation based on Google Ajax language API Li jianfu (School of mechanical engineering, Hebei University of Technology, Tangshan 063009, China) Abstract To: In the process of developing application software and websites, translation functions are increasingly required. With the Google Ajax language API, we can develop a full-text translation web service that supports over 30 languages around the world. Keywords: Google Ajax language API; web service; JSON; full-text translation The realization of Web Service of full text translations Based on Google Ajax language API Li jianfu (Hebei Polytechnic University, Tangshan Hebei 063009) Abstract: In the process of developing application software and website, the translation function will be used in more conditions. taking advantage of Google Ajax language API, we will be able to develop the web service supporting full text translations in more than 30 ages. key words: Google Ajax language API, web services, JSON, full text translation. I. Introduction With the rapid development of the Internet in China, the network is increasingly closely integrated with our daily lives, and multi-language communication is becoming more and more frequent, and machine translation is increasingly needed. As Program There are more and more places to provide full-text translation for users when developing software. Google Ajax language API is a very mature and easy-to-use API, with this feature, we can achieve full-text translation in over 30 languages around the world. 2. Introduction to Google Ajax language API and Related Technologies
 
1. Google Ajax language API
You can use the Google Ajax language API to translate and detect a given piece of text.
 
2. Web Service
Web Services is a new platform for establishing interoperable distributed applications. The Web service platform is a set of standards that define how applications implement interoperability on the web. You can use any language you like to write Web Services on any platform you like, and we can query and access these services through web service standards. Web Service is an application that uses standard Internet protocols, such as Hypertext Transfer Protocol (HTTP) and XML, to visually display functions on the Internet and on the enterprise intranet. Web Service is based on some common protocols, such as HTTP, soap, XML, WSDL, and UDDI. These protocols involve operating systems, object models, and Programming Language The Web Service has a strong vitality.
 
3. JSON
JSON (JavaScript Object Notation) is a lightweight data exchange format. Easy to read and write. It is also easy to parse and generate machines. It is based on a subset of JavaScript programming language, standard ECMA-262 3rd edition-December 1999. JSON uses a completely language-independent text format, but it also uses a habit similar to the C language family (including C, C ++, C #, Java, JavaScript, Perl, Python, and so on ). These features make JSON an ideal data exchange language. The Google Ajax language API supports interfaces in non-JavaScript environments. It interacts with data in JSON format. In. NET Framework 2.0, JSON data cannot be deserialized directly. Here, we use the open-source JSON. Net class library for JSON deserialization. JSON. Net can be downloaded at http://www.codeplex.com/json. we use json.net 1.3.1. Iii. Web service implementation

First, use Visual Studio 2008 to create an ASP. NET web service application"

Because Google Ajax language API returns data in JSON format. So we will first reference the downloaded "newtonsoft. JSON. dll" to the project, and add the new "Web Service" to name the file "WebService. asmx ".

The flowchart of services. asmx is as follows:

 

We need to obtain JSON data returned from Google. The implementation method is as follows:

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

 

Implementation principle: Use webrequest to submit the content to be translated to the Google server "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0is the version of Google Ajax language API, And the Q parameter is the content to be translated, langpair transmits the language of the content to be translated. "% 7C" is followed by the translated language. Then, use httpwebresponse to obtain the data returned by Google.

The abbreviated format must be used, for example, simplified Chinese (zh-CN), Traditional Chinese (zh-tw), English (En), and Japanese (JA. Google Ajax language API supports many languages, which are not listed here. For details, refer to the "Translation api reference" on the official website of Google Ajax language API ". Http://code.google.com/apis/ajaxlanguage/documentation/reference.html#LangNameArray)

The JSON data format returned by Google is:

{"Responsedata ":{
"Translatedtext": "ciao Mondo"
},
"Responsedetails": NULL,
"Responsestatus": 200
};

Declare the anti-serialization class according to the JSON data format returned by Google.

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

After deserializing the data returned by Google, you can display the translated data returned after deserialization to the user.

/** // <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>
[Webmethod (description = "Multi Language translate.")]
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;
}
}

 

This sentence "(translatestring) newtonsoft. JSON. javascriptconvert. deserializeobject (substring (strtranslatestring, strrequestlanguage, substring), typeof (translatestring);" serves getgoogletranslatejsonstring (...) The string returned by the method calls the static method newtonsoft. JSON. javascriptconvert. deserializeobject of JSON. Net to implement deserialization and convert it to the translatestring type.

This Web Service has basically been completed. For ease of use, we have two built-in Chinese and English translation methods.

/** // <Summary>
/// Translation
/// By default, English is translated into simplified Chinese
/// </Summary>
/// <Param name = "translatestring"> content to be translated </param>
/// <Returns> </returns>
[Webmethod (description = "translate English to Chinese.")]
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>
[Webmethod (description = "translate Chinese to English.")]
Public String translatechinesetoenglish (string strtranslatestring)
{
Return multi‑agetranslate (strtranslatestring, "ZH-CN", "en ");
}

 

ThisWeb ServiceRun

References

[1] Lin Hong-zhi. Principles and development practices of Web Services. Beijing: Electronics Industry Press, 2003.11

Download: http://www.box.net/shared/plpnnrlf8l

This article from http://www.cnblogs.com/moozi/archive/2008/11/07/1329113.html

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.