簡體   English   中英

C#多語言應用程序菜單條項目

[英]C# Multilingual Application Menu Strip Item

我正在嘗試使我的程序使用多語言,而我幾乎做到了。 其余控件運行正常,但我的菜單欄項目出現了問題。

當我在英語和土耳其語子菜單之間切換程序的語言時,子菜單條項目會更改,但是主菜單項不會有所更改。

在此處輸入圖片說明

如您所見,選擇土耳其語項目時,子菜單項使用土耳其語,選擇英語項目時,子菜單項也使用英語。

這是我切換語言的代碼:

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));
                }
        }
    }

您的ApplyResources僅應用於dropDownItem而不是主要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));
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM