【问题标题】:How do I change the culture of a WinForms application at runtime如何在运行时更改 WinForms 应用程序的文化
【发布时间】:2011-09-26 14:09:42
【问题描述】:

我已经用 C# 创建了 Windows 窗体程序。我在本地化方面遇到了一些问题。我有两种语言的资源文件(一种是英语,另一种是法语)。我想在运行时单击每个语言按钮并更改语言。

但是当我点击按钮时,它不起作用。我正在使用此代码。

private void btnfrench_Click(object sender, EventArgs e)
{
    getlanguage("fr-FR");
}

private void getlanguage(string lan)
{
    foreach (Control c in this.Controls)
    {
        ComponentResourceManager cmp = 
            new ComponentResourceManager(typeof(BanksForm));
        cmp.ApplyResources(c, c.Name, new CultureInfo(lan));
    }
}

请大家帮忙解决这个问题......

非常感谢....

【问题讨论】:

    标签: c# .net winforms localization


    【解决方案1】:

    这行得通:

    private void button1_Click(object sender, EventArgs e)
    {
        System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-BE");
        ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
        resources.ApplyResources(this, "$this");
        applyResources(resources, this.Controls);
    }
    
    private void applyResources(ComponentResourceManager resources, Control.ControlCollection ctls)
    {
        foreach (Control ctl in ctls)
        {
            resources.ApplyResources(ctl, ctl.Name);
            applyResources(resources, ctl.Controls);
        }
    }
    

    请小心避免添加这种没人会使用的口哨。它充其量只是一个演示功能,实际上用户不会即时更改他们的母语。

    【讨论】:

      【解决方案2】:

      您可能必须在控件上递归调用 ApplyResources:

      private void btnfrench_Click(object sender, EventArgs e)
      {
          ApplyResourceToControl(
              this, 
              new ComponentResourceManager(typeof(BanksForm)), 
              new CultureInfo("fr-FR"))
      }
      
      private void ApplyResourceToControl(
         Control control, 
         ComponentResourceManager cmp, 
         CultureInfo cultureInfo)
      {
          cmp.ApplyResources(control, control.Name, cultureInfo);
      
          foreach (Control child in control.Controls)
          {
              ApplyResourceToControl(child, cmp, cultureInfo);
          }
      }
      

      【讨论】:

        【解决方案3】:

        在运行时更新 CultureInfo 可能会重置组件大小。此代码保留控件的大小和位置 (但仍然会有可见的闪烁,使用 SuspendLayout() 无法修复)

        
            private void langItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
            {
                //I store the language codes in the Tag field of list items
                var itemClicked = e.ClickedItem;
                string culture = itemClicked.Tag.ToString().ToLower();
        
                Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(culture);
                ApplyResourceToControl(
                this,
                new ComponentResourceManager(typeof(GUI)),
                new CultureInfo(culture));           
            }
        
            private void ApplyResourceToControl(
               Control control,
               ComponentResourceManager cmp,
               CultureInfo cultureInfo)
            {
                foreach (Control child in control.Controls)
                {
                    //Store current position and size of the control
                    var childSize = child.Size;
                    var childLoc = child.Location;
                    //Apply CultureInfo to child control
                    ApplyResourceToControl(child, cmp, cultureInfo);
                    //Restore position and size
                    child.Location = childLoc;
                    child.Size = childSize;
                }
                //Do the same with the parent control
                var parentSize = control.Size;
                var parentLoc = control.Location;
                cmp.ApplyResources(control, control.Name, cultureInfo);
                control.Location = parentLoc;
                control.Size = parentSize;
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-16
          • 1970-01-01
          相关资源
          最近更新 更多