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.ViewModel;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
@ -15,6 +16,7 @@ namespace STranslate
{
private MainVM vm;
private Process process = new Process();
public MainWindow()
{
InitializeComponent();
@ -23,12 +25,15 @@ namespace STranslate
InitialTray();
DeepLStart();
//if (HotKeys.InputTranslate.Conflict || HotKeys.CrosswordTranslate.Conflict || HotKeys.ScreenShotTranslate.Conflict)
//{
// MessageBox.Show("全局快捷键有冲突,请您到设置中重新设置");
//}
vm = (MainVM)DataContext;
}
private void InitialTray()
{
//notifyIcon.BalloonTipText = "STranslate已运行";
@ -234,7 +239,9 @@ namespace STranslate
/// <param name="e"></param>
private void Exit_Click(object sender, EventArgs e)
{
notifyIcon.Dispose();
DeepLStop();
Environment.Exit(0);
}
private void InitView()
@ -247,5 +254,28 @@ namespace STranslate
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 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>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<PlatformTarget>x64</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
@ -39,6 +39,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
@ -160,6 +161,9 @@
<Resource Include="Images\copy2.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
<Content Include="Static\deepl-x86_64-pc-windows-gnu.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="STranslate.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

@ -51,13 +51,14 @@ namespace STranslate.Utils
var respStr = await HttpUtil.PostAsync(url, reqStr);
var resp = JsonConvert.DeserializeObject<DeeplResp>(respStr);
if (resp.Code == 200)
if (resp == null || resp.Code != 200)
{
return resp.Data;
}
return string.Empty;
}
return resp?.Data ?? string.Empty;
}
/// <summary>
/// 百度翻译异步接口
/// </summary>

@ -11,12 +11,23 @@ namespace STranslate.ViewModel
{
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";
public static ConfigModel config = new ConfigModel();
private static Dictionary<string, LanguageEnum> LanguageEnumDict { get => TranslateUtil.GetEnumList<LanguageEnum>(); }
public MainVM()
{
//启动deepl
//Task.Run(() => RunCmdUtil.RunCmd(AppDomain.CurrentDomain.BaseDirectory + "\\deepl-x86_64-pc-windows-gnu.exe"));
//初始化界面参数
InputCombo = LanguageEnumDict.Keys.ToList();
InputComboSelected = LanguageEnum.AUTO.GetDescription();
@ -79,9 +90,9 @@ namespace STranslate.ViewModel
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)
{

Loading…
Cancel
Save