feat: add config

dev_without_third_libraries
ZGGSONG 2 years ago
parent 18ff0a71b6
commit 319087825d

@ -10,6 +10,7 @@
WindowStartupLocation="CenterScreen"
MouseLeftButtonDown="Window_MouseLeftButtonDown"
AllowsTransparency="True"
ShowInTaskbar="False"
ResizeMode="NoResize"
KeyDown="Window_KeyDown"
Topmost="True"

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

@ -16,6 +16,10 @@
<None Remove="Images\translate_x64.png" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="YamlDotNet" Version="12.2.1" />
</ItemGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>..\vendor\Newtonsoft.Json.dll</HintPath>

@ -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<ConfigModel>(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());
}
}
}
}

@ -11,9 +11,17 @@ 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()
{
try
{
config = ConfigUtil.ReadConfig(ConfigPath);
TranslateCmd = new RelayCommand((_) =>
{
return string.IsNullOrEmpty(InputTxt) ? false : true;
@ -29,9 +37,7 @@ namespace STranslate.ViewModel
//获取结果
//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)
{
@ -41,6 +47,11 @@ namespace STranslate.ViewModel
OutputTxt = translateResp;
});
}
catch (Exception ex)
{
OutputTxt = ex.Message;
}
}
public ICommand TranslateCmd { get; private set; }

Loading…
Cancel
Save