I have created Windows Form Program in C#. I have some problems with localization. I have resource files in 3 languages. I want to click each language button and change lang
I wrote a RuntimeLocalizer
class with following features:
Control
s and SubControl
s in a FormSubItem
s of all MenuStrip
sUsage Example: RuntimeLocalizer.ChangeCulture(MainForm, "en-US");
using System.Windows.Forms;
using System.Globalization;
using System.Threading;
using System.ComponentModel;
public static class RuntimeLocalizer
{
public static void ChangeCulture(Form frm, string cultureCode)
{
CultureInfo culture = CultureInfo.GetCultureInfo(cultureCode);
Thread.CurrentThread.CurrentUICulture = culture;
ComponentResourceManager resources = new ComponentResourceManager(frm.GetType());
ApplyResourceToControl(resources, frm, culture);
resources.ApplyResources(frm, "$this", culture);
}
private static void ApplyResourceToControl(ComponentResourceManager res, Control control, CultureInfo lang)
{
if (control.GetType() == typeof(MenuStrip)) // See if this is a menuStrip
{
MenuStrip strip = (MenuStrip)control;
ApplyResourceToToolStripItemCollection(strip.Items, res, lang);
}
foreach (Control c in control.Controls) // Apply to all sub-controls
{
ApplyResourceToControl(res, c, lang);
res.ApplyResources(c, c.Name, lang);
}
// Apply to self
res.ApplyResources(control, control.Name, lang);
}
private static void ApplyResourceToToolStripItemCollection(ToolStripItemCollection col, ComponentResourceManager res, CultureInfo lang)
{
for (int i = 0; i < col.Count; i++) // Apply to all sub items
{
ToolStripItem item = (ToolStripMenuItem)col[i];
if (item.GetType() == typeof(ToolStripMenuItem))
{
ToolStripMenuItem menuitem = (ToolStripMenuItem)item;
ApplyResourceToToolStripItemCollection(menuitem.DropDownItems, res, lang);
}
res.ApplyResources(item, item.Name, lang);
}
}
}
You will need to reload the controls for it to reflect the New culture values
ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
and then you would have to apply for each control using resources.ApplyResources
Please have a look here
Changing the CurrentUICulture will not automatically reload the resources. You need to perform it manually (http://msdn.microsoft.com/en-us/magazine/cc163609.aspx#S8)
You can copy the code related to the localization from InitializeComponent() into another function:
void LoadResources(){
this.Title = MyApp.Resources.MainFormCaption;
this.lblWelcomeMessage.Text = MyApp.Resources.UserWelcome;
}
Thanks V4Vendetta and others.. Solution is...
private void RussianFlag_Click(object sender, EventArgs e)
{
if (currentLanguage != "RUS")
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("ru-RU");
ChangeLanguage("ru-RU");
}
}
.... .... ...
private void ChangeLanguage(string lang)
{
foreach (Control c in this.Controls)
{
ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
resources.ApplyResources(c, c.Name, new CultureInfo(lang));
if (c.ToString().StartsWith("System.Windows.Forms.GroupBox"))
{
foreach (Control child in c.Controls)
{
ComponentResourceManager resources_child = new ComponentResourceManager(typeof(Form1));
resources_child.ApplyResources(child, child.Name, new CultureInfo(lang));
}
}
}
}