winforms 如何在c#中切换表单语言?

5lwkijsr  于 2023-05-18  发布在  C#
关注(0)|答案(1)|浏览(173)

你好,我有C# Windows窗体11窗体。我使用所有形式的语言属性来设计他们在2种语言(默认和英语美国)。在我的设置形式,我有2个单选按钮,我想当用户点击他们的所有形式的语言改变。我找到了2个例子,但没有一个工作如何做到这一点?
这是我的第一次尝试:(不工作,并且混淆了窗体上控件的位置)

private void radioButtonDefault_Click(object sender, EventArgs e)
        {
            Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentUICulture;
            // Reload the form or the controls to apply the new language
            this.Controls.Clear();
            InitializeComponent();
        }

        private void radioButtonEnglish_Click(object sender, EventArgs e)
        {
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
            // Reload the form or the controls to apply the new language
            this.Controls.Clear();
            InitializeComponent();
        }

这是我的第二次尝试:(未运行获取太多错误)

private void radioButtonDefault_Click(object sender, EventArgs e)

{
    SwitchToDefaultLanguage();
}

private void radioButtonEnglish_Click(object sender, EventArgs e)
{
    SwitchToEnglish();
}

private static void ApplyResources(Control control, string name, CultureInfo culture)
{
    System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(control.GetType(), name, culture);
    control.Text = resources.GetString("$this.Text");
    foreach (Control child in control.Controls)
    {
        ApplyResources(child, child.Name, culture);
    }
}

private void SwitchToDefaultLanguage()
{
    this.SuspendLayout();
    this.Localizable = true;
    this.components = new System.ComponentModel.Container();
    // Load the resources for the default language
    System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(SettingForm));
    resources.ApplyResources(this, "$this");
    // Apply the resources to all the controls of the form
    ApplyResources(this, this.Name, CultureInfo.CurrentUICulture);
    this.ResumeLayout(false);
}

private void SwitchToEnglish()
{
    this.SuspendLayout();
    this.Localizable = true;
    this.components = new System.ComponentModel.Container();
    // Load the resources for the English language
    System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(SettingForm));
    resources.ApplyResources(this, "$this");
    // Apply the resources to all the controls of the form
    ApplyResources(this, this.Name, CultureInfo.GetCultureInfo("en-US"));
    this.ResumeLayout(false);
}

怎样才能解决我的问题?请帮帮忙?

bihw5rsg

bihw5rsg1#

在运行时更改语言

设置语言的正确方法是在显示main之前。所以对于主窗体,最好在启动时执行,否则需要仔细编写考虑控件之间差异的逻辑。这并不像链接帖子中显示的那么简单。例如,ComboBox、ListBox、ListView、TreeView、DataGridView、ToolStrip、ContextMenuStrip、MenuStrip、StatusStrip或其他一些控件需要与其他控件不同的逻辑。
如果出于任何原因,您想继续尝试在运行时重新加载资源,请查看我的示例How to change language of a menu at run-time,但我不会在生产环境中使用它,我将依赖于重新启动应用程序。

重新启动时更改语言

如果你想知道如何重新启动应用程序,我将如何处理它:

  • 我将使用应用程序设置来存储所选语言。
  • 我会在启动时设置语言
  • 我将定义一个支持的语言列表,并使用代码,我将在主窗体的菜单下显示这些语言。
  • 在菜单项的click事件处理程序中,我将所选语言保存在设置中,并在用户权限下重新启动应用程序。

1.我将在项目中添加一个Settings.settings文件。然后,我将添加一个Culture设置,该设置具有字符串类型,其值等于我想用作默认区域性的区域性,比如说“fa-IR”。

1.在program.cs中,我将定义支持的区域性列表,并根据设置中的值设置当前线程的当前区域性和当前UI区域性:

using System.Globalization;
namespace WinFormsSample
{
    internal static class Program
    {
        public static string[] SupportedCultures = new[] {
            "fa-IR",
            "en-US",
        };
        [STAThread]
        static void Main()
        {
            ApplicationConfiguration.Initialize();
            var culture = Settings.Default.Culture;
            if(SupportedCultures.Contains(culture))
            {
                Thread.CurrentThread.CurrentUICulture =
                    Thread.CurrentThread.CurrentCulture =
                    CultureInfo.GetCultureInfo(Settings.Default.Culture);
            }
            Application.Run(new Form1());
        }
    }
}

1.然后在主窗体中,我将添加一个语言菜单项,比如languageToolStripMenuItem。然后在表单启动时,我将添加支持的语言作为菜单项,并根据设置设置设置所选语言,还添加一个click事件处理程序,以在选择新语言时保存设置,然后请求重新启动应用程序:

public Form1()
{
    InitializeComponent();
    foreach (var culture in Program.SupportedCultures)
    {
        var cultureItem = new ToolStripMenuItem();
        cultureItem.Text = CultureInfo.GetCultureInfo(culture).NativeName;
        cultureItem.Tag = culture;
        cultureItem.Click += (sender, args) =>
        {
            var selected = (ToolStripMenuItem)sender;
            selected.Checked = true;
            selected.Owner.Items.OfType<ToolStripMenuItem>()
                .Except(new[] { selected })
                .ToList().ForEach(x => x.Checked = false);
            Settings.Default.Culture = (string)selected.Tag;
            Settings.Default.Save();
            if (MessageBox.Show("To apply the selected language you " +
                "need to restart the application.\n" +
                "Do you want to restart now?\n" +
                "If you choose no or cancel, the language " +
                "applies the next time you open the application.",
                "Restart application?",
                MessageBoxButtons.YesNoCancel,
                MessageBoxIcon.Question,
                MessageBoxDefaultButton.Button3) == DialogResult.Yes)
            {
                Application.Restart();
            }
        };
        languageToolStripMenuItem.DropDownItems.Add(cultureItem);
    }
    var item = languageToolStripMenuItem.DropDown.Items
        .OfType<ToolStripMenuItem>().Where(x => $"{x.Tag}" == Settings.Default.Culture)
        .FirstOrDefault();
    if (item != null)
        item.Checked = true;
}

相关问题