多国语言资源本地化

设置 Form_Login的 Localizable 属性为 true, 设置该属性后,.net 将根据不同的语言,为应用程序生成不同的资源文件

更改 Form_Login的 Language 属性为想要支持的另一种语言,此例中我们选用 English

此时.net 将为 Form_Login生成另一个资源文件,在本例中名为 Form_Login.resx
在资源文件中要像下面的label1这样设置属性,才能在Language属性选择为这种语言时这个页面为这种语言

>>label1.Name  label1

>>label1.Parent $this

>>label1.Type System.Windows.Forms.Label, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

>>label1.ZOrder 4

        /// <summary>
        /// Get The Resource for the  language which is selected.
        /// </summary>
        /// <param name="FormType">eg:typeof(Form_Login)</param>
        /// <param name="sLanguageCode">eg:"en","zh-CN","zh-HK"</param>
        private void GetResource(string sLanguageCode,Type FormType)
        {
             
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(sLanguageCode);
            System.ComponentModel.ComponentResourceManager res = new ComponentResourceManager(FormType);
            foreach (Control ctl in Controls)
            {
                res.ApplyResources(ctl, ctl.Name);
            }
        }


GetResource("zh-HK", typeof(Form_Login));//在Form_Login页面使用“中国香港繁体”语言的资源:Form_Login.zh-HK.resx

结果:

其为每种语言生成了一个DLL文件:bin\Debug\zh-HK\项目名.resources.dll

当窗体里的panel或datagridview里的控件都要翻译时,上面那个代码行不通,我改为如下(写在类里不能直接使用Controls集合):

        /// <summary>
        /// Get The Resource for the  language which is selected.
        /// </summary>
        /// <param name="FormType">eg:typeof(Form_Login)</param>
        /// <param name="sLanguageCode">eg:"en","zh-CN","zh-HK"</param>
        private void GetResource(string sLanguageCode, Type FormType, Control.ControlCollection Controls)
        {
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(sLanguageCode);
            System.ComponentModel.ComponentResourceManager res = new ComponentResourceManager(FormType);
            foreach (Control ctl in Controls)
            {
                res.ApplyResources(ctl, ctl.Name);
                string a = ctl.GetType().ToString();
                if (a == "System.Windows.Forms.Panel")
                {
                    foreach (Control item in ctl.Controls)
                    {
                        res.ApplyResources(item, item.Name);
                    }
                }
                if (a == "System.Windows.Forms.DataGridView")
                {
                    DataGridView dgvSend = ctl as DataGridView;
                    foreach (DataGridViewColumn item in dgvSend.Columns)
                    {
                        res.ApplyResources(item, item.Name);
                    }
                }
            }
        }

因为Controls是Form的属性,而且上面没有改到窗体的标题,所以GetResource方法改为


 

        private void GetResource(string sLanguageCode,Form form)
        {
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(sLanguageCode);
            System.ComponentModel.ComponentResourceManager res = new ComponentResourceManager(form.GetType());
            res.ApplyResources(form,"$this");//窗体标题
            foreach (Control ctl in form.Controls)
            {
                res.ApplyResources(ctl, ctl.Name);
                string a = ctl.GetType().ToString();
                if (a == "System.Windows.Forms.Panel")
                {
                    foreach (Control item in ctl.Controls)
                    {
                        res.ApplyResources(item, item.Name);
                    }
                }
                if (a == "System.Windows.Forms.DataGridView")
                {
                    DataGridView dgvSend = ctl as DataGridView;
                    foreach (DataGridViewColumn item in dgvSend.Columns)
                    {
                        res.ApplyResources(item, item.Name);
                    }
                }
            }
        }


 

添加xml文件作为资源文件:

1.添加现有文件XML_Info11.xml

2.属性=>生成操作=>嵌入的资源

3.获取xml元素的值:

        private void LoadResources()
        {
            //get current Assembly object.

            System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();

            //load embedded resource into stream

            String ResourceName = "如何添加及使用资源文件.XML_Info11.xml";//ProjectName.FileName

            System.IO.Stream str = asm.GetManifestResourceStream(ResourceName);
            System.IO.StreamReader reader = new System.IO.StreamReader(str);

            //*** convert stream into XDocument and load in  

            System.Xml.Linq.XDocument xdoc = System.Xml.Linq.XDocument.Parse(reader.ReadToEnd());
            this.Text = xdoc.Element("rss").Element("channel").Element("title").Value.ToString();
        }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值