From 319087825d0707f0195e400b1cd2cb1586dc09e5 Mon Sep 17 00:00:00 2001 From: ZGGSONG Date: Thu, 15 Dec 2022 19:41:01 +0800 Subject: [PATCH] feat: add config --- STranslate/MainWindow.xaml | 1 + STranslate/Model/ConfigModel.cs | 25 ++++++++++++++++ STranslate/STranslate.csproj | 4 +++ STranslate/Utils/ConfigUtil.cs | 49 +++++++++++++++++++++++++++++++ STranslate/ViewModel/MainVM.cs | 51 ++++++++++++++++++++------------- 5 files changed, 110 insertions(+), 20 deletions(-) create mode 100644 STranslate/Model/ConfigModel.cs create mode 100644 STranslate/Utils/ConfigUtil.cs diff --git a/STranslate/MainWindow.xaml b/STranslate/MainWindow.xaml index 32da477..68b0eca 100644 --- a/STranslate/MainWindow.xaml +++ b/STranslate/MainWindow.xaml @@ -10,6 +10,7 @@ WindowStartupLocation="CenterScreen" MouseLeftButtonDown="Window_MouseLeftButtonDown" AllowsTransparency="True" + ShowInTaskbar="False" ResizeMode="NoResize" KeyDown="Window_KeyDown" Topmost="True" diff --git a/STranslate/Model/ConfigModel.cs b/STranslate/Model/ConfigModel.cs new file mode 100644 index 0000000..a39182c --- /dev/null +++ b/STranslate/Model/ConfigModel.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace STranslate.Model +{ + public class ConfigModel + { + public string service { get; set; } + public Baidu baidu { get; set; } + public DeepL deepl { get; set; } + } + + public class Baidu + { + public string appid { get; set; } + public string secretKey { get; set; } + } + public class DeepL + { + public string url { get; set; } + } +} \ No newline at end of file diff --git a/STranslate/STranslate.csproj b/STranslate/STranslate.csproj index 993cf6a..36351be 100644 --- a/STranslate/STranslate.csproj +++ b/STranslate/STranslate.csproj @@ -16,6 +16,10 @@ + + + + ..\vendor\Newtonsoft.Json.dll diff --git a/STranslate/Utils/ConfigUtil.cs b/STranslate/Utils/ConfigUtil.cs new file mode 100644 index 0000000..cfcea57 --- /dev/null +++ b/STranslate/Utils/ConfigUtil.cs @@ -0,0 +1,49 @@ +using STranslate.Model; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using YamlDotNet.Serialization; + +namespace STranslate.Utils +{ + public class ConfigUtil + { + public static ConfigModel ReadConfig(string path) + { + using (TextReader reader = File.OpenText(path)) + { + try + { + var config = new ConfigModel(); + var deserializer = new Deserializer(); + config = deserializer.Deserialize(reader); + return config; + } + catch (Exception ex) + { + throw ex; + } + } + + } + + public static void WriteConfig(string path, ConfigModel configs) + { + var serializer = new Serializer(); + StringWriter strWriter = new StringWriter(); + + serializer.Serialize(strWriter, configs); + serializer.Serialize(Console.Out, configs); + + using (TextWriter writer = File.CreateText(path)) + { + writer.Write(strWriter.ToString()); + } + + + } + } +} diff --git a/STranslate/ViewModel/MainVM.cs b/STranslate/ViewModel/MainVM.cs index 4fcb691..450e5fc 100644 --- a/STranslate/ViewModel/MainVM.cs +++ b/STranslate/ViewModel/MainVM.cs @@ -11,35 +11,46 @@ namespace STranslate.ViewModel { public class MainVM : BaseVM { + private static readonly string ConfigPath = @"D:\STranslate.yml"; + public static ConfigModel config = new ConfigModel(); + private string Text; public MainVM() { - TranslateCmd = new RelayCommand((_) => - { - return string.IsNullOrEmpty(InputTxt) ? false : true; - }, async (_) => + try { - Text = InputTxt; + config = ConfigUtil.ReadConfig(ConfigPath); + + + TranslateCmd = new RelayCommand((_) => + { + return string.IsNullOrEmpty(InputTxt) ? false : true; + }, async (_) => + { + Text = InputTxt; - //清空输入框 - InputTxt = ""; + //清空输入框 + InputTxt = ""; - OutputTxt = "翻译中..."; + OutputTxt = "翻译中..."; - //获取结果 - //var translateResp = await TranslateUtil.TranslateDeepLAsync(InputTxt, LanguageEnum.EN, LanguageEnum.auto); + //获取结果 + //var translateResp = await TranslateUtil.TranslateDeepLAsync(InputTxt, LanguageEnum.EN, LanguageEnum.auto); - var appId = ""; - var secretKey = ""; - var translateResp = await TranslateUtil.TranslateBaiduAsync(appId, secretKey, Text, LanguageEnum.EN, LanguageEnum.auto); + var translateResp = await TranslateUtil.TranslateBaiduAsync(config.baidu.appid, config.baidu.secretKey, Text, LanguageEnum.EN, LanguageEnum.auto); - if (translateResp == string.Empty) - { - OutputTxt = "翻译出错,请稍候再试..."; - return; - } - OutputTxt = translateResp; - }); + if (translateResp == string.Empty) + { + OutputTxt = "翻译出错,请稍候再试..."; + return; + } + OutputTxt = translateResp; + }); + } + catch (Exception ex) + { + OutputTxt = ex.Message; + } } public ICommand TranslateCmd { get; private set; }