diff --git a/STranslate/Helper/ConfigHelper.cs b/STranslate/Helper/ConfigHelper.cs new file mode 100644 index 0000000..cf2aad0 --- /dev/null +++ b/STranslate/Helper/ConfigHelper.cs @@ -0,0 +1,60 @@ +using Newtonsoft.Json; +using STranslate.Model; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace STranslate.Helper +{ + public class ConfigHelper + { + public T ReadConfig() + { + try + { + return JsonConvert.DeserializeObject(File.ReadAllText(_CnfName)); + } + catch (Exception) + { + throw new Exception("读取配置错误,请检查配置文件"); + } + } + + public void WriteConfig(object obj) + { + File.WriteAllText(_CnfName, JsonConvert.SerializeObject(obj, Formatting.Indented)); + } + + public ConfigHelper() + { + if (!Directory.Exists(_ApplicationData))//判断是否存在 + { + Directory.CreateDirectory(_ApplicationData);//创建新路径 + } + if (!File.Exists(_CnfName))//文件不存在 + { + FileStream fs1 = new FileStream(_CnfName, FileMode.Create, FileAccess.ReadWrite); + fs1.Close(); + WriteConfig(new ConfigModel().InitialConfig()); + } + } + + /// + /// 配置文件 + /// + private static string _CnfName { get => $"{_ApplicationData}\\stranslate.json"; } + + /// + /// C:\Users\user\AppData\Local\STranslate + /// + private static readonly string _ApplicationData + = $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}" + + $"\\{Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location)}"; + private static ConfigHelper _Instance; + public static ConfigHelper Instance { get => _Instance ?? (_Instance = new ConfigHelper()); } + } +} diff --git a/STranslate/Model/ConfigModel.cs b/STranslate/Model/ConfigModel.cs new file mode 100644 index 0000000..5a75f71 --- /dev/null +++ b/STranslate/Model/ConfigModel.cs @@ -0,0 +1,69 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace STranslate.Model +{ + public class ConfigModel + { + public ConfigModel() + { + } + + public ConfigModel InitialConfig() + { + var defaultServer = new Server + { + Name = "zggsong", + Api = "https://zggsong.cn/tt" + }; + return new ConfigModel + { + IsBright = true, + SelectServer = defaultServer, + Servers = new Server[] + { + defaultServer, + new Server + { + Name = "zu1k", + Api = "https://deepl.deno.dev/translate" + }, + new Server + { + Name = "local", + Api = "http://127.0.0.1:8000/translate" + } + } + }; + } + + /// + /// 是否亮色模式 + /// + [JsonProperty("isBright")] + public bool IsBright { get; set; } + + [JsonProperty("selectServer")] + public Server SelectServer { get; set; } + + /// + /// 服务 + /// + [JsonProperty("servers")] + public Server[] Servers { get; set; } + + } + + public class Server + { + [JsonProperty("name")] + public string Name { get; set; } + + [JsonProperty("api")] + public string Api { get; set; } + } +} diff --git a/STranslate/Model/TranslateInterface.cs b/STranslate/Model/TranslateInterface.cs deleted file mode 100644 index 033ba89..0000000 --- a/STranslate/Model/TranslateInterface.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace STranslate.Model -{ - public class TranslationInterface - { - public string Name { get; set; } - public string Api { get; set; } - } -} diff --git a/STranslate/STranslate.csproj b/STranslate/STranslate.csproj index c604936..ad0ae21 100644 --- a/STranslate/STranslate.csproj +++ b/STranslate/STranslate.csproj @@ -82,12 +82,13 @@ MSBuild:Compile Designer + - + True diff --git a/STranslate/ViewModel/MainVM.cs b/STranslate/ViewModel/MainVM.cs index 4adde03..be4f450 100644 --- a/STranslate/ViewModel/MainVM.cs +++ b/STranslate/ViewModel/MainVM.cs @@ -19,14 +19,16 @@ namespace STranslate.ViewModel public MainVM() { + if (!InitialConfig()) + { + Task.Delay(3000); + Environment.Exit(-1); + } InputCombo = LanguageEnumDict.Keys.ToList(); InputComboSelected = LanguageEnum.AUTO.GetDescription(); OutputCombo = LanguageEnumDict.Keys.ToList(); OutputComboSelected = LanguageEnum.AUTO.GetDescription(); - //初始化接口 - SelectedTranslationInterface = TranslationInterface[1]; - //复制输入 CopyInputCmd = new RelayCommand((_) => true, (_) => { @@ -72,6 +74,32 @@ namespace STranslate.ViewModel } #region handle + + /// + /// 初始化配置文件 + /// + /// + private bool InitialConfig() + { + try + { + GlobalConfig = ConfigHelper.Instance.ReadConfig(); + //更新服务 + TranslationInterface = GlobalConfig.Servers.ToList(); + + if (TranslationInterface.Count < 1) throw new Exception("尚未配置任何翻译接口服务"); + + //初始化接口 + SelectedTranslationInterface = GlobalConfig.SelectServer; + + return true; + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error); + return false; + } + } /// /// 自动识别语种 /// @@ -179,7 +207,7 @@ namespace STranslate.ViewModel OutputTxt = ex.Message; } } -#endregion handle + #endregion handle #region Params private string translateResp; @@ -191,6 +219,11 @@ namespace STranslate.ViewModel public ICommand CopyLargeHumpResultCmd { get; private set; } public ICommand ThemeConvertCmd { get; private set; } + /// + /// 全局配置文件 + /// + public ConfigModel GlobalConfig; + /// /// 识别语种 /// @@ -233,27 +266,11 @@ namespace STranslate.ViewModel /// /// 目标接口 /// - private List _TranslationInterface = new List - { - new TranslationInterface - { - Name = "zu1k", - Api = "https://deepl.deno.dev/translate" - }, - new TranslationInterface - { - Name = "zggsong", - Api = "https://zggsong.cn/tt" - }, - new TranslationInterface - { - Name = "local", - Api = "http://127.0.0.1:8000/translate" - } - }; - public List TranslationInterface { get => _TranslationInterface; set => UpdateProperty(ref _TranslationInterface, value); } - private TranslationInterface _SelectedTranslationInterface; - public TranslationInterface SelectedTranslationInterface { get => _SelectedTranslationInterface; set => UpdateProperty(ref _SelectedTranslationInterface, value); } + 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(); } private static readonly string _ThemeDark = "pack://application:,,,/STranslate;component/Style/Dark.xaml"; private static readonly string _ThemeDefault = "pack://application:,,,/STranslate;component/Style/Default.xaml";