C#国际化解决方案策略

在C#项目软件开发中,经常会涉及到窗体界面语言的选择和设置,便于软件能够国际化。今天自己动手实践了一下。

1、新建Windows窗体应用程序,在窗体中放置两个label1和label2,分别命名为中文,英文;

2、在窗体中放置两个RadioButton按钮,分别命名为radioButton1和radioButton2;

3、当选中“中文”(radioButton1)时,将其属性Checked设置为True,

同时将窗体属性中的“Language"设为”中文(简体)“将”Localizable"属性设置为“True”,

然后,双击窗体中的radioButton按钮跳转到该事件的方法,并写入如下代码:

        private void radioButton1_CheckedChanged(object sender, System.EventArgs e)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture =
               new System.Globalization.CultureInfo("zh-CN");
            ApplyResource();
        }

4、同3所示,选中“英文”(radioButton)时,将其属性Checked设置为”True“,

同时将窗体属性中的“Language"设为”英文(美国)“将”Localizable"属性设置为“True”,

然后,将窗体中的相应的设置按钮,比如Lable、Button等的名称命名为英语,

最后补充事件(radioButton2)代码,如下所示:

        private void radioButton2_CheckedChanged(object sender, System.EventArgs e)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture =
                new System.Globalization.CultureInfo("en-US");
            ApplyResource();
        }

5、对于ApplyResource()方法其作用是实现对当前语言界面的动态加载,实现如下所示:

        private void ApplyResource()
        {
            foreach (Control ctl in this.Controls)
            {
                resource.ApplyResources(ctl, ctl.Name);
            }
            this.ResumeLayout(false);
            this.PerformLayout();
            resource.ApplyResources(this, "$this");
        }

6、对于上述操作实现的结构如图:  

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值