Make control localizable

This commit is contained in:
Korney Czukowski 2017-12-19 18:14:22 +01:00
parent acfb9b2629
commit 40b0c94905
3 changed files with 105 additions and 18 deletions

View File

@ -28,42 +28,33 @@
/// </summary>
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);

View File

@ -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<bool> _languageListInitialized = new TaskCompletionSource<bool>();
[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<string, string> 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<KeyValuePair<string, string>> cultures = CultureInfo.GetCultures(CultureTypes.AllCultures)
.Where(c => this.IsLanguageAssemblyAvailable(lookupPaths, c))
.Select(c => new KeyValuePair<string, string>(c.Name, c.NativeName))
.ToList();
cultures.Insert(0, new KeyValuePair<string, string>("", 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<string, string>("", value);
}
private void LanguagePackComboBox_SizeChanged(object sender, EventArgs e)
{
cbCulture.Left = label1.Margin.Left + label1.Width + label1.Margin.Right;

View File

@ -117,4 +117,79 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="cbCulture.Anchor" type="System.Windows.Forms.AnchorStyles, System.Windows.Forms">
<value>Right</value>
</data>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="cbCulture.Location" type="System.Drawing.Point, System.Drawing">
<value>112, 3</value>
</data>
<data name="cbCulture.Size" type="System.Drawing.Size, System.Drawing">
<value>145, 21</value>
</data>
<assembly alias="mscorlib" name="mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="cbCulture.TabIndex" type="System.Int32, mscorlib">
<value>61</value>
</data>
<data name="&gt;&gt;cbCulture.Name" xml:space="preserve">
<value>cbCulture</value>
</data>
<data name="&gt;&gt;cbCulture.Type" xml:space="preserve">
<value>System.Windows.Forms.ComboBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;cbCulture.Parent" xml:space="preserve">
<value>$this</value>
</data>
<data name="&gt;&gt;cbCulture.ZOrder" xml:space="preserve">
<value>0</value>
</data>
<data name="label1.Anchor" type="System.Windows.Forms.AnchorStyles, System.Windows.Forms">
<value>Left</value>
</data>
<data name="label1.AutoSize" type="System.Boolean, mscorlib">
<value>True</value>
</data>
<data name="label1.Location" type="System.Drawing.Point, System.Drawing">
<value>3, 6</value>
</data>
<data name="label1.Size" type="System.Drawing.Size, System.Drawing">
<value>100, 13</value>
</data>
<data name="label1.TabIndex" type="System.Int32, mscorlib">
<value>62</value>
</data>
<data name="label1.Text" xml:space="preserve">
<value>Use language pack</value>
</data>
<data name="label1.TextAlign" type="System.Drawing.ContentAlignment, System.Drawing">
<value>MiddleLeft</value>
</data>
<data name="&gt;&gt;label1.Name" xml:space="preserve">
<value>label1</value>
</data>
<data name="&gt;&gt;label1.Type" xml:space="preserve">
<value>System.Windows.Forms.Label, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;label1.Parent" xml:space="preserve">
<value>$this</value>
</data>
<data name="&gt;&gt;label1.ZOrder" xml:space="preserve">
<value>1</value>
</data>
<metadata name="$this.Localizable" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<data name="$this.AutoScaleDimensions" type="System.Drawing.SizeF, System.Drawing">
<value>6, 13</value>
</data>
<data name="$this.Size" type="System.Drawing.Size, System.Drawing">
<value>260, 27</value>
</data>
<data name="&gt;&gt;$this.Name" xml:space="preserve">
<value>LanguagePackComboBox</value>
</data>
<data name="&gt;&gt;$this.Type" xml:space="preserve">
<value>System.Windows.Forms.UserControl, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
</root>