Skip to content
This repository has been archived by the owner on Aug 17, 2024. It is now read-only.

Commit

Permalink
调整布局,新增留存翻译记录功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Isayama-Kagura committed Feb 22, 2023
1 parent 1263241 commit 48b7f58
Show file tree
Hide file tree
Showing 20 changed files with 266 additions and 179 deletions.
6 changes: 3 additions & 3 deletions TsubakiTranslator/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@


<!--Secondary-->
<SolidColorBrush x:Key="SecondaryHueLightBrush" Color="#fff64f"/>
<SolidColorBrush x:Key="SecondaryHueLightBrush" Color="#FFF6F028"/>
<SolidColorBrush x:Key="SecondaryHueLightForegroundBrush" Color="#FFFFFF"/>
<SolidColorBrush x:Key="SecondaryHueMidBrush" Color="#ffd740"/>
<SolidColorBrush x:Key="SecondaryHueMidBrush" Color="#FFE4BB24"/>
<SolidColorBrush x:Key="SecondaryHueMidForegroundBrush" Color="#FFFFFF"/>
<SolidColorBrush x:Key="SecondaryHueDarkBrush" Color="#c8a600"/>
<SolidColorBrush x:Key="SecondaryHueDarkBrush" Color="#FFCB8834"/>
<SolidColorBrush x:Key="SecondaryHueDarkForegroundBrush" Color="#FFFFFF"/>

</ResourceDictionary>
Expand Down
12 changes: 6 additions & 6 deletions TsubakiTranslator/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ public partial class App : Application
private static WindowConfig windowConfig;
private static GamesConfig gamesConfig;
private static TranslateAPIConfig translateAPIConfig;
private static OcrConfig ocrConfig;
private static OtherConfig otherConfig;

public static WindowConfig WindowConfig { get => windowConfig; }
public static GamesConfig GamesConfig { get => gamesConfig; }
public static TranslateAPIConfig TranslateAPIConfig { get => translateAPIConfig; }

public static OcrConfig OcrConfig { get => ocrConfig; }
public static OtherConfig OtherConfig { get => otherConfig; }

protected override void OnStartup(StartupEventArgs e)
{
Expand All @@ -38,9 +38,9 @@ protected override void OnStartup(StartupEventArgs e)
if (translateAPIConfig == null)
translateAPIConfig = new TranslateAPIConfig();

ocrConfig = FileHandler.DeserializeObject<OcrConfig>(System.AppDomain.CurrentDomain.BaseDirectory + @"config/OcrConfig.json");
if (ocrConfig == null)
ocrConfig = new OcrConfig();
otherConfig = FileHandler.DeserializeObject<OtherConfig>(System.AppDomain.CurrentDomain.BaseDirectory + @"config/OtherConfig.json");
if (otherConfig == null)
otherConfig = new OtherConfig();

foreach (var proc in Process.GetProcessesByName("TsubakiTranslator").Where(proc => proc.Id != Process.GetCurrentProcess().Id))
{
Expand All @@ -60,7 +60,7 @@ protected override void OnExit(ExitEventArgs e)
FileHandler.SerializeObject<WindowConfig>(WindowConfig, System.AppDomain.CurrentDomain.BaseDirectory + @"config/WindowConfig.json");
FileHandler.SerializeObject<GamesConfig>(GamesConfig, System.AppDomain.CurrentDomain.BaseDirectory + @"config/GamesData.json");
FileHandler.SerializeObject<TranslateAPIConfig>(TranslateAPIConfig, System.AppDomain.CurrentDomain.BaseDirectory + @"config/APIConfig.json");
FileHandler.SerializeObject<OcrConfig>(OcrConfig, System.AppDomain.CurrentDomain.BaseDirectory + @"config/OcrConfig.json");
FileHandler.SerializeObject<OtherConfig>(OtherConfig, System.AppDomain.CurrentDomain.BaseDirectory + @"config/OtherConfig.json");
}

}
Expand Down
45 changes: 25 additions & 20 deletions TsubakiTranslator/BasicLibrary/FileHandler.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
using System;
using System.IO;
using System.Text.Json;
using System.Windows;
using System.Windows.Forms;

namespace TsubakiTranslator.BasicLibrary
{
class FileHandler
{
//public static string SelectPath()
//{
// string path = string.Empty;
// var openFileDialog = new OpenFileDialog()
// {
// Filter = "可执行文件|*.exe|所有文件|*.*"//如果需要筛选txt文件("Files (*.txt)|*.txt")
// };
// var result = openFileDialog.ShowDialog();
// if (result == true)
// {
// path = openFileDialog.FileName;
// }
// return path;
//}
public static string SelectFolderPath()
{
string path = "";
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "请选择文件夹作为路径";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
path = dialog.SelectedPath; // "e:/go"
}

return path;

}

public static void SerializeObject<T>(T value, string path)
{
Expand All @@ -34,14 +33,14 @@ public static void SerializeObject<T>(T value, string path)
}
catch (Exception e)
{
MessageBox.Show(e.Message);
System.Windows.MessageBox.Show(e.Message);
}
}

public static T DeserializeObject<T>(string path)
{
T result = default(T);
if (CreateFileIfNotExist(path))
if (CreateFile(path))
return result;

try
Expand All @@ -57,13 +56,13 @@ public static T DeserializeObject<T>(string path)
}
catch (Exception e)
{
MessageBox.Show(e.Message);
System.Windows.MessageBox.Show(e.Message);
}

return result;
}

public static bool CreateFileIfNotExist(string path)
public static bool CreateFile(string path)
{
string directory = System.IO.Path.GetDirectoryName(path);
bool flag = false;
Expand All @@ -83,7 +82,13 @@ public static bool CreateFileIfNotExist(string path)

}


public static void AppendTextToFile(string text, string path)
{
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine(text);
}
}

}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
using CommunityToolkit.Mvvm.ComponentModel;
using System;

namespace TsubakiTranslator.BasicLibrary
{
public partial class OcrConfig : ObservableObject
public partial class OtherConfig : ObservableObject
{
[ObservableProperty]
private bool isAutoScreenshot = false;
[ObservableProperty]
private ScreenshotHotkey screenshotHotkey = new ScreenshotHotkey();
[ObservableProperty]
private int interval = 2;
private int interval = 3;
[ObservableProperty]
private string sourceLanguage = "Japanese";
[ObservableProperty]
private bool saveLogEnabled = false;
[ObservableProperty]
private string logFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

}
public partial class ScreenshotHotkey : ObservableObject
Expand Down
3 changes: 0 additions & 3 deletions TsubakiTranslator/BasicLibrary/TranslateAPIConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ namespace TsubakiTranslator.BasicLibrary
{
public partial class TranslateAPIConfig : ObservableObject
{
[ObservableProperty]
private string sourceLanguage = "Japanese";

[ObservableProperty]
private bool ttsIsEnabled;
[ObservableProperty]
Expand Down
44 changes: 41 additions & 3 deletions TsubakiTranslator/BasicLibrary/TranslateDataList.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TsubakiTranslator.BasicLibrary;

namespace TsubakiTranslator
{
Expand All @@ -20,27 +23,43 @@ public TranslateData(string sourceText, Dictionary<string, string> resultText)
public class TranslateDataList
{

public int MaxLength { get; }
private int MaxLength { get; }
private LinkedList<TranslateData> list;

private LinkedListNode<TranslateData> currentData;
private LinkedListNode<TranslateData> CurrentData { get => currentData; }

private string DataLogFilePath { get; }

public TranslateDataList(int maxLength)
{
MaxLength = maxLength;
list = new LinkedList<TranslateData>();
}
public TranslateDataList(int maxLength, string logPath)
{
MaxLength = maxLength;
list = new LinkedList<TranslateData>();

DateTime dt = DateTime.Now;
DataLogFilePath = logPath + "\\translated_" + string.Format("{0:yyMMddHHmmss}", dt) + ".log";
FileHandler.CreateFile(DataLogFilePath);

}

/// <summary>
/// 输入string数组,0项是源文本,从1开始是翻译结果
/// 输入TranslateData,插入队尾
/// </summary>
/// <param name="sourceText"></param>
/// <param name="resultText"></param>
public void AddTranslateData(TranslateData translateData)
{
if (list.Count >= MaxLength)
{
if (DataLogFilePath != null)
SaveDataToFile(list.First.Value);
list.RemoveFirst();
}
list.AddLast(translateData);
currentData = list.Last;
}
Expand Down Expand Up @@ -81,5 +100,24 @@ public int Count()
{
return list.Count();
}

public void SaveAllDataToFile()
{
foreach (TranslateData data in list)
{
SaveDataToFile(data);
}
}
private void SaveDataToFile(TranslateData data)
{
var sb = new StringBuilder();
sb.AppendLine(data.SourceText);
foreach (string key in data.ResultText.Keys)
sb.Append(key).Append(": ").Append(data.ResultText[key]).AppendLine();

string result = sb.ToString();

FileHandler.AppendTextToFile(result, DataLogFilePath);
}
}
}
4 changes: 2 additions & 2 deletions TsubakiTranslator/BasicLibrary/TranslateHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace TsubakiTranslator.BasicLibrary
class TranslateHandler
{
//对翻译API进行初始化。
public static LinkedList<ITranslator> GetSelectedTranslators(TranslateAPIConfig translateAPIConfig)
public static LinkedList<ITranslator> GetSelectedTranslators(TranslateAPIConfig translateAPIConfig, string srcLang)
{
LinkedList<ITranslator> translators = new LinkedList<ITranslator>();

Expand Down Expand Up @@ -87,7 +87,7 @@ public static LinkedList<ITranslator> GetSelectedTranslators(TranslateAPIConfig
}


Dictionary<string, string> langDict = GetAPISourceLangDict(translateAPIConfig.SourceLanguage);
Dictionary<string, string> langDict = GetAPISourceLangDict(srcLang);

foreach (var t in translators)
t.SourceLanguage = langDict[t.Name];
Expand Down
14 changes: 7 additions & 7 deletions TsubakiTranslator/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

<materialDesign:ColorZone x:Name="ColorZone" Mode="PrimaryMid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Grid.Row="0" Grid.Column="1"
materialDesign:ShadowAssist.ShadowDepth="Depth2"
materialDesign:ElevationAssist.Elevation="Dp2"
MouseLeftButtonDown="ColorZone_MouseLeftButtonDown"
MouseDoubleClick="ColorZone_MouseDoubleClick">
<DockPanel>
Expand All @@ -61,9 +61,9 @@
<Image Source="/Resources/Icon/Tsubaki.png" Stretch="Uniform" Height="70" Width="70"
HorizontalAlignment="Center" Grid.Column="0" Grid.Row="0" />

<materialDesign:Card Grid.Row="1" Grid.ColumnSpan="2" materialDesign:ShadowAssist.ShadowDepth="Depth2" >
<materialDesign:Card Grid.Row="1" Grid.ColumnSpan="2" materialDesign:ElevationAssist.Elevation="Dp2" >
<TabControl materialDesign:ColorZoneAssist.Mode="Standard" Background="WhiteSmoke"
materialDesign:ShadowAssist.ShadowDepth="Depth0"
materialDesign:ElevationAssist.Elevation="Dp0"
Style="{StaticResource MaterialDesignNavigatilRailTabControl}"
>
<TabItem Width="120" IsSelected="True" >
Expand Down Expand Up @@ -104,12 +104,12 @@
<materialDesign:PackIcon Width="24"
Height="24"
HorizontalAlignment="Center"
Kind="TextRecognition" />
Kind="Cog" />
<TextBlock HorizontalAlignment="Center"
Text="OCR配置" />
Text="其他配置" />
</StackPanel>
</TabItem.Header>
<local:OcrSettingPage />
<local:OtherSettingPage />
</TabItem>

<TabItem Width="120">
Expand All @@ -119,7 +119,7 @@
<materialDesign:PackIcon Width="24"
Height="24"
HorizontalAlignment="Center"
Kind="Cog" />
Kind="MicrosoftWindows" />
<TextBlock HorizontalAlignment="Center"
Text="界面设置" />
</StackPanel>
Expand Down
54 changes: 0 additions & 54 deletions TsubakiTranslator/OcrSettingPage.xaml

This file was deleted.

Loading

0 comments on commit 48b7f58

Please sign in to comment.