一款自用的翻译小工具,开源了
TranslationTool
作者:WPFDevelopersOrg - 唐宋元明清|驚鏵
原文链接:https://github.com/Kybs0/TranslationTool
此项目使用
WPF MVVM
开发。框架使用大于等于
.NET461
。Visual Studio 2019
。最初是支持以下:
Google
有道
金山
百度
目前支持
有道
、百度
翻译 其他接口均无法访问。项目中使用
WebRequest
进行http
翻译请求,Unicode转字符串
,MD5加密
,ScriptControl执行JS
,winmm
播放音频,SpeechSynthesizer
文本转语音,下载音频文件
等。更多代码请下载源码[1],欢迎给项目一个
Star
。各种翻译
WebAPI
。实体类。
帮助类。
界面。
1)调用API部分代码如下:
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Translation.Api;
using Translation.WebApi.WebRequestHelper;
namespace Translation.WebApi.YouDaoApi
{
/// <summary>
/// 官方API翻译
/// </summary>
public class YouDaoUnOfficialWordApiService : WebRequestBase
{
public static async Task<EnglishWordTranslationData> GetWordsAsync(string queryText)
{
if (string.IsNullOrWhiteSpace(queryText))
{
return new EnglishWordTranslationData();
}
try
{
var requestUrl = GetRequestUrl(queryText.Trim());
var result = await RequestUrlAsync(requestUrl);
var youDaoResponse = JsonConvert.DeserializeObject<IntergateYouDaoDataResponse>(result);
var translationData = new EnglishWordTranslationData
{
Word = youDaoResponse.Word ?? string.Empty,
DetailJson = result,
UkPronounce = youDaoResponse.EnglishToChineseData?.GetUkPronounce() ?? new PronounceInfo(),
UsPronounce = youDaoResponse.EnglishToChineseData?.GetUsPronounce() ?? new PronounceInfo(),
Translations = youDaoResponse.EnglishToChineseData?.GetTranslation() ?? new List<SematicInfo>(),
Phrases = youDaoResponse.PhraseDictionary?.GetPhrases() ?? new List<PhraseInfo>(),
Synonyms = youDaoResponse.SynonymDictionary?.GetSynonymWords() ?? new List<SynonymInfo>(),
Cognates = youDaoResponse.CognateWordDictionary?.GetCognateWords() ?? new List<CognateInfo>(),
Sentences = youDaoResponse.CollinsDictionary?.GetCollinsSentences() ?? new List<SentenceInfo>(),
};
return translationData;
}
catch (Exception e)
{
return new EnglishWordTranslationData();
}
}
private static string GetRequestUrl(string queryWord)
{
var dictString = GetDictString();
var requestUrl = string.Format("http://dict.youdao.com/jsonapi?xmlVersion=5.1&jsonversion=2&client=mobile&" +
"q={0}&dicts={1}&network=5g", queryWord, dictString);
return requestUrl;
}
/// <summary>
/// 获取字典字符串
/// 需要查询哪些字典。目前已知
/// </summary>
/// <returns></returns>
private static string GetDictString()
{
var dictionaryQueryModel = new DictionaryQueryModel()
{
Dictionaries = new List<List<string>>()
{
new List<string>()
{
"ec",
//"ec21",
"phrs",
"syno",
"rel_word",
"collins"
},
},
};
var requestString = WebRequestTransformHelper.GetRequestString(dictionaryQueryModel);
return requestString;
}
}
}
鸣谢 - 唐宋元明清
参考资料
[1]
源码: https://github.com/Kybs0/TranslationTool