Search code examples
c#winformsmultilingualcontextmenustrip

C# Multilingual Application Menu Strip Item


i'm trying to make my program multilingual, and i almost did it. Rest of the controls are doing ok but i'm having a trouble with my menu strip items.

When i switch the language of the program between English and Turkish sub-menu strip items change, but the main-menu items don't change somehow.

enter image description here

As you can see, when Turkish item is selected, sub-menu items are in Turkish, and when English item is selected, sub-menu items are in English as well.

Here is the code that i switch languages :

private void türkçeToolStripMenuItem_Click(object sender, EventArgs e)
    {
        türkçeToolStripMenuItem.Checked = true;
        ingilizceToolStripMenuItem.Checked = false;

        ChangeLanguage(typeof(MainForm), "tr");
    }

    private void ingilizceToolStripMenuItem_Click_1(object sender, EventArgs e)
    {
        ingilizceToolStripMenuItem.Checked = true;
        türkçeToolStripMenuItem.Checked = false;

        ChangeLanguage(typeof(MainForm), "en");
    }

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

        foreach (ToolStripItem item in metroContextMenu1.Items)
        {
            if (item is ToolStripDropDownItem)
                foreach (ToolStripItem dropDownItem in ((ToolStripDropDownItem)item).DropDownItems)
                {
                    resources.ApplyResources(dropDownItem, dropDownItem.Name, new CultureInfo(lang));
                }
        }
    }

Solution

  • Your ApplyResources is only applied to the dropDownItem and not the main item.

        foreach (ToolStripItem item in metroContextMenu1.Items)
        {
            if (item is ToolStripDropDownItem)
                foreach (ToolStripItem dropDownItem in ((ToolStripDropDownItem)item).DropDownItems)
                {
                    resources.ApplyResources(dropDownItem, dropDownItem.Name, new CultureInfo(lang));
                }
            //Also apply resources to main toolstrip items. 
            resources.ApplyResources(item, item.Name, new CultureInfo(lang));
        }