diff --git a/README.md b/README.md index 3bd6b0f..ed679fc 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -WPF 开发的一款即用即走的翻译工具 +WPF 开发的一款即开即用、即用即走的翻译工具 ## 功能 @@ -8,8 +8,9 @@ WPF 开发的一款即用即走的翻译工具 - [x] 实现基本翻译功能 - [x] 优化防止多开 - [x] 添加划词翻译 -- [ ] 添加复制结果蛇形、大小驼峰 -- [ ] 优化 DeepL 服务并打包成库引入进项目 +- [x] 添加复制结果蛇形、大小驼峰 +- [x] 优化 DeepL 服务并打包成库引入进项目 +> 自己构造的接口测试下来还是不够稳定,本来也是启发于 [ZU1k](https://github.com/zu1k) 就先用他开发的接口吧 - [ ] 添加 OCR 翻译 - [ ] 优化划词翻译(未选中则取消翻译) - [ ] 添加软件缓存功能 @@ -22,11 +23,9 @@ WPF 开发的一款即用即走的翻译工具 ## 使用 -首先在软件运行目录的 `STranslate.yml` 中添加自己的配置,这里推荐百度通用翻译 API 免费版 [申请教程(参照Bob WIKI)](https://bobtranslate.com/service/translate/baidu.html) - 打开软件后会静默在后台,等待调用 -1. 全局监听快捷键(日后升级成自定义) -- `Alt` + `A` 打开软件主界面,输入需要翻译的内容,选择目标语言,如果识别语种不正确则手动指定即可,翻译结束后选择复制或点击一键复制即可 +1. 全局监听快捷键 +- `Alt` + `A` 打开软件界面,输入内容按回车翻译 - `Alt` + `D` 复制当前鼠标选中内容并翻译 2. 软件内快捷键 @@ -39,5 +38,6 @@ WPF 开发的一款即用即走的翻译工具 ## 参考项目 监听全局快捷键功能参考如下项目,在此表示感谢 - -- [https://github.com/NPCDW/WpfTool](https://github.com/NPCDW/WpfTool) \ No newline at end of file +- [https://github.com/NPCDW/WpfTool](https://github.com/NPCDW/WpfTool) +翻译接口使用项目 [ZU1K](https://github.com/zu1k/removed-2022-07-12/releases/tag/0.1.2) +- [https://github.com/zu1k/removed-2022-07-12/releases/tag/0.1.2](https://github.com/zu1k/removed-2022-07-12/releases/tag/0.1.2) \ No newline at end of file diff --git a/STranslate/Images/copy.png b/STranslate/Images/copy.png deleted file mode 100644 index 9e3a2b3..0000000 Binary files a/STranslate/Images/copy.png and /dev/null differ diff --git a/STranslate/Images/copy_h.png b/STranslate/Images/copy_h.png new file mode 100644 index 0000000..4938c34 Binary files /dev/null and b/STranslate/Images/copy_h.png differ diff --git a/STranslate/Images/copy_h2.png b/STranslate/Images/copy_h2.png new file mode 100644 index 0000000..4353dbd Binary files /dev/null and b/STranslate/Images/copy_h2.png differ diff --git a/STranslate/Images/copy_s.png b/STranslate/Images/copy_s.png new file mode 100644 index 0000000..b834f50 Binary files /dev/null and b/STranslate/Images/copy_s.png differ diff --git a/STranslate/MainWindow.xaml b/STranslate/MainWindow.xaml index 9af804e..bbee53d 100644 --- a/STranslate/MainWindow.xaml +++ b/STranslate/MainWindow.xaml @@ -108,12 +108,7 @@ HorizontalAlignment="Left" VerticalAlignment="Bottom" Command="{Binding CopyInputCmd}"> - - - - - - + @@ -186,14 +181,9 @@ + + diff --git a/STranslate/MainWindow.xaml.cs b/STranslate/MainWindow.xaml.cs index 78fcdcf..fe3a4df 100644 --- a/STranslate/MainWindow.xaml.cs +++ b/STranslate/MainWindow.xaml.cs @@ -42,6 +42,8 @@ namespace STranslate notifyIcon.Icon = new System.Drawing.Icon(System.Windows.Application.GetResourceStream(new Uri("Images/translate.ico", UriKind.Relative)).Stream); notifyIcon.Visible = true; + notifyIcon.MouseDoubleClick += NotifyIcon_MouseDoubleClick; + System.Windows.Forms.MenuItem CrossWordTranslateMenuItemBTN = new System.Windows.Forms.MenuItem("划词翻译"); CrossWordTranslateMenuItemBTN.Click += new EventHandler(CrossWordTranslateMenuItem_Click); @@ -68,17 +70,6 @@ namespace STranslate } - /// - /// 显示主窗口 - /// - /// - /// - private void OpenMainWin_Click(object sender, EventArgs e) - { - this.Show(); - this.Activate(); - } - /// /// 移动 /// @@ -182,10 +173,32 @@ namespace STranslate { vm.InputTxt = string.Empty; vm.OutputTxt = string.Empty; - vm.HumpRet = string.Empty; + vm.SmallHumpRet = string.Empty; vm.SnakeRet = string.Empty; } + /// + /// 显示主窗口 + /// + /// + /// + private void OpenMainWin_Click(object sender, EventArgs e) + { + this.Show(); + this.Activate(); + } + + + /// + /// 左键双击 + /// + /// + /// + private void NotifyIcon_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e) + { + OpenMainWin_Click(null, null); + } + /// /// 输入翻译 /// diff --git a/STranslate/Properties/Resources.Designer.cs b/STranslate/Properties/Resources.Designer.cs index 984f0aa..c691968 100644 --- a/STranslate/Properties/Resources.Designer.cs +++ b/STranslate/Properties/Resources.Designer.cs @@ -63,9 +63,29 @@ namespace STranslate.Properties { /// /// 查找 System.Drawing.Bitmap 类型的本地化资源。 /// - internal static System.Drawing.Bitmap copy { + internal static System.Drawing.Bitmap copy_h { get { - object obj = ResourceManager.GetObject("copy", resourceCulture); + object obj = ResourceManager.GetObject("copy_h", resourceCulture); + return ((System.Drawing.Bitmap)(obj)); + } + } + + /// + /// 查找 System.Drawing.Bitmap 类型的本地化资源。 + /// + internal static System.Drawing.Bitmap copy_h2 { + get { + object obj = ResourceManager.GetObject("copy_h2", resourceCulture); + return ((System.Drawing.Bitmap)(obj)); + } + } + + /// + /// 查找 System.Drawing.Bitmap 类型的本地化资源。 + /// + internal static System.Drawing.Bitmap copy_s { + get { + object obj = ResourceManager.GetObject("copy_s", resourceCulture); return ((System.Drawing.Bitmap)(obj)); } } diff --git a/STranslate/Properties/Resources.resx b/STranslate/Properties/Resources.resx index 3ae3893..fdc2bb6 100644 --- a/STranslate/Properties/Resources.resx +++ b/STranslate/Properties/Resources.resx @@ -118,12 +118,18 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - ..\images\copy.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a - ..\images\copy2.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + ..\images\copy_h.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + + ..\images\copy_h2.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + + ..\images\copy_s.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + ..\images\crossword.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a diff --git a/STranslate/STranslate.csproj b/STranslate/STranslate.csproj index 7178ec4..7a42658 100644 --- a/STranslate/STranslate.csproj +++ b/STranslate/STranslate.csproj @@ -134,9 +134,6 @@ SettingsSingleFileGenerator Settings.Designer.cs - - PreserveNewest - PreserveNewest @@ -161,6 +158,15 @@ PreserveNewest + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + PreserveNewest diff --git a/STranslate/STranslate.yml b/STranslate/STranslate.yml index 8818743..b36eebd 100644 --- a/STranslate/STranslate.yml +++ b/STranslate/STranslate.yml @@ -1,6 +1,6 @@ -service: baidu +service: deepl baidu: - appid: 20220608001242348 - secretKey: nN2CGpQCbTodUnKDW04k + appid: + secretKey: deepl: url: http://127.0.0.1:8000/translate \ No newline at end of file diff --git a/STranslate/ViewModel/MainVM.cs b/STranslate/ViewModel/MainVM.cs index 7263020..9f663f9 100644 --- a/STranslate/ViewModel/MainVM.cs +++ b/STranslate/ViewModel/MainVM.cs @@ -61,13 +61,21 @@ namespace STranslate.ViewModel { Clipboard.SetText(SnakeRet); }); - //复制驼峰结果 - CopyHumpResultCmd = new RelayCommand((_) => + //复制小驼峰结果 + CopySmallHumpResultCmd = new RelayCommand((_) => { - return string.IsNullOrEmpty(HumpRet) ? false : true; + return string.IsNullOrEmpty(SmallHumpRet) ? false : true; }, (_) => { - Clipboard.SetText(HumpRet); + Clipboard.SetText(SmallHumpRet); + }); + //复制大驼峰结果 + CopyLargeHumpResultCmd = new RelayCommand((_) => + { + return string.IsNullOrEmpty(LargeHumpRet) ? false : true; + }, (_) => + { + Clipboard.SetText(LargeHumpRet); }); //翻译 @@ -104,8 +112,9 @@ namespace STranslate.ViewModel var splitList = OutputTxt.Split(' ').ToList(); if (splitList.Count > 1) { - //SnakeRet = GenSnakeString(splitList); - //HumpRet = GenHumpString(splitList); + SnakeRet = GenSnakeString(splitList); + SmallHumpRet = GenHumpString(splitList, true); //小驼峰 + LargeHumpRet = GenHumpString(splitList, false); //大驼峰 } } catch (Exception ex) @@ -129,23 +138,30 @@ namespace STranslate.ViewModel }); return ret.Substring(1); } + /// /// 构造驼峰结果 /// /// + /// 是否为小驼峰 /// - private string GenHumpString(List req) + private string GenHumpString(List req, bool isSmallHump) { - //TODO: I'm your father 出错情况 - //TODO: 构造出错 - var ret = string.Empty; - var arr = req.ToArray(); - ret += arr[0].Substring(0, 1).ToLower() + arr[0].Substring(1); - for (int i = 1; i < arr.Length; i++) + string ret = string.Empty; + var array = req.ToArray(); + for (var j = 0; j < array.Length; j++) { - ret += arr[i].Substring(0, 1).ToUpper() + arr[0].Substring(1); + char[] chars = array[j].ToCharArray(); + if (j == 0 && isSmallHump) chars[0] = char.ToLower(chars[0]); + chars[0] = char.ToUpper(chars[0]); + for (int i = 1; i < chars.Length; i++) + { + chars[i] = char.ToLower(chars[i]); + } + ret += new string(chars); } return ret; + } #endregion handle @@ -156,12 +172,24 @@ namespace STranslate.ViewModel public ICommand CopyInputCmd { get; private set; } public ICommand CopyResultCmd { get; private set; } public ICommand CopySnakeResultCmd { get; private set; } - public ICommand CopyHumpResultCmd { get; private set; } + public ICommand CopySmallHumpResultCmd { get; private set; } + public ICommand CopyLargeHumpResultCmd { get; private set; } + /// + /// 构造蛇形结果 + /// private string _SnakeRet; public string SnakeRet { get => _SnakeRet; set => UpdateProperty(ref _SnakeRet, value); } - private string _HumpRet; - public string HumpRet { get => _HumpRet; set => UpdateProperty(ref _HumpRet, value); } + /// + /// 构造驼峰结果 + /// + private string _SmallHumpRet; + public string SmallHumpRet { get => _SmallHumpRet; set => UpdateProperty(ref _SmallHumpRet, value); } + /// + /// 构造驼峰结果 + /// + private string _LargeHumpRet; + public string LargeHumpRet { get => _LargeHumpRet; set => UpdateProperty(ref _LargeHumpRet, value); } private string _InputTxt; public string InputTxt { get => _InputTxt; set => UpdateProperty(ref _InputTxt, value); }