diff --git a/STranslate/MainWindow.xaml b/STranslate/MainWindow.xaml index 800293e..2967ea6 100644 --- a/STranslate/MainWindow.xaml +++ b/STranslate/MainWindow.xaml @@ -28,8 +28,9 @@ CornerRadius="10"> - + + - + - + Height="{Binding ElementName=Input, Path=Height}"> - - + + + + - + Height="{Binding ElementName=Output, Path=Height}"> { - this.Show(); - this.Activate(); - this.TextBoxInput.Text = "123"; - System.Diagnostics.Debug.Print("alt + d"); //复制内容 - //KeyboardUtil.Press(Key.LeftCtrl); - //KeyboardUtil.Type(Key.C); - //KeyboardUtil.Release(Key.LeftCtrl); + KeyboardUtil.Press(Key.LeftCtrl); + KeyboardUtil.Type(Key.C); + KeyboardUtil.Release(Key.LeftCtrl); + System.Diagnostics.Debug.Print(Clipboard.GetText()); //this.Show(); //this.Activate(); - + //this.TextBoxInput.Text = "123"; //this.TextBoxInput.Text = Clipboard.GetText(); //this.TextBoxInput.Focus(); @@ -85,6 +81,7 @@ namespace STranslate if (e.Key == Key.Escape) { this.Hide(); + this.TextBoxInput.Text = string.Empty; this.TextBoxOutput.Text = string.Empty; } //退出 Ctrl+Q @@ -141,8 +138,9 @@ namespace STranslate /// private void Window_Deactivated(object sender, EventArgs e) { - this.Hide(); - this.TextBoxOutput.Text = string.Empty; + //this.Hide(); + //this.TextBoxInput.Text = string.Empty; + //this.TextBoxOutput.Text = string.Empty; } } } diff --git a/STranslate/Model/LanguageEnum.cs b/STranslate/Model/LanguageEnum.cs index d37c20c..fa50b3e 100644 --- a/STranslate/Model/LanguageEnum.cs +++ b/STranslate/Model/LanguageEnum.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -8,30 +9,65 @@ namespace STranslate.Model { public enum LanguageEnum { - auto, //自动 + [Description("自动")] + AUTO, //自动 + [Description("德语")] DE, //德语 + [Description("英语")] EN, //英语 + [Description("西班牙语")] ES, //西班牙语 + [Description("法语")] FR, //法语 + [Description("意大利语")] IT, //意大利语 + [Description("日语")] JA, //日语 + [Description("荷兰语")] NL, //荷兰语 + [Description("波兰语")] PL, //波兰语 + [Description("葡萄牙语")] PT, //葡萄牙语 + [Description("俄语")] RU, //俄语 + [Description("中文")] ZH, //中文 + [Description("保加利亚语")] BG, //保加利亚语 + [Description("捷克语")] CS, //捷克语 + [Description("丹麦语")] DA, //丹麦语 + [Description("希腊语")] EL, //希腊语 + [Description("爱沙尼亚语")] ET, //爱沙尼亚语 + [Description("芬兰语")] FI, //芬兰语 + [Description("匈牙利语")] HU, //匈牙利语 + [Description("立陶宛语")] LT, //立陶宛语 + [Description("拉脱维亚语")] LV, //拉脱维亚语 + [Description("罗马尼亚语")] RO, //罗马尼亚语 + [Description("斯洛伐克语")] SK, //斯洛伐克语 + [Description("斯洛文尼亚语")] SL, //斯洛文尼亚语 + [Description("瑞典语")] SV, //瑞典语 } + //主要这个GetDescription()方法,看不懂没关系,如何调用 + static class EnumExtensions + { + public static string GetDescription(this Enum val) + { + var field = val.GetType().GetField(val.ToString()); + var customAttribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)); + return customAttribute == null ? val.ToString() : ((DescriptionAttribute)customAttribute).Description; + } + } } diff --git a/STranslate/Utils/HotKeysUtil.cs b/STranslate/Utils/HotKeysUtil.cs index 153c550..bb51fff 100644 --- a/STranslate/Utils/HotKeysUtil.cs +++ b/STranslate/Utils/HotKeysUtil.cs @@ -7,8 +7,9 @@ using System.Windows.Interop; namespace STranslate.Utils { + //TODO: 另一个方案: https://www.cnblogs.com/leolion/p/4693514.html /// - /// 引用自 https://zhuanlan.zhihu.com/p/445050708 + /// 引用自 https://blog.csdn.net/weixin_44879611/article/details/103275347 /// static class HotkeysUtil { diff --git a/STranslate/Utils/KeyboardUtil.cs b/STranslate/Utils/KeyboardUtil.cs index a8bcf05..66653af 100644 --- a/STranslate/Utils/KeyboardUtil.cs +++ b/STranslate/Utils/KeyboardUtil.cs @@ -14,7 +14,7 @@ namespace STranslate.Utils #region Operate Mouse Keyboard - + //引用自: https://www.cnblogs.com/sixty/archive/2009/08/09/1542210.html /// /// Native methods /// diff --git a/STranslate/Utils/TranslateUtil.cs b/STranslate/Utils/TranslateUtil.cs index 93990ac..e7706db 100644 --- a/STranslate/Utils/TranslateUtil.cs +++ b/STranslate/Utils/TranslateUtil.cs @@ -31,7 +31,7 @@ namespace STranslate.Utils return string.Empty; } - public static async Task TranslateDeepLAsync(string text, LanguageEnum target, LanguageEnum source = LanguageEnum.auto) + public static async Task TranslateDeepLAsync(string text, LanguageEnum target, LanguageEnum source = LanguageEnum.AUTO) { var req = new DeeplReq() { @@ -50,7 +50,16 @@ namespace STranslate.Utils return string.Empty; } - public static async Task TranslateBaiduAsync(string appID, string secretKey, string text, LanguageEnum target, LanguageEnum source = LanguageEnum.auto) + /// + /// 百度翻译异步接口 + /// + /// 应用ID + /// 应用Secret + /// 需要翻译的文本 + /// 目标语言 + /// 当前语言 + /// + 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(); @@ -102,5 +111,20 @@ namespace STranslate.Utils // 返回加密的字符串 return sb.ToString(); } + /// + /// 枚举信息 + /// + /// + /// + public static Dictionary GetEnumList() where T : Enum + { + var dict = new Dictionary(); + List list = Enum.GetValues(typeof(T)).OfType().ToList(); + list.ForEach(x => + { + dict.Add(x.GetDescription(), x); + }); + return dict; + } } } diff --git a/STranslate/ViewModel/MainVM.cs b/STranslate/ViewModel/MainVM.cs index 450e5fc..947a992 100644 --- a/STranslate/ViewModel/MainVM.cs +++ b/STranslate/ViewModel/MainVM.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Windows; using System.Windows.Input; namespace STranslate.ViewModel @@ -13,12 +14,19 @@ namespace STranslate.ViewModel { private static readonly string ConfigPath = @"D:\STranslate.yml"; public static ConfigModel config = new ConfigModel(); + private static Dictionary LanguageEnumDict { get => TranslateUtil.GetEnumList(); } private string Text; public MainVM() { try { + //初始化界面参数 + InputCombo = LanguageEnumDict.Keys.ToList(); + InputComboSelected = LanguageEnum.AUTO.GetDescription(); + OutputCombo = LanguageEnumDict.Keys.ToList(); + OutputComboSelected = LanguageEnum.EN.GetDescription(); + config = ConfigUtil.ReadConfig(ConfigPath); @@ -37,7 +45,7 @@ namespace STranslate.ViewModel //获取结果 //var translateResp = await TranslateUtil.TranslateDeepLAsync(InputTxt, LanguageEnum.EN, LanguageEnum.auto); - var translateResp = await TranslateUtil.TranslateBaiduAsync(config.baidu.appid, config.baidu.secretKey, Text, LanguageEnum.EN, LanguageEnum.auto); + var translateResp = await TranslateUtil.TranslateBaiduAsync(config.baidu.appid, config.baidu.secretKey, Text, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]); if (translateResp == string.Empty) { @@ -45,7 +53,9 @@ namespace STranslate.ViewModel return; } OutputTxt = translateResp; + Clipboard.SetText(OutputTxt); }); + } catch (Exception ex) { @@ -57,8 +67,21 @@ namespace STranslate.ViewModel private string _InputTxt; public string InputTxt { get => _InputTxt; set => UpdateProperty(ref _InputTxt, value); } + private string _OutputTxt; public string OutputTxt { get => _OutputTxt; set => UpdateProperty(ref _OutputTxt, value); } + private List _InputCombo; + public List InputCombo { get => _InputCombo; set => UpdateProperty(ref _InputCombo, value); } + + private string _InputComboSelected; + public string InputComboSelected { get => _InputComboSelected; set => UpdateProperty(ref _InputComboSelected, value); } + + private List _OutputCombo; + public List OutputCombo { get => _OutputCombo; set => UpdateProperty(ref _OutputCombo, value); } + + private string _OutputComboSelected; + public string OutputComboSelected { get => _OutputComboSelected; set => UpdateProperty(ref _OutputComboSelected, value); } + } }