feat: add source and target language

master
ZGGSONG 2 years ago
parent eed26e82a8
commit 054336d85f

@ -28,8 +28,9 @@
CornerRadius="10"> CornerRadius="10">
<Grid> <Grid>
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="3*"/>
<RowDefinition Height="5*"/> <RowDefinition Height="5*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="8*"/>
</Grid.RowDefinitions> </Grid.RowDefinitions>
<hc:NotifyIcon Text="STranslate" <hc:NotifyIcon Text="STranslate"
@ -39,20 +40,21 @@
Icon="/Images/translate.ico"/> Icon="/Images/translate.ico"/>
<!--input--> <!--input-->
<Grid Margin="10"> <Grid x:Name="Input"
Margin="10,10,10,0">
<!--输入框--> <!--输入框-->
<Border BorderBrush="#000" <Border x:Name="BorderInput" BorderBrush="#000"
BorderThickness="1" BorderThickness="1"
CornerRadius="4" CornerRadius="4"
Margin="5" Margin="5"
MaxHeight="200"> Height="{Binding ElementName=Input, Path=Height}">
<TextBox x:Name="TextBoxInput" <TextBox x:Name="TextBoxInput"
Background="Transparent" Background="Transparent"
BorderBrush="{x:Null}" BorderBrush="{x:Null}"
Margin="5" Margin="5"
BorderThickness="0" BorderThickness="0"
FontSize="18" FontSize="18"
MinHeight="35" Width="{Binding ElementName=BorderInput, Path=ActualWidth}"
TextWrapping="Wrap" TextWrapping="Wrap"
VerticalAlignment="Top" VerticalAlignment="Top"
HorizontalAlignment="Left" HorizontalAlignment="Left"
@ -64,19 +66,36 @@
</TextBox> </TextBox>
</Border> </Border>
</Grid> </Grid>
<!--output--> <!--mid-->
<Grid Margin="10" <StackPanel Grid.Row="1"
Grid.Row="1"> Margin="10,0,10,0"
Orientation="Horizontal">
<Label Content="翻译结果: " <Label Content="翻译结果: "
VerticalAlignment="Top" VerticalAlignment="Center"
HorizontalAlignment="Left"/> HorizontalAlignment="Left"/>
<Label Content="当前语言:"
Margin="10,0,0,0"/>
<ComboBox x:Name="InCombo"
Style="{StaticResource ComboBoxBaseStyle}"
ItemsSource="{Binding InputCombo}"
SelectedItem="{Binding InputComboSelected}"/>
<Label Content="目标语言:"/>
<ComboBox x:Name="OutCombo"
Style="{StaticResource ComboBoxBaseStyle}"
ItemsSource="{Binding OutputCombo}"
SelectedItem="{Binding OutputComboSelected}"/>
</StackPanel>
<!--output-->
<Grid Margin="10,0,10,10"
x:Name="Output"
Grid.Row="2">
<!--输出框--> <!--输出框-->
<Border BorderBrush="#000" <Border x:Name="BorderOutput"
BorderBrush="#000"
BorderThickness="1" BorderThickness="1"
CornerRadius="4" CornerRadius="4"
Margin="5" Margin="5"
MaxHeight="200"> Height="{Binding ElementName=Output, Path=Height}">
<TextBox x:Name="TextBoxOutput" <TextBox x:Name="TextBoxOutput"
Background="Transparent" Background="Transparent"
BorderBrush="{x:Null}" BorderBrush="{x:Null}"
@ -84,7 +103,8 @@
IsReadOnly="True" IsReadOnly="True"
Margin="5" Margin="5"
FontSize="18" FontSize="18"
MinHeight="35" Width="{Binding ElementName=BorderOutput, Path=ActualWidth}"
Height="{Binding ElementName=BorderOutput, Path=Height}"
VerticalAlignment="Top" VerticalAlignment="Top"
HorizontalAlignment="Left" HorizontalAlignment="Left"
TextWrapping="Wrap" TextWrapping="Wrap"

@ -34,22 +34,18 @@ namespace STranslate
this.Show(); this.Show();
this.Activate(); this.Activate();
this.TextBoxInput.Focus(); this.TextBoxInput.Focus();
System.Diagnostics.Debug.Print("alt + a");
}); });
HotkeysUtil.Regist(HotkeyModifiers.MOD_ALT, Key.D, () => HotkeysUtil.Regist(HotkeyModifiers.MOD_ALT, Key.D, () =>
{ {
this.Show();
this.Activate();
this.TextBoxInput.Text = "123";
System.Diagnostics.Debug.Print("alt + d");
//复制内容 //复制内容
//KeyboardUtil.Press(Key.LeftCtrl); KeyboardUtil.Press(Key.LeftCtrl);
//KeyboardUtil.Type(Key.C); KeyboardUtil.Type(Key.C);
//KeyboardUtil.Release(Key.LeftCtrl); KeyboardUtil.Release(Key.LeftCtrl);
System.Diagnostics.Debug.Print(Clipboard.GetText());
//this.Show(); //this.Show();
//this.Activate(); //this.Activate();
//this.TextBoxInput.Text = "123";
//this.TextBoxInput.Text = Clipboard.GetText(); //this.TextBoxInput.Text = Clipboard.GetText();
//this.TextBoxInput.Focus(); //this.TextBoxInput.Focus();
@ -85,6 +81,7 @@ namespace STranslate
if (e.Key == Key.Escape) if (e.Key == Key.Escape)
{ {
this.Hide(); this.Hide();
this.TextBoxInput.Text = string.Empty;
this.TextBoxOutput.Text = string.Empty; this.TextBoxOutput.Text = string.Empty;
} }
//退出 Ctrl+Q //退出 Ctrl+Q
@ -141,8 +138,9 @@ namespace STranslate
/// <param name="e"></param> /// <param name="e"></param>
private void Window_Deactivated(object sender, EventArgs e) private void Window_Deactivated(object sender, EventArgs e)
{ {
this.Hide(); //this.Hide();
this.TextBoxOutput.Text = string.Empty; //this.TextBoxInput.Text = string.Empty;
//this.TextBoxOutput.Text = string.Empty;
} }
} }
} }

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -8,30 +9,65 @@ namespace STranslate.Model
{ {
public enum LanguageEnum public enum LanguageEnum
{ {
auto, //自动 [Description("自动")]
AUTO, //自动
[Description("德语")]
DE, //德语 DE, //德语
[Description("英语")]
EN, //英语 EN, //英语
[Description("西班牙语")]
ES, //西班牙语 ES, //西班牙语
[Description("法语")]
FR, //法语 FR, //法语
[Description("意大利语")]
IT, //意大利语 IT, //意大利语
[Description("日语")]
JA, //日语 JA, //日语
[Description("荷兰语")]
NL, //荷兰语 NL, //荷兰语
[Description("波兰语")]
PL, //波兰语 PL, //波兰语
[Description("葡萄牙语")]
PT, //葡萄牙语 PT, //葡萄牙语
[Description("俄语")]
RU, //俄语 RU, //俄语
[Description("中文")]
ZH, //中文 ZH, //中文
[Description("保加利亚语")]
BG, //保加利亚语 BG, //保加利亚语
[Description("捷克语")]
CS, //捷克语 CS, //捷克语
[Description("丹麦语")]
DA, //丹麦语 DA, //丹麦语
[Description("希腊语")]
EL, //希腊语 EL, //希腊语
[Description("爱沙尼亚语")]
ET, //爱沙尼亚语 ET, //爱沙尼亚语
[Description("芬兰语")]
FI, //芬兰语 FI, //芬兰语
[Description("匈牙利语")]
HU, //匈牙利语 HU, //匈牙利语
[Description("立陶宛语")]
LT, //立陶宛语 LT, //立陶宛语
[Description("拉脱维亚语")]
LV, //拉脱维亚语 LV, //拉脱维亚语
[Description("罗马尼亚语")]
RO, //罗马尼亚语 RO, //罗马尼亚语
[Description("斯洛伐克语")]
SK, //斯洛伐克语 SK, //斯洛伐克语
[Description("斯洛文尼亚语")]
SL, //斯洛文尼亚语 SL, //斯洛文尼亚语
[Description("瑞典语")]
SV, //瑞典语 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;
}
}
} }

@ -7,8 +7,9 @@ using System.Windows.Interop;
namespace STranslate.Utils namespace STranslate.Utils
{ {
//TODO: 另一个方案: https://www.cnblogs.com/leolion/p/4693514.html
/// <summary> /// <summary>
/// 引用自 https://zhuanlan.zhihu.com/p/445050708 /// 引用自 https://blog.csdn.net/weixin_44879611/article/details/103275347
/// </summary> /// </summary>
static class HotkeysUtil static class HotkeysUtil
{ {

@ -14,7 +14,7 @@ namespace STranslate.Utils
#region Operate Mouse Keyboard #region Operate Mouse Keyboard
//引用自: https://www.cnblogs.com/sixty/archive/2009/08/09/1542210.html
/// <summary> /// <summary>
/// Native methods /// Native methods
/// </summary> /// </summary>

@ -31,7 +31,7 @@ namespace STranslate.Utils
return string.Empty; return string.Empty;
} }
public static async Task<string> TranslateDeepLAsync(string text, LanguageEnum target, LanguageEnum source = LanguageEnum.auto) public static async Task<string> TranslateDeepLAsync(string text, LanguageEnum target, LanguageEnum source = LanguageEnum.AUTO)
{ {
var req = new DeeplReq() var req = new DeeplReq()
{ {
@ -50,7 +50,16 @@ namespace STranslate.Utils
return string.Empty; return string.Empty;
} }
public static async Task<string> TranslateBaiduAsync(string appID, string secretKey, string text, LanguageEnum target, LanguageEnum source = LanguageEnum.auto) /// <summary>
/// 百度翻译异步接口
/// </summary>
/// <param name="appID">应用ID</param>
/// <param name="secretKey">应用Secret</param>
/// <param name="text">需要翻译的文本</param>
/// <param name="target">目标语言</param>
/// <param name="source">当前语言</param>
/// <returns></returns>
public static async Task<string> TranslateBaiduAsync(string appID, string secretKey, string text, LanguageEnum target, LanguageEnum source = LanguageEnum.AUTO)
{ {
Random rd = new Random(); Random rd = new Random();
string salt = rd.Next(100000).ToString(); string salt = rd.Next(100000).ToString();
@ -102,5 +111,20 @@ namespace STranslate.Utils
// 返回加密的字符串 // 返回加密的字符串
return sb.ToString(); return sb.ToString();
} }
/// <summary>
/// 枚举信息
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static Dictionary<string, T> GetEnumList<T>() where T : Enum
{
var dict = new Dictionary<string, T>();
List<T> list = Enum.GetValues(typeof(T)).OfType<T>().ToList();
list.ForEach(x =>
{
dict.Add(x.GetDescription(), x);
});
return dict;
}
} }
} }

@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input; using System.Windows.Input;
namespace STranslate.ViewModel namespace STranslate.ViewModel
@ -13,12 +14,19 @@ namespace STranslate.ViewModel
{ {
private static readonly string ConfigPath = @"D:\STranslate.yml"; private static readonly string ConfigPath = @"D:\STranslate.yml";
public static ConfigModel config = new ConfigModel(); public static ConfigModel config = new ConfigModel();
private static Dictionary<string, LanguageEnum> LanguageEnumDict { get => TranslateUtil.GetEnumList<LanguageEnum>(); }
private string Text; private string Text;
public MainVM() public MainVM()
{ {
try try
{ {
//初始化界面参数
InputCombo = LanguageEnumDict.Keys.ToList();
InputComboSelected = LanguageEnum.AUTO.GetDescription();
OutputCombo = LanguageEnumDict.Keys.ToList();
OutputComboSelected = LanguageEnum.EN.GetDescription();
config = ConfigUtil.ReadConfig(ConfigPath); 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.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) if (translateResp == string.Empty)
{ {
@ -45,7 +53,9 @@ namespace STranslate.ViewModel
return; return;
} }
OutputTxt = translateResp; OutputTxt = translateResp;
Clipboard.SetText(OutputTxt);
}); });
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -57,8 +67,21 @@ namespace STranslate.ViewModel
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); }
private string _OutputTxt; private string _OutputTxt;
public string OutputTxt { get => _OutputTxt; set => UpdateProperty(ref _OutputTxt, value); } public string OutputTxt { get => _OutputTxt; set => UpdateProperty(ref _OutputTxt, value); }
private List<string> _InputCombo;
public List<string> InputCombo { get => _InputCombo; set => UpdateProperty(ref _InputCombo, value); }
private string _InputComboSelected;
public string InputComboSelected { get => _InputComboSelected; set => UpdateProperty(ref _InputComboSelected, value); }
private List<string> _OutputCombo;
public List<string> OutputCombo { get => _OutputCombo; set => UpdateProperty(ref _OutputCombo, value); }
private string _OutputComboSelected;
public string OutputComboSelected { get => _OutputComboSelected; set => UpdateProperty(ref _OutputComboSelected, value); }
} }
} }