正确的方式在运行时改变语言 [英] Proper way to change language at runtime

查看:211
本文介绍了正确的方式在运行时改变语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是在运行时改变形式语言的正确方法是什么?

What is the proper way to change Form language at runtime?


  1. 手动设置所有的控制使用递归像<一个href=\"http://stackoverflow.com/questions/7556367/how-do-i-change-the-culture-of-a-winforms-application-at-runtime\">this

  2. 保存语言选择文件>重新启动应用程序>加载languge
    选择之前的InitializeComponent();

  3. 使用窗体构造函数,替换活动实例(如果这甚至有可能)

  4. 有些东西

有那么这个一半多线程编写的,但没有提供关于什么是做这种正确的方法真正的答案?

There is so much half written threads about this but none provides real answer on what is proper way to do this?

更新:结果
为了澄清我的问题:

UPDATE:
To clarify my question:

做这样的事情:

public Form1()
{
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("de");
    this.InitializeComponent();
}

工作正常,我所有的控制和一切其他资源得到正确转换。
和做类似:

works fine and all my controls and everything else in resources get translated correctly. And doing something like:

private void button1_Click(object sender, EventArgs e)
{
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");
}

什么都不做,形式停留在语言,我之前的InitializeComponent()设置;

推荐答案

我相信汉斯帕桑特的评论显示的解决方案可能是唯一的(一般的)解决方案。

I believe the solution shown in Hans Passant's comment might be the only (general) solution.

就个人而言,我用这个基类需要进行本地化的形式:

Personally, I use this base class for all forms that need to be localized:

public class LocalizedForm : Form
{
    /// <summary>
    /// Occurs when current UI culture is changed
    /// </summary>
    [Browsable(true)]
    [Description("Occurs when current UI culture is changed")]
    [EditorBrowsable(EditorBrowsableState.Advanced)]
    [Category("Property Changed")]
    public event EventHandler CultureChanged;

    protected CultureInfo culture;
    protected ComponentResourceManager resManager;

    /// <summary>
    /// Current culture of this form
    /// </summary>
    [Browsable(false)]
    [Description("Current culture of this form")]
    [EditorBrowsable(EditorBrowsableState.Never)]
    public CultureInfo Culture
    {
        get { return this.culture; }
        set
        {
            if (this.culture != value)
            {
                this.ApplyResources(this, value);

                this.culture = value;
                this.OnCultureChanged();
            }
        }
    }

    public LocalizedForm()
    {
        this.resManager = new ComponentResourceManager(this.GetType());
        this.culture = CultureInfo.CurrentUICulture;
    }

    private void ApplyResources(Control parent, CultureInfo culture)
    {
        this.resManager.ApplyResources(parent, parent.Name, culture);

        foreach (Control ctl in parent.Controls)
        {
            this.ApplyResources(ctl, culture);
        }
    }

    protected void OnCultureChanged()
    {
        var temp = this.CultureChanged;
        if (temp != null)
            temp(this, EventArgs.Empty);
    }
}

然后,而不是直接更改 Thread.CurrentThread.CurrentUICulture ,我用静态管理器类此属性更改UI文化:

Then instead of directly changing Thread.CurrentThread.CurrentUICulture, I use this property in static manager class to change UI culture:

public static CultureInfo GlobalUICulture
{
    get { return Thread.CurrentThread.CurrentUICulture; }
    set
    {
        if (GlobalUICulture.Equals(value) == false)
        {
            foreach (var form in Application.OpenForms.OfType<LocalizedForm>())
            {
                form.Culture = value;
            }

            Thread.CurrentThread.CurrentUICulture = value;
        }
    }
}

这篇关于正确的方式在运行时改变语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