perf: update the selected service in the configuration file

dev_config
SONGE9B3\song_pd_win11 2 years ago
parent 83235deda4
commit aab8f0d387

@ -46,14 +46,13 @@ namespace STranslate.Helper
/// <summary> /// <summary>
/// 配置文件 /// 配置文件
/// </summary> /// </summary>
private static string _CnfName { get => $"{_ApplicationData}\\stranslate.json"; } private static string _CnfName => $"{_ApplicationData}\\{_AppName.ToLower()}.json";
/// <summary> /// <summary>
/// C:\Users\user\AppData\Local\STranslate /// C:\Users\user\AppData\Local\STranslate
/// </summary> /// </summary>
private static readonly string _ApplicationData private static string _ApplicationData => $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}\\{_AppName}";
= $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}" private static readonly string _AppName = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location);
+ $"\\{Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location)}";
private static ConfigHelper _Instance; private static ConfigHelper _Instance;
public static ConfigHelper Instance { get => _Instance ?? (_Instance = new ConfigHelper()); } public static ConfigHelper Instance { get => _Instance ?? (_Instance = new ConfigHelper()); }
} }

@ -48,8 +48,8 @@
<ComboBox Style="{DynamicResource cmbstyle}" <ComboBox Style="{DynamicResource cmbstyle}"
Margin="0,5,10,0" Margin="0,5,10,0"
ItemsSource="{Binding TranslationInterface}" ItemsSource="{Binding TranslationInterface}"
SelectedItem="{Binding SelectedTranslationInterface, Mode=TwoWay, NotifyOnSourceUpdated=True}"
DisplayMemberPath="Name" DisplayMemberPath="Name"
SelectedItem="{Binding SelectedTranslationInterface}"
HorizontalAlignment="Right"/> HorizontalAlignment="Right"/>
</Grid> </Grid>

@ -15,18 +15,19 @@ namespace STranslate.Model
public ConfigModel InitialConfig() public ConfigModel InitialConfig()
{ {
var defaultServer = new Server
{
Name = "zggsong",
Api = "https://zggsong.cn/tt"
};
return new ConfigModel return new ConfigModel
{ {
IsBright = true, IsBright = true,
SelectServer = defaultServer, SourceLanguage = LanguageEnum.AUTO,
TargetLanguage = LanguageEnum.AUTO,
SelectServer = 0,
Servers = new Server[] Servers = new Server[]
{ {
defaultServer, new Server
{
Name = "zggsong",
Api = "https://zggsong.cn/tt"
},
new Server new Server
{ {
Name = "zu1k", Name = "zu1k",
@ -47,8 +48,14 @@ namespace STranslate.Model
[JsonProperty("isBright")] [JsonProperty("isBright")]
public bool IsBright { get; set; } public bool IsBright { get; set; }
[JsonProperty("sourceLanguage")]
public LanguageEnum SourceLanguage { get; set; }
[JsonProperty("targetLanguage")]
public LanguageEnum TargetLanguage { get; set; }
[JsonProperty("selectServer")] [JsonProperty("selectServer")]
public Server SelectServer { get; set; } public int SelectServer { get; set; }
/// <summary> /// <summary>
/// 服务 /// 服务

@ -24,10 +24,6 @@ namespace STranslate.ViewModel
Task.Delay(3000); Task.Delay(3000);
Environment.Exit(-1); Environment.Exit(-1);
} }
InputCombo = LanguageEnumDict.Keys.ToList();
InputComboSelected = LanguageEnum.AUTO.GetDescription();
OutputCombo = LanguageEnumDict.Keys.ToList();
OutputComboSelected = LanguageEnum.AUTO.GetDescription();
//复制输入 //复制输入
CopyInputCmd = new RelayCommand((_) => true, (_) => CopyInputCmd = new RelayCommand((_) => true, (_) =>
@ -83,14 +79,27 @@ namespace STranslate.ViewModel
{ {
try try
{ {
InputCombo = LanguageEnumDict.Keys.ToList();
OutputCombo = LanguageEnumDict.Keys.ToList();
GlobalConfig = ConfigHelper.Instance.ReadConfig<ConfigModel>(); GlobalConfig = ConfigHelper.Instance.ReadConfig<ConfigModel>();
//更新服务 //更新服务
TranslationInterface = GlobalConfig.Servers.ToList(); TranslationInterface = GlobalConfig.Servers.ToList();
if (TranslationInterface.Count < 1) throw new Exception("尚未配置任何翻译接口服务"); if (TranslationInterface.Count < 1) throw new Exception("尚未配置任何翻译接口服务");
//初始化接口 try
SelectedTranslationInterface = GlobalConfig.SelectServer; {
//配置读取接口
SelectedTranslationInterface = TranslationInterface[GlobalConfig.SelectServer];
}
catch (Exception ex)
{
throw new Exception($"配置文件选择服务索引出错, 请修改配置文件后重试", ex);
}
//从配置读取source target
InputComboSelected = GlobalConfig.SourceLanguage.GetDescription();
OutputComboSelected = GlobalConfig.TargetLanguage.GetDescription();
return true; return true;
} }
@ -130,18 +139,8 @@ namespace STranslate.ViewModel
{ {
return new Tuple<string, string>(LanguageEnum.ZH.GetDescription(), LanguageEnum.EN.GetDescription()); return new Tuple<string, string>(LanguageEnum.ZH.GetDescription(), LanguageEnum.EN.GetDescription());
} }
#if false
//如果输入是中文
if (Regex.IsMatch(text, @"^[\u4e00-\u9fa5]+$"))
{
return new Tuple<string, string>(LanguageEnum.ZH.GetDescription(), LanguageEnum.EN.GetDescription());
}
else
{
return new Tuple<string, string>(LanguageEnum.EN.GetDescription(), LanguageEnum.ZH.GetDescription());
}
#endif
} }
/// <summary> /// <summary>
/// 翻译 /// 翻译
/// </summary> /// </summary>
@ -268,7 +267,7 @@ namespace STranslate.ViewModel
/// </summary> /// </summary>
private List<Server> _TranslationInterface; private List<Server> _TranslationInterface;
public List<Server> TranslationInterface { get => _TranslationInterface; set => UpdateProperty(ref _TranslationInterface, value); } public List<Server> TranslationInterface { get => _TranslationInterface; set => UpdateProperty(ref _TranslationInterface, value); }
private Server _SelectedTranslationInterface; private Server _SelectedTranslationInterface;
public Server SelectedTranslationInterface { get => _SelectedTranslationInterface; set => UpdateProperty(ref _SelectedTranslationInterface, value); } public Server SelectedTranslationInterface { get => _SelectedTranslationInterface; set => UpdateProperty(ref _SelectedTranslationInterface, value); }
private static Dictionary<string, LanguageEnum> LanguageEnumDict { get => Util.Util.GetEnumList<LanguageEnum>(); } private static Dictionary<string, LanguageEnum> LanguageEnumDict { get => Util.Util.GetEnumList<LanguageEnum>(); }

Loading…
Cancel
Save