master
ZGGSONG 2 years ago
commit 98605e2092

4
.gitignore vendored

@ -0,0 +1,4 @@
.vs/
bin/
obj/
*.user

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

@ -0,0 +1,9 @@
<Application x:Class="STranslate.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:STranslate"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

@ -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
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}

@ -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)
)]

@ -0,0 +1,117 @@
<Window x:Class="STranslate.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:STranslate"
xmlns:vm="clr-namespace:STranslate.ViewModel"
mc:Ignorable="d"
Background="Transparent"
WindowStartupLocation="CenterScreen"
MouseLeftButtonDown="Window_MouseLeftButtonDown"
AllowsTransparency="True"
ResizeMode="NoResize"
KeyDown="Window_KeyDown"
Topmost="True"
WindowStyle="None"
Height="450"
Width="400">
<Window.DataContext>
<vm:MainVM/>
</Window.DataContext>
<Border BorderThickness="0"
Background="BlanchedAlmond"
CornerRadius="10">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="3*"/>
<RowDefinition Height="5*"/>
</Grid.RowDefinitions>
<!--input-->
<Grid Margin="10">
<!--输入框-->
<Border BorderBrush="#000"
BorderThickness="1"
CornerRadius="4"
Margin="5"
MaxHeight="200">
<TextBox Background="Transparent"
BorderBrush="{x:Null}"
Margin="5"
BorderThickness="0"
FontSize="18"
MinHeight="35"
TextWrapping="Wrap"
Text="{Binding InputTxt,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
<TextBox.InputBindings>
<KeyBinding Command="{Binding TranslateCmd}" Key="Enter"/>
</TextBox.InputBindings>
</TextBox>
</Border>
</Grid>
<!--output-->
<Grid Margin="10"
Grid.Row="1">
<Label Content="翻译结果: "/>
<!--输出框-->
<Border BorderBrush="#000"
BorderThickness="1"
CornerRadius="4"
Margin="5"
MaxHeight="200">
<TextBox Background="Transparent"
BorderBrush="{x:Null}"
BorderThickness="0"
IsReadOnly="True"
Margin="5"
FontSize="18"
MinHeight="35"
TextWrapping="Wrap"
Text="{Binding OutputTxt}"/>
</Border>
<!--<Button Content="复制"
Margin="0,0,10,0"
VerticalAlignment="Top"
HorizontalAlignment="Right"
Height="20"
Width="30">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="1,1" StartPoint="0,0">
<GradientStop Color="#C0C0C0" Offset="0"/>
<GradientStop Color="#787878" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="1,1" StartPoint="0,0">
<GradientStop Color="LightGray" Offset="0"/>
<GradientStop Color="Gray" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
<Button.Template>
<ControlTemplate TargetType="Button">
<Border BorderThickness="1" CornerRadius="4" Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Border>
</ControlTemplate>
</Button.Template>
</Button>-->
</Grid>
</Grid>
</Border>
</Window>

@ -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
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
/// <summary>
/// 移动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
/// <summary>
/// 快捷键
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
}
}
}
}

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

@ -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, //瑞典语
}
}

@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<None Remove=".gitignore" />
</ItemGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>..\vendor\Newtonsoft.Json.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

@ -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<DeeplResp>(responseString);
return resp;
}
}
}

@ -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
{
/// <summary>
/// 通知
/// </summary>
public class BaseVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void UpdateProperty<T>(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));
}
}
/// <summary>
/// Command
/// </summary>
public class RelayCommand : ICommand
{
private readonly Predicate<object> _canExecute;
private readonly Action<object> _execute;
public RelayCommand(Predicate<object> canExecute, Action<object> 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);
}
}
/// <summary>
/// 单例辅助类
/// </summary>
public abstract class SingletonMode<T> where T : class
{
public static T _Instance;
public static T Instance()
{
Type type = typeof(T);
lock (type)
{
if (SingletonMode<T>._Instance == null)
{
SingletonMode<T>._Instance = (Activator.CreateInstance(typeof(T), true) as T);
}
return SingletonMode<T>._Instance;
}
}
}
}

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

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

Binary file not shown.