Introduction
The Compact Framework does not support changing Thread.CurrentUICulture
to change a form's language on the fly.
Background
This code defines a custom version of System.Globalization.ComponentResourceManager
in the global namespace that inherits from the standard version and overrides ApplyResource()
to use a custom culture. Code generated in InitializeComponent()
will compile using this version, and the Forms Editor will not break because you do not change the generated code. Wrap the function in "pragma warning disable 436" to suppress the warning about using an ambiguous class reference.
Use the References node in the Properties sheet to change the alias for System from "global" to "global,ms".
extern alias ms;
using cm = ms::System.ComponentModel;
using System.Globalization;
using Wm2005Global;
namespace System.ComponentModel
{
class ComponentResourceManager : cm.ComponentResourceManager
{
public ComponentResourceManager(Type type) : base(type) { }
public override void ApplyResources(object value,
string objectName, CultureInfo culture)
{
if (culture == null)
{
culture = Program.Culture;
}
base.ApplyResources(value, objectName, culture);
}
}
}
This shows how Visual Studio generates the normal code and uses the special version of ComponentResourceManager
by default. The #pragma
disables the warnings about using the ambiguous class:
#region Windows Form Designer generated code
#pragma warning disable 436
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources =
new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
resources.ApplyResources(this.button1, "button1");
this.button1.Name = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
resources.ApplyResources(this.button2, "button2");
this.button2.Name = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
resources.ApplyResources(this, "$this");
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#pragma warning restore 436
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
I think this is the cleanest possible solution to Globalization on Windows Mobile 2005. You do not have to change the default language for the entire device, and you can even change the language on a form from a menu item without closing it! This also demonstrates a technique for disposing all the controls on a form, calling InitializeComponent()
and Form_Load()
again, so it's like your form is recreated without closing it.