.Net应用程序的国际化、本地化和多语言支持

    其实在.Net中,Microsoft已经为开发全球通用的应用程序提供了广泛的支持。在VS2005中,创建一个Windows应用程序,可以在设计Form时,看到属性Language,这就为程序提供了这方面的支持。只要将Language属性设为不种语言分别设计,程序便可以在不同语言的系统中正确显示了。另外只要更改系统的语言环境,程序就可以显示不同的语言了。但这并不能让人满意,往往我们都需要让程序在不更改系统语言环境的前提下,进行多语言的切换。这就需要开发人员在程序中编写部分代码了。

    在.NET Framework中,可以在程序中更改 Thread.CurrentThread.CurrentCulture 来改变程序的语言环境。这里的CurrentCulture 是一个 CultureInfo 类,CultureInfo 类呈现区域性特定的信息,如关联的语言、子语言、国家/地区、日历和区域性约定。这样就可以很方便的实现程序在不同的语言之间自由的切换了。

    而在.NET Compact Framework中,Thread.CurrentThread.CurrentCulture 却是不可更改的,这就让开发人员在设备操作系统开发的程序时,要实现多语言就麻烦了一点。其实在.Net中,是用资源文件来保存这些不同语言的数据的,我们只需要在程序中读取资源文件中特定区域性资源数据就可以了。在.Net中,Microsoft提供了ResourceManager

ComponentResourceManager 类来访问区域性特定资源。在设备应用程序中,设计了不同语言的界面后,就已经拥有了不同语言的资源文件了,如图:

image image image image

其中Form1.resx、Form1.en.resx、Form1.zh-CHS.resx三个是资源文件,分别对应了默认语言、英语、简体中文这三种语言环境程序所加载的对应数据。可以打开设计文件Form1.Designer.cs看一下VS是怎么处理的。代码如下:

       /// <summary>
       /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.label1 = new System.Windows.Forms.Label();
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // label1
            // 
            resources.ApplyResources(this.label1, "label1");
            this.label1.Name = "label1";
            // 
            // comboBox1
            // 
            this.comboBox1.Items.Add(resources.GetString("comboBox1.Items"));
            this.comboBox1.Items.Add(resources.GetString("comboBox1.Items1"));
            this.comboBox1.Items.Add(resources.GetString("comboBox1.Items2"));
            resources.ApplyResources(this.comboBox1, "comboBox1");
            this.comboBox1.Name = "comboBox1";
            // 
            // button1
            // 
            resources.ApplyResources(this.button1, "button1");
            this.button1.Name = "button1";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
            resources.ApplyResources(this, "$this");
            this.Controls.Add(this.button1);
            this.Controls.Add(this.comboBox1);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.ResumeLayout(false);
        }

 

 

于是我们就可以自己写代码来实现多种语言之间切换了,下面是一段简单代码:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 切换语言显示
        /// </summary>
        /// <param name="name">区域性名称</param>
        public void ChangeLanguage(string name)
        {
            ComponentResourceManager resources = new ComponentResourceManager(this.GetType());
            CultureInfo ci = null;
            if (!string.IsNullOrEmpty(name)) ci = new CultureInfo(name);

            this.SuspendLayout();
            resources.ApplyResources(this.label1, "label1", ci);
            resources.ApplyResources(this.comboBox1, "comboBox1", ci);
            this.comboBox1.Items.Clear();
            this.comboBox1.Items.Add(resources.GetString("comboBox1.Items", ci));
            this.comboBox1.Items.Add(resources.GetString("comboBox1.Items1", ci));
            this.comboBox1.Items.Add(resources.GetString("comboBox1.Items2", ci));
            resources.ApplyResources(this.button1, "button1", ci);
            resources.ApplyResources(this, "$this", ci);
            this.ResumeLayout(false);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ChangeLanguage(this.comboBox1.Text);
        }
    }

在整个应用程序中,如何实现比较好,那就看具体情况了。

 

参考资料: 对应用程序进行全球化和本地化.NET Compact Framework 中的编码和本地化

posted on 2009-10-16 15:30  Lotrix  阅读(2194)  评论(0编辑  收藏  举报

导航