1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| internal CultureInfo EnglishCulture = new CultureInfo("en-US");
internal CultureInfo GermanCulture = new CultureInfo("de-DE");
internal CultureInfo FrenchCulture = new CultureInfo("fr-FR");
public void ApplyCulture(CultureInfo culture)
{
// Applies culture to current Thread.
Thread.CurrentThread.CurrentUICulture = culture;
// Create a resource manager for this Form and determine its fields via reflection.
ComponentResourceManager resources = new ComponentResourceManager(this.GetType());
FieldInfo[] fieldInfos = this.GetType().GetFields(BindingFlags.Instance |
BindingFlags.DeclaredOnly | BindingFlags.NonPublic);
// Call SuspendLayout for Form and all fields derived from Control, so assignment of
// localized text doesn't change layout immediately.
this.SuspendLayout();
for (int index = 0; index < fieldInfos.Length; index++)
{
if (fieldInfos[index].FieldType.IsSubclassOf(typeof(Control)))
{
fieldInfos[index].FieldType.InvokeMember("SuspendLayout",
BindingFlags.InvokeMethod, null,
fieldInfos[index].GetValue(this), null);
}
}
// If available, assign localized text to Form and fields with Text property.
String text = resources.GetString("$this.Text");
if (text != null)
this.Text = text;
for (int index = 0; index < fieldInfos.Length; index++)
{
if (fieldInfos[index].FieldType.GetProperty("Text", typeof(String)) != null)
{
text = resources.GetString(fieldInfos[index].Name + ".Text");
if (text != null)
{
fieldInfos[index].FieldType.InvokeMember("Text",
BindingFlags.SetProperty, null,
fieldInfos[index].GetValue(this), new object[] { text });
}
}
}
// Call ResumeLayout for Form and all fields derived from Control to resume layout logic.
// Call PerformLayout, so layout changes due to assignment of localized text are performed.
for (int index = 0; index < fieldInfos.Length; index++)
{
if (fieldInfos[index].FieldType.IsSubclassOf(typeof(Control)))
{
fieldInfos[index].FieldType.InvokeMember("ResumeLayout",
BindingFlags.InvokeMethod, null,
fieldInfos[index].GetValue(this), new object[] { false });
}
}
this.ResumeLayout(false);
this.PerformLayout();
} |
Partager