交换语言(的CultureInfo /全球化)不影响ToolStripMenuItems [英] Switching language (cultureinfo/globalization) does not affect ToolStripMenuItems

查看:132
本文介绍了交换语言(的CultureInfo /全球化)不影响ToolStripMenuItems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows窗体应用程序项目,并在主窗体上我有一个菜单条。在这个菜单中的某些地方剥离,可以选择不同的语言。因为如果用户选择英语,一切都该主窗体上(和其他人在未来)应该变成英语的例子

I have a Windows Forms application project, and on the main form I have a menu strip. Some place in this menu strip it is possible to select various languages. For example if the user selects "English", everything on this main form (and others in the future) should be turned into English language.

我把这个教程:
点击

这正常工作与唱片公司等,但它不与工具条菜单项,在所有工作。他们只是留在其默认的文本

This works fine with labels and such, but it does not work at all with the tool strip menu items. They just stay with their default text.

我试过两行添加到 ChangeLanguage 方法:

I tried to add two more lines to the ChangeLanguage method:

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



但它失败,并说:

But it fails and says:

找不到适合指定区域性或中性文化的任何资源。确保System.Windows.Forms.ToolStripMenuItem.resources在编译时正确嵌入或链接到程序集System.Windows.Forms的,或者确保所有需要的附属程序集都可加载并完全签名。

Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "System.Windows.Forms.ToolStripMenuItem.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.

不知道如何着手 - 感谢任何帮助。

Not sure how to proceed - any help appreciated.

推荐答案

您必须删除您的foreach循环的最后2行。
这行说,你正在寻找System.Windows.Forms.ToolStripMenuItem.resx文件的定位信息,但你要在你的窗体资源文件的样子。

You have to remove the last 2 lines in your foreach loop. That lines say that you are looking for the localization information in System.Windows.Forms.ToolStripMenuItem.resx file, but you want to look in your Forms resources file.

ToolstripMenuItems被添加到ToolStripItems DropDownItems集合,而不是你的表格控件集合。这可以帮助你解决你的问题。

ToolstripMenuItems are added to an ToolStripItems DropDownItems Collection and not to the Controls collection of your Form. This might help you solving your problem.

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

如果您有进一步下拉。项目你应该考虑一个递归方法

If you have further drop down items you should consider a recursive approach.

编辑:我的第一个评论

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

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

这篇关于交换语言(的CultureInfo /全球化)不影响ToolStripMenuItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