1 Star 3 Fork 0

Yumho/有道翻译

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
YouDao.cs 3.38 KB
一键复制 编辑 原始数据 按行查看 历史
jackma 提交于 2021-02-06 21:09 . 修复传参类型bug
using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using Newtonsoft.Json.Linq;
using RestSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace YouDao
{
[Icon("pack://application:,,,/YouDao;component/Resources/Icon.png")]
public class YouDao : Command, ICommandExecutableInServerSide
{
//重写插件名称
public override string ToString()
{
return "有道翻译";
}
//该插件只能在服务端命令中使用
public override CommandScope GetCommandScope()
{
return CommandScope.ServerSide;
}
[DisplayName("原文")]
[Description("待翻译的内容")]
[FormulaProperty(true)]
public object Src { get; set; }
[DisplayName("翻译类型")]
[Description("默认AUTO为自动翻译为中文,此外还支持类型 类似于ZH_CN2EN或者EN2ZH_CN")]
[FormulaProperty(true)]
public object Type { get; set; }
public ExecuteResult Execute(IServerCommandExecuteContext dataContext)
{
try
{
//request
string Src = dataContext.EvaluateFormula(this.Src).ToString();
string Type = dataContext.EvaluateFormula(this.Type).ToString();
var baseUrl= "http://fanyi.youdao.com/translate?&doctype=json&type=" + "AUTO" + "&i=" + Src;
//url
if (Type != null)
{
baseUrl = "http://fanyi.youdao.com/translate?&doctype=json&type=" + Type + "&i=" + Src;
}
Console.WriteLine(baseUrl);
var client = new RestClient(baseUrl);
var request = new RestRequest(Method.POST);
IRestResponse response = client.Execute(request);
var res = response.Content;
var result = JObject.Parse(res).GetValue("translateResult");
string tgt = result[0][0]["tgt"].ToString();
if (response.StatusCode == HttpStatusCode.OK)
{
return new ExecuteResult()
{
ErrCode = 0,
Message = tgt,
AllowWriteResultToResponse = true
};
}
else
{
return new ExecuteResult()
{
ErrCode = 2,
Message = response.StatusCode.ToString()+response.Content.ToString(),
AllowWriteResultToResponse = true
};
}
}
catch (Exception e)
{
return new ExecuteResult()
{
ErrCode = 1,
Message = e.Message,
AllowWriteResultToResponse = true
};
}
}
public enum TypeEnum
{
AUTO,
ZH_CN2EN,
ZH_CN2JA,
ZH_CN2KR,
ZH_CN2FR,
ZH_CN2RU,
ZH_CN2SP,
EN2ZH_CN,
JA2ZH_CN,
KR2ZH_CN,
FR2ZH_CN,
RU2ZH_CN,
SP2ZH_CN
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/yumho/youdao-translation.git
git@gitee.com:yumho/youdao-translation.git
yumho
youdao-translation
有道翻译
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385