From 40b0c9490537e1ab0ebbc31ee0b68caf33b14e63 Mon Sep 17 00:00:00 2001 From: Korney Czukowski Date: Tue, 19 Dec 2017 18:14:22 +0100 Subject: [PATCH] Make control localizable --- .../DS4Forms/LanguagePackComboBox.Designer.cs | 17 +---- DS4Windows/DS4Forms/LanguagePackComboBox.cs | 31 ++++++-- DS4Windows/DS4Forms/LanguagePackComboBox.resx | 75 +++++++++++++++++++ 3 files changed, 105 insertions(+), 18 deletions(-) diff --git a/DS4Windows/DS4Forms/LanguagePackComboBox.Designer.cs b/DS4Windows/DS4Forms/LanguagePackComboBox.Designer.cs index de400a5..1ed1ff2 100644 --- a/DS4Windows/DS4Forms/LanguagePackComboBox.Designer.cs +++ b/DS4Windows/DS4Forms/LanguagePackComboBox.Designer.cs @@ -28,42 +28,33 @@ /// private void InitializeComponent() { + System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(LanguagePackComboBox)); this.cbCulture = new System.Windows.Forms.ComboBox(); this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // cbCulture // - this.cbCulture.Anchor = System.Windows.Forms.AnchorStyles.Right; + resources.ApplyResources(this.cbCulture, "cbCulture"); this.cbCulture.DisplayMember = "Value"; this.cbCulture.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cbCulture.FormattingEnabled = true; - this.cbCulture.Location = new System.Drawing.Point(112, 3); this.cbCulture.Name = "cbCulture"; - this.cbCulture.Size = new System.Drawing.Size(145, 21); - this.cbCulture.TabIndex = 61; this.cbCulture.ValueMember = "Key"; // // label1 // - this.label1.Anchor = System.Windows.Forms.AnchorStyles.Left; - this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(3, 6); + resources.ApplyResources(this.label1, "label1"); this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(100, 13); - this.label1.TabIndex = 62; - this.label1.Text = "Use language pack"; - this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; this.label1.SizeChanged += new System.EventHandler(this.LanguagePackComboBox_SizeChanged); // // LanguagePackComboBox // - this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + resources.ApplyResources(this, "$this"); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.cbCulture); this.Controls.Add(this.label1); this.Name = "LanguagePackComboBox"; - this.Size = new System.Drawing.Size(260, 27); this.SizeChanged += new System.EventHandler(this.LanguagePackComboBox_SizeChanged); this.Resize += new System.EventHandler(this.LanguagePackComboBox_SizeChanged); this.ResumeLayout(false); diff --git a/DS4Windows/DS4Forms/LanguagePackComboBox.cs b/DS4Windows/DS4Forms/LanguagePackComboBox.cs index 2b4e6fb..c85898c 100644 --- a/DS4Windows/DS4Forms/LanguagePackComboBox.cs +++ b/DS4Windows/DS4Forms/LanguagePackComboBox.cs @@ -13,8 +13,10 @@ namespace DS4Windows.DS4Forms { public partial class LanguagePackComboBox : UserControl { + private string _invariantCultureText = "No (English UI)"; private string _probingPath = ""; private string _languageAssemblyName = "DS4Windows.resources.dll"; + private TaskCompletionSource _languageListInitialized = new TaskCompletionSource(); [Category("Action")] [Description("Fires when the combo box selected index is changed.")] @@ -27,7 +29,15 @@ namespace DS4Windows.DS4Forms [Category("Data")] [Description("Text used for invariant culture name in the combo box.")] [Localizable(true)] - public string InvariantCultureText { get; set; } + public string InvariantCultureText + { + get { return _invariantCultureText; } + [System.Diagnostics.CodeAnalysis.SuppressMessage("InvariantCultureText_Changed call will complete when ready, no need for a warning", "CS4014:Await.Warning")] + set { + _invariantCultureText = value; + InvariantCultureText_Changed(value); + } + } [Category("Data")] [Description("Text for label before the combo box.")] @@ -89,6 +99,7 @@ namespace DS4Windows.DS4Forms cbCulture.SelectedValueChanged += new EventHandler(CbCulture_SelectedValueChanged); cbCulture.Enabled = true; + _languageListInitialized.SetResult(true); }); } @@ -102,10 +113,12 @@ namespace DS4Windows.DS4Forms .ToList(); lookupPaths.Insert(0, exeLocation); - // Get all culture for which satellite folder found with culture code. - Dictionary cultures = CultureInfo.GetCultures(CultureTypes.AllCultures) - .Where(c => c.Equals(CultureInfo.InvariantCulture) || this.IsLanguageAssemblyAvailable(lookupPaths, c)) - .ToDictionary(c => c.Name, c => c.Equals(CultureInfo.InvariantCulture) ? this.InvariantCultureText : c.NativeName); + // Get all culture for which satellite folder found with culture code, then insert invariant culture at the beginning. + List> cultures = CultureInfo.GetCultures(CultureTypes.AllCultures) + .Where(c => this.IsLanguageAssemblyAvailable(lookupPaths, c)) + .Select(c => new KeyValuePair(c.Name, c.NativeName)) + .ToList(); + cultures.Insert(0, new KeyValuePair("", this.InvariantCultureText)); return new BindingSource(cultures, null); } @@ -117,6 +130,14 @@ namespace DS4Windows.DS4Forms .Count() > 0; } + private async Task InvariantCultureText_Changed(string value) + { + // Normally the completion flag will be long set by the time this method is called. + await _languageListInitialized.Task; + BindingSource dataSource = ((BindingSource)cbCulture.DataSource); + dataSource[0] = new KeyValuePair("", value); + } + private void LanguagePackComboBox_SizeChanged(object sender, EventArgs e) { cbCulture.Left = label1.Margin.Left + label1.Width + label1.Margin.Right; diff --git a/DS4Windows/DS4Forms/LanguagePackComboBox.resx b/DS4Windows/DS4Forms/LanguagePackComboBox.resx index 1af7de1..175e22a 100644 --- a/DS4Windows/DS4Forms/LanguagePackComboBox.resx +++ b/DS4Windows/DS4Forms/LanguagePackComboBox.resx @@ -117,4 +117,79 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Right + + + + 112, 3 + + + 145, 21 + + + + 61 + + + cbCulture + + + System.Windows.Forms.ComboBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + $this + + + 0 + + + Left + + + True + + + 3, 6 + + + 100, 13 + + + 62 + + + Use language pack + + + MiddleLeft + + + label1 + + + System.Windows.Forms.Label, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + $this + + + 1 + + + True + + + 6, 13 + + + 260, 27 + + + LanguagePackComboBox + + + System.Windows.Forms.UserControl, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + \ No newline at end of file