心白水

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

之前做过一个桌面翻译工具,桌面每日一句--桌面翻译工具(有道翻译,微软翻译,Google翻译) 获取金山每日一句,目前因为 金山每日一句页面改变导致每日一句功能失败,不过这工具自己用得最多的还是翻译功能,干脆把翻译独立出来。

另外,最近在逛知乎发现有人分享了必应词典的第三方api,所以顺道拿来完善,api作者分享页面:https://zhuanlan.zhihu.com/p/22421123

这个必应词典用起来很简单直接访问地址http://xtk.azurewebsites.net/BingDictService.aspx?Word=x  x是待翻译的词语,返回json,最后就是解析json就行,解析jason用的是开源库:http://dynamicjson.codeplex.com/

直接贴代码:

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.IO;
  4 using System.Linq;
  5 using System.Net;
  6 using System.Text;
  7 using System.Threading.Tasks;
  8 using System.Web;
  9 using Codeplex.Data;
 10 
 11 namespace BingTranslate
 12 {
 13     public class BingDictApi
 14     {
 15 
 16 
 17         private static string GetSource(string PageUrl)
 18         {
 19             try
 20             {
 21                 WebRequest request = WebRequest.Create(PageUrl); //WebRequest.Create方法,返回WebRequest的子类HttpWebRequest
 22                 request.Timeout = 5000;//设置超时等待
 23                 WebResponse response = request.GetResponse(); //WebRequest.GetResponse方法,返回对 Internet 请求的响应
 24                 Stream resStream = response.GetResponseStream(); //WebResponse.GetResponseStream 方法,从 Internet 资源返回数据流。 
 25                 Encoding enc = Encoding.GetEncoding("utf-8"); // 如果是乱码就改成 utf-8 / GB2312
 26                 StreamReader sr = new StreamReader(resStream, enc); //命名空间:System.IO。 StreamReader 类实现一个 TextReader (TextReader类,表示可读取连续字符系列的读取器),使其以一种特定的编码从字节流中读取字符。 
 27                 string source = sr.ReadToEnd(); //输出(HTML代码),ContentHtml为Multiline模式的TextBox控件
 28                 resStream.Close();
 29                 //Console.Write(source);
 30                 sr.Close();
 31                 return source;
 32             }
 33             catch (Exception ex)
 34             {
 35 
 36                 Console.WriteLine(ex.Message);
 37                 return "";
 38             }
 39 
 40         }
 41 
 42 
 43         public BingDictobject Translate(string word)
 44         {
 45             BingDictobject dictobject = new BingDictobject();
 46             string encodedStr = HttpUtility.UrlEncode(word);
 47             string url = string.Format("http://xtk.azurewebsites.net/BingDictService.aspx?Word={0}", encodedStr);
 48             string text = GetSource(url);
 49             if (text.Contains("An error occurs."))
 50             {
 51                 return null;
 52             }
 53             var json = DynamicJson.Parse(text);
 54             dictobject.word = json.word;
 55             if (json.pronunciation())//判断属性是否存在
 56             {
 57                 var jsPronunciation = json.pronunciation;
 58                 if (jsPronunciation!= null)
 59                 {
 60                     Pronunciation pronunciation = new Pronunciation();
 61                     pronunciation.AmE = jsPronunciation.AmE;
 62                     pronunciation.AmEmp3 = jsPronunciation.AmEmp3;
 63                     pronunciation.BrE = jsPronunciation.BrE;
 64                     pronunciation.BrEmp3 = jsPronunciation.BrEmp3;
 65                     dictobject.pronunciation = pronunciation;
 66                 }
 67                 
 68             }
 69             if (json.defs())
 70             {
 71                 var jsdef = json.defs;
 72                 if (jsdef!= null)
 73                 {
 74                     Def[] defs = jsdef;
 75                     dictobject.defs = defs;
 76                 }
 77                 
 78             }
 79 
 80             if (json.sams())
 81             {
 82                 var jssam = json.sams;
 83                 if (jssam!=null)
 84                 {
 85                     Sam[] sams = jssam;
 86                     dictobject.sams = sams;
 87                 }
 88                 
 89             }
 90             return dictobject;
 91         }
 92         public class BingDictobject
 93         {
 94             public string word { get; set; }
 95             public Pronunciation pronunciation { get; set; }
 96             public Def[] defs { get; set; }
 97             public Sam[] sams { get; set; }
 98         }
 99 
100         public class Pronunciation
101         {
102             public string AmE { get; set; }
103             public string AmEmp3 { get; set; }
104             public string BrE { get; set; }
105             public string BrEmp3 { get; set; }
106         }
107 
108         public class Def
109         {
110             public string pos { get; set; }
111             public string def { get; set; }
112         }
113 
114         public class Sam
115         {
116             public string eng { get; set; }
117             public string chn { get; set; }
118             public string mp3Url { get; set; }
119             public string mp4Url { get; set; }
120         }
121 
122     }
123 }

调用方法:

 1 using BingTranslate;
 2 
 3 namespace BingTranslateTest
 4 {
 5     class Program
 6     {
 7         static void Main(string[] args)
 8         {
 9             BingDictApi bing = new BingDictApi();
10             bing.Translate("china");
11         }
12     }
13 }

 

翻译效果:

 

工具附带其它翻译api,不再一一说明,原理也很简单,想研究的可以使用反汇编工具查看

使用也很简单,喜欢的可以下载使用:

启动后,在图盘图标中调出设计界面,设置好快捷键(默认ctrl+T) 按快捷键就能调出翻译界面

 

 

下载地址:XQSimpleTranslation.zip

posted on 2016-09-16 18:38  心白水  阅读(7920)  评论(5编辑  收藏  举报