简体   繁体   中英

C# how to change multi language in all windows form?

C# how to change multi language in all windows form?

I use this method to change other language, but it only change current windows form.

I want to be choosing one language for all windows form. Thanks for answering.

Here is my code:

        private void ChineseTToolStripMenuItem_Click(object sender, EventArgs e)
    {
        ChangeLanguage("zh-tw"); ;
    }

    private void englishToolStripMenuItem_Click(object sender, EventArgs e)
    {
        ChangeLanguage("en");
    }

    private void ChineseSToolStripMenuItem_Click(object sender, EventArgs e)
    {
        ChangeLanguage("zh-cn");
    }

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

First you should make the ChangeLanguage method to allow calling it on any control or form. Here is a version which is recursive and which takes a Control as a parameter.

Note: Forms are Controls , too.

So you can call this method to change all forms you have a handle of..

public ComponentResourceManager resources;

private void ChangeLanguage(Control ctl, string lang)
{
    resources.ApplyResources(ctl, ctl.Name, new CultureInfo(lang));
    foreach (Control c in ctl.Controls) ChangeLanguage(c, lang);
}

Next you need to keep a list of your open forms in some way. A List<Form> is a good way.

Instead of calling the ChangeLanguage function on only the current window you call a ChangeLanguageOnAllforms function. So if you have maybe a List<Form> called myFormsList you can do:

void ChangeLanguageOnAllforms(  string lang )
{
  foreach (Form f in myFormsList) 
  {
     if (f != null)
     { 
        resources = new ComponentResourceManager(typeof(f));
        ChangeLanguage(f, languageString);
     }
  }
}

You should also keep a public variable to hold the current language!

public string language = "en"; 

And you should also upon opening any new Form say form7

  • add it to the list and
  • call the ChangeLanguage( form7, language )

You should think about keeping the list of forms uptodate by removing closed forms from it. If you make it a public property the closing form can remove itself from it, if it has a reference to your main form..

If possible, you should set the language of your application in your start-up code.

CultureInfo.CurrentUICulture = new CultureInfo("zh-cn");

In .Net 4.5, there is also a new property called DefaultThreadCurrentUICulture . You can use it to set the default culture for all new threads.

If neither is applicable to your usage scenario, create a BaseLangForm class, which derives from Form , and call your ChangeLanguage method from the OnLoad event. All your forms must derive from BaseLangForm from the point forward.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM