You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

102 lines
3.7 KiB

2 years ago
using STranslate.Model;
using STranslate.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
2 years ago
using System.Windows.Input;
namespace STranslate.ViewModel
{
public class MainVM : BaseVM
{
2 years ago
private static readonly string ConfigPath = @"D:\STranslate.yml";
public static ConfigModel config = new ConfigModel();
private static Dictionary<string, LanguageEnum> LanguageEnumDict { get => TranslateUtil.GetEnumList<LanguageEnum>(); }
2 years ago
private string Text;
2 years ago
public MainVM()
{
2 years ago
try
2 years ago
{
//初始化界面参数
InputCombo = LanguageEnumDict.Keys.ToList();
InputComboSelected = LanguageEnum.AUTO.GetDescription();
OutputCombo = LanguageEnumDict.Keys.ToList();
OutputComboSelected = LanguageEnum.EN.GetDescription();
2 years ago
config = ConfigUtil.ReadConfig(ConfigPath);
//手动复制翻译结果
CopyTranslateResultCmd = new RelayCommand((_) =>
{
return string.IsNullOrEmpty(OutputTxt) ? false : true;
}, (_) =>
{
System.Diagnostics.Debug.Print("手动复制翻译结果: " + OutputTxt);
Clipboard.SetText(OutputTxt);
});
2 years ago
TranslateCmd = new RelayCommand((_) =>
{
return string.IsNullOrEmpty(InputTxt) ? false : true;
}, async (_) =>
{
Text = InputTxt;
2 years ago
//清空输入框
InputTxt = "";
2 years ago
2 years ago
OutputTxt = "翻译中...";
2 years ago
//获取结果
//var translateResp = await TranslateUtil.TranslateDeepLAsync(InputTxt, LanguageEnum.EN, LanguageEnum.auto);
var translateResp = await TranslateUtil.TranslateBaiduAsync(config.baidu.appid, config.baidu.secretKey, Text, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]);
2 years ago
if (translateResp == string.Empty)
{
OutputTxt = "翻译出错,请稍候再试...";
return;
}
OutputTxt = translateResp;
Clipboard.SetText(OutputTxt);
2 years ago
});
2 years ago
}
catch (Exception ex)
{
OutputTxt = ex.Message;
}
2 years ago
}
public ICommand TranslateCmd { get; private set; }
public ICommand CopyTranslateResultCmd { get; private set; }
2 years ago
private string _InputTxt;
public string InputTxt { get => _InputTxt; set => UpdateProperty(ref _InputTxt, value); }
2 years ago
private string _OutputTxt;
public string OutputTxt { get => _OutputTxt; set => UpdateProperty(ref _OutputTxt, value); }
private List<string> _InputCombo;
public List<string> InputCombo { get => _InputCombo; set => UpdateProperty(ref _InputCombo, value); }
private string _InputComboSelected;
public string InputComboSelected { get => _InputComboSelected; set => UpdateProperty(ref _InputComboSelected, value); }
private List<string> _OutputCombo;
public List<string> OutputCombo { get => _OutputCombo; set => UpdateProperty(ref _OutputCombo, value); }
private string _OutputComboSelected;
public string OutputComboSelected { get => _OutputComboSelected; set => UpdateProperty(ref _OutputComboSelected, value); }
private static readonly MainVM _Instance = new MainVM();
public static MainVM Instance { get => _Instance; }
2 years ago
}
}