Search code examples
c#resourcesmenustrip

Exception Localizing MenuStrip Control


I have a MenuStrip control with all translations entered for several languages. When I try to change the culture dynamically all controls are changing the language as it must be except the MenuStrip. Here is the code which I used specifically for MenuStrip:

    protected ComponentResourceManager res2;

    private void signOutToolStripMenuItem_Click(object sender, EventArgs e)
    {
        LoginForm login = new LoginForm();
        if (login.ShowDialog() == DialogResult.OK)
        {
            //this is for other controls
            resManager = new ComponentResourceManager(this.GetType());
            this.ApplyResources(this, Thread.CurrentThread.CurrentCulture);

            //this is for MenuStrip
            res2 = new ComponentResourceManager(typeof(MenuStrip));
            this.ApplyResourcesToolStripMenuItem(Thread.CurrentThread.CurrentCulture);
        }

    }

    private void ApplyResourcesToolStripMenuItem(CultureInfo lang)
    {
        foreach (ToolStripItem m in this.menuStrip1.Items)
        {
            //resources.ApplyResources(m, m.Name, lang);
            if (m is ToolStripMenuItem)
            {
                string text = res2.GetString(m.Name + ".Text", lang); //Exception is thrown here
                if (text != null)
                    m.Text = text;
            }
        }
    }

I used another developer's code sample from this forum and made minor changes. Now when I change culture at runtime, I get the following exception:

An unhandled exception of type 'System.Resources.MissingManifestResourceException' occurred in mscorlib.dll

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

I have checked the resource file and all MenuStripItems have all translations/values there...

I failed to find solution in the internet, so would highly appreciate your help.

Thanks in advance!


Solution

  • So since I'm a beginner in localizing, the solution is much more easier than one that I have posted. Maybe this can help for other beginners too.

    So I removed all my code which actually was a bulk of headache and replaced it with the following code:

    Thread.CurrentThread.CurrentUICulture = new CultureInfo(StaticValues.currentCulture);
    this.Controls.Clear();
    this.InitializeComponent();
    

    And it worked easily. And yes, thanks to Adriano Repetti!