Skip to content
This repository was archived by the owner on Aug 22, 2023. It is now read-only.

Commit 39f0ec7

Browse files
committed
删除机翻接口代码; 更新Dict-Mini.json词典支持; 增加启动参数
1 parent 5de2b4c commit 39f0ec7

File tree

4 files changed

+39
-69
lines changed

4 files changed

+39
-69
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -362,4 +362,4 @@ MigrationBackup/
362362
# Fody - auto-generated XML schema
363363
FodyWeavers.xsd
364364

365-
Translator/baidu.ini
365+
.idea

Translator/MainWindow.xaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
</Grid.ColumnDefinitions>
7474

7575
<TextBlock
76-
Text="版本:0.2.4"
76+
Text="版本:0.3"
7777
Grid.Column="2"
7878
HorizontalAlignment="Right"
7979
VerticalAlignment="Bottom"
@@ -97,7 +97,7 @@
9797
<Separator Opacity="0" Height="10"/>
9898
<mah:ToggleSwitch x:Name="MarkCheckBox" Toggled="MarkCheckBox_OnToggled">标记中英文重复</mah:ToggleSwitch>
9999
<Separator Opacity="0" Height="10"/>
100-
<mah:ToggleSwitch x:Name="TransCheckBox" Toggled="TransCheckBox_OnToggled">机翻</mah:ToggleSwitch>
100+
<!-- <mah:ToggleSwitch x:Name="TransCheckBox" Toggled="TransCheckBox_OnToggled">机翻</mah:ToggleSwitch> -->
101101
<Separator Opacity="0" Height="10"/>
102102
<TextBlock TextWrapping="Wrap" Text="为防止滥用,10次翻译后会自动关闭机翻功能,需自行重开。" Foreground="#EF5350" Visibility="Hidden" x:Name="TransTips"/>
103103
</StackPanel>

Translator/MainWindow.xaml.cs

+34-59
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
using System;
2-
using System.CodeDom;
3-
using System.CodeDom.Compiler;
42
using System.Collections;
53
using System.Collections.Generic;
64
using System.ComponentModel;
75
using System.Diagnostics;
86
using System.IO;
97
using System.Linq;
10-
using System.Net.Mime;
118
using System.Text;
129
using System.Windows;
1310
using System.Windows.Controls;
1411
using System.Windows.Input;
1512
using System.Windows.Media.Animation;
1613
using System.Text.RegularExpressions;
17-
using System.Threading;
1814
using System.Threading.Tasks;
19-
using System.Windows.Forms.VisualStyles;
2015
using System.Xml;
2116
using ICSharpCode.AvalonEdit.Highlighting;
2217
using ICSharpCode.AvalonEdit.Highlighting.Xshd;
@@ -25,9 +20,8 @@
2520
using ICSharpCode.AvalonEdit.CodeCompletion;
2621
using System.Windows.Media;
2722
using ICSharpCode.AvalonEdit;
28-
using MahApps.Metro;
29-
using MahApps.Metro.Controls;
3023
using MahApps.Metro.IconPacks;
24+
using Newtonsoft.Json;
3125

3226
namespace Translator;
3327
/// <summary>
@@ -41,7 +35,7 @@ public partial class MainWindow
4135
private string FileDir = "";
4236
private StringBuilder EnJsonText = new();
4337
private ScrollViewer ScrollViewer;
44-
private Hashtable Dict = new();
38+
private Dictionary<string, List<string>> Dict = new();
4539
private DictObject dictObject = new ();
4640
private bool baidu = false;
4741

@@ -257,38 +251,23 @@ private async void TransWordList_OnSelectionChanged(object sender, SelectionChan
257251
await GetTransDict();
258252
}
259253

260-
private int count = 0;
254+
// private int count = 0;
261255

262256
private async Task GetTransDict()
263257
{
264258
dictObject.DictTrans.Clear();
265259
await Task.Delay(new Random().Next(50,300));
266260
TranslateSelector.ItemsSource = null;
267-
if (Dict.Contains(AvalonText.Text))
268-
dictObject.DictTrans.Add(new DictTransList(){Trans = (string)Dict[AvalonText.Text], MyIcon = PackIconUniconsKind.BookAlt});
269-
if (TransCheckBox.IsOn)
270-
{
271-
count += 1;
272-
if (count > 10)
273-
{
274-
TransCheckBox.IsOn = false;
275-
TransCloseTips();
276-
}
277-
else
261+
if (Dict.ContainsKey(AvalonText.Text))
262+
foreach (var value in Dict[AvalonText.Text])
278263
{
279-
var google = await TransApi.Google.Contect(AvalonText.Text);
280-
if (google != "") dictObject.DictTrans.Add(new DictTransList(){Trans = google, MyIcon = PackIconUniconsKind.Google});
281-
try
282-
{
283-
var baidu = await TransApi.Baidu.Contect(AvalonText.Text);
284-
if (baidu != "") dictObject.DictTrans.Add(new DictTransList(){Trans = baidu, MyIcon = PackIconUniconsKind.Bold});
285-
}
286-
catch (Exception e)
287-
{
288-
// nothing :)
289-
}
264+
dictObject.DictTrans.Add(
265+
new DictTransList()
266+
{
267+
Trans = value,
268+
MyIcon = PackIconUniconsKind.BookAlt
269+
});
290270
}
291-
}
292271

293272
if (dictObject.DictTrans.Count > 0)
294273
Dispatcher.Invoke(() =>
@@ -437,32 +416,28 @@ private void Grid_Loaded(object sender, RoutedEventArgs e)
437416

438417
ScrollViewer.ScrollToHorizontalOffset(0);
439418

440-
if (File.Exists("./dict.json"))
441-
Dict = SerialText(File.ReadAllText("./dict.json"), out var deleted);
442-
443-
if (File.Exists("./baidu.ini"))
419+
if (File.Exists("./Dict-Mini.json"))
420+
Dict = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(File.ReadAllText("./Dict-Mini.json"));
421+
// Dict = SerialText(File.ReadAllText("./Dict-Mini.json"), out var deleted);
422+
423+
var args = Environment.GetCommandLineArgs();
424+
if (args.Length <= 1) return;
425+
FileDir = args[1];
426+
var enDir = Path.Combine(FileDir, "en_us.json");
427+
var zhDir = Path.Combine(FileDir, "zh_cn.json");
428+
if (File.Exists(zhDir))
444429
{
445-
string id = "", key = "";
446-
// 读取百度翻译的配置文件,不用IniFile类
447-
var baiduIni = File.ReadAllText("./baidu.ini");
448-
var baiduIniLines = baiduIni.Split('\n');
449-
foreach (var line in baiduIniLines)
450-
{
451-
var lineSplit = line.Split('=');
452-
if (lineSplit[0] == "id")
453-
id = lineSplit[1];
454-
if (lineSplit[0] == "key")
455-
key = lineSplit[1];
456-
}
457-
// 判断id和key是否为空
458-
if (id != "" && key != "")
459-
{
460-
baidu = true;
461-
TransApi.Baidu.SetTransAPI(id, key);
462-
// MessageBox.Show(id);
463-
// MessageBox.Show(key);
464-
}
430+
var enAllText = File.ReadAllText(enDir);
431+
var zhAllText = File.ReadAllText(zhDir);
432+
SerialJson(enAllText, zhAllText);
465433
}
434+
else
435+
{
436+
var enAllText = File.ReadAllText(enDir);
437+
SerialJson(enAllText, "{}");
438+
}
439+
FirstPage.Visibility = Visibility.Hidden;
440+
SyncScrollPos();
466441
}
467442

468443
private List<string> formatList1 = new();
@@ -544,8 +519,8 @@ private void MarkCheckBox_OnToggled(object sender, RoutedEventArgs e)
544519

545520
private async void TransCheckBox_OnToggled(object sender, RoutedEventArgs e)
546521
{
547-
count = 0;
548-
if (TransCheckBox.IsOn) TransTips.Visibility = Visibility.Hidden;
522+
// count = 0;
523+
// if (TransCheckBox.IsOn) TransTips.Visibility = Visibility.Hidden;
549524
await GetTransDict();
550525
}
551526

@@ -577,7 +552,7 @@ private async void SearchWordList_OnSelectionChanged(object sender, SelectionCha
577552

578553
private void OpenGithubSite(object sender, RoutedEventArgs e)
579554
{
580-
Process.Start("https://github.com/Tryanks/Minecraft-Mods-Translator");
555+
Process.Start("https://github.com/CFPATools/Minecraft-Mods-Translator");
581556
}
582557
}
583558

Translator/Translator.csproj

+2-7
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@
275275
<Version>4.11.0</Version>
276276
</PackageReference>
277277
<PackageReference Include="Newtonsoft.Json">
278-
<Version>13.0.1</Version>
278+
<Version>13.0.2</Version>
279279
</PackageReference>
280280
<PackageReference Include="System.Diagnostics.DiagnosticSource">
281281
<Version>4.3.0</Version>
@@ -288,12 +288,7 @@
288288
</PackageReference>
289289
</ItemGroup>
290290
<ItemGroup>
291-
<Content Include="baidu.ini">
292-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
293-
</Content>
294-
<Content Include="dict.json">
295-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
296-
</Content>
291+
<Content Include="dict.json" />
297292
</ItemGroup>
298293
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
299294
</Project>

0 commit comments

Comments
 (0)