zoukankan      html  css  js  c++  java
  • 使用自定义类实现工程多语言

      由于时间紧促,来不及去看.Net的全球化实现方法,所以自己写了个资源文件的实现方法,来实现工程的多语言版需求。原理很简单,将一个窗体会用到的所有字符串,包括各种控件的显示文本,用序列化方法保存到文件中。定义一个继承类BaseForm,在窗体加载中读出资源文件,实现语言化。
      以下让我们来具体实现。

      一、ResCollection类
      此类实现了资源文件的结构定义。

      1using System;
      2using System.Collections;
      3
      4namespace FaibClass.Language
      5{
      6
      7    [Serializable()]
      8    public class ResCollection
      9    {
     10        Hashtable hast = new Hashtable();
     11        private string m_Copyright;
     12        private string m_Description;
     13
     14        public string Copyright
     15        {
     16            get{return m_Copyright;}
     17            set{m_Copyright = value;}
     18        }

     19
     20        public string Description
     21        {
     22            get{return m_Description;}
     23            set{m_Description = value;}
     24        }

     25
     26        public ResCollection()
     27        {
     28        }

     29
     30        public int Count
     31        {
     32            get{return hast.Count;}
     33        }

     34
     35        public string this[int Index]
     36        {
     37            get
     38            {
     39                string[] s = new string[this.Count];
     40                hast.Keys.CopyTo(s, 0);
     41                return s[Index];
     42            }

     43        }

     44
     45        public ValueList this[string Section]
     46        {
     47            get
     48            {
     49                return hast[Section] as ValueList;
     50            }

     51            set
     52            {
     53                if(hast.ContainsKey(Section))
     54                {
     55                    hast[Section] = value;
     56                }

     57            }

     58        }

     59
     60        public void Add(string Section, string Key, string Value)
     61        {
     62            if(hast.ContainsKey(Section))
     63            {
     64                (hast[Section] as ValueList).Add(Key, Value);
     65            }

     66            else
     67            {
     68                ValueList vl = new ValueList();
     69                vl.Add(Key, Value);
     70                hast.Add(Section, vl);
     71            }

     72        }

     73
     74        public void Clear()
     75        {
     76            hast.Clear();
     77        }

     78
     79        public void Remove(string Section)
     80        {
     81            hast.Remove(Section);
     82        }

     83    }

     84
     85    [Serializable()]
     86    public class ValueList
     87    {
     88        Hashtable hast = new Hashtable();
     89
     90        public ValueList()
     91        {
     92        }

     93
     94        public int Count
     95        {
     96            get{return hast.Count;}
     97        }

     98
     99        public string this[int Index]
    100        {
    101            get
    102            {
    103                string[] s = new string[this.Count];
    104                hast.Keys.CopyTo(s, 0);
    105                return s[Index];
    106            }

    107        }

    108
    109        public string this[string Key]
    110        {
    111            get
    112            {
    113                if(hast.ContainsKey(Key))
    114                {
    115                    return hast[Key].ToString();
    116                }

    117                else
    118                {
    119                    return "";
    120                }

    121            }

    122            set
    123            {
    124                if(hast.ContainsKey(Key))
    125                {
    126                    hast[Key] = value;
    127                }

    128            }

    129        }

    130
    131        public void Add(string Key, string Value)
    132        {
    133            hast.Add(Key, Value);
    134        }

    135
    136        public void Clear()
    137        {
    138            hast.Clear();
    139        }

    140
    141        public void Remove(string Key)
    142        {
    143            hast.Remove(Key);
    144        }

    145    }

    146}

      二、ResManager类
      此类实现了资源文件结构的序列化保存与读取,并语言化WinForm控件。
      1using System;
      2using System.Collections;
      3using System.Runtime.Serialization;
      4using System.Runtime.Serialization.Formatters;
      5using System.Runtime.Serialization.Formatters.Binary;
      6using System.Windows.Forms;
      7using System.IO;
      8
      9namespace FaibClass.Language
     10{
     11    public class ResManager
     12    {
     13        private ValueList vl = new ValueList();
     14        private ValueList basevl = new ValueList();
     15
     16        public ResManager(Form Target, string FileName, string Section)
     17        {
     18            BinaryFormatter binf = new BinaryFormatter();
     19            Stream str = File.Open(FileName, FileMode.Open);
     20            ResCollection resc = binf.Deserialize(str) as ResCollection;
     21            str.Close();
     22            basevl = resc["Base"];
     23            vl = resc[Section];
     24
     25            if(Target == null)return;
     26            if(vl == null)return;
     27            string strText = vl["Text"];
     28            if(strText != "")
     29            {
     30                Target.Text = strText;
     31            }

     32            SearchChildControl(Target);
     33            if(Target.Menu != null)
     34            {
     35                SearchMenuItem(Target.Menu);
     36            }

     37        }

     38
     39        public ValueList ValueList
     40        {
     41            get{return vl;}
     42        }

     43
     44        public ValueList BaseValueList
     45        {
     46            get{return basevl;}
     47        }

     48
     49        //语言化字控件
     50        private void SearchChildControl(Control Owner)
     51        {
     52            foreach(Control ctl in Owner.Controls)
     53            {
     54                string strText = vl[ctl.Name];
     55                if(strText != "")
     56                {
     57                    ctl.Text = strText;
     58                }

     59                switch(ctl.GetType().Name)
     60                {
     61                    case "ToolBar":
     62                        SearchToolbarButton(ctl as ToolBar);
     63                        break;
     64                    case "StatusBar":
     65                        SearchStatusBarPanel(ctl as StatusBar);
     66                        break;
     67                    case "TabControl":
     68                        SearchTabPage(ctl as TabControl);
     69                        break;
     70                    case "ListView":
     71                        SearchColumnHeader(ctl as ListView);
     72                        break;
     73                }

     74                if(ctl.HasChildren)
     75                {
     76                    SearchChildControl(ctl);
     77                }

     78            }

     79        }

     80
     81        //语言化菜单
     82        private void SearchMenuItem(Menu Owner)
     83        {
     84            foreach(MenuItem mnu in Owner.MenuItems)
     85            {
     86                string strText = vl[mnu.Text];
     87                if(strText != "")
     88                {
     89                    mnu.Text = strText;
     90                }

     91                SearchMenuItem(mnu);
     92            }

     93        }

     94        //语言化工具栏
     95        private void SearchToolbarButton(ToolBar Owner)
     96        {
     97            foreach(ToolBarButton tlbbtn in Owner.Buttons)
     98            {
     99                string strText = vl[tlbbtn.ToolTipText];
    100                if(strText != "")
    101                {
    102                    tlbbtn.ToolTipText = strText;
    103                }

    104            }

    105        }

    106        //语言化状态栏
    107        private void SearchStatusBarPanel(StatusBar Owner)
    108        {
    109            foreach(StatusBarPanel stapnl in Owner.Panels)
    110            {
    111                string strText = vl[stapnl.Text];
    112                if(strText != "")
    113                {
    114                    stapnl.Text = strText;
    115                }

    116            }

    117        }

    118        //语言化分页
    119        private void SearchTabPage(TabControl Owner)
    120        {
    121            foreach(TabPage tab in Owner.TabPages)
    122            {
    123                string strText = vl[tab.Text];
    124                if(strText != "")
    125                {
    126                    tab.Text = strText;
    127                }

    128                SearchChildControl(tab);
    129            }

    130        }

    131        //语言化列表
    132        private void SearchColumnHeader(ListView Owner)
    133        {
    134            foreach(ColumnHeader colhdr in Owner.Columns)
    135            {
    136                string strText = vl[colhdr.Text];
    137                if(strText != "")
    138                {
    139                    colhdr.Text = strText;
    140                }

    141            }

    142        }

    143
    144        public static ResCollection Open(string FileName)
    145        {
    146            BinaryFormatter binf = new BinaryFormatter();
    147            Stream str = File.Open(FileName, FileMode.Open);
    148            ResCollection resc = binf.Deserialize(str) as ResCollection;
    149            str.Close();
    150            return resc;
    151        }

    152
    153        public static void Save(string FileName, ResCollection resc)
    154        {
    155            BinaryFormatter binf = new BinaryFormatter();
    156            Stream str = File.Open(FileName, FileMode.Create);
    157            binf.AssemblyFormat = FormatterAssemblyStyle.Simple;
    158            binf.TypeFormat = FormatterTypeStyle.TypesWhenNeeded;
    159            binf.Serialize(str, resc);
    160            str.Close();
    161        }

    162    }

    163}

    164

      Menu、Toolbar、StatusBar、ListView、TabPage是以Text来命名资源名的,其余控件使用控件的Name来命名。

      三、继承类BaseForm
     1using System;
     2using System.Windows.Forms;
     3using System.ComponentModel;
     4using FaibClass.Language;
     5
     6namespace FaibSoft.CheaterMember
     7{
     8    public class BaseForm: Form
     9    {
    10        private ResManager resm;
    11        public ValueList _ValueList;
    12        public ValueList _BaseValueList;
    13
    14        [Browsable(false)]
    15        public ValueList BaseValueList
    16        {
    17            get
    18            {
    19                return _BaseValueList;
    20            }

    21        }

    22        [Browsable(false)]
    23        public ValueList ValueList
    24        {
    25            get
    26            {
    27                return _ValueList;
    28            }

    29        }

    30
    31        public BaseForm()
    32        {
    33        }

    34
    35        protected override void OnLoad(EventArgs e)
    36        {
    37            try
    38            {
    39                resm = new ResManager(this, Common.LanguageFile, this.GetType().Name);
    40                _BaseValueList = resm.BaseValueList;
    41                _ValueList = resm.ValueList;
    42            }

    43            catch{}
    44            base.OnLoad (e);
    45        }

    46
    47    }

    48}

    49

      使用本窗体资源直接使用ValueList["资源名"],使用公共资源使用BaseValueList["资源名"]

      相关下载资源编辑器 、示例
  • 相关阅读:
    现在这些“创业”的人都是什么心态?
    普及什么是“国家队”,国家队“黑阔”,安全公司“黑客”
    浅谈C++源码的过国内杀软的免杀
    十大谷歌Google搜索技巧分享
    寻找被黑金毁掉的黑客精神
    黑客偷你的密码干什么?
    七个高效的文本编辑习惯(以Vim为例)
    HTTPS是如何保证连接安全,你知道吗?
    分析与提取QQ木马盗号技术
    leetcode304
  • 原文地址:https://www.cnblogs.com/faib/p/666614.html
Copyright © 2011-2022 走看看