dev_config
SONGE9B3\song_pd_win11 2 years ago
parent 492ef81f7c
commit 83235deda4

@ -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<T>()
{
try
{
return JsonConvert.DeserializeObject<T>(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());
}
}
/// <summary>
/// 配置文件
/// </summary>
private static string _CnfName { get => $"{_ApplicationData}\\stranslate.json"; }
/// <summary>
/// C:\Users\user\AppData\Local\STranslate
/// </summary>
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()); }
}
}

@ -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"
}
}
};
}
/// <summary>
/// 是否亮色模式
/// </summary>
[JsonProperty("isBright")]
public bool IsBright { get; set; }
[JsonProperty("selectServer")]
public Server SelectServer { get; set; }
/// <summary>
/// 服务
/// </summary>
[JsonProperty("servers")]
public Server[] Servers { get; set; }
}
public class Server
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("api")]
public string Api { get; set; }
}
}

@ -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; }
}
}

@ -82,12 +82,13 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Helper\ConfigHelper.cs" />
<Compile Include="Helper\GetWordsHelper.cs" />
<Compile Include="Helper\HotkeysHelper.cs" />
<Compile Include="Helper\MvvmHelper.cs" />
<Compile Include="Helper\NativeMethodHelper.cs" />
<Compile Include="Helper\StartupHelper.cs" />
<Compile Include="Model\TranslateInterface.cs" />
<Compile Include="Model\ConfigModel.cs" />
<Compile Include="Model\VisibilityConverter.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>

@ -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
/// <summary>
/// 初始化配置文件
/// </summary>
/// <returns></returns>
private bool InitialConfig()
{
try
{
GlobalConfig = ConfigHelper.Instance.ReadConfig<ConfigModel>();
//更新服务
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;
}
}
/// <summary>
/// 自动识别语种
/// </summary>
@ -191,6 +219,11 @@ namespace STranslate.ViewModel
public ICommand CopyLargeHumpResultCmd { get; private set; }
public ICommand ThemeConvertCmd { get; private set; }
/// <summary>
/// 全局配置文件
/// </summary>
public ConfigModel GlobalConfig;
/// <summary>
/// 识别语种
/// </summary>
@ -233,27 +266,11 @@ namespace STranslate.ViewModel
/// <summary>
/// 目标接口
/// </summary>
private List<TranslationInterface> _TranslationInterface = new List<TranslationInterface>
{
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> TranslationInterface { get => _TranslationInterface; set => UpdateProperty(ref _TranslationInterface, value); }
private TranslationInterface _SelectedTranslationInterface;
public TranslationInterface SelectedTranslationInterface { get => _SelectedTranslationInterface; set => UpdateProperty(ref _SelectedTranslationInterface, value); }
private List<Server> _TranslationInterface;
public List<Server> TranslationInterface { get => _TranslationInterface; set => UpdateProperty(ref _TranslationInterface, value); }
private Server _SelectedTranslationInterface;
public Server SelectedTranslationInterface { get => _SelectedTranslationInterface; set => UpdateProperty(ref _SelectedTranslationInterface, value); }
private static Dictionary<string, LanguageEnum> LanguageEnumDict { get => Util.Util.GetEnumList<LanguageEnum>(); }
private static readonly string _ThemeDark = "pack://application:,,,/STranslate;component/Style/Dark.xaml";
private static readonly string _ThemeDefault = "pack://application:,,,/STranslate;component/Style/Default.xaml";

Loading…
Cancel
Save