Document directory
- 1. Code Section
- 2. How to use Google Translate:
Background
Some time ago, I was working on a small function similar to "one-click translation" and needed to translate long texts in multiple languages. I thought it was very simple, but I found it online, I found that there was no perfect solution. I had to search for a variety of translation APIs from the Internet, and figured out how to use them, but they were not perfect, there are roughly the following defects:
- Microsoft API: using WCF for translation, a maximum of MB characters/month is allowed. If the limit is exceeded, you will be charged. In addition, HTML translation is not perfect. attributes in HTML tags are also translated into Chinese. Strange results such as:
- Youdaoapi: the language is not supported enough. Only English, Japanese, and Chinese are mutually translated. the maximum number of characters to be translated for each request cannot exceed 200, and the Access frequency is limited.
- Googleapi: the Google translation API has obvious advantages, and the Translation results are very accurate. Especially for HTML, Google can accurately skip the HTML Tag section and only translate text, but it is paid for use (V1 is the free version, but the interface has been closed), its price is 1 million characters/20 dollars, its price naturally goes without saying. During use, it is found that the maximum number of characters to be translated each time cannot exceed 2 K, but an exception is reported if it exceeds K in actual use.
- Baiduapi: There are Access frequency and length restrictions, and the Translation results are not ideal.
Under the various restrictions of the four major translation giants, we have to find another way ......
Transfer
In view of the accuracy of Google translation, Google was chosen as its goal, but its price was very expensive, so I had to make an article on its translation page.
During the test, we found that some parameters can be added to the GET request to implement the translation function. As a result, the translation function of the first get version was released. The steps are roughly divided into the following two steps:
- Construct a query string and send a request to the translation page
- Obtain the returned HTML and filter out the expected results. (The younger brother is not talented. Because of this, he trained the regular expression into a pair of ^_^)
However, the get method has the same problem as the API. If the get method exceeds the fixed length, a request exception is reported. So I can only think of other methods.
Later, the younger brother tried to construct a POST request to access it ...... Actually succeeded ...... I am so happy that the Code is not exclusive. I am posting it here. I hope you can share this with me.
Source code: 1. Code Section
1). Translator interface for future extension
View code
Using system; namespace common. translator {// <summary> // language translator interface // zhangqingfeng 2012-7-27 add /// </Summary> Public interface itranslator {// <summary> /// translate text /// zhangqingfeng 2012-7-27 add /// </Summary> /// <Param name = "sourcetext"> Source Text </param> /// <Param name =" sourcelanguagecode "> source language type code, for example: En, ZH-CN, ZH-TW, Ru, and so on </param> // <Param name = "targetlanguagecode"> Target Language type code, such: en, ZH-CN, ZH-TW, Ru, etc. </param> // <returns> translation result </returns> string translate (string sourcetext, string sourcelanguagecode, string targetlanguagecode ); /// <summary> // translate the text [automatically detect the source language type] // zhangqingfeng 2012-7-27 add /// </Summary> /// <Param name = "sourcetext"> Source Text </param> /// <Param name = "targetlanguagecode"> Target Language type code, for example, en, ZH-CN, ZH-TW, and Ru </param> // <returns> translation result </returns> string translate (string sourcetext, string targetlanguagecode );}}
2). Google POST Request Method translator implementation class
View code
3). Factory type with false modulus and false samples ...... Don't spray it. It's just a simple factory implementation.
View code
Using system; namespace common. translator {// <summary> /// translator's factory class // </Summary> public class translatorfactory {/// <summary> /// translator // </ summary> // <Param name = "type"> translator type, currently, only Google Translate </param> // <returns> translator object </returns> Public static itranslator createtranslator (string type) {itranslator translator = NULL; Switch (type) is provided) {Case "Microsoft": break; Case "youdao": break; default: Translator = new googletranslator (); break;} return translator ;}}}
4). Test class:
View code
Using system; using system. collections. generic; using common. translator; namespace consoletest {class program {static void main (string [] ARGs) {itranslator translator = translatorfactory. createtranslator ("google"); List <string> lst = new list <string> (); lst. add ("soft leather case for Apple iPad 1/2/3 with ploybag package"); lst. add ("soft leather case, case for Apple iPad, iPad case, Pu case"); lst. add ("1x iPad leather case"); lst. add ("slim enough to slip into a backpack, bag or briefcase, tough enough to protect your iPad from whatever it may find in there. slip your iPad into Elan sleeve then flip the tab closure down to secure it. then gently pull the tab to slide your iPad out quickly and safely. stain-resistant synthetic outer shell with smooth micro suede interior. "); lst. add (@ "<p> <span> fit for Apple iPad 1/2/3 leather case </span> </span> </P> <p> <span> PU Leather material with top quality </span> </span> </span> </P> <p> <span> three bright colors to meet your demand </span> </span> </P> <p> <span> durable and waterproof, practical and favorable </span> </P> <p> <span> protect your tablet from scratches, damage and dirt </span> </P> <p> <span> unique design allows easy to controls & amp; ports </span> <span> & nbsp; <br/> <br/> <br/> <br/> <br/> <br/> </span> </P> "); string strsource = string. join ("\ n", lst. toarray (); string STR = translator. translate (strsource, "ZH-CN"); console. writeline (STR); console. writeline ("finished ");}}}
5). Final running result:
PS: the translation result of this method is from Google's translation page, which is basically the same as that of Google, but its judgment on HTML is far inferior to that of Google API, so don't be surprised, maybe the younger brother will have time to complete it. If you have any need, you can also change the source code.
2. How to use Google Translate:
1). Code
View code
Using system; // <summary> // website translator [provided by Google, full-page translator] /// zhangqingfeng 2012-7-27 add /// description: /// if you need to use Google language translation on the page, perform the following two steps: // 1. put the googletranslatemeta content in this class into the head, for example: response. write (sitetranslator. googletranslatemeta); // 2. put the googletranslatecontrol content in this class into the page where you need to display the "Select language" // </Summary> public class sitetranslator {public sitetranslator () {}/// <summary> /// Google Translate the meta tag on the entire page and place it in the head area. /// </Summary> pub LIC const string googletranslatemeta = @ "<meta name =" "Google-translate-Customization" "content =" "d83361947aca6bed-626a88e71258e3a8-g3d7ae31013d34b9c-e"> </meta> "; /// <summary> /// Google full-page translation control, place it in the place where the "Select language" box needs to be displayed // </Summary> Public const string googletranslatecontrol = @ "<Div id =" "google_translate_element"> </div> <script type = "" text/JavaScript ""> function googletranslateelementinit () {new Google. Translate. translateelement ({pagelanguage: 'zh-cn', layout: Google. translate. translateelement. inlinelayout. simple, autodisplay: false}, 'Google _ translate_element ');} </SCRIPT> <SCRIPT type = "" text/JavaScript "" src = "" // translate.google.com/translate_a/element.js? CB = googletranslateelementinit ""> </SCRIPT> ";}
2). For example, you only need to add the two variables to the head of the HTML page and the place where you want to see the "Select language" box.
PS: This is the first time that my younger brother has written a blog. There are many big birds in the garden and they are ugly in front of you. I just hope you don't have to repeat the wheel when you encounter such problems, it may be better to change the wheel of the younger brother than those APIs. What's more important: Unlimited, free, you know. Pai_^
If the code is useful to everyone, I hope you can support it. In addition, I don't know how to upload the compressed package attachment in the garden, but I can only send external links. I hope you will give me some advice.
Code packaging: http://www.kuaipan.cn/file/id_18864681775005998.html
---------------------------------------------------------------- The program is not our world, the Code is not our world, HTML is not our world, CSS is not our world, JavaScript is not our world, PHP, Java, and C # are not our world. Only life is our world. Affection, friendship, and love are all of us.
Article Source: http://www.cnblogs.com/feng8621/archive/2012/08/11/2633979.html