Csharp: Baidu translation, csharp Baidu Translation

Source: Internet
Author: User

Csharp: Baidu translation, csharp Baidu Translation

 

Reference: http://api.fanyi.baidu.com/api/trans/product/index

Http://developer.baidu.com/wiki/index.php? Title = % E5 % B8 % AE % E5 % 8A % A9 % E6 % 96% E6 % A1 % A3 % E9 % A6 % 87% E9 % A1 % B5/% E7 % 96% BE % E5 % BA % A6 % E7 % BF % BB % E8 % AF % 91API

Winform:

/// <Summary> /// tu juwen /// </summary> public partial class Form5: Form {TranClass tranClass = new TranClass (); string url = ""; string youAPI_Id = "geovindu"; // <summary> /// </summary> /// <returns> </returns> DataTable setLanguage () {DataTable dt = new DataTable (); dt. columns. add ("id", typeof (int); dt. columns. add ("name", typeof (string); dt. rows. add (1, "Chinese-English"); dt. rows. add (2, "English-Chinese "); Dt. rows. add (3, "Chinese-Japanese"); dt. rows. add (4, "Japanese-Chinese"); dt. rows. add (5, "Chinese -- Korean"); dt. rows. add (6, "Korean-Chinese"); dt. rows. add (7, "Spanish-Chinese"); dt. rows. add (8, "Chinese -- Spanish"); return dt ;}/// <summary> //// </summary> public Form5 () {InitializeComponent ();} /// <summary> //// </summary> /// <param name = "sender"> </param> /// <param name = "e "> </param> private void Form5_Load (object sender, eventArgs e) {t His. cbxTranslateChange. dataSource = setLanguage (); this. cbxTranslateChange. valueMember = "id"; this. cbxTranslateChange. displayMember = "name ";} /// <summary> /// translation /// </summary> /// <param name = "sender"> </param> /// <param name =" e "> </param> private void button#click (object sender, eventArgs e) {WebClient client = new WebClient (); // reference System. net string fromTranslate = tbxFromContent. text; // Translation If (! String. isNullOrEmpty (fromTranslate) {// client_id is your own api_id, q is the translation object, from is the translation language, to is the translated language string url = string. format ("http://openapi.baidu.com/public/2.0/bmt/translate? Client_id = {0} & q = {1} & from = {2} & to = {3} ", youAPI_Id, fromTranslate, tranClass. from, tranClass. to); var buffer = client. downloadData (url); string result = Encoding. UTF8.GetString (buffer); StringReader sr = new StringReader (result); JsonTextReader jsonReader = new JsonTextReader (sr); // reference Newtonsoft. json comes with JsonSerializer serializer = new JsonSerializer (); var r = serializer. deserialize <TranClass> (jsonReader); // convert tbxToContent because the obtained object is a json object. text = r. trans_result [0]. dst; // dst is the translated value }}/// <summary> //// </summary> // <param name = "sender"> </param >/// <param name = "e"> </param> private void cbxTranslateChange_SelectedIndexChanged (object sender, eventArgs e) {ComboBox comboBox = sender as ComboBox; switch (comboBox. selectedIndex) {case 1: tranClass. from = "zh"; tranClass. to = "en"; break; case 2: tranClass. from = "en"; tranClass. to = "zh"; break; case 3: tranClass. from = "zh"; tranClass. to = "jp"; break; case 4: tranClass. from = "jp"; tranClass. to = "zh"; break; case 5: tranClass. from = "zh"; tranClass. to = "kor"; break; case 6: tranClass. from = "kor"; tranClass. to = "zh"; break; case 7: tranClass. from = "zh"; tranClass. to = "spa"; break; case 8: tranClass. from = "spa"; tranClass. to = "zh"; break; default: tranClass. from = "auto"; tranClass. to = "auto"; break ;}} public class TranClass {public string From {get; set;} public string To {get; set ;} public List <Trans_result> Trans_result {get; set ;}} public class Trans_result {public string src {get; set ;} public string dst {get; set ;}} /* language abbreviation name auto Automatic Detection zh Chinese en english yue Cantonese wyw classical Chinese jp Japanese kor Korean fra French spa Spanish th Thai ara Arabic ru Russian pt Portuguese de German it Italian el Greek nl Dutch pl Polish bul Portuguese (est) Arabic dan Danish fin Finnish cs Czech rom Romanian slo Swedish swe Swedish hu Hungary cht traditional Chinese characters */}

  

 

Webform:

