控制语言变更标签

时间:2016-06-16 06:06:56

标签: c#-4.0

我有几个按钮控件标记:添加,编辑,复制,删除,多个车辆选择。菜单栏中有一个选项可以更改语言(英语,西班牙语,德语等...)。有没有简单的方法可以根据窗体中选定的语言更改按钮控件标签?

1 个答案:

答案 0 :(得分:0)

您可以通过切换应用程序的语言并将此更改应用于所有打开的表单及其控件来实现:

private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
        try
        {

            if (Thread.CurrentThread.CurrentCulture.Name == "de-DE")
            {
                CultureInfo culture = new CultureInfo("en-US");
                Thread.CurrentThread.CurrentCulture = culture;
                Thread.CurrentThread.CurrentUICulture = culture;
                ChangeLanguage("en-US");
            }

            else
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE");
                ChangeLanguage("de-DE");
            }
        }
        catch(Exception)
        { }
    }

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

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

    private static void applyResources(ComponentResourceManager manager, Control.ControlCollection ctls, string lang)
    {
        foreach (Control ctl in ctls)
        {
            // here your logic should be accurate enough to apply each control on form
            // This is just an example and may or may not work, just giving a thought that you can do it in this way
            manager.ApplyResources(ctl, ctl.Name, new CultureInfo(lang));
            applyResources(manager, ctl.Controls, lang);
        }
    }

我希望这会帮助你更多地沉迷其中。 :)