feat: add multiple replication methods

dev_without_third_libraries
DESKTOP-3BO4HSG\ksat 2 years ago
parent 5712cd7bd9
commit 8510b50cd0

@ -1,5 +1,5 @@
WPF 开发的一款即用即走的翻译工具 WPF 开发的一款即开即用、即用即走的翻译工具
## 功能 ## 功能
@ -8,8 +8,9 @@ WPF 开发的一款即用即走的翻译工具
- [x] 实现基本翻译功能 - [x] 实现基本翻译功能
- [x] 优化防止多开 - [x] 优化防止多开
- [x] 添加划词翻译 - [x] 添加划词翻译
- [ ] 添加复制结果蛇形、大小驼峰 - [x] 添加复制结果蛇形、大小驼峰
- [ ] 优化 DeepL 服务并打包成库引入进项目 - [x] 优化 DeepL 服务并打包成库引入进项目
> 自己构造的接口测试下来还是不够稳定,本来也是启发于 [ZU1k](https://github.com/zu1k) 就先用他开发的接口吧
- [ ] 添加 OCR 翻译 - [ ] 添加 OCR 翻译
- [ ] 优化划词翻译(未选中则取消翻译) - [ ] 优化划词翻译(未选中则取消翻译)
- [ ] 添加软件缓存功能 - [ ] 添加软件缓存功能
@ -22,11 +23,9 @@ WPF 开发的一款即用即走的翻译工具
## 使用 ## 使用
首先在软件运行目录的 `STranslate.yml` 中添加自己的配置,这里推荐百度通用翻译 API 免费版 [申请教程参照Bob WIKI](https://bobtranslate.com/service/translate/baidu.html)
打开软件后会静默在后台,等待调用 打开软件后会静默在后台,等待调用
1. 全局监听快捷键(日后升级成自定义) 1. 全局监听快捷键
- `Alt` + `A` 打开软件主界面,输入需要翻译的内容,选择目标语言,如果识别语种不正确则手动指定即可,翻译结束后选择复制或点击一键复制即可 - `Alt` + `A` 打开软件界面,输入内容按回车翻译
- `Alt` + `D` 复制当前鼠标选中内容并翻译 - `Alt` + `D` 复制当前鼠标选中内容并翻译
2. 软件内快捷键 2. 软件内快捷键
@ -39,5 +38,6 @@ WPF 开发的一款即用即走的翻译工具
## 参考项目 ## 参考项目
监听全局快捷键功能参考如下项目,在此表示感谢 监听全局快捷键功能参考如下项目,在此表示感谢
- [https://github.com/NPCDW/WpfTool](https://github.com/NPCDW/WpfTool) - [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)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

@ -109,11 +109,6 @@
VerticalAlignment="Bottom" VerticalAlignment="Bottom"
Command="{Binding CopyInputCmd}"> Command="{Binding CopyInputCmd}">
<Image Source="/Images/copy2.png"/> <Image Source="/Images/copy2.png"/>
<hc:FloatingBlock.ContentTemplate>
<DataTemplate>
<TextBlock Text="已复制" />
</DataTemplate>
</hc:FloatingBlock.ContentTemplate>
</Button> </Button>
</Grid> </Grid>
</Border> </Border>
@ -186,14 +181,9 @@
<Button Cursor="Hand" <Button Cursor="Hand"
HorizontalAlignment="Left" HorizontalAlignment="Left"
VerticalAlignment="Bottom" VerticalAlignment="Bottom"
ToolTip="普通" ToolTip="直接复制"
Command="{Binding CopyResultCmd}"> Command="{Binding CopyResultCmd}">
<Image Source="/Images/copy2.png"/> <Image Source="/Images/copy2.png"/>
<hc:FloatingBlock.ContentTemplate>
<DataTemplate>
<TextBlock Text="已复制" />
</DataTemplate>
</hc:FloatingBlock.ContentTemplate>
</Button> </Button>
<!--蛇形复制--> <!--蛇形复制-->
<Button Cursor="Hand" <Button Cursor="Hand"
@ -202,25 +192,23 @@
VerticalAlignment="Bottom" VerticalAlignment="Bottom"
ToolTip="蛇形" ToolTip="蛇形"
Command="{Binding CopySnakeResultCmd}"> Command="{Binding CopySnakeResultCmd}">
<Image Source="/Images/copy2.png"/> <Image Source="/Images/copy_s.png"/>
<hc:FloatingBlock.ContentTemplate>
<DataTemplate>
<TextBlock Text="已复制" />
</DataTemplate>
</hc:FloatingBlock.ContentTemplate>
</Button> </Button>
<!--驼峰复制--> <!--驼峰复制-->
<Button Cursor="Hand" <Button Cursor="Hand"
HorizontalAlignment="Left" HorizontalAlignment="Left"
VerticalAlignment="Bottom" VerticalAlignment="Bottom"
ToolTip="驼峰" ToolTip="小驼峰"
Command="{Binding CopyHumpResultCmd}"> Command="{Binding CopySmallHumpResultCmd}">
<Image Source="/Images/copy2.png"/> <Image Source="/Images/copy_h.png"/>
<hc:FloatingBlock.ContentTemplate> </Button>
<DataTemplate> <!--驼峰复制-->
<TextBlock Text="已复制" /> <Button Cursor="Hand"
</DataTemplate> HorizontalAlignment="Left"
</hc:FloatingBlock.ContentTemplate> VerticalAlignment="Bottom"
ToolTip="大驼峰"
Command="{Binding CopySmallHumpResultCmd}">
<Image Source="/Images/copy_h2.png"/>
</Button> </Button>
</StackPanel> </StackPanel>
</Grid> </Grid>

@ -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.Icon = new System.Drawing.Icon(System.Windows.Application.GetResourceStream(new Uri("Images/translate.ico", UriKind.Relative)).Stream);
notifyIcon.Visible = true; notifyIcon.Visible = true;
notifyIcon.MouseDoubleClick += NotifyIcon_MouseDoubleClick;
System.Windows.Forms.MenuItem CrossWordTranslateMenuItemBTN = new System.Windows.Forms.MenuItem("划词翻译"); System.Windows.Forms.MenuItem CrossWordTranslateMenuItemBTN = new System.Windows.Forms.MenuItem("划词翻译");
CrossWordTranslateMenuItemBTN.Click += new EventHandler(CrossWordTranslateMenuItem_Click); CrossWordTranslateMenuItemBTN.Click += new EventHandler(CrossWordTranslateMenuItem_Click);
@ -68,17 +70,6 @@ namespace STranslate
} }
/// <summary>
/// 显示主窗口
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OpenMainWin_Click(object sender, EventArgs e)
{
this.Show();
this.Activate();
}
/// <summary> /// <summary>
/// 移动 /// 移动
/// </summary> /// </summary>
@ -182,10 +173,32 @@ namespace STranslate
{ {
vm.InputTxt = string.Empty; vm.InputTxt = string.Empty;
vm.OutputTxt = string.Empty; vm.OutputTxt = string.Empty;
vm.HumpRet = string.Empty; vm.SmallHumpRet = string.Empty;
vm.SnakeRet = string.Empty; vm.SnakeRet = string.Empty;
} }
/// <summary>
/// 显示主窗口
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OpenMainWin_Click(object sender, EventArgs e)
{
this.Show();
this.Activate();
}
/// <summary>
/// 左键双击
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void NotifyIcon_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
OpenMainWin_Click(null, null);
}
/// <summary> /// <summary>
/// 输入翻译 /// 输入翻译
/// </summary> /// </summary>

