From c0b666f4f9fda3efd9e1e8cc0177b00a793e3198 Mon Sep 17 00:00:00 2001 From: "DESKTOP-3BO4HSG\\ksat" Date: Sat, 24 Dec 2022 14:05:16 +0800 Subject: [PATCH] perf: no local api no local serve --- STranslate/MainWindow.xaml.cs | 5 ++++- STranslate/ViewModel/MainVM.cs | 13 +++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/STranslate/MainWindow.xaml.cs b/STranslate/MainWindow.xaml.cs index fe3a4df..11a2834 100644 --- a/STranslate/MainWindow.xaml.cs +++ b/STranslate/MainWindow.xaml.cs @@ -20,6 +20,8 @@ namespace STranslate public MainWindow() { InitializeComponent(); + + vm = (MainVM)DataContext; InitView(); @@ -31,7 +33,6 @@ namespace STranslate //{ // MessageBox.Show("全局快捷键有冲突,请您到设置中重新设置"); //} - vm = (MainVM)DataContext; } private void InitialTray() @@ -272,6 +273,7 @@ namespace STranslate /// private void DeepLStart() { + if (!string.IsNullOrEmpty(vm.config.deepl?.url) && vm.config.deepl?.url != vm.defaultApi) return; string strPathExe = AppDomain.CurrentDomain.BaseDirectory + "\\Static\\deepl-x86_64-pc-windows-gnu.exe"; process.StartInfo.FileName = strPathExe; process.StartInfo.Arguments = null;//-s -t 可以用来关机、开机或重启 @@ -288,6 +290,7 @@ namespace STranslate /// private void DeepLStop() { + if (!string.IsNullOrEmpty(vm.config.deepl?.url) && vm.config.deepl?.url != vm.defaultApi) return; process.Kill();//等待退出。 } } diff --git a/STranslate/ViewModel/MainVM.cs b/STranslate/ViewModel/MainVM.cs index 363bf67..0f118c9 100644 --- a/STranslate/ViewModel/MainVM.cs +++ b/STranslate/ViewModel/MainVM.cs @@ -18,9 +18,9 @@ namespace STranslate.ViewModel //[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(); + public string defaultApi = "http://127.0.0.1:8000/translate"; + private string ConfigPath => $"{AppDomain.CurrentDomain.BaseDirectory}STranslate.yml"; + public ConfigModel config = new ConfigModel(); private static Dictionary LanguageEnumDict { get => TranslateUtil.GetEnumList(); } public MainVM() @@ -34,7 +34,6 @@ namespace STranslate.ViewModel OutputCombo = LanguageEnumDict.Keys.ToList(); OutputComboSelected = LanguageEnum.EN.GetDescription(); - //TODO: fix no config config = ConfigUtil.ReadConfig(ConfigPath); //复制输入 @@ -94,12 +93,14 @@ namespace STranslate.ViewModel { try { - //清空输入框 OutputTxt = "翻译中..."; //获取结果 - var translateResp = await TranslateUtil.TranslateDeepLAsync(config.deepl.url, InputTxt, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]); + //DeepL Api + var translateResp = await TranslateUtil.TranslateDeepLAsync(config.deepl?.url ?? defaultApi, InputTxt, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]); + + //百度 Api //var translateResp = await TranslateUtil.TranslateBaiduAsync(config.baidu.appid, config.baidu.secretKey, InputTxt, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]); if (translateResp == string.Empty)