commit 98605e209246f2efb0e986038e2af2a6cbdf1e79 Author: ZGGSONG Date: Wed Dec 14 19:08:38 2022 +0800 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d0dd0fd --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.vs/ +bin/ +obj/ +*.user \ No newline at end of file diff --git a/STranslate.sln b/STranslate.sln new file mode 100644 index 0000000..e49b65f --- /dev/null +++ b/STranslate.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31702.278 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "STranslate", "STranslate\STranslate.csproj", "{03C38651-89E7-4239-A2FD-AD261653B057}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {03C38651-89E7-4239-A2FD-AD261653B057}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {03C38651-89E7-4239-A2FD-AD261653B057}.Debug|Any CPU.Build.0 = Debug|Any CPU + {03C38651-89E7-4239-A2FD-AD261653B057}.Release|Any CPU.ActiveCfg = Release|Any CPU + {03C38651-89E7-4239-A2FD-AD261653B057}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {07ED2423-013E-426C-B6D4-3B69B873877D} + EndGlobalSection +EndGlobal diff --git a/STranslate/App.xaml b/STranslate/App.xaml new file mode 100644 index 0000000..7be6f55 --- /dev/null +++ b/STranslate/App.xaml @@ -0,0 +1,9 @@ + + + + + diff --git a/STranslate/App.xaml.cs b/STranslate/App.xaml.cs new file mode 100644 index 0000000..7f94bd6 --- /dev/null +++ b/STranslate/App.xaml.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace STranslate +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App : Application + { + } +} diff --git a/STranslate/AssemblyInfo.cs b/STranslate/AssemblyInfo.cs new file mode 100644 index 0000000..8b5504e --- /dev/null +++ b/STranslate/AssemblyInfo.cs @@ -0,0 +1,10 @@ +using System.Windows; + +[assembly: ThemeInfo( + ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located + //(used if a resource is not found in the page, + // or application resource dictionaries) + ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located + //(used if a resource is not found in the page, + // app, or any theme specific resource dictionaries) +)] diff --git a/STranslate/MainWindow.xaml b/STranslate/MainWindow.xaml new file mode 100644 index 0000000..7150bc0 --- /dev/null +++ b/STranslate/MainWindow.xaml @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/STranslate/MainWindow.xaml.cs b/STranslate/MainWindow.xaml.cs new file mode 100644 index 0000000..9451d36 --- /dev/null +++ b/STranslate/MainWindow.xaml.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace STranslate +{ + /// + /// Interaction logic for MainWindow.xaml + /// + public partial class MainWindow : Window + { + public MainWindow() + { + InitializeComponent(); + } + + /// + /// 移动 + /// + /// + /// + private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) + { + DragMove(); + } + + /// + /// 快捷键 + /// + /// + /// + private void Window_KeyDown(object sender, KeyEventArgs e) + { + //置顶/取消置顶 Ctrl+T + if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control) && e.Key == Key.T) + { + Topmost = Topmost != true; + Opacity = Topmost ? 1 : 0.9; + } + //最小化 Ctrl+M + if (e.Key == Key.Escape) + { + WindowState = WindowState.Minimized; + } + //退出 Ctrl+Q + if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control) && e.Key == Key.Q) + { + Application.Current.Shutdown(); + } + //缩小 Ctrl+[ + if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control) && e.Key == Key.OemOpenBrackets) + { + if (Width < 245) + { + return; + } + Width /= 1.2; + Height /= 1.2; + } + //放大 Ctrl+] + if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control) && e.Key == Key.OemCloseBrackets) + { + if (Width > 600) + { + return; + } + Width *= 1.2; + Height *= 1.2; + } + //恢复界面大小 Ctrl+P + if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control) && e.Key == Key.P) + { + Width = 400; + Height = 450; + } + } + } +} diff --git a/STranslate/Model/DeeplModel.cs b/STranslate/Model/DeeplModel.cs new file mode 100644 index 0000000..598259a --- /dev/null +++ b/STranslate/Model/DeeplModel.cs @@ -0,0 +1,30 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace STranslate.Model +{ + public class DeeplReq + { + [JsonProperty("text")] + public string Text { get; set; } + + [JsonProperty("source_lang")] + public string SourceLang { get; set; } + + [JsonProperty("target_lang")] + public string TargetLang { get; set; } + } + + public class DeeplResp + { + [JsonProperty("code")] + public int Code { get; set; } + + [JsonProperty("data")] + public string Data { get; set; } + } +} diff --git a/STranslate/Model/LanguageEnum.cs b/STranslate/Model/LanguageEnum.cs new file mode 100644 index 0000000..3c6b912 --- /dev/null +++ b/STranslate/Model/LanguageEnum.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace STranslate.Model +{ + public enum LanguageEnum + { + DE, //德语 + EN, //英语 + ES, //西班牙语 + FR, //法语 + IT, //意大利语 + JA, //日语 + NL, //荷兰语 + PL, //波兰语 + PT, //葡萄牙语 + RU, //俄语 + ZH, //中文 + BG, //保加利亚语 + CS, //捷克语 + DA, //丹麦语 + EL, //希腊语 + ET, //爱沙尼亚语 + FI, //芬兰语 + HU, //匈牙利语 + LT, //立陶宛语 + LV, //拉脱维亚语 + RO, //罗马尼亚语 + SK, //斯洛伐克语 + SL, //斯洛文尼亚语 + SV, //瑞典语 + } +} diff --git a/STranslate/STranslate.csproj b/STranslate/STranslate.csproj new file mode 100644 index 0000000..51be18d --- /dev/null +++ b/STranslate/STranslate.csproj @@ -0,0 +1,19 @@ + + + + WinExe + net5.0-windows + true + + + + + + + + + ..\vendor\Newtonsoft.Json.dll + + + + diff --git a/STranslate/Utils/HttpUtil.cs b/STranslate/Utils/HttpUtil.cs new file mode 100644 index 0000000..9c2a87b --- /dev/null +++ b/STranslate/Utils/HttpUtil.cs @@ -0,0 +1,35 @@ +using Newtonsoft.Json; +using STranslate.Model; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; + +namespace STranslate.Utils +{ + public static class HttpUtil + { + public static DeeplResp Post(string url, DeeplReq req) + { + //json参数 + string jsonParam = JsonConvert.SerializeObject(req); + var request = (HttpWebRequest)WebRequest.Create(url); + request.Method = "POST"; + request.ContentType = "application/json;charset=UTF-8"; + byte[] byteData = Encoding.UTF8.GetBytes(jsonParam); + int length = byteData.Length; + request.ContentLength = length; + Stream writer = request.GetRequestStream(); + writer.Write(byteData, 0, length); + writer.Close(); + var response = (HttpWebResponse)request.GetResponse(); + var responseString = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd(); + var resp = JsonConvert.DeserializeObject(responseString); + return resp; + } + } +} diff --git a/STranslate/Utils/MVVMUtil.cs b/STranslate/Utils/MVVMUtil.cs new file mode 100644 index 0000000..4cb3f94 --- /dev/null +++ b/STranslate/Utils/MVVMUtil.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Input; + +namespace STranslate.Utils +{ + /// + /// 通知 + /// + public class BaseVM : INotifyPropertyChanged + { + public event PropertyChangedEventHandler PropertyChanged; + protected void UpdateProperty(ref T properValue, T newValue, [CallerMemberName] string properName = "") + { + if (object.Equals(properValue, newValue)) + return; + properValue = newValue; + NotifyPropertyChanged(properName); + } + + public void NotifyPropertyChanged([CallerMemberName] string propertyName = null) + { + this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + } + /// + /// Command + /// + public class RelayCommand : ICommand + { + private readonly Predicate _canExecute; + private readonly Action _execute; + + public RelayCommand(Predicate canExecute, Action execute) + { + this._canExecute = canExecute; + this._execute = execute; + } + + public event EventHandler CanExecuteChanged + { + add { CommandManager.RequerySuggested += value; } + remove { CommandManager.RequerySuggested -= value; } + } + + public bool CanExecute(object parameter) + { + return _canExecute(parameter); + } + + public void Execute(object parameter) + { + _execute(parameter); + } + } + + /// + /// 单例辅助类 + /// + public abstract class SingletonMode where T : class + { + public static T _Instance; + public static T Instance() + { + Type type = typeof(T); + lock (type) + { + if (SingletonMode._Instance == null) + { + SingletonMode._Instance = (Activator.CreateInstance(typeof(T), true) as T); + } + return SingletonMode._Instance; + } + } + } +} diff --git a/STranslate/Utils/TranslateUtil.cs b/STranslate/Utils/TranslateUtil.cs new file mode 100644 index 0000000..50b9408 --- /dev/null +++ b/STranslate/Utils/TranslateUtil.cs @@ -0,0 +1,29 @@ +using STranslate.Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace STranslate.Utils +{ + public static class TranslateUtil + { + private static readonly string _url = "http://172.17.209.47:8080/translate"; + public static string Translate(string input, LanguageEnum source, LanguageEnum target) + { + var req = new DeeplReq() + { + Text = input, + SourceLang = source.ToString(), + TargetLang = target.ToString(), + }; + var resp = HttpUtil.Post(_url, req); + if (resp.Code == 200) + { + return resp.Data; + } + return string.Empty; + } + } +} diff --git a/STranslate/ViewModel/MainVM.cs b/STranslate/ViewModel/MainVM.cs new file mode 100644 index 0000000..9722069 --- /dev/null +++ b/STranslate/ViewModel/MainVM.cs @@ -0,0 +1,36 @@ +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.Input; + +namespace STranslate.ViewModel +{ + public class MainVM : BaseVM + { + public MainVM() + { + TranslateCmd = new RelayCommand((_) => + { + return string.IsNullOrEmpty(InputTxt) ? false : true; + }, (_) => + { + OutputTxt = TranslateUtil.Translate(InputTxt, LanguageEnum.ZH, LanguageEnum.EN); + //清空输入框 + InputTxt = ""; + + }); + } + + public ICommand TranslateCmd { get; private set; } + + private string _InputTxt; + public string InputTxt { get => _InputTxt; set => UpdateProperty(ref _InputTxt, value); } + private string _OutputTxt; + public string OutputTxt { get => _OutputTxt; set => UpdateProperty(ref _OutputTxt, value); } + + } +} diff --git a/vendor/Newtonsoft.Json.dll b/vendor/Newtonsoft.Json.dll new file mode 100644 index 0000000..1971a35 Binary files /dev/null and b/vendor/Newtonsoft.Json.dll differ