运行时本地化

时间:2011-08-08 10:37:13

标签: c# .net winforms localization

我在C#中创建了Windows窗体程序。 本地化有一些问题。 我有3种语言的资源文件。 我想单击每个语言按钮并在运行时更改语言。 当我在InitializeComponent()之前更改语言时,它会起作用。 但是,当我点击按钮时,它不起作用。 我正在使用此代码。

private void RussianFlag_Click(object sender, EventArgs e)
{
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("ru-RU");
}

4 个答案:

答案 0 :(得分:13)

我写了一个RuntimeLocalizer类,其中包含以下功能:

  • 更改并更新表单中所有ControlSubControl的本地化
  • 还会更改所有SubItem s
  • 的所有MenuStrip的本地化

用法示例:RuntimeLocalizer.ChangeCulture(MainForm, "en-US");


using System.Windows.Forms;
using System.Globalization;
using System.Threading;
using System.ComponentModel;

public static class RuntimeLocalizer
{
    public static void ChangeCulture(Form frm, string cultureCode)
    {
        CultureInfo culture = CultureInfo.GetCultureInfo(cultureCode);

        Thread.CurrentThread.CurrentUICulture = culture;

        ComponentResourceManager resources = new ComponentResourceManager(frm.GetType());

        ApplyResourceToControl(resources, frm, culture);
        resources.ApplyResources(frm, "$this", culture);
    }

    private static void ApplyResourceToControl(ComponentResourceManager res, Control control, CultureInfo lang)
    {
        if (control.GetType() == typeof(MenuStrip))  // See if this is a menuStrip
        {
            MenuStrip strip = (MenuStrip)control;

            ApplyResourceToToolStripItemCollection(strip.Items, res, lang);
        }

        foreach (Control c in control.Controls) // Apply to all sub-controls
        {
            ApplyResourceToControl(res, c, lang);
            res.ApplyResources(c, c.Name, lang);
        }

        // Apply to self
        res.ApplyResources(control, control.Name, lang);
    }

    private static void ApplyResourceToToolStripItemCollection(ToolStripItemCollection col, ComponentResourceManager res, CultureInfo lang)
    {
        for (int i = 0; i < col.Count; i++)     // Apply to all sub items
        {
            ToolStripItem item = (ToolStripMenuItem)col[i];

            if (item.GetType() == typeof(ToolStripMenuItem))
            {
                ToolStripMenuItem menuitem = (ToolStripMenuItem)item;
                ApplyResourceToToolStripItemCollection(menuitem.DropDownItems, res, lang);
            }

            res.ApplyResources(item, item.Name, lang);
        }
    }
}

答案 1 :(得分:4)

您需要为其重新加载控件以反映新文化值

ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));

然后您必须使用resources.ApplyResources

申请每个控件

look here

答案 2 :(得分:1)

更改CurrentUICulture不会自动重新加载资源。您需要手动执行它(http://msdn.microsoft.com/en-us/magazine/cc163609.aspx#S8)

您可以从InitializeComponent()复制与本地化相关的代码 进入另一个功能:

void LoadResources(){

    this.Title = MyApp.Resources.MainFormCaption;
    this.lblWelcomeMessage.Text = MyApp.Resources.UserWelcome;

}

答案 3 :(得分:-1)

感谢V4Vendetta和其他人.. 解决方案是......

private void RussianFlag_Click(object sender, EventArgs e)
        {
            if (currentLanguage != "RUS")
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("ru-RU");
                ChangeLanguage("ru-RU");
            }
        }

.... .... ...

private void ChangeLanguage(string lang)
        {
            foreach (Control c in this.Controls)
            {
                ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
                resources.ApplyResources(c, c.Name, new CultureInfo(lang));
                if (c.ToString().StartsWith("System.Windows.Forms.GroupBox"))
                {
                    foreach (Control child in c.Controls)
                    {
                        ComponentResourceManager resources_child = new ComponentResourceManager(typeof(Form1));
                        resources_child.ApplyResources(child, child.Name, new CultureInfo(lang));
                    }
                }
            }
        }