2
votes

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?

2
In comboBox1_SelectedIndexChanged put Console.Writeline(comboBox1.SelectedItem.ToString()); so you can see where is the errorTinwor
man this is gonna solve nothing ...franzp
I know but I want know if the comboBox1_SelectedIndexChanged is called in the formLoadTinwor
yes it is SettingsForm but what ever, its not in main formfranzp

2 Answers

10
votes
 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.

2
votes
private void ChangeLanguage(CultureInfo culture)
{
      Application.CurrentCulture = culture;
      CultureInfo.DefaultThreadCurrentCulture = culture;
      CultureInfo.DefaultThreadCurrentUICulture = culture;

      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture.Name);
      Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(culture.Name);
}

You'll need to have appropriate resources for all languages though.