繁体   English   中英

C#如何选择语言?

[英]C# How to select language?

大家好,我一直在自己的组合框中编写代码,我得到了其中的一些项目3种语言,英语,法语和德语,我希望当我按程序上的Apply按钮时,所有文本形式都可以更改,但是可以吗?使其正常工作:

private void ApplyButtonOptions_Click(object sender, EventArgs e)
    {
        Properties.Settings.Default.Save();

        if (comboBox1.SelectedItem.ToString() == "English")
        {
            comboBox1.SelectedItem = englishLanguage;
        }

        if (comboBox1.SelectedItem.ToString() == "German")
        {
            comboBox1.SelectedItem = GermanLanguage;
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(new System.Globalization.CultureInfo("de"));
        }
    }

首先为您要支持的所有语言设置UI控件的文本(如果需要,还可以设置大小)。

这是如何的: https : //msdn.microsoft.com/zh-cn/library/y99d1cd3%28v=vs.71%29.aspx

然后,您必须创建一个方法来更新当前Form上的所有UI控件。 您可以在单独的静态帮助器类中创建此方法,如下所示:

public static class ResourceLoader
{
    public static void ChangeLanguage(System.Windows.Forms.Form form, System.Globalization.CultureInfo language)
    {
        var resources = new System.ComponentModel.ComponentResourceManager(form.GetType());

        foreach (Control control in form.Controls)
        {
            resources.ApplyResources(control, control.Name, language);
        }

        // These may not be needed, check if you need them.
        Thread.CurrentThread.CurrentUICulture = language;
        Thread.CurrentThread.CurrentCulture = language;
    }
}

该代码基于Suprotim Agarwal的文章

在这里阅读有关CurrentCultureCurrentUICulture 的区别.NET中CultureInfo的CurrentCulture和CurrentUICulture属性之间的区别是什么?

在按钮单击事件处理程序中,您只需调用此方法:

private void ApplyButton_Click(object sender, EventArgs e)
{
    var cultureInfo = new System.Globalization.CultureInfo(cboCultureInfo.SelectedItem.ToString());

    ResourceLoader.ChangeLanguage(this, cultureInfo);
}

暂无
暂无

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

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