TranClass tranClass = new TranClass (); string url = "www.dusystem.com"; string youAPI_Id = "geovindu "; /// <summary> /// tu juwen /// </summary> /// <returns> </returns> DataTable setLanguage () {DataTable dt = new DataTable (); dt. columns. add ("id", typeof (int); dt. columns. add ("name", typeof (string); dt. rows. add (1, "Chinese-English"); dt. rows. add (2, "English -- Chinese"); dt. rows. add (3, "Chinese-Japanese"); dt. rows. add (4, "Japanese-Chinese "); Dt. rows. add (5, "Chinese -- Korean"); dt. rows. add (6, "Korean-Chinese"); dt. rows. add (7, "Spanish-Chinese"); dt. rows. add (8, "Chinese-Spanish"); return dt ;} /// <summary> //// </summary> /// <param name = "sender"> </param> /// <param name = "e "> </param> protected void Page_Load (object sender, eventArgs e) {if (! IsPostBack) {this. dropDownList1.DataSource = setLanguage (); this. dropDownList1.DataTextField = "name"; this. dropDownList1.DataValueField = "id"; this. dropDownList1.DataBind (); // DropDownList1.SelectedIndex = 0 ;}} /// <summary> //// </summary> /// <param name = "sender"> </param> /// <param name = "e "> </param> protected void button#click (object sender, eventArgs e) {int k = int. parse (DropDownList 1. selectedValue); switch (k) {case 1: tranClass. from = "zh"; tranClass. to = "en"; break; case 2: tranClass. from = "en"; tranClass. to = "zh"; break; case 3: tranClass. from = "zh"; tranClass. to = "jp"; break; case 4: tranClass. from = "jp"; tranClass. to = "zh"; break; case 5: tranClass. from = "zh"; tranClass. to = "kor"; break; case 6: tranClass. from = "kor"; tranClass. to = "zh"; break; case 7: tranC Lass. from = "zh"; tranClass. to = "spa"; break; case 8: tranClass. from = "spa"; tranClass. to = "zh"; break; default: tranClass. from = "auto"; tranClass. to = "auto"; break;} WebClient client = new WebClient (); // reference System. net string fromTranslate = this. textBox1.Text; // The content before translation if (object. equals (tranClass, null) {tranClass. from = "zh"; tranClass. to = "en";} if (! String. isNullOrEmpty (fromTranslate) {// client_id is your own api_id, q is the translation object, from is the translation language, to is the translated language string url = string. format ("http://openapi.baidu.com/public/2.0/bmt/translate? Client_id = {0} & q = {1} & from = {2} & to = {3} ", youAPI_Id, fromTranslate, tranClass. from, tranClass. to); var buffer = client. downloadData (url); string result = Encoding. UTF8.GetString (buffer); StringReader sr = new StringReader (result); JsonTextReader jsonReader = new JsonTextReader (sr); // reference Newtonsoft. json built-in while (jsonReader. read () {// Response. write (jsonReader. value. toString () + "<br/>"); // jsonReader. to KenType. toString () + "" + jsonReader. valueType. toString () + "" +} Response. write (CallInterface (fromTranslate, tranClass. from, tranClass. to); var j = JsonConvert. deserializeObject <TranClass> (result); // JsonSerializer serializer = new JsonSerializer (); // TranClass p1 = (TranClass) serializer. deserialize (new JsonTextReader (sr), typeof (TranClass); // TranClass r = serializer. deserialize <TranClass> (js OnReader); // conversion deserialization error due to the json object obtained. // this. textBox2.Text = r. trans_result [0]. dst; // dst is the translated value if (! Object. equals (j, null) {this. textBox2.Text = j. trans_result [0]. dst ;}}} /// <summary> //// </summary> /// <param name = "sender"> </param> /// <param name = "e "> </param> protected void dropdownlistincluselectedindexchanged (object sender, eventArgs e) {switch (DropDownList1.SelectedIndex) {case 0: tranClass. from = "zh"; tranClass. to = "en"; break; case 1: tranClass. from = "en"; tranClass. to = "zh "; Break; case 2: tranClass. from = "zh"; tranClass. to = "jp"; break; case 3: tranClass. from = "jp"; tranClass. to = "zh"; break; case 4: tranClass. from = "zh"; tranClass. to = "kor"; break; case 5: tranClass. from = "kor"; tranClass. to = "zh"; break; case 6: tranClass. from = "zh"; tranClass. to = "spa"; break; case 7: tranClass. from = "spa"; tranClass. to = "zh"; break; default: tranClass. from = "auto "; TranClass. to = "auto"; break ;}} /// <summary> /// request Baidu interface //// https://github.com/lbv/litjson /// </summary> /// <param name = "sub"> text </param> /// <param name = "from"> from language </param> /// <param name = "to"> translated as language </param> /// <returns> </returns> private string CallInterface (string sub, string from, string to) {HttpContext. current. application. lock (); if (HttpContext. current. application ["baidu"] = = Null) {HttpContext. current. application ["baidu"] = 1;} else {HttpContext. current. application ["baidu"] = (int) HttpContext. current. application ["baidu"] + 1;} HttpContext. current. application. unLock (); string url = string. format ("http://openapi.baidu.com/public/2.0/bmt/translate? Client_id = {0} & q = {1} & from = {2} & to = {3} ", youAPI_Id, sub, from, to); string down = "", result = ""; try {WebClient wc = new WebClient (); down = wc. downloadString (url);} catch (Exception) {} JsonData jd = JsonMapper. toObject (down); if (JsonDataContainsKey (jd, "error_code") {result + = sub;} else {JsonData jdResult = jd ["trans_result"]; for (int j = 0; j <jdResult. count; j ++) {JsonData jdDst = jd Result [j] ["dst"]; result + = jdDst. toString () ;}} return result ;} /// <summary> //// </summary> /// <param name = "data"> </param> /// <param name = "key "> </param> // <returns> </returns> public bool JsonDataContainsKey (JsonData data, string key) {bool result = false; if (data = null) return result; if (! Data. isObject) {return result;} IDictionary tdictionary = data as IDictionary; if (tdictionary = null) return result; if (tdictionary. contains (key) {result = true;} return result ;}} /// <summary> ///// </summary> public class TranClass {public string From {get; set;} public string To {get; set ;} public List <Trans_result> Trans_result {get; set ;}} /// <summary> ///// </summary> public class Trans_result {// <summary> // original // </summary> public string src {get; set ;}//< summary> /// translated value /// </summary> public string dst {get; set ;}/ * {"from ": "zh", "to": "en", "trans_result": [{"src": "\ u4e2d \ u56fd", "dst ": "China"}]} */}

  

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.