@ -63,9 +63,29 @@ namespace STranslate.Properties {
/// <summary> /// <summary>
/// 查找 System.Drawing.Bitmap 类型的本地化资源。 /// 查找 System.Drawing.Bitmap 类型的本地化资源。
/// </summary> /// </summary>
internal static System.Drawing.Bitmap copy { internal static System.Drawing.Bitmap copy_h {
get { get {
object obj = ResourceManager.GetObject("copy", resourceCulture); object obj = ResourceManager.GetObject("copy_h", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
/// <summary>
/// 查找 System.Drawing.Bitmap 类型的本地化资源。
/// </summary>
internal static System.Drawing.Bitmap copy_h2 {
get {
object obj = ResourceManager.GetObject("copy_h2", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
/// <summary>
/// 查找 System.Drawing.Bitmap 类型的本地化资源。
/// </summary>
internal static System.Drawing.Bitmap copy_s {
get {
object obj = ResourceManager.GetObject("copy_s", resourceCulture);
return ((System.Drawing.Bitmap)(obj)); return ((System.Drawing.Bitmap)(obj));
} }
} }

@ -118,12 +118,18 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="copy" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\images\copy.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="copy2" type="System.Resources.ResXFileRef, System.Windows.Forms"> <data name="copy2" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\images\copy2.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value> <value>..\images\copy2.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data> </data>
<data name="copy_h" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\images\copy_h.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="copy_h2" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\images\copy_h2.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="copy_s" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\images\copy_s.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="crossword" type="System.Resources.ResXFileRef, System.Windows.Forms"> <data name="crossword" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\images\crossword.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value> <value>..\images\crossword.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data> </data>

@ -134,9 +134,6 @@
<Generator>SettingsSingleFileGenerator</Generator> <Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput> <LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None> </None>
<Resource Include="Images\copy.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
<Resource Include="Images\crossword.ico"> <Resource Include="Images\crossword.ico">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource> </Resource>
@ -161,6 +158,15 @@
<Resource Include="Images\copy2.png"> <Resource Include="Images\copy2.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource> </Resource>
<Resource Include="Images\copy_h.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
<Resource Include="Images\copy_h2.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
<Resource Include="Images\copy_s.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
<Content Include="Static\deepl-x86_64-pc-windows-gnu.exe"> <Content Include="Static\deepl-x86_64-pc-windows-gnu.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>

@ -1,6 +1,6 @@
service: baidu service: deepl
baidu: baidu:
appid: 20220608001242348 appid:
secretKey: nN2CGpQCbTodUnKDW04k secretKey:
deepl: deepl:
url: http://127.0.0.1:8000/translate url: http://127.0.0.1:8000/translate

@ -61,13 +61,21 @@ namespace STranslate.ViewModel
{ {
Clipboard.SetText(SnakeRet); 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(); var splitList = OutputTxt.Split(' ').ToList();
if (splitList.Count > 1) if (splitList.Count > 1)
{ {
//SnakeRet = GenSnakeString(splitList); SnakeRet = GenSnakeString(splitList);
//HumpRet = GenHumpString(splitList); SmallHumpRet = GenHumpString(splitList, true); //小驼峰
LargeHumpRet = GenHumpString(splitList, false); //大驼峰
} }
} }
catch (Exception ex) catch (Exception ex)
@ -129,23 +138,30 @@ namespace STranslate.ViewModel
}); });
return ret.Substring(1); return ret.Substring(1);
} }
/// <summary> /// <summary>
/// 构造驼峰结果 /// 构造驼峰结果
/// </summary> /// </summary>
/// <param name="req"></param> /// <param name="req"></param>
/// <param name="isSmallHump">是否为小驼峰</param>
/// <returns></returns> /// <returns></returns>
private string GenHumpString(List<string> req) private string GenHumpString(List<string> req, bool isSmallHump)
{ {
//TODO: I'm your father 出错情况 string ret = string.Empty;
//TODO: 构造出错 var array = req.ToArray();
var ret = string.Empty; for (var j = 0; j < array.Length; j++)
var arr = req.ToArray(); {
ret += arr[0].Substring(0, 1).ToLower() + arr[0].Substring(1); char[] chars = array[j].ToCharArray();
for (int i = 1; i < arr.Length; i++) if (j == 0 && isSmallHump) chars[0] = char.ToLower(chars[0]);
chars[0] = char.ToUpper(chars[0]);
for (int i = 1; i < chars.Length; i++)
{ {
ret += arr[i].Substring(0, 1).ToUpper() + arr[0].Substring(1); chars[i] = char.ToLower(chars[i]);
}
ret += new string(chars);
} }
return ret; return ret;
} }
#endregion handle #endregion handle
@ -156,12 +172,24 @@ namespace STranslate.ViewModel
public ICommand CopyInputCmd { get; private set; } public ICommand CopyInputCmd { get; private set; }
public ICommand CopyResultCmd { get; private set; } public ICommand CopyResultCmd { get; private set; }
public ICommand CopySnakeResultCmd { 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; }
/// <summary>
/// 构造蛇形结果
/// </summary>
private string _SnakeRet; private string _SnakeRet;
public string SnakeRet { get => _SnakeRet; set => UpdateProperty(ref _SnakeRet, value); } public string SnakeRet { get => _SnakeRet; set => UpdateProperty(ref _SnakeRet, value); }
private string _HumpRet; /// <summary>
public string HumpRet { get => _HumpRet; set => UpdateProperty(ref _HumpRet, value); } /// 构造驼峰结果
/// </summary>
private string _SmallHumpRet;
public string SmallHumpRet { get => _SmallHumpRet; set => UpdateProperty(ref _SmallHumpRet, value); }
/// <summary>
/// 构造驼峰结果
/// </summary>
private string _LargeHumpRet;
public string LargeHumpRet { get => _LargeHumpRet; set => UpdateProperty(ref _LargeHumpRet, value); }
private string _InputTxt; private string _InputTxt;
public string InputTxt { get => _InputTxt; set => UpdateProperty(ref _InputTxt, value); } public string InputTxt { get => _InputTxt; set => UpdateProperty(ref _InputTxt, value); }

Loading…
Cancel
Save