feat: add deepl process

dev_without_third_libraries
DESKTOP-3BO4HSG\ksat 2 years ago
parent 6a02666227
commit 5712cd7bd9

@ -1,6 +1,7 @@
using STranslate.Utils; using STranslate.Utils;
using STranslate.ViewModel; using STranslate.ViewModel;
using System; using System;
using System.Diagnostics;
using System.Windows; using System.Windows;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Interop; using System.Windows.Interop;
@ -15,6 +16,7 @@ namespace STranslate
{ {
private MainVM vm; private MainVM vm;
private Process process = new Process();
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
@ -23,12 +25,15 @@ namespace STranslate
InitialTray(); InitialTray();
DeepLStart();
//if (HotKeys.InputTranslate.Conflict || HotKeys.CrosswordTranslate.Conflict || HotKeys.ScreenShotTranslate.Conflict) //if (HotKeys.InputTranslate.Conflict || HotKeys.CrosswordTranslate.Conflict || HotKeys.ScreenShotTranslate.Conflict)
//{ //{
// MessageBox.Show("全局快捷键有冲突,请您到设置中重新设置"); // MessageBox.Show("全局快捷键有冲突,请您到设置中重新设置");
//} //}
vm = (MainVM)DataContext; vm = (MainVM)DataContext;
} }
private void InitialTray() private void InitialTray()
{ {
//notifyIcon.BalloonTipText = "STranslate已运行"; //notifyIcon.BalloonTipText = "STranslate已运行";
@ -234,7 +239,9 @@ namespace STranslate
/// <param name="e"></param> /// <param name="e"></param>
private void Exit_Click(object sender, EventArgs e) private void Exit_Click(object sender, EventArgs e)
{ {
notifyIcon.Dispose(); notifyIcon.Dispose();
DeepLStop();
Environment.Exit(0); Environment.Exit(0);
} }
private void InitView() private void InitView()
@ -247,5 +254,28 @@ namespace STranslate
private BitmapImage LockImgPath = new BitmapImage(new Uri("pack://application:,,,/Images/lock3.png")); private BitmapImage LockImgPath = new BitmapImage(new Uri("pack://application:,,,/Images/lock3.png"));
private BitmapImage UnLockImgPath = new BitmapImage(new Uri("pack://application:,,,/Images/unlock3.png")); private BitmapImage UnLockImgPath = new BitmapImage(new Uri("pack://application:,,,/Images/unlock3.png"));
private System.Windows.Forms.NotifyIcon notifyIcon = new System.Windows.Forms.NotifyIcon(); private System.Windows.Forms.NotifyIcon notifyIcon = new System.Windows.Forms.NotifyIcon();
/// <summary>
/// 新进程开启DeepL
/// </summary>
private void DeepLStart()
{
string strPathExe = AppDomain.CurrentDomain.BaseDirectory + "\\Static\\deepl-x86_64-pc-windows-gnu.exe";
process.StartInfo.FileName = strPathExe;
process.StartInfo.Arguments = null;//-s -t 可以用来关机、开机或重启
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = false; //true
process.StartInfo.RedirectStandardOutput = false; //true
process.StartInfo.RedirectStandardError = false;
process.StartInfo.CreateNoWindow = true;
process.Start();//启动
}
/// <summary>
/// 关闭DeepL进程
/// </summary>
private void DeepLStop()
{
process.Kill();//等待退出。
}
} }
} }

@ -31,7 +31,7 @@
<BootstrapperEnabled>true</BootstrapperEnabled> <BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget> <PlatformTarget>x64</PlatformTarget>
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType> <DebugType>full</DebugType>
<Optimize>false</Optimize> <Optimize>false</Optimize>
@ -39,6 +39,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants> <DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget> <PlatformTarget>AnyCPU</PlatformTarget>
@ -160,6 +161,9 @@
<Resource Include="Images\copy2.png"> <Resource Include="Images\copy2.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource> </Resource>
<Content Include="Static\deepl-x86_64-pc-windows-gnu.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="STranslate.yml"> <Content Include="STranslate.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>

@ -51,11 +51,12 @@ namespace STranslate.Utils
var respStr = await HttpUtil.PostAsync(url, reqStr); var respStr = await HttpUtil.PostAsync(url, reqStr);
var resp = JsonConvert.DeserializeObject<DeeplResp>(respStr); var resp = JsonConvert.DeserializeObject<DeeplResp>(respStr);
if (resp.Code == 200) if (resp == null || resp.Code != 200)
{ {
return resp.Data; return string.Empty;
} }
return string.Empty;
return resp?.Data ?? string.Empty;
} }
/// <summary> /// <summary>

@ -11,12 +11,23 @@ namespace STranslate.ViewModel
{ {
public class MainVM : BaseVM public class MainVM : BaseVM
{ {
/// <summary>
/// 引入Go项目库
/// </summary>
/// <returns></returns>
//[System.Runtime.InteropServices.DllImport("deepl.dll", EntryPoint = "run")]
//extern static void run();
private static string ConfigPath => $"{AppDomain.CurrentDomain.BaseDirectory}STranslate.yml"; private static string ConfigPath => $"{AppDomain.CurrentDomain.BaseDirectory}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 static Dictionary<string, LanguageEnum> LanguageEnumDict { get => TranslateUtil.GetEnumList<LanguageEnum>(); }
public MainVM() public MainVM()
{ {
//启动deepl
//Task.Run(() => RunCmdUtil.RunCmd(AppDomain.CurrentDomain.BaseDirectory + "\\deepl-x86_64-pc-windows-gnu.exe"));
//初始化界面参数 //初始化界面参数
InputCombo = LanguageEnumDict.Keys.ToList(); InputCombo = LanguageEnumDict.Keys.ToList();
InputComboSelected = LanguageEnum.AUTO.GetDescription(); InputComboSelected = LanguageEnum.AUTO.GetDescription();
@ -79,9 +90,9 @@ namespace STranslate.ViewModel
OutputTxt = "翻译中..."; OutputTxt = "翻译中...";
//获取结果 //获取结果
//var translateResp = await TranslateUtil.TranslateDeepLAsync(config.deepl.url, InputTxt, LanguageEnum.EN, LanguageEnum.AUTO); var translateResp = await TranslateUtil.TranslateDeepLAsync(config.deepl.url, InputTxt, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]);
var translateResp = await TranslateUtil.TranslateBaiduAsync(config.baidu.appid, config.baidu.secretKey, InputTxt, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]); //var translateResp = await TranslateUtil.TranslateBaiduAsync(config.baidu.appid, config.baidu.secretKey, InputTxt, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]);
if (translateResp == string.Empty) if (translateResp == string.Empty)
{ {

Loading…
Cancel
Save