0

I have the following method that changes the language of the winform.

    private void LoadLanguage(string lang)
    {
        foreach (Control c in this.Controls)
        {
            ComponentResourceManager resources = new ComponentResourceManager(typeof(MainForm));
            resources.ApplyResources(c, c.Name, new CultureInfo(lang));
        }
    }

I call this method on the Form_Load method. Inside the form i have a tab control but the tabPage text property does not change. On the other hand the Label are changed correctly to the appropriate language. Any suggestions?

Roman
  • 19,581
  • 6
  • 68
  • 84
pikk
  • 837
  • 5
  • 21
  • 38

1 Answers1

2

Remove your method and try to do like this in Program.cs file:

//Add this line
Thread.CurrentThread.CurrentUICulture = new CultureInfo(languageString);
Application.Run(new Form());

Edit:

The main thing why your code not working is that you applying language for form controls. This means that you applying to TabControl control, but TabControl also have controls(tab pages) "inside". So you need to iterate recursively through controls to apply language for all controls and sub controls. Try this code:

private void LoadLanguage(string lang)
{
    ComponentResourceManager resources = new ComponentResourceManager(typeof(main));
    CultureInfo cultureInfo = new CultureInfo(lang);

    doRecursiveLoading(this, cultureInfo, resources);
}

private void doRecursiveLoading(Control parent, CultureInfo cultureInfo,  ComponentResourceManager resources)
{
    foreach (Control c in parent.Controls)
    {
        resources.ApplyResources(c, c.Name, cultureInfo);
        if (c.Controls.Count > 0)
            doRecursiveLoading(c, cultureInfo, resources);
     }
 }
Renatas M.
  • 11,694
  • 1
  • 43
  • 62
  • Yes but I am asking the user to choose the language on the first form. Do you know how can I do this to work in program.cs? – pikk Jan 18 '12 at 11:53
  • I was looking for changing the title of my form. You have to add this line : this.Text = resources.GetString("MainTitle", cultureInfo); You have to add this line just after the call of doRecursiveLoading in the method LoadLanguage and adding a field "MainTitle" in the resource file of each language. – monstergold Jun 11 '13 at 10:53