Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
576 views
in Technique[技术] by (71.8m points)

.net - Change language at runtime in C# winform

I want to change Language but when I compile this an exception pop up. it says

"Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "System.Type.resources" was correctly embedded or linked into assembly "mscorlib" at compile time, or that all the satellite assemblies required are loadable and fully signed."

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBox1.SelectedItem.ToString() == "English")
        {
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("En");
            ChangeLanguage("En");
        }
        else if (comboBox1.SelectedItem.ToString() == "German")
        {
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("De");
            ChangeLanguage("De");
        }
    }


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

Any suggestions?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
 ComponentResourceManager resources = new ComponentResourceManager(typeof(Type));

The argument to the constructor is wrong, you are telling it to find the resources for System.Type. Which is why it is complaining that it can't find "System.Type.resources". It will never find those.

You need to pass the type of the form that you actually want to localize. Use this.GetType() instead. Albeit that this probably will just localize your Options form and not the rest of the windows in your app. You could iterate Application.OpenForms() instead. It is also necessary to apply the localization to all the controls. Not just the ones on the form, also the ones that are located inside containers like panels. Thus:

    private static void ChangeLanguage(string lang) {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
        foreach (Form frm in Application.OpenForms) {
            localizeForm(frm);
        }
    }

    private static void localizeForm(Form frm) {
        var manager = new ComponentResourceManager(frm.GetType());
        manager.ApplyResources(frm, "$this");
        applyResources(manager, frm.Controls);
    }

    private static void applyResources(ComponentResourceManager manager, Control.ControlCollection ctls) {
        foreach (Control ctl in ctls) {
            manager.ApplyResources(ctl, ctl.Name);
            applyResources(manager, ctl.Controls);
        }
    }

Be careful with wiz-bang features like this. Nobody actually changes their native language while they are using your program.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...