切换语言(cultureinfo / globalization)不会影响ToolStripMenuItems

我有一个Windows窗体应用程序项目,并在主窗体上我有一个菜单条。 在这个菜单条中的一些地方可以select各种语言。 例如,如果用户select“英文”,则将该主表格(以及其他未来的表格)上的所有内容都转换成英文。

我接受了这个教程: 点击

这对标签等工作正常,但它不工作的工具条菜单项。 他们只是保持默认的文本。

我试图给ChangeLanguage方法添加两行:

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

但它失败了,并说:

找不到适合特定文化或中性文化的资源。 确保在编译时“System.Windows.Forms.ToolStripMenuItem.resources”已正确embedded或链接到程序集“System.Windows.Forms”中,或者所有需要的附属程序集均可加载并完全签名。

不知道如何继续 – 任何帮助表示赞赏。

你必须删除你的foreach循环中的最后两行。 这句话说你正在寻找System.Windows.Forms.ToolStripMenuItem.resx文件中的本地化信息,但是你想看看你的Forms资源文件。

ToolstripMenuItems被添加到ToolStripItems的DropDownItems集合,而不是您的窗体的Controls集合。 这可能会帮助你解决你的问题。

 private void ChangeLanguage(string lang) { ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1)); foreach (Control c in this.Controls) { resources.ApplyResources(c, c.Name, new CultureInfo(lang)); } foreach (ToolStripItem item in toolStrip1.Items) { if (item is ToolStripDropDownItem) foreach (ToolStripItem dropDownItem in ((ToolStripDropDownItem)item).DropDownItems) { resources.ApplyResources(dropDownItem, dropDownItem.Name, new CultureInfo(lang)); } } } 

如果你有更多的下拉项目,你应该考虑一个递归的方法。

编辑:我的第一个评论

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

ChangeLanguage(toolStrip1.Items); }

 private void ChangeLanguage(ToolStripItemCollection collection) { foreach (ToolStripItem item in collection) { resources.ApplyResources(item, item.Name, new CultureInfo(lang)); if (item is ToolStripDropDownItem) ChangeLanguage(((ToolStripDropDownItem)item).DropDownItems); } }