diff --git a/STranslate/Images/translate.ico b/STranslate/Images/translate.ico new file mode 100644 index 0000000..f1840be Binary files /dev/null and b/STranslate/Images/translate.ico differ diff --git a/STranslate/Images/translate.svg b/STranslate/Images/translate.svg new file mode 100644 index 0000000..aa4c158 --- /dev/null +++ b/STranslate/Images/translate.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/STranslate/Images/translate_x128.png b/STranslate/Images/translate_x128.png new file mode 100644 index 0000000..d9f1b3e Binary files /dev/null and b/STranslate/Images/translate_x128.png differ diff --git a/STranslate/Images/translate_x256.png b/STranslate/Images/translate_x256.png new file mode 100644 index 0000000..77ef186 Binary files /dev/null and b/STranslate/Images/translate_x256.png differ diff --git a/STranslate/Images/translate_x64.png b/STranslate/Images/translate_x64.png new file mode 100644 index 0000000..203e704 Binary files /dev/null and b/STranslate/Images/translate_x64.png differ diff --git a/STranslate/MainWindow.xaml b/STranslate/MainWindow.xaml index 7150bc0..32da477 100644 --- a/STranslate/MainWindow.xaml +++ b/STranslate/MainWindow.xaml @@ -15,7 +15,8 @@ Topmost="True" WindowStyle="None" Height="450" - Width="400"> + Width="400" + Icon="/Images/translate.ico"> diff --git a/STranslate/Model/BaiduModel.cs b/STranslate/Model/BaiduModel.cs new file mode 100644 index 0000000..6b6d310 --- /dev/null +++ b/STranslate/Model/BaiduModel.cs @@ -0,0 +1,37 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace STranslate.Model +{ + public class BaiduModel + { + public string Text { get; set; } + public string From { get; set; } + public string TO { get; set; } + public string AppId { get; set; } + public string Salt { get; set; } + public string Sign { get; set; } + } + public class BaiduResp + { + [JsonProperty("from")] + public string From { get; set; } + [JsonProperty("to")] + public string To { get; set; } + [JsonProperty("trans_result")] + public TransResult[] TransResult { get; set; } + } + + public class TransResult + { + + [JsonProperty("src")] + public string Src { get; set; } + [JsonProperty("dst")] + public string Dst { get; set; } + } +} diff --git a/STranslate/Model/LanguageEnum.cs b/STranslate/Model/LanguageEnum.cs index 3c6b912..d37c20c 100644 --- a/STranslate/Model/LanguageEnum.cs +++ b/STranslate/Model/LanguageEnum.cs @@ -8,6 +8,7 @@ namespace STranslate.Model { public enum LanguageEnum { + auto, //自动 DE, //德语 EN, //英语 ES, //西班牙语 diff --git a/STranslate/Properties/Resources.Designer.cs b/STranslate/Properties/Resources.Designer.cs new file mode 100644 index 0000000..2c315c9 --- /dev/null +++ b/STranslate/Properties/Resources.Designer.cs @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// 此代码由工具生成。 +// 运行时版本:4.0.30319.42000 +// +// 对此文件的更改可能会导致不正确的行为,并且如果 +// 重新生成代码,这些更改将会丢失。 +// +//------------------------------------------------------------------------------ + +namespace STranslate.Properties { + using System; + + + /// + /// 一个强类型的资源类,用于查找本地化的字符串等。 + /// + // 此类是由 StronglyTypedResourceBuilder + // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 + // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen + // (以 /str 作为命令选项),或重新生成 VS 项目。 + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// 返回此类使用的缓存的 ResourceManager 实例。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("STranslate.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// 重写当前线程的 CurrentUICulture 属性,对 + /// 使用此强类型资源类的所有资源查找执行重写。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// 查找 System.Byte[] 类型的本地化资源。 + /// + internal static byte[] translate { + get { + object obj = ResourceManager.GetObject("translate", resourceCulture); + return ((byte[])(obj)); + } + } + + /// + /// 查找 System.Byte[] 类型的本地化资源。 + /// + internal static byte[] translate_x128 { + get { + object obj = ResourceManager.GetObject("translate_x128", resourceCulture); + return ((byte[])(obj)); + } + } + + /// + /// 查找 System.Byte[] 类型的本地化资源。 + /// + internal static byte[] translate_x256 { + get { + object obj = ResourceManager.GetObject("translate_x256", resourceCulture); + return ((byte[])(obj)); + } + } + + /// + /// 查找 System.Byte[] 类型的本地化资源。 + /// + internal static byte[] translate_x64 { + get { + object obj = ResourceManager.GetObject("translate_x64", resourceCulture); + return ((byte[])(obj)); + } + } + + /// + /// 查找 System.Byte[] 类型的本地化资源。 + /// + internal static byte[] translate1 { + get { + object obj = ResourceManager.GetObject("translate1", resourceCulture); + return ((byte[])(obj)); + } + } + } +} diff --git a/STranslate/Properties/Resources.resx b/STranslate/Properties/Resources.resx new file mode 100644 index 0000000..7147c61 --- /dev/null +++ b/STranslate/Properties/Resources.resx @@ -0,0 +1,136 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + + ..\images\translate.svg;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + ..\images\translate.ico;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + ..\images\translate_x128.png;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + ..\images\translate_x256.png;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + ..\images\translate_x64.png;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/STranslate/STranslate.csproj b/STranslate/STranslate.csproj index 51be18d..993cf6a 100644 --- a/STranslate/STranslate.csproj +++ b/STranslate/STranslate.csproj @@ -4,10 +4,16 @@ WinExe net5.0-windows true + Images\translate.ico + + + + + @@ -16,4 +22,37 @@ + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + + + + True + True + Resources.resx + + + + + + ResXFileCodeGenerator + Resources.Designer.cs + + + diff --git a/STranslate/Utils/HttpUtil.cs b/STranslate/Utils/HttpUtil.cs index 9c2a87b..183dd0a 100644 --- a/STranslate/Utils/HttpUtil.cs +++ b/STranslate/Utils/HttpUtil.cs @@ -31,5 +31,42 @@ namespace STranslate.Utils var resp = JsonConvert.DeserializeObject(responseString); return resp; } + + /// + /// 异步Post请求 + /// + /// + /// + /// + public static async Task PostAsync(string url, string req) + { + + using (var client = new HttpClient()) + { + var content = new StringContent(req, Encoding.UTF8, "application/json"); + + var respContent = await client.PostAsync(url, content); + + string respStr = await respContent.Content.ReadAsStringAsync(); +; + return respStr; + } + } + /// + /// 异步Get请求 + /// + /// + /// + public static async Task GetAsync(string urlpath) + { + using (var client = new HttpClient()) + { + var respContent = await client.GetAsync(urlpath); + + string respStr = await respContent.Content.ReadAsStringAsync(); + + return respStr; + } + } } } diff --git a/STranslate/Utils/TranslateUtil.cs b/STranslate/Utils/TranslateUtil.cs index 50b9408..93990ac 100644 --- a/STranslate/Utils/TranslateUtil.cs +++ b/STranslate/Utils/TranslateUtil.cs @@ -1,13 +1,18 @@ -using STranslate.Model; +using Newtonsoft.Json; +using STranslate.Model; using System; using System.Collections.Generic; +using System.IO; using System.Linq; +using System.Net; +using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; +using System.Web; namespace STranslate.Utils { - public static class TranslateUtil + public class TranslateUtil { private static readonly string _url = "http://172.17.209.47:8080/translate"; public static string Translate(string input, LanguageEnum source, LanguageEnum target) @@ -25,5 +30,77 @@ namespace STranslate.Utils } return string.Empty; } + + public static async Task TranslateDeepLAsync(string text, LanguageEnum target, LanguageEnum source = LanguageEnum.auto) + { + var req = new DeeplReq() + { + Text = text, + SourceLang = source.ToString(), + TargetLang = target.ToString(), + }; + var reqStr = JsonConvert.SerializeObject(req); + var respStr = await HttpUtil.PostAsync(_url, reqStr); + var resp = JsonConvert.DeserializeObject(respStr); + + if (resp.Code == 200) + { + return resp.Data; + } + return string.Empty; + } + + public static async Task TranslateBaiduAsync(string appID, string secretKey, string text, LanguageEnum target, LanguageEnum source = LanguageEnum.auto) + { + Random rd = new Random(); + string salt = rd.Next(100000).ToString(); + string sign = EncryptString(appID + text + salt + secretKey); + string url = "http://api.fanyi.baidu.com/api/trans/vip/translate?"; + url += "q=" + HttpUtility.UrlEncode(text); + url += "&from=" + source.ToString().ToLower(); + url += "&to=" + target.ToString().ToLower(); + url += "&appid=" + appID; + url += "&salt=" + salt; + url += "&sign=" + sign; +#if false + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); + request.Method = "GET"; + request.ContentType = "text/html;charset=UTF-8"; + request.UserAgent = null; + request.Timeout = 6000; + HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + Stream myResponseStream = response.GetResponseStream(); + StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); + string retString = myStreamReader.ReadToEnd(); + myStreamReader.Close(); + myResponseStream.Close(); +#endif + + var retString = await HttpUtil.GetAsync(url); + var resp = JsonConvert.DeserializeObject(retString); + if (resp.From != null) + { + return resp.TransResult[0]?.Dst; + } + return string.Empty; + } + // 计算MD5值 + public static string EncryptString(string str) + { + MD5 md5 = MD5.Create(); + // 将字符串转换成字节数组 + byte[] byteOld = Encoding.UTF8.GetBytes(str); + // 调用加密方法 + byte[] byteNew = md5.ComputeHash(byteOld); + // 将加密结果转换为字符串 + StringBuilder sb = new StringBuilder(); + foreach (byte b in byteNew) + { + // 将字节转换成16进制表示的字符串, + sb.Append(b.ToString("x2")); + } + // 返回加密的字符串 + return sb.ToString(); + } } } diff --git a/STranslate/ViewModel/MainVM.cs b/STranslate/ViewModel/MainVM.cs index 9722069..4fcb691 100644 --- a/STranslate/ViewModel/MainVM.cs +++ b/STranslate/ViewModel/MainVM.cs @@ -11,17 +11,34 @@ namespace STranslate.ViewModel { public class MainVM : BaseVM { + private string Text; public MainVM() { TranslateCmd = new RelayCommand((_) => { return string.IsNullOrEmpty(InputTxt) ? false : true; - }, (_) => + }, async (_) => { - OutputTxt = TranslateUtil.Translate(InputTxt, LanguageEnum.ZH, LanguageEnum.EN); + Text = InputTxt; + //清空输入框 InputTxt = ""; + OutputTxt = "翻译中..."; + + //获取结果 + //var translateResp = await TranslateUtil.TranslateDeepLAsync(InputTxt, LanguageEnum.EN, LanguageEnum.auto); + + var appId = ""; + var secretKey = ""; + var translateResp = await TranslateUtil.TranslateBaiduAsync(appId, secretKey, Text, LanguageEnum.EN, LanguageEnum.auto); + + if (translateResp == string.Empty) + { + OutputTxt = "翻译出错,请稍候再试..."; + return; + } + OutputTxt = translateResp; }); }