From aab8f0d3874c5ac374008ab5891af1d34ed8664e Mon Sep 17 00:00:00 2001 From: "SONGE9B3\\song_pd_win11" Date: Tue, 3 Jan 2023 18:32:28 +0800 Subject: [PATCH] perf: update the selected service in the configuration file --- STranslate/Helper/ConfigHelper.cs | 7 +++---- STranslate/MainWindow.xaml | 2 +- STranslate/Model/ConfigModel.cs | 23 +++++++++++++------- STranslate/ViewModel/MainVM.cs | 35 +++++++++++++++---------------- 4 files changed, 36 insertions(+), 31 deletions(-) diff --git a/STranslate/Helper/ConfigHelper.cs b/STranslate/Helper/ConfigHelper.cs index cf2aad0..9866b56 100644 --- a/STranslate/Helper/ConfigHelper.cs +++ b/STranslate/Helper/ConfigHelper.cs @@ -46,14 +46,13 @@ namespace STranslate.Helper /// /// 配置文件 /// - private static string _CnfName { get => $"{_ApplicationData}\\stranslate.json"; } + private static string _CnfName => $"{_ApplicationData}\\{_AppName.ToLower()}.json"; /// /// C:\Users\user\AppData\Local\STranslate /// - private static readonly string _ApplicationData - = $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}" - + $"\\{Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location)}"; + private static string _ApplicationData => $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}\\{_AppName}"; + private static readonly string _AppName = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location); private static ConfigHelper _Instance; public static ConfigHelper Instance { get => _Instance ?? (_Instance = new ConfigHelper()); } } diff --git a/STranslate/MainWindow.xaml b/STranslate/MainWindow.xaml index 8879597..9fcaaee 100644 --- a/STranslate/MainWindow.xaml +++ b/STranslate/MainWindow.xaml @@ -48,8 +48,8 @@ diff --git a/STranslate/Model/ConfigModel.cs b/STranslate/Model/ConfigModel.cs index 5a75f71..de9a49d 100644 --- a/STranslate/Model/ConfigModel.cs +++ b/STranslate/Model/ConfigModel.cs @@ -15,18 +15,19 @@ namespace STranslate.Model public ConfigModel InitialConfig() { - var defaultServer = new Server - { - Name = "zggsong", - Api = "https://zggsong.cn/tt" - }; return new ConfigModel { IsBright = true, - SelectServer = defaultServer, + SourceLanguage = LanguageEnum.AUTO, + TargetLanguage = LanguageEnum.AUTO, + SelectServer = 0, Servers = new Server[] { - defaultServer, + new Server + { + Name = "zggsong", + Api = "https://zggsong.cn/tt" + }, new Server { Name = "zu1k", @@ -47,8 +48,14 @@ namespace STranslate.Model [JsonProperty("isBright")] public bool IsBright { get; set; } + [JsonProperty("sourceLanguage")] + public LanguageEnum SourceLanguage { get; set; } + + [JsonProperty("targetLanguage")] + public LanguageEnum TargetLanguage { get; set; } + [JsonProperty("selectServer")] - public Server SelectServer { get; set; } + public int SelectServer { get; set; } /// /// 服务 diff --git a/STranslate/ViewModel/MainVM.cs b/STranslate/ViewModel/MainVM.cs index be4f450..fec6a84 100644 --- a/STranslate/ViewModel/MainVM.cs +++ b/STranslate/ViewModel/MainVM.cs @@ -24,10 +24,6 @@ namespace STranslate.ViewModel Task.Delay(3000); Environment.Exit(-1); } - InputCombo = LanguageEnumDict.Keys.ToList(); - InputComboSelected = LanguageEnum.AUTO.GetDescription(); - OutputCombo = LanguageEnumDict.Keys.ToList(); - OutputComboSelected = LanguageEnum.AUTO.GetDescription(); //复制输入 CopyInputCmd = new RelayCommand((_) => true, (_) => @@ -83,14 +79,27 @@ namespace STranslate.ViewModel { try { + InputCombo = LanguageEnumDict.Keys.ToList(); + OutputCombo = LanguageEnumDict.Keys.ToList(); GlobalConfig = ConfigHelper.Instance.ReadConfig(); //更新服务 TranslationInterface = GlobalConfig.Servers.ToList(); if (TranslationInterface.Count < 1) throw new Exception("尚未配置任何翻译接口服务"); - //初始化接口 - SelectedTranslationInterface = GlobalConfig.SelectServer; + try + { + //配置读取接口 + SelectedTranslationInterface = TranslationInterface[GlobalConfig.SelectServer]; + } + catch (Exception ex) + { + throw new Exception($"配置文件选择服务索引出错, 请修改配置文件后重试", ex); + } + + //从配置读取source target + InputComboSelected = GlobalConfig.SourceLanguage.GetDescription(); + OutputComboSelected = GlobalConfig.TargetLanguage.GetDescription(); return true; } @@ -130,18 +139,8 @@ namespace STranslate.ViewModel { return new Tuple(LanguageEnum.ZH.GetDescription(), LanguageEnum.EN.GetDescription()); } -#if false - //如果输入是中文 - if (Regex.IsMatch(text, @"^[\u4e00-\u9fa5]+$")) - { - return new Tuple(LanguageEnum.ZH.GetDescription(), LanguageEnum.EN.GetDescription()); - } - else - { - return new Tuple(LanguageEnum.EN.GetDescription(), LanguageEnum.ZH.GetDescription()); - } -#endif } + /// /// 翻译 /// @@ -268,7 +267,7 @@ namespace STranslate.ViewModel /// private List _TranslationInterface; public List TranslationInterface { get => _TranslationInterface; set => UpdateProperty(ref _TranslationInterface, value); } - + private Server _SelectedTranslationInterface; public Server SelectedTranslationInterface { get => _SelectedTranslationInterface; set => UpdateProperty(ref _SelectedTranslationInterface, value); } private static Dictionary LanguageEnumDict { get => Util.Util.GetEnumList(); }