2014-01-05 17:56:02 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
2014-01-07 23:27:05 +08:00
|
|
|
|
using System.Threading;
|
2014-01-05 17:56:02 +08:00
|
|
|
|
using System.Windows.Forms;
|
2014-01-07 23:27:05 +08:00
|
|
|
|
using System.Windows.Forms.VisualStyles;
|
2014-01-05 17:56:02 +08:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
2014-01-29 18:33:24 +08:00
|
|
|
|
namespace Wox.Plugin.Fanyi
|
2014-01-05 17:56:02 +08:00
|
|
|
|
{
|
|
|
|
|
public class TranslateResult
|
|
|
|
|
{
|
|
|
|
|
public string from { get; set; }
|
|
|
|
|
public string to { get; set; }
|
|
|
|
|
public List<SrcDst> trans_result { get; set; }
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class SrcDst
|
|
|
|
|
{
|
|
|
|
|
public string src { get; set; }
|
|
|
|
|
public string dst { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Main : IPlugin
|
|
|
|
|
{
|
|
|
|
|
private string translateURL = "http://openapi.baidu.com/public/2.0/bmt/translate";
|
|
|
|
|
private string baiduKey = "SnPcDY3iH5jDbklRewkG2D2v";
|
|
|
|
|
|
2014-01-15 22:45:02 +08:00
|
|
|
|
private static string AssemblyDirectory
|
2014-01-05 17:56:02 +08:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
|
|
|
|
|
UriBuilder uri = new UriBuilder(codeBase);
|
|
|
|
|
string path = Uri.UnescapeDataString(uri.Path);
|
|
|
|
|
return Path.GetDirectoryName(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Result> Query(Query query)
|
|
|
|
|
{
|
|
|
|
|
List<Result> results = new List<Result>();
|
|
|
|
|
if (query.ActionParameters.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
results.Add(new Result()
|
|
|
|
|
{
|
|
|
|
|
Title = "Start to translate between Chinese and English",
|
|
|
|
|
SubTitle = "Powered by baidu api",
|
|
|
|
|
IcoPath = "Images\\translate.png"
|
|
|
|
|
});
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Dictionary<string, string> data = new Dictionary<string, string>();
|
|
|
|
|
data.Add("from", "auto");
|
|
|
|
|
data.Add("to", "auto");
|
|
|
|
|
data.Add("q", query.RawQuery.Substring(3));
|
|
|
|
|
data.Add("client_id", baiduKey);
|
|
|
|
|
HttpWebResponse response = HttpRequest.CreatePostHttpResponse(translateURL, data, null, null, Encoding.UTF8, null);
|
|
|
|
|
Stream s = response.GetResponseStream();
|
|
|
|
|
if (s != null)
|
|
|
|
|
{
|
|
|
|
|
StreamReader reader = new StreamReader(s, Encoding.UTF8);
|
|
|
|
|
string json = reader.ReadToEnd();
|
|
|
|
|
TranslateResult o = JsonConvert.DeserializeObject<TranslateResult>(json);
|
|
|
|
|
foreach (SrcDst srcDst in o.trans_result)
|
|
|
|
|
{
|
|
|
|
|
string dst = srcDst.dst;
|
|
|
|
|
results.Add(new Result()
|
|
|
|
|
{
|
|
|
|
|
Title = dst,
|
|
|
|
|
SubTitle = "Copy to clipboard",
|
|
|
|
|
IcoPath = "Images\\translate.png",
|
2014-02-09 20:55:18 +08:00
|
|
|
|
Action = (c) =>
|
2014-01-05 17:56:02 +08:00
|
|
|
|
{
|
|
|
|
|
Clipboard.SetText(dst);
|
|
|
|
|
context.ShowMsg("translation has been copyed to your clipboard.", "",
|
|
|
|
|
AssemblyDirectory + "\\Images\\translate.png");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Init(PluginInitContext context)
|
|
|
|
|
{
|
|
|
|
|
this.context = context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private PluginInitContext context { get; set; }
|
|
|
|
|
}
|
|
|
|
|
}
|