本地化的WinForm,在运行时加载Resources.lang-LANG.resx?

时间:2015-08-09 22:38:12

标签: c# winforms

我有一个翻译过的" Form1"。更改ComboBox中的语言后,它只加载 Form1.lang-LANG.resx 而不是 Resources.lang-LANG.resx ,是否可以加载这个太?

这里是代码:

private void metroComboBoxLanguageSelector_SelectedIndexChanged(object sender, EventArgs e)
    {
        CultureInfo cui;

        var cultureInfo = CurrentThread.CurrentCulture;
        var cultureName = cultureInfo.Name;

        try
        {
            switch (metroComboBoxLanguageSelector.SelectedItem.ToString())
            {
                case "English":
                    cui = new CultureInfo("en-US");
                    SetCultureInfo(this, cui);
                    break;

                case "Deutsch":
                    cui = new CultureInfo("de-DE");
                    SetCultureInfo(this, cui);
                    break;

                case "Polski":
                    cui = new CultureInfo("pl-PL");
                    SetCultureInfo(this, cui);
                    break;

                default:
                    cui = new CultureInfo(cultureName);
                    SetCultureInfo(this, cui);
                    break;
            }
        }
        finally
        {
            ListLang();
        }
    }

private void SetCultureInfo(Control parent, CultureInfo cui)
    {
        foreach (Control c in parent.Controls)
        {
            var resources = new ComponentResourceManager(typeof (MainForm));
            resources.ApplyResources(c, c.Name, cui);

            if (c.Controls.Count > 0)
            {
                SetCultureInfo(c, cui);
            }
        }
    }

    private void ListItems()
    {
        var cultureInfo = CurrentThread.CurrentCulture;
        var nativeName = cultureInfo.Parent.NativeName;

        metroComboBoxLanguageSelector.Items.Add(nativeName + " (System)");
        metroComboBoxLanguageSelector.Items.Add("English");
        metroComboBoxLanguageSelector.Items.Add("Deutsch");
        metroComboBoxLanguageSelector.Items.Add("Polski");
        metroComboBoxLanguageSelector.SelectedIndex = 0;
    }

是否可以在运行时加载指定语言的所有.resx,或者像 CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(value);

将保存ComboBox的设置。

谢谢。

0 个答案:

没有答案