运行时的本地化

本文关键字:本地化 运行时 | 更新日期: 2023-09-27 18:04:52

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

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

运行时的本地化

我编写了一个RuntimeLocalizer类,具有以下特性:

  • 更改和更新表格中所有Control s和SubControl s的定位
  • 还更改了所有MenuStrip s的所有SubItem s的定位

使用示例: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);
        }
    }
}

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

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

,然后你必须使用resources.ApplyResources

申请每个控件

请看这里

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

感谢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));
                    }
                }
            }
        }