feat: add crosswordtranslate

dev_without_third_libraries
DESKTOP-3BO4HSG\ksat 2 years ago
parent 3bc94bfbe9
commit 2fe17e7f65

@ -30,7 +30,7 @@
<RowDefinition Height="8*" />
</Grid.RowDefinitions>
<hc:NotifyIcon Text="STranslate"
<!--<hc:NotifyIcon Text="STranslate"
Click="NotifyIcon_Click"
IsBlink="False"
Visibility="Visible"
@ -48,7 +48,7 @@
Header="退出" />
</ContextMenu>
</hc:NotifyIcon.ContextMenu>
</hc:NotifyIcon>
</hc:NotifyIcon>-->
<!--input-->
<Grid x:Name="Input"

@ -3,6 +3,7 @@ using STranslate.ViewModel;
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
namespace STranslate
{
@ -11,46 +12,66 @@ namespace STranslate
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitHwnd();
InitTray();
this.DataContext = MainVM.Instance;
}
private void InitHwnd()
{
var helper = new WindowInteropHelper(this);
helper.EnsureHandle();
}
/// <summary>
/// 监听全局快捷键
/// 初始化托盘
/// </summary>
/// <param name="e"></param>
protected override void OnSourceInitialized(EventArgs e)
private void InitTray()
{
HotkeysUtil.InitialHook(this);
HotkeysUtil.Regist(HotkeyModifiers.MOD_ALT, Key.A, () =>
{
this.Show();
this.Activate();
this.TextBoxInput.Focus();
});
#if false
HotkeysUtil.Regist(HotkeyModifiers.MOD_ALT, Key.D, () =>
{
//复制内容
KeyboardUtil.Press(Key.LeftCtrl);
KeyboardUtil.Type(Key.C);
KeyboardUtil.Release(Key.LeftCtrl);
System.Threading.Thread.Sleep(200);
System.Diagnostics.Debug.Print(Clipboard.GetText());
//notifyIcon.BalloonTipText = "程序开始运行";
//notifyIcon.ShowBalloonTip(1000);
notifyIcon.Text = "STranslate";
notifyIcon.Icon = new System.Drawing.Icon(@"D:\CodePepo\STranslate\STranslate\Images\translate2.ico");
notifyIcon.Visible = true;
//this.Show();
//this.Activate();
//this.TextBoxInput.Text = "123";
System.Windows.Forms.MenuItem inputTranslateButton = new System.Windows.Forms.MenuItem("输入翻译");
inputTranslateButton.Click += new EventHandler(InputTranslate_Click);
//this.TextBoxInput.Text = Clipboard.GetText();
//this.TextBoxInput.Focus();
System.Windows.Forms.MenuItem crosswordTranslateButton = new System.Windows.Forms.MenuItem("划词翻译");
crosswordTranslateButton.Click += new EventHandler(CrosswordTranslate_Click);
//KeyboardUtil.Type(Key.Enter);
});
#endif
}
System.Windows.Forms.MenuItem screenshotTranslationButton = new System.Windows.Forms.MenuItem("截图翻译");
screenshotTranslationButton.Click += new EventHandler(ScreenshotTranslation_Click);
public MainWindow()
System.Windows.Forms.MenuItem openMainWinButton = new System.Windows.Forms.MenuItem("显示主窗口");
openMainWinButton.Click += new EventHandler(OpenMainWin_Click);
System.Windows.Forms.MenuItem exitButton = new System.Windows.Forms.MenuItem("退出");
exitButton.Click += new EventHandler(Exit_Click);
System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] {
inputTranslateButton,
crosswordTranslateButton,
screenshotTranslationButton,
openMainWinButton,
exitButton
};
notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);
}
/// <summary>
/// 显示主窗口
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OpenMainWin_Click(object sender, EventArgs e)
{
InitializeComponent();
this.TextBoxInput.Focus();
this.DataContext = MainVM.Instance;
this.Show();
this.Activate();
}
/// <summary>
@ -62,9 +83,8 @@ namespace STranslate
{
DragMove();
}
/// <summary>
/// 快捷键
/// 软件运行时快捷键
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
@ -77,12 +97,12 @@ namespace STranslate
MainVM.Instance.InputTxt = string.Empty;
MainVM.Instance.OutputTxt = string.Empty;
}
//退出 Ctrl+Q
//退出 Ctrl+Shift+Q
if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control)
&& e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Shift)
&& e.Key == Key.Q)
{
Application.Current.Shutdown();
Exit_Click(null, null);
}
#if false
//置顶/取消置顶 Ctrl+T
@ -119,11 +139,71 @@ namespace STranslate
}
#endif
}
/// <summary>
/// 监听全局快捷键
/// </summary>
/// <param name="e"></param>
protected override void OnSourceInitialized(EventArgs e)
{
IntPtr handle = new WindowInteropHelper(this).Handle;
HotKeysUtil2.RegisterHotKey(handle);
private void NotifyIcon_Click(object sender, RoutedEventArgs e)
HwndSource source = HwndSource.FromHwnd(handle);
source.AddHook(WndProc);
#if false
HotkeysUtil.InitialHook(this);
HotkeysUtil.Regist(HotkeyModifiers.MOD_ALT, Key.A, () =>
{
this.Show();
this.Activate();
this.TextBoxInput.Focus();
});
HotkeysUtil.Regist(HotkeyModifiers.MOD_ALT, Key.D, () =>
{
//复制内容
KeyboardUtil.Press(Key.LeftCtrl);
KeyboardUtil.Type(Key.C);
KeyboardUtil.Release(Key.LeftCtrl);
System.Threading.Thread.Sleep(200);
System.Diagnostics.Debug.Print(Clipboard.GetText());
//this.Show();
//this.Activate();
//this.TextBoxInput.Text = "123";
//this.TextBoxInput.Text = Clipboard.GetText();
//this.TextBoxInput.Focus();
//KeyboardUtil.Type(Key.Enter);
});
#endif
}
/// <summary>
/// 热键的功能
/// </summary>
/// <param name="m"></param>
protected IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handle)
{
this.Show();
this.Activate();
switch (msg)
{
case 0x0312: //这个是window消息定义的 注册的热键消息
//Console.WriteLine(wParam.ToString());
if (wParam.ToString().Equals(HotKeysUtil2.InputTranslateId + ""))
{
this.InputTranslate_Click(null, null);
}
else if (wParam.ToString().Equals(HotKeysUtil2.CrosswordTranslateId + ""))
{
this.CrosswordTranslate_Click(null, null);
}
else if (wParam.ToString().Equals(HotKeysUtil2.ScreenShotTranslateId + ""))
{
this.ScreenshotTranslation_Click(null, null);
}
break;
}
return IntPtr.Zero;
}
/// <summary>
@ -134,8 +214,69 @@ namespace STranslate
private void Window_Deactivated(object sender, EventArgs e)
{
this.Hide();
}
/// <summary>
/// 截图翻译
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ScreenshotTranslation_Click(object sender, EventArgs e)
{
MessageBox.Show("开发中");
}
/// <summary>
/// 划词翻译
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CrosswordTranslate_Click(object sender, EventArgs e)
{
ClearTextBox();
var sentence = GetWords.Get();
this.Show();
this.Activate();
this.TextBoxInput.Text = sentence.Trim();
_ = MainVM.Instance.Translate();
}
/// <summary>
/// 输入翻译
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void InputTranslate_Click(object sender, EventArgs e)
{
ClearTextBox();
Show();
Activate();
TextBoxInput.Focus();
}
/// <summary>
/// 退出程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Exit_Click(object sender, EventArgs e)
{
notifyIcon.Dispose();
Environment.Exit(0);
}
/// <summary>
/// 清空输入输出框
/// </summary>
private void ClearTextBox()
{
MainVM.Instance.InputTxt = string.Empty;
MainVM.Instance.OutputTxt = string.Empty;
}
/// <summary>
/// 托盘图标
/// </summary>
private System.Windows.Forms.NotifyIcon notifyIcon = new System.Windows.Forms.NotifyIcon();
}
}

@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STranslate.Model
{
public static class HotKeys
{
public static class InputTranslate
{
public static byte Modifiers = 1;
public static int Key = 65;
public static String Text = "A";
public static bool Conflict = false;
}
public static class CrosswordTranslate
{
public static byte Modifiers = 0;
public static int Key = 113;
public static String Text = "F2";
public static bool Conflict = false;
}
public static class ScreenShotTranslate
{
public static byte Modifiers = 1;
public static int Key = 83;
public static String Text = "S";
public static bool Conflict = false;
}
}
}

@ -85,11 +85,15 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Model\HotKeys.cs" />
<Compile Include="Utils\ConfigUtil.cs" />
<Compile Include="Utils\GetWordsUtil.cs" />
<Compile Include="Utils\HotKeysUtil.cs" />
<Compile Include="Utils\HotKeysUtil2.cs" />
<Compile Include="Utils\HttpUtil.cs" />
<Compile Include="Utils\KeyboardUtil.cs" />
<Compile Include="Utils\MvvmUtil.cs" />
<Compile Include="Utils\NativeMethod.cs" />
<Compile Include="Utils\TranslateUtil.cs" />
<Compile Include="ViewModel\MainVM.cs" />
<Page Include="MainWindow.xaml">

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

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace STranslate.Utils
{
public class GetWords
{
public static String Get()
{
SendCtrlC();
Thread.Sleep(200);
return NativeMethod.GetText();
}
private static void SendCtrlC()
{
//IntPtr hWnd = GetForegroundWindow();
//SetForegroundWindow(hWnd);
uint KEYEVENTF_KEYUP = 2;
NativeMethod.keybd_event(System.Windows.Forms.Keys.ControlKey, 0, 0, 0);
NativeMethod.keybd_event(System.Windows.Forms.Keys.C, 0, 0, 0);
NativeMethod.keybd_event(System.Windows.Forms.Keys.C, 0, KEYEVENTF_KEYUP, 0);
NativeMethod.keybd_event(System.Windows.Forms.Keys.ControlKey, 0, KEYEVENTF_KEYUP, 0);// 'Left Control Up
}
private static String GetDataFromClipboard()
{
try
{
if (Clipboard.ContainsText()) //检查是否存在文本
{
string res = Clipboard.GetText();
if (!string.IsNullOrWhiteSpace(res))
{
return res;
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
return null;
}
return null;
}
}
}

@ -0,0 +1,94 @@
using STranslate.Model;
using System;
namespace STranslate.Utils
{
internal class HotKeysUtil2
{
public static IntPtr mainFormHandle;
public static int InputTranslateId = 854;
public static byte InputTranslateModifiers;
public static int InputTranslateKey;
public static int CrosswordTranslateId = 855;
public static byte CrosswordTranslateModifiers;
public static int CrosswordTranslateKey;
public static int ScreenShotTranslateId = 856;
public static byte ScreenShotTranslateModifiers;
public static int ScreenShotTranslateKey;
public static void RegisterHotKey(IntPtr mainFormHandle)
{
HotKeysUtil2.mainFormHandle = mainFormHandle;
InputTranslateModifiers = HotKeys.InputTranslate.Modifiers;
InputTranslateKey = HotKeys.InputTranslate.Key;
CrosswordTranslateModifiers = HotKeys.CrosswordTranslate.Modifiers;
CrosswordTranslateKey = HotKeys.CrosswordTranslate.Key;
ScreenShotTranslateModifiers = HotKeys.ScreenShotTranslate.Modifiers;
ScreenShotTranslateKey = HotKeys.ScreenShotTranslate.Key;
if (HotKeys.InputTranslate.Key != 0)
{
HotKeys.InputTranslate.Conflict = !NativeMethod.RegisterHotKey(mainFormHandle, InputTranslateId, HotKeys.InputTranslate.Modifiers, HotKeys.InputTranslate.Key);
}
if (HotKeys.CrosswordTranslate.Key != 0)
{
HotKeys.CrosswordTranslate.Conflict = !NativeMethod.RegisterHotKey(mainFormHandle, CrosswordTranslateId, HotKeys.CrosswordTranslate.Modifiers, HotKeys.CrosswordTranslate.Key);
}
if (HotKeys.ScreenShotTranslate.Key != 0)
{
HotKeys.ScreenShotTranslate.Conflict = !NativeMethod.RegisterHotKey(mainFormHandle, ScreenShotTranslateId, HotKeys.ScreenShotTranslate.Modifiers, HotKeys.ScreenShotTranslate.Key);
}
}
public static void UnRegisterHotKey()
{
NativeMethod.UnregisterHotKey(mainFormHandle, InputTranslateId);
NativeMethod.UnregisterHotKey(mainFormHandle, CrosswordTranslateId);
NativeMethod.UnregisterHotKey(mainFormHandle, ScreenShotTranslateId);
}
public static void ReRegisterHotKey()
{
if (HotKeys.InputTranslate.Key == 0)
{
NativeMethod.UnregisterHotKey(mainFormHandle, InputTranslateId);
}
else if (InputTranslateModifiers != HotKeys.InputTranslate.Modifiers || InputTranslateKey != HotKeys.InputTranslate.Key)
{
{
NativeMethod.UnregisterHotKey(mainFormHandle, InputTranslateId);
HotKeys.InputTranslate.Conflict = !NativeMethod.RegisterHotKey(mainFormHandle, InputTranslateId, HotKeys.InputTranslate.Modifiers, HotKeys.InputTranslate.Key);
}
}
InputTranslateModifiers = HotKeys.InputTranslate.Modifiers;
InputTranslateKey = HotKeys.InputTranslate.Key;
if (HotKeys.CrosswordTranslate.Key == 0)
{
NativeMethod.UnregisterHotKey(mainFormHandle, CrosswordTranslateId);
}
else if (CrosswordTranslateModifiers != HotKeys.CrosswordTranslate.Modifiers || CrosswordTranslateKey != HotKeys.CrosswordTranslate.Key)
{
{
NativeMethod.UnregisterHotKey(mainFormHandle, CrosswordTranslateId);
HotKeys.CrosswordTranslate.Conflict = !NativeMethod.RegisterHotKey(mainFormHandle, CrosswordTranslateId, HotKeys.CrosswordTranslate.Modifiers, HotKeys.CrosswordTranslate.Key);
}
}
CrosswordTranslateModifiers = HotKeys.CrosswordTranslate.Modifiers;
CrosswordTranslateKey = HotKeys.CrosswordTranslate.Key;
if (HotKeys.ScreenShotTranslate.Key == 0)
{
NativeMethod.UnregisterHotKey(mainFormHandle, ScreenShotTranslateId);
}
else if (ScreenShotTranslateModifiers != HotKeys.ScreenShotTranslate.Modifiers || ScreenShotTranslateKey != HotKeys.ScreenShotTranslate.Key)
{
NativeMethod.UnregisterHotKey(mainFormHandle, ScreenShotTranslateId);
HotKeys.ScreenShotTranslate.Conflict = !NativeMethod.RegisterHotKey(mainFormHandle, ScreenShotTranslateId, HotKeys.ScreenShotTranslate.Modifiers, HotKeys.ScreenShotTranslate.Key);
}
ScreenShotTranslateModifiers = HotKeys.ScreenShotTranslate.Modifiers;
ScreenShotTranslateKey = HotKeys.ScreenShotTranslate.Key;
}
}
}

@ -0,0 +1,252 @@
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace STranslate.Utils
{
internal class NativeMethod
{
/// <summary>
/// 获取进程句柄
/// </summary>
/// <param name="lpModuleName"></param>
/// <returns></returns>
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
/// <summary>
/// 设置窗口在最前端
/// </summary>
/// <param name="hWnd">窗口句柄</param>
/// <returns></returns>
[DllImport("User32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// 获取最前端的窗口
/// </summary>
/// <returns>窗口句柄</returns>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetForegroundWindow();
/// <summary>
/// 模拟触发键盘的按键
/// </summary>
/// <param name="vk">按下的键</param>
/// <param name="bScan"></param>
/// <param name="dwFlags">触发的方式0按下2抬起</param>
/// <param name="dwExtraInfo"></param>
[DllImport("user32.dll")]
public static extern void keybd_event(System.Windows.Forms.Keys vk, byte bScan, uint dwFlags, uint dwExtraInfo);
/// <summary>
/// 注册全局热键
/// </summary>
/// <param name="hWnd">要定义热键的窗口的句柄</param>
/// <param name="id">定义热键ID不能与其它ID重复全局唯一</param>
/// <param name="fsModifiers">标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效</param>
/// <param name="vk">定义热键的内容</param>
/// <returns>成功返回值不为0失败返回值为0。要得到扩展错误信息调用GetLastError</returns>
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, byte fsModifiers, int vk);
/// <summary>
/// 取消注册全局热键
/// </summary>
/// <param name="hWnd">要取消热键的窗口的句柄</param>
/// <param name="id">要取消热键的ID</param>
/// <returns></returns>
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
/// <summary>
/// 加载鼠标样式从文件中
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern IntPtr LoadCursorFromFile(string fileName);
/// <summary>
/// 设置鼠标样式
/// </summary>
/// <param name="cursorHandle"></param>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern IntPtr SetCursor(IntPtr cursorHandle);
/// <summary>
/// 销毁鼠标样式
/// </summary>
/// <param name="cursorHandle"></param>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern uint DestroyCursor(IntPtr cursorHandle);
/// <summary>
/// 隐藏焦点,隐藏光标闪烁
/// </summary>
/// <param name="hWnd"></param>
/// <returns></returns>
[DllImport("user32", EntryPoint = "HideCaret")]
public static extern bool HideCaret(IntPtr hWnd);
[DllImport("kernel32.dll")]
public static extern bool SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
/// <summary>
/// 打开剪切板
/// </summary>
/// <param name="hWndNewOwner"></param>
/// <returns></returns>
[DllImport("User32")]
internal static extern bool OpenClipboard(IntPtr hWndNewOwner);
/// <summary>
/// 关闭剪切板
/// </summary>
/// <returns></returns>
[DllImport("User32")]
internal static extern bool CloseClipboard();
/// <summary>
/// 清空剪切板
/// </summary>
/// <returns></returns>
[DllImport("User32")]
internal static extern bool EmptyClipboard();
/// <summary>
/// 剪切板格式化的数据是否可用
/// </summary>
/// <param name="format"></param>
/// <returns></returns>
[DllImport("User32")]
internal static extern bool IsClipboardFormatAvailable(int format);
/// <summary>
/// 获取剪切板数据
/// </summary>
/// <param name="uFormat"></param>
/// <returns></returns>
[DllImport("User32")]
internal static extern IntPtr GetClipboardData(int uFormat);
/// <summary>
/// 设置剪切板数据
/// </summary>
/// <param name="uFormat"></param>
/// <param name="hMem"></param>
/// <returns></returns>
[DllImport("User32", CharSet = CharSet.Unicode)]
internal static extern IntPtr SetClipboardData(int uFormat, IntPtr hMem);
const int HORZRES = 8;
const int VERTRES = 10;
const int LOGPIXELSX = 88;
const int LOGPIXELSY = 90;
const int DESKTOPVERTRES = 117;
const int DESKTOPHORZRES = 118;
[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr ptr);
[DllImport("gdi32.dll")]
static extern int GetDeviceCaps(
IntPtr hdc, // handle to DC
int nIndex // index of capability
);
[DllImport("user32.dll", EntryPoint = "ReleaseDC")]
static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
/// <summary>
/// 获取系统dpi
/// </summary>
/// <returns></returns>
public static double GetDpi()
{
double dDpi = 1;
IntPtr desktopDc = GetDC(IntPtr.Zero);
float horizontalDPI = GetDeviceCaps(desktopDc, LOGPIXELSX);
float verticalDPI = GetDeviceCaps(desktopDc, LOGPIXELSY);
int dpi = (int)(horizontalDPI + verticalDPI) / 2;
dDpi = 1 + ((dpi - 96) / 24) * 0.25;
if (dDpi < 1)
{
dDpi = 1;
}
ReleaseDC(IntPtr.Zero, desktopDc);
return dDpi;
}
/// <summary>
/// 获取窗口标题
/// </summary>
/// <returns></returns>
[DllImport("user32", SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
/// <summary>
/// 获取类的名字
/// </summary>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
/// <summary>
/// 根据坐标获取窗口句柄
/// </summary>
/// <returns></returns>
[DllImport("user32")]
public static extern IntPtr WindowFromPoint(System.Drawing.Point Point);
/// <summary>
/// 窗口置顶与取消置顶
/// </summary>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hPos, int x, int y, int cx, int cy, uint nflags);
/// <summary>
/// 定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)
/// </summary>
[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Ctrl = 2,
Shift = 4,
WindowsKey = 8
}
#region Clipboard
internal static void SetText(string text)
{
if (!OpenClipboard(IntPtr.Zero))
{
SetText(text);
return;
}
EmptyClipboard();
SetClipboardData(13, Marshal.StringToHGlobalUni(text));
CloseClipboard();
}
internal static string GetText()
{
string value = string.Empty;
OpenClipboard(IntPtr.Zero);
if (IsClipboardFormatAvailable(13))
{
IntPtr ptr = GetClipboardData(13);
if (ptr != IntPtr.Zero)
{
value = Marshal.PtrToStringUni(ptr);
}
}
CloseClipboard();
return value;
}
#endregion
}
}

@ -3,6 +3,7 @@ using STranslate.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
@ -14,8 +15,6 @@ namespace STranslate.ViewModel
public static ConfigModel config = new ConfigModel();
private static Dictionary<string, LanguageEnum> LanguageEnumDict { get => TranslateUtil.GetEnumList<LanguageEnum>(); }
private string Text;
public MainVM()
{
//初始化界面参数
@ -27,7 +26,7 @@ namespace STranslate.ViewModel
//TODO: fix no config
config = ConfigUtil.ReadConfig(ConfigPath);
//手动复制翻译结果
//复制翻译结果
CopyTranslateResultCmd = new RelayCommand((_) =>
{
return string.IsNullOrEmpty(OutputTxt) ? false : true;
@ -42,32 +41,32 @@ namespace STranslate.ViewModel
return string.IsNullOrEmpty(InputTxt) ? false : true;
}, async (_) =>
{
try
{
Text = InputTxt;
//清空输入框
InputTxt = "";
OutputTxt = "翻译中...";
await Translate();
});
}
public async Task Translate()
{
try
{
//清空输入框
OutputTxt = "翻译中...";
//获取结果
//var translateResp = await TranslateUtil.TranslateDeepLAsync(config.deepl.url, Text, LanguageEnum.EN, LanguageEnum.AUTO);
//获取结果
//var translateResp = await TranslateUtil.TranslateDeepLAsync(config.deepl.url, InputTxt, LanguageEnum.EN, LanguageEnum.AUTO);
var translateResp = await TranslateUtil.TranslateBaiduAsync(config.baidu.appid, config.baidu.secretKey, Text, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]);
var translateResp = await TranslateUtil.TranslateBaiduAsync(config.baidu.appid, config.baidu.secretKey, InputTxt, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]);
if (translateResp == string.Empty)
{
OutputTxt = "翻译出错,请稍候再试...";
return;
}
OutputTxt = translateResp;
}
catch (Exception ex)
if (translateResp == string.Empty)
{
OutputTxt = ex.Message;
OutputTxt = "翻译出错,请稍候再试...";
return;
}
});
OutputTxt = translateResp;
}
catch (Exception ex)
{
OutputTxt = ex.Message;
}
}
public ICommand TranslateCmd { get; private set; }

Loading…
Cancel
Save