[SOLVED] Change language (culture) during runtime (C#)

Status
Not open for further replies.

alexxx

Advanced Member level 4
Joined
Apr 17, 2011
Messages
1,013
Helped
273
Reputation
552
Reaction score
270
Trophy points
1,383
Location
Greece
Activity points
7,936
Hi!

I would like to create a multilanguage project in C#. I created a test project for that, with a simple form that contains just one button. I want the button to have an english text on it when english language is selected, and a greek text when greek language is selected. After I changed the "Localizable" property to true, I selected the two languages and two resource files were created, one for each language.

What I accomplished so far, is to change the language before initialization:

Code:
public Form1()
{
  Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
  //Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("el-GR"); 

  InitializeComponent();
}

What I want to do is change the language during runtime, when button is pressed:

Code:
private void button1_Click(object sender, EventArgs e)
{
  //Code for language change should be placed here
}

Any suggestions?
Thanks!
 

Thank you bassa!

Too many messy and complicated solutions I found during my search, but yours was exactly what I needed, simple and short.

I still have a problem though. With this solution all controls changed when resources are applied. But what about MenuStrip, ToolStrip and ContextMenuStrip? I added all those three on the form, but they don't change with the new resources implementation. In resources window I see all the resources for these items, but they do not change. So what I need is (following bassa's reference):

Code:
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));
  }

  //I need some code in here to apply resources to menuStrip1, toolStrip1 and contextMenuStrip1 as well
}

Thanks.

---------- Post added at 10:56 ---------- Previous post was at 09:47 ----------

I added the bold part in the ChangeLanguage() routine:

Code:
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));
  }


[B]  
ComponentResourceManager resourcesss = new ComponentResourceManager(typeof(Form1));
CultureInfo cult = new CultureInfo(lang);

  foreach (ToolStripItem m in menuStrip1.Items) //menu.Items)
  {
    string text = resourcesss.GetString(m.Name + ".Text", cult);
    if (text != null)
    {
      m.Text = text;
    }
  }

  foreach (ToolStripItem co in contextMenuStrip1.Items) //menu.Items)
  {
    string text = resourcesss.GetString(co.Name + ".Text", cult);
    if (text != null)
    {
      co.Text = text;
    }
  }

  foreach (ToolStripItem t in toolStrip1.Items) //menu.Items)
  {
    string text = resourcesss.GetString(t.Name + ".Text", cult);
    if (text != null)
    {
        t.Text = text;
    }
  }[/B]
}


c# - How to change language at runtime without layout troubles - Stack Overflow

Now toolStrip1, menuStrip1 and contextMenuStrip1 are changing but not their submenus as well. I guess this is the last part I will have to work on. But if someone knows the way, it would be great to share it.
 
Last edited:

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…