简体   繁体   English

在C#Winform中在运行时更改语言

[英]Change language at runtime in C# winform

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." “找不到适合于指定区域性或中性区域性的任何资源。请确保在编译时已将“ System.Type.resources”正确嵌入或链接到程序集“ mscorlib”中,或者确保所需的所有附属程序集都可加载且完全签。”

 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. 构造函数的参数是错误的,您要告诉它查找System.Type的资源。 Which is why it is complaining that it can't find "System.Type.resources". 这就是为什么它抱怨找不到“ 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. 请改用this.GetType() 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. 您可以代替Application.OpenForms()。 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) {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(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. 请谨慎使用类似wiz-bang的功能。 Nobody actually changes their native language while they are using your program. 在使用您的程序时,实际上没有人更改其母语。

private void ChangeLanguage(CultureInfo culture)
{
      Application.CurrentCulture = culture;
      CultureInfo.DefaultThreadCurrentCulture = culture;
      CultureInfo.DefaultThreadCurrentUICulture = culture;

      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture.Name);
      Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(culture.Name);
}

You'll need to have appropriate resources for all languages though. 不过,您需要为所有语言提供适当的资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM