在C#的winform更改语言运行时 [英] Change language at runtime in C# winform

查看:226
本文介绍了在C#的winform更改语言运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要改变语言,但是当我编译这个异常弹出。它说:

  

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

 私人无效comboBox1_SelectedIndexChanged(对象发件人,EventArgs的)
    {
        如果(comboBox1.SelectedItem.ToString()==英语)
        {
            Thread.CurrentThread.CurrentUICulture =新的CultureInfo(EN);
            ChangeLanguage(恩);
        }
        否则,如果(comboBox1.SelectedItem.ToString()==德国)
        {
            Thread.CurrentThread.CurrentUICulture =新的CultureInfo(德);
            ChangeLanguage(德);
        }
    }


    私人无效ChangeLanguage(字符串郎)
    {
        的foreach(在this.Controls控制C)
        {
            ComponentResourceManager资源=新ComponentResourceManager(typeof运算(类型));
            resources.ApplyResources(C,c.Name,新的CultureInfo(郎));
        }
    }
 

有什么建议?

解决方案

  ComponentResourceManager资源=新ComponentResourceManager(typeof运算(类型));
 

该参数的构造函数是错误的,你告诉它来寻找资源的System.Type。这就是为什么它是抱怨无法找到System.Type.resources。它会永远找不到它们。

您需要传递,你居然要本地化的窗体的类型。使用 this.GetType()代替。尽管这可能只是本地化选项的形式,而不是在您的应用程序窗口的其余部分。你可以遍历Application.OpenForms()来代替。它也有必要本地化应用于的所有的控制。不只是在窗体上的,还有那些位于内部的容器,如面板。因此:

 私有静态无效ChangeLanguage(字符串郎){
        的foreach(在Application.OpenForms表格FRM){
            localizeForm(FRM);
        }
    }

    私有静态无效localizeForm(表格FRM){
        VAR经理=新ComponentResourceManager(frm.GetType());
        manager.ApplyResources(FRM,$这一);
        applyResources(经理,frm.Controls);
    }

    私有静态无效applyResources(ComponentResourceManager经理,Control.ControlCollection的CTL){
        的foreach(CTL​​中控制CTL){
            manager.ApplyResources(CTL,ctl.Name);
            applyResources(经理,ctl.Controls);
        }
    }
 

要小心这样的奇才邦功能。实际上没有人,而他们使用你的程序改变他们的母语。

I want to change Language but when I compile this an exception pop up. it says

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

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBox1.SelectedItem.ToString() == "English")
        {
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("En");
            ChangeLanguage("En");
        }
        else if (comboBox1.SelectedItem.ToString() == "German")
        {
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("De");
            ChangeLanguage("De");
        }
    }


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

Any suggestions?

解决方案

 ComponentResourceManager resources = new ComponentResourceManager(typeof(Type));

The argument to the constructor is wrong, you are telling it to find the resources for System.Type. Which is why it is complaining that it can't find "System.Type.resources". It will never find those.

You need to pass the type of the form that you actually want to localize. Use this.GetType() instead. Albeit that this probably will just localize your Options form and not the rest of the windows in your app. You could iterate Application.OpenForms() instead. It is also necessary to apply the localization to all the controls. Not just the ones on the form, also the ones that are located inside containers like panels. Thus:

    private static void ChangeLanguage(string lang) {
        foreach (Form frm in Application.OpenForms) {
            localizeForm(frm);
        }
    }

    private static void localizeForm(Form frm) {
        var manager = new ComponentResourceManager(frm.GetType());
        manager.ApplyResources(frm, "$this");
        applyResources(manager, frm.Controls);
    }

    private static void applyResources(ComponentResourceManager manager, Control.ControlCollection ctls) {
        foreach (Control ctl in ctls) {
            manager.ApplyResources(ctl, ctl.Name);
            applyResources(manager, ctl.Controls);
        }
    }

Be careful with wiz-bang features like this. Nobody actually changes their native language while they are using your program.

这篇关于在C#的winform更改语言运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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