Implemented the DeepL translation API
This commit is contained in:
parent
a179e8dd23
commit
e659bb7191
@ -89,6 +89,7 @@
|
||||
this.buttonDeepL.TabIndex = 2;
|
||||
this.toolTip.SetToolTip(this.buttonDeepL, "Auto-generate this text by using DeepL sourced by the source culture.");
|
||||
this.buttonDeepL.UseVisualStyleBackColor = true;
|
||||
this.buttonDeepL.Click += new System.EventHandler(this.buttonDeepL_Click);
|
||||
//
|
||||
// toolTip
|
||||
//
|
||||
|
@ -10,10 +10,12 @@ public sealed partial class Translation : UserControl
|
||||
{
|
||||
private readonly string culture = "en-US";
|
||||
private readonly Timer saveTimer;
|
||||
private readonly Timer translationTimer;
|
||||
|
||||
private bool isLoading = false;
|
||||
private int currentTranslationId = -1;
|
||||
private bool isDeepLSourceCulture = false;
|
||||
private Translation? mainCultureTranslation = null;
|
||||
|
||||
public Translation()
|
||||
{
|
||||
@ -30,10 +32,17 @@ public sealed partial class Translation : UserControl
|
||||
this.saveTimer = new Timer
|
||||
{
|
||||
Enabled = false, // disable timer for now,
|
||||
Interval = 1_000, // 1 second interval
|
||||
Interval = 1_000, // 1 second interval,
|
||||
AutoReset = false, // runs only once
|
||||
};
|
||||
this.translationTimer = new Timer
|
||||
{
|
||||
Enabled = false, // disable timer for now,
|
||||
Interval = 1_600, // 1.6 second interval,
|
||||
AutoReset = false, // runs only once
|
||||
};
|
||||
this.saveTimer.Elapsed += this.SaveChanges;
|
||||
this.translationTimer.Elapsed += this.TriggerTranslateNow;
|
||||
this.Load += async (sender, args) => await this.LateSetup(cultureInfo);
|
||||
}
|
||||
|
||||
@ -47,6 +56,8 @@ public sealed partial class Translation : UserControl
|
||||
{
|
||||
this.labelHead.Text = $"Culture: {cultureInfo.Code} (DeepL source culture)";
|
||||
this.toolTip.SetToolTip(this.buttonDeepL, "Replaces all other translations by DeepL translations using this culture as source culture.\nWarning: already translated texts will be replaced as well.");
|
||||
if(this.Parent.Parent is Translations translations)
|
||||
translations.RegisterMainCultureTranslationComponent(this);
|
||||
}
|
||||
}
|
||||
|
||||
@ -86,6 +97,13 @@ public sealed partial class Translation : UserControl
|
||||
this.saveTimer.Start();
|
||||
if(this.ParentForm is UI_WinForms.Main main)
|
||||
main.AddDeferredSaveOperation(this.currentTranslationId, () => this.SaveChanges(null, null));
|
||||
|
||||
if (this.isDeepLSourceCulture && await AppSettings.GetDeepLAction() == SettingDeepLAction.AUTOMATIC_ALL)
|
||||
{
|
||||
if (this.translationTimer.Enabled)
|
||||
this.translationTimer.Stop();
|
||||
this.translationTimer.Start();
|
||||
}
|
||||
}
|
||||
|
||||
private async void SaveChanges(object? sender, ElapsedEventArgs e)
|
||||
@ -96,4 +114,37 @@ public sealed partial class Translation : UserControl
|
||||
if(this.ParentForm is UI_WinForms.Main main)
|
||||
main.RemoveDeferredSaveOperation(this.currentTranslationId);
|
||||
}
|
||||
|
||||
private async void TriggerTranslateNow(object? sender, ElapsedEventArgs e)
|
||||
{
|
||||
if (this.currentTranslationId < 0 || this.isDeepLSourceCulture == false)
|
||||
return;
|
||||
|
||||
if(this.Parent.Parent is Translations translations)
|
||||
await translations.MainCultureWasChanged();
|
||||
}
|
||||
|
||||
internal void SetMainCultureTranslation(Translation translation) => this.mainCultureTranslation = translation;
|
||||
|
||||
internal string GetCurrentText() => this.textBox.Text;
|
||||
|
||||
internal async Task Translate()
|
||||
{
|
||||
if (this.mainCultureTranslation is null)
|
||||
return;
|
||||
|
||||
var text = await Processor.DeepL.Translate(this.mainCultureTranslation.GetCurrentText(), this.culture);
|
||||
if (this.textBox.InvokeRequired)
|
||||
this.textBox.Invoke(() => this.textBox.Text = text);
|
||||
else
|
||||
this.textBox.Text = text;
|
||||
}
|
||||
|
||||
private async void buttonDeepL_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (this.isDeepLSourceCulture)
|
||||
this.TriggerTranslateNow(null, null);
|
||||
else
|
||||
await this.Translate();
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using Processor;
|
||||
using DataModel.Database;
|
||||
using Processor;
|
||||
using Timer = System.Timers.Timer;
|
||||
|
||||
namespace UI_WinForms.Components;
|
||||
@ -8,6 +9,8 @@ public partial class Translations : UserControl
|
||||
private readonly Dictionary<string, Translation> translationComponents = new();
|
||||
private readonly Timer clearTimer;
|
||||
|
||||
private Translation? mainCulture = null;
|
||||
|
||||
public Translations()
|
||||
{
|
||||
this.InitializeComponent();
|
||||
@ -87,4 +90,26 @@ public partial class Translations : UserControl
|
||||
this.panelTranslations.Controls.Add(translationComponent);
|
||||
}
|
||||
}
|
||||
|
||||
internal void RegisterMainCultureTranslationComponent(Translation trans)
|
||||
{
|
||||
this.mainCulture = trans;
|
||||
|
||||
// Register the main culture translation component into all the other
|
||||
// translation components:
|
||||
foreach (var (_, translation) in this.translationComponents.Where(n => n.Value != trans))
|
||||
translation.SetMainCultureTranslation(trans);
|
||||
}
|
||||
|
||||
internal async Task MainCultureWasChanged()
|
||||
{
|
||||
if(this.mainCulture is null || await AppSettings.GetDeepLMode() == SettingDeepLMode.DISABLED || await AppSettings.GetDeepLAction() == SettingDeepLAction.MANUAL)
|
||||
return;
|
||||
|
||||
var allTasks = new List<Task>();
|
||||
foreach (var (_, translation) in this.translationComponents.Where(n => n.Value != this.mainCulture))
|
||||
allTasks.Add(translation.Translate());
|
||||
|
||||
await Task.WhenAll(allTasks);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user