-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
274 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
STranslate/ViewModels/Preference/Services/TranslatorSTranslate.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
using CommunityToolkit.Mvvm.Input; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using STranslate.Model; | ||
using STranslate.Util; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace STranslate.ViewModels.Preference.Services | ||
{ | ||
public partial class TranslatorSTranslate : ObservableObject, ITranslator | ||
{ | ||
public TranslatorSTranslate() | ||
: this(Guid.NewGuid(), "", "STranslate") { } | ||
|
||
public TranslatorSTranslate( | ||
Guid guid, | ||
string url, | ||
string name = "", | ||
IconType icon = IconType.STranslate, | ||
string appID = "", | ||
string appKey = "", | ||
bool isEnabled = true, | ||
ServiceType type = ServiceType.STranslateService | ||
) | ||
{ | ||
Identify = guid; | ||
Url = url; | ||
Name = name; | ||
Icon = icon; | ||
AppID = appID; | ||
AppKey = appKey; | ||
IsEnabled = isEnabled; | ||
Type = type; | ||
} | ||
|
||
[ObservableProperty] | ||
private Guid _identify = Guid.Empty; | ||
|
||
[JsonIgnore] | ||
[ObservableProperty] | ||
private ServiceType _type = 0; | ||
|
||
[JsonIgnore] | ||
[ObservableProperty] | ||
public bool _isEnabled = true; | ||
|
||
[JsonIgnore] | ||
[ObservableProperty] | ||
private string _name = string.Empty; | ||
|
||
[JsonIgnore] | ||
[ObservableProperty] | ||
private IconType _icon = IconType.STranslate; | ||
|
||
[JsonIgnore] | ||
[ObservableProperty] | ||
public string _url = string.Empty; | ||
|
||
[JsonIgnore] | ||
[ObservableProperty] | ||
public string _AppID = string.Empty; | ||
|
||
[JsonIgnore] | ||
[ObservableProperty] | ||
public string _appKey = string.Empty; | ||
|
||
[JsonIgnore] | ||
[ObservableProperty] | ||
[property: JsonIgnore] | ||
public TranslationResult _data = TranslationResult.Reset; | ||
|
||
[JsonIgnore] | ||
public List<IconType> Icons { get; private set; } = Enum.GetValues(typeof(IconType)).OfType<IconType>().ToList(); | ||
|
||
[JsonIgnore] | ||
public string Tips { get; set; } = "本地服务,无需配置"; | ||
|
||
#region Show/Hide Encrypt Info | ||
|
||
[JsonIgnore] | ||
[ObservableProperty] | ||
[property: JsonIgnore] | ||
private bool _idHide = true; | ||
|
||
[JsonIgnore] | ||
[ObservableProperty] | ||
[property: JsonIgnore] | ||
private bool _keyHide = true; | ||
|
||
private void ShowEncryptInfo(string? obj) | ||
{ | ||
if (obj == null) | ||
return; | ||
|
||
if (obj.Equals(nameof(AppID))) | ||
{ | ||
IdHide = !IdHide; | ||
} | ||
else if (obj.Equals(nameof(AppKey))) | ||
{ | ||
KeyHide = !KeyHide; | ||
} | ||
} | ||
|
||
private RelayCommand<string>? showEncryptInfoCommand; | ||
|
||
[JsonIgnore] | ||
public IRelayCommand<string> ShowEncryptInfoCommand => showEncryptInfoCommand ??= new RelayCommand<string>(new Action<string?>(ShowEncryptInfo)); | ||
|
||
#endregion Show/Hide Encrypt Info | ||
|
||
public async Task<TranslationResult> TranslateAsync(object request, CancellationToken token) | ||
{ | ||
if (request is RequestModel req) | ||
{ | ||
//https://github.com/Baozisoftware/go-dll/wiki/C%23%E8%B0%83%E7%94%A8Go%E7%89%88DLL#%E5%85%B3%E4%BA%8Ego%E7%9A%84%E6%95%B0%E7%BB%84%E5%88%87%E7%89%87%E8%BF%94%E5%9B%9E%E9%97%AE%E9%A2%98 | ||
//加入这个就不崩溃了 | ||
Environment.SetEnvironmentVariable("GODEBUG", "cgocheck=0"); | ||
var sourceBytes = Encoding.UTF8.GetBytes(req.SourceLang); | ||
var targetBytes = Encoding.UTF8.GetBytes(req.TargetLang); | ||
var contentBytes = Encoding.UTF8.GetBytes(req.Text); | ||
var result = await Task.Run(() => GoUtil.Execute(sourceBytes, targetBytes, contentBytes)); | ||
var resp = GoUtil.GoStringToCSharpString(result); | ||
|
||
// 解析JSON数据 | ||
var parsedData = JsonConvert.DeserializeObject<JObject>(resp ?? throw new Exception("请求结果为空")) ?? throw new Exception($"反序列化失败: {resp}"); | ||
|
||
// 提取content的值 | ||
var data = parsedData["data"]?.ToString() ?? throw new Exception("未获取到结果"); | ||
|
||
return string.IsNullOrEmpty(data) ? TranslationResult.Fail("获取结果为空") : TranslationResult.Success(data); | ||
} | ||
|
||
throw new Exception($"请求数据出错: {request}"); | ||
} | ||
|
||
public Task TranslateAsync(object request, Action<string> OnDataReceived, CancellationToken token) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
STranslate/Views/Preference/Service/TextSTranslateServicesPage.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<UserControl x:Class="STranslate.Views.Preference.Service.TextSTranslateServicesPage" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:common="clr-namespace:STranslate.Style.Commons;assembly=STranslate.Style" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:vm="clr-namespace:STranslate.ViewModels.Preference.Services" | ||
d:DataContext="{d:DesignInstance Type=vm:TranslatorSTranslate}" | ||
d:DesignHeight="450" | ||
d:DesignWidth="800" | ||
Background="Transparent" | ||
FontSize="{DynamicResource FontSize_Content}" | ||
mc:Ignorable="d"> | ||
<Border Padding="10,20" | ||
BorderBrush="{DynamicResource BorderBrushColor}" | ||
BorderThickness="1" | ||
CornerRadius="4"> | ||
<StackPanel> | ||
<Grid> | ||
<Grid.ColumnDefinitions> | ||
<ColumnDefinition Width="80" /> | ||
<ColumnDefinition Width="*" /> | ||
</Grid.ColumnDefinitions> | ||
<TextBlock Text="类型: " /> | ||
|
||
<Border Grid.Column="1" | ||
Width="88" | ||
HorizontalAlignment="Left" | ||
BorderBrush="{DynamicResource ThemeAccentColor}" | ||
BorderThickness="2" | ||
CornerRadius="5"> | ||
<TextBlock Padding="5,2" FontWeight="Black" Text="本地服务" /> | ||
</Border> | ||
</Grid> | ||
|
||
<Grid Margin="0,10,0,0"> | ||
<Grid.ColumnDefinitions> | ||
<ColumnDefinition Width="80" /> | ||
<ColumnDefinition Width="*" /> | ||
</Grid.ColumnDefinitions> | ||
<TextBlock Text="名称: " /> | ||
|
||
<common:PlaceholderTextBox Grid.Column="1" | ||
MinWidth="160" | ||
HorizontalAlignment="Left" | ||
Placeholder="STranslate" | ||
Text="{Binding Name, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" /> | ||
</Grid> | ||
|
||
<Grid Margin="0,10,0,0"> | ||
<Grid.ColumnDefinitions> | ||
<ColumnDefinition Width="80" /> | ||
<ColumnDefinition Width="*" /> | ||
</Grid.ColumnDefinitions> | ||
<TextBlock Text="图标: " /> | ||
|
||
<ComboBox Grid.Column="1" | ||
Width="120" | ||
HorizontalAlignment="Left" | ||
BorderThickness="1" | ||
ItemsSource="{Binding Icons}" | ||
SelectedValue="{Binding Icon}" /> | ||
</Grid> | ||
|
||
<Grid Margin="0,10,0,0"> | ||
<Grid.ColumnDefinitions> | ||
<ColumnDefinition Width="80" /> | ||
<ColumnDefinition Width="*" /> | ||
</Grid.ColumnDefinitions> | ||
<TextBlock Text="说明: " /> | ||
<TextBlock Grid.Column="1" Text="本地服务" /> | ||
<TextBlock Grid.Column="1" Margin="80,0,0,0" Style="{DynamicResource InfoTextBlock}" ToolTip="{Binding Tips}" /> | ||
</Grid> | ||
</StackPanel> | ||
</Border> | ||
</UserControl> |
26 changes: 26 additions & 0 deletions
26
STranslate/Views/Preference/Service/TextSTranslateServicesPage.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.Diagnostics; | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using STranslate.Model; | ||
|
||
namespace STranslate.Views.Preference.Service | ||
{ | ||
public partial class TextSTranslateServicesPage : UserControl | ||
{ | ||
public TextSTranslateServicesPage(ITranslator vm) | ||
{ | ||
InitializeComponent(); | ||
|
||
DataContext = vm; | ||
} | ||
|
||
/// <summary> | ||
/// 通过缓存加载View时刷新ViewModel | ||
/// </summary> | ||
/// <param name="vm"></param> | ||
public void UpdateVM(ITranslator vm) | ||
{ | ||
DataContext = vm; | ||
} | ||
} | ||
} |