当前位置: 代码迷 >> C# >> 不同窗体之间的本地化语言切换,该如何解决
  详细解决方案

不同窗体之间的本地化语言切换,该如何解决

热度:94   发布时间:2016-05-05 02:40:52.0
不同窗体之间的本地化语言切换
我有两窗体,Form1 ,Form2.  Form1中实例化了Form2; Form2 form2=new Form();点击按钮事件弹出一个新的窗体。现在Form2中有一个按钮,点击按钮切换语言。 但是现在只切换Form2控件的语言 怎么通过Form2的按钮刷新Form1中控件的语言。

希望大侠们多多帮助 ,万分感谢!

------解决思路----------------------
试着回调一下form1中的一些函数,load等
------解决思路----------------------
。。。如果只是满足你的需要,拿办法多的是,你怎么更新form2的只要有form1的引用就怎么更新form1;
通常我们做多语言,是封装的一个专门的库通过读取配置文件来设置每个窗体每个控件的文本显示;
比如每个窗体都在load中调用UpdateLanguage(this);根据一些约定就可以实现语言的设置;
那么当在某个窗口中选择了语言切换,只要一开始将所有窗体保存在一个列表中,将之遍历一遍,UpdateLanguage(LstAllForms[i]);就行了;
------解决思路----------------------
切换了语言
if(!System.Threading.Thread.CurrentThread.CurrentUICulture.Name.Equals(cultureName, StringComparison.OrdinalIgnoreCase))
{
      System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(cultureName);
      foreach(var form in Application.OpenForms)
      {
             form.ApplyResource();
      }
}

你可以写一个BaseForm作为基类,ApplyResource弄成一个虚方法,实现通用的逻辑。窗体可以override自己去处理特殊的控件

        /// <summary>
        /// 当语言选项发生变化时的事件处理
        /// </summary>
        public event EventHandler LanguageChanged;

        public void OnLanguageChanged()
        {
             if (this.LanguageChanged != null) this.LanguageChanged(this, EventArgs.Empty);
        }

        public virtual void ApplyResource()
        {
            var resources = new System.ComponentModel.ComponentResourceManager(this.GetType());
            this.SuspendLayout();
            resources.ApplyResources(form, "$this");
            ApplyChildrenResources(resources, this);
            //主菜单
            if (this.MainMenuStrip != null)
            {
                this.MainMenuStrip.ApplyMenuItemsResources(resources);
            }
            //右键菜单
            if (this.ContextMenuStrip != null)
            {
                this.ContextMenuStrip.ApplyMenuItemsResources(resources);
            }
            this.OnLanguageChanged();      //触发事件
            this.ResumeLayout();            
        }

        public static void ApplyChildrenResources(ComponentResourceManager resources, Control parent)
        {
            foreach (Control ctrl in parent.Controls)
            {                
                if (ctrl is UserControl)
                {
                    //加载用户控件的资源
                    ctrl.SuspendLayout();
                    var ucResources = new System.ComponentModel.ComponentResourceManager(ctrl.GetType());
                    ucResources.ApplyResources(ctrl, "$this");
                    ApplyChildrenResources(ucResources, ctrl);
                    ctrl.ResumeLayout();                    
                }
                else                
                {
                    var menu = ctrl as ToolStrip;
                    if (menu != null)
                    {
                        menu.ApplyMenuItemsResources(resources);
                    }                    
                    else
                    {
                        var dgv = ctrl as DataGridView;
                        if (dgv != null)
                        {
                            foreach (DataGridViewColumn col in dgv.Columns)
                            {
                                resources.ApplyResources(col, col.Name);
                            }
                        }
                        else
                        {
                            var combox = ctrl as ComboBox;
                            if (combox != null && combox.Items.Count != 0)
                            {
                                combox.Items[0] = Resources.DefaultSelectedItem;
                            }
                        }                        
                    }
                }
                resources.ApplyResources(ctrl, ctrl.Name);
                ApplyChildrenResources(resources, ctrl);
            }
        }

        public static void ApplyMenuItemsResources(this ToolStrip menu, ComponentResourceManager resources)
        {
            foreach (ToolStripItem item in menu.Items)
            {
                resources.ApplyResources(item, item.Name);
                var dropDownItem = item as ToolStripDropDownItem;
                if (dropDownItem != null)
                {
                    dropDownItem.ApplyDropDownItemsResources(resources);
                }
            }
        }

        static void ApplyDropDownItemsResources(this ToolStripDropDownItem parentItem, ComponentResourceManager resources)
        {
            if (parentItem.HasDropDownItems)
            {
                foreach (ToolStripDropDownItem subItem in parentItem.DropDownItems)
                {
                    resources.ApplyResources(subItem, subItem.Name);
                    subItem.ApplyDropDownItemsResources(resources);
                }
            }
        }   

加上事件,未考虑到的控件,处理一下事件就行了
  相关解决方案