From e659bb7191783d978010793a3f2fa319a41ccb2e Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Wed, 21 Sep 2022 19:57:59 +0200 Subject: [PATCH] Implemented the DeepL translation API --- .../Components/Translation.Designer.cs | 1 + .../UI WinForms/Components/Translation.cs | 53 ++++++++++++++++++- .../UI WinForms/Components/Translations.cs | 27 +++++++++- 3 files changed, 79 insertions(+), 2 deletions(-) diff --git a/I18N Commander/UI WinForms/Components/Translation.Designer.cs b/I18N Commander/UI WinForms/Components/Translation.Designer.cs index dc5cb15..1a260fe 100644 --- a/I18N Commander/UI WinForms/Components/Translation.Designer.cs +++ b/I18N Commander/UI WinForms/Components/Translation.Designer.cs @@ -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 // diff --git a/I18N Commander/UI WinForms/Components/Translation.cs b/I18N Commander/UI WinForms/Components/Translation.cs index 48d01cd..e438bae 100644 --- a/I18N Commander/UI WinForms/Components/Translation.cs +++ b/I18N Commander/UI WinForms/Components/Translation.cs @@ -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(); + } } \ No newline at end of file diff --git a/I18N Commander/UI WinForms/Components/Translations.cs b/I18N Commander/UI WinForms/Components/Translations.cs index 21538e5..178ddd8 100644 --- a/I18N Commander/UI WinForms/Components/Translations.cs +++ b/I18N Commander/UI WinForms/Components/Translations.cs @@ -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 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(); + foreach (var (_, translation) in this.translationComponents.Where(n => n.Value != this.mainCulture)) + allTasks.Add(translation.Translate()); + + await Task.WhenAll(allTasks); + } } \ No newline at end of file