From 8314e7157b50029a229f4aca6fc80301f20f6c5e Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Wed, 15 Feb 2023 20:32:47 +0100 Subject: [PATCH] Implemented copy function for text element's keys --- .../Processor/Generators/GodotCSV.cs | 18 ++--------- .../Processor/TextElementProcessor.cs | 32 +++++++++++++++++++ I18N Commander/Processor/Version.cs | 2 +- .../UI WinForms/Components/TextElements.cs | 14 ++++++-- 4 files changed, 48 insertions(+), 18 deletions(-) diff --git a/I18N Commander/Processor/Generators/GodotCSV.cs b/I18N Commander/Processor/Generators/GodotCSV.cs index 8ed4129..0b39d92 100644 --- a/I18N Commander/Processor/Generators/GodotCSV.cs +++ b/I18N Commander/Processor/Generators/GodotCSV.cs @@ -58,23 +58,11 @@ public class GodotCSV : IGenerator foreach (var section in sections.OrderBy(n => n.DataKey)) foreach (var textElement in section.TextElements.OrderBy(n => n.Code)) { - // - // Build the path to the text element: - // - var textElementPath = new List(section.Depth + 2) - { - textElement.Code - }; - - var parent = section; - while (parent is not null) - { - textElementPath.Add(parent.DataKey); - parent = parent.Parent; - } + // Get the key for this text element: + var key = await TextElementProcessor.GetKey(textElement.Id); // Write the path to the text element as key: - await writer.WriteAsync($"{string.Join(".", textElementPath.ReverseIt())}{delimiter}"); + await writer.WriteAsync($"{key}{delimiter}"); // Load all translations: var translations = textElement.Translations.DistinctBy(n => n.Culture).ToDictionary(translation => translation.Culture, translation => translation.Text); diff --git a/I18N Commander/Processor/TextElementProcessor.cs b/I18N Commander/Processor/TextElementProcessor.cs index 1924cab..cb6809b 100644 --- a/I18N Commander/Processor/TextElementProcessor.cs +++ b/I18N Commander/Processor/TextElementProcessor.cs @@ -128,4 +128,36 @@ public static class TextElementProcessor { } } + + public static async Task GetKey(int id) + { + await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); + var textElement = await db.TextElements.FirstAsync(n => n.Id == id); + + // + // Build the path to the text element: + // + var textElementPath = new List(textElement.Section.Depth + 2) + { + textElement.Code + }; + + var parent = textElement.Section; + while (parent is not null) + { + // Load the parent's parent section: + await db.Entry(parent).Reference(n => n.Parent).LoadAsync(); + + // Add the parent's key to the path: + textElementPath.Add(parent.DataKey); + + // Go to parent's parent: + parent = parent.Parent; + } + + textElementPath.Add("I18N"); + + // Write the path to the text element as key: + return $"{string.Join(".", textElementPath.ReverseIt())}"; + } } \ No newline at end of file diff --git a/I18N Commander/Processor/Version.cs b/I18N Commander/Processor/Version.cs index 5048169..d6932d2 100644 --- a/I18N Commander/Processor/Version.cs +++ b/I18N Commander/Processor/Version.cs @@ -2,5 +2,5 @@ public static class Version { - public static string Text => $"v0.9.5 (2023-02-15), .NET {Environment.Version}"; + public static string Text => $"v0.9.6 (2023-02-15), .NET {Environment.Version}"; } \ No newline at end of file diff --git a/I18N Commander/UI WinForms/Components/TextElements.cs b/I18N Commander/UI WinForms/Components/TextElements.cs index 2b01dc1..902f570 100644 --- a/I18N Commander/UI WinForms/Components/TextElements.cs +++ b/I18N Commander/UI WinForms/Components/TextElements.cs @@ -86,6 +86,13 @@ public partial class TextElements : UserControl } } }; + + // When the user press Ctrl + C, copy the text element's key to the clipboard: + this.listTextElements.KeyDown += (sender, e) => + { + if (e is {Control: true, KeyCode: Keys.C}) + this.buttonCopyKey.PerformClick(); + }; } // Loads all the text elements for the current section. @@ -236,8 +243,11 @@ public partial class TextElements : UserControl private async void textBoxFilter_KeyUp(object sender, KeyEventArgs e) => await this.LoadTextElements(); - private void buttonCopyKey_Click(object sender, EventArgs e) + private async void buttonCopyKey_Click(object sender, EventArgs e) { - + if(this.DesignMode || this.currentTextElement is null) + return; + + Clipboard.SetText(await TextElementProcessor.GetKey(this.currentTextElement.Id)); } } \ No newline at end of file