From c07571c4771faa0d3e8615c376cbc2f107505b96 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Fri, 5 Aug 2022 21:34:20 +0200 Subject: [PATCH] Added DeepL source culture --- .../UI WinForms/Components/Setting.cs | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/I18N Commander/UI WinForms/Components/Setting.cs b/I18N Commander/UI WinForms/Components/Setting.cs index 0d08121..639487b 100644 --- a/I18N Commander/UI WinForms/Components/Setting.cs +++ b/I18N Commander/UI WinForms/Components/Setting.cs @@ -150,6 +150,59 @@ public sealed partial class Setting : UserControl return new Setting(settingData); } + internal readonly record struct ComboBoxItem(string DisplayText, int CultureIndex) + { + public override string ToString() => this.DisplayText; + } + + private static async Task ShowDeepLSourceCultureSettingAsync() + { + var currentSourceCultureIndex = await AppSettings.GetDeepLSourceCultureIndex(); + + // We load the corresponding culture for that index. As dropdown items, we show + // all other available cultures: + var allCultures = await AppSettings.GetCultureInfos(); + var sourceCulture = allCultures.FirstOrDefault(n => n.Index == currentSourceCultureIndex); + + // Attention: We have to store the culture's index, because the index is not + // continuous and can change when the user adds or removes a culture! + var settingData = new SettingUIData( + Icon: Icons.icons8_chat_bubble_512, + SettingName: () => "DeepL Source Culture", + SettingExplanation: () => "The source culture is used to translate the missing translations.", + SetupDataControl: () => + { + var dropdown = new ComboBox(); + var currentCultureDropdownIndex = 0; + for (var n = 0; n < allCultures.Count; n++) + { + var cultureInfo = allCultures[n]; + if(cultureInfo.Index == currentSourceCultureIndex) + currentCultureDropdownIndex = n; + + dropdown.Items.Add(new ComboBoxItem($"{cultureInfo.Index}.: {cultureInfo.Code}", cultureInfo.Index)); + } + + dropdown.SelectedIndex = currentCultureDropdownIndex; + + // Setup the change event handler: + dropdown.SelectedValueChanged += async (sender, args) => + { + if(dropdown.SelectedItem is ComboBoxItem selectedItem) + await AppSettings.SetDeepLSourceCultureIndex(selectedItem.CultureIndex); + }; + + // Apply the desired layout: + dropdown.Dock = DockStyle.Fill; + dropdown.DropDownStyle = ComboBoxStyle.DropDownList; + + return dropdown; + } + ); + + return new Setting(settingData); + } + private static IEnumerable> ShowCultureSettingsAsync() { var isFirstCulture = true; // We need this flag to distinguish the first task from the others. @@ -206,6 +259,7 @@ public sealed partial class Setting : UserControl public static IEnumerable> GetAllSettings() { + yield return ShowDeepLSourceCultureSettingAsync(); foreach (var setting in ShowCultureSettingsAsync()) { yield return setting;