简体   繁体   English

C#如何选择语言?

[英]C# How to select language?

Hey guys I been working on a code in my combobox I got some items in it 3 languages english, french, and german I'm hoping when I press the apply button on my program all of the text changes in the form, though can't get it to work: 大家好,我一直在自己的组合框中编写代码,我得到了其中的一些项目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"));
        }
    }

First set the UI controls' text (and size if needed) for all the languages you want to support. 首先为您要支持的所有语言设置UI控件的文本(如果需要,还可以设置大小)。

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

Then you have to create a method that will update all the UI controls on the current Form . 然后,您必须创建一个方法来更新当前Form上的所有UI控件。 You may create this method in a separate static helper class like this: 您可以在单独的静态帮助器类中创建此方法,如下所示:

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;
    }
}

This code is based on Suprotim Agarwal's article . 该代码基于Suprotim Agarwal的文章

Read about the differences between CurrentCulture and CurrentUICulture here: What is the difference between CurrentCulture and CurrentUICulture properties of CultureInfo in .NET? 在这里阅读有关CurrentCultureCurrentUICulture 的区别.NET中CultureInfo的CurrentCulture和CurrentUICulture属性之间的区别是什么?

In the button click event handler you only have to call this method: 在按钮单击事件处理程序中,您只需调用此方法:

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