Winform下的语言国际化,几行代码轻松实现

   最近做了一些关于winform的项目,需要用到winform的语言国际化,在初使化的时候用起来非常方便。可以参考一下:

   

   核心逻辑:

   

 

 

预览效果演示:

 

OK,以下是核心代码和操作流程

 

一,添加LanguageHelper类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WFInfor
{
    public class LanguageHelper
    {

        #region SetAllLang
        /// <summary>
        /// Set language
        /// </summary>
        /// <param name="lang">language:zh-CN, en-US</param>
        private static void SetAllLang(string lang)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
            Form frm = null;

            string name = "MainForm";

            frm = (Form)Assembly.Load("CameraTest").CreateInstance(name);

            if (frm != null)
            {
                System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager();
                resources.ApplyResources(frm, "$this");
                AppLang(frm, resources);
            }
        }
        #endregion

        #region SetLang
        /// <summary>
        /// 
        /// </summary>
        /// <param name="lang">language:zh-CN, en-US</param>
        /// <param name="form">the form you need to set</param>
        /// <param name="formType">the type of the form </param>
        public static void SetLang(string lang, Form form, Type formType)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
            if (form != null)
            {
                System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(formType);
                resources.ApplyResources(form, "$this");
                AppLang(form, resources);
            }
        }
        #endregion

        #region AppLang for control
        /// <summary>
        ///  loop set the propery of the control
        /// </summary>
        /// <param name="control"></param>
        /// <param name="resources"></param>
        private static void AppLang(Control control, System.ComponentModel.ComponentResourceManager resources)
        {
            if (control is MenuStrip)
            {
                resources.ApplyResources(control, control.Name);
                MenuStrip ms = (MenuStrip)control;
                if (ms.Items.Count > 0)
                {
                    foreach (ToolStripMenuItem c in ms.Items)
                    {
                        AppLang(c, resources);
                    }
                }
            }

            foreach (Control c in control.Controls)
            {
                resources.ApplyResources(c, c.Name);
                AppLang(c, resources);
            }
        }
        #endregion

        #region AppLang for menuitem
        /// <summary>
        /// set the toolscript 
        /// </summary>
        /// <param name="item"></param>
        /// <param name="resources"></param>
        private static void AppLang(ToolStripMenuItem item, System.ComponentModel.ComponentResourceManager resources)
        {
            if (item is ToolStripMenuItem)
            {
                resources.ApplyResources(item, item.Name);
                ToolStripMenuItem tsmi = (ToolStripMenuItem)item;
                if (tsmi.DropDownItems.Count > 0)
                {
                    foreach (ToolStripMenuItem c in tsmi.DropDownItems)
                    {
                        AppLang(c, resources);
                    }
                }
            }
        }
        #endregion
    }
}

 

二,添加对应的资源文件(在文件夹下复制就可以了)

     注意:格式必须是  Form名.语言名.rexs

     我添加了两个,一个中文,一个英文

 

三,修改资源文件,添加需要切换语言的控件

 

 

中文资源文件修改:

 

英文资源文件修改:

 

四,调用代码:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WFInfor
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private void cbxchose_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxchose.SelectedItem.ToString() == "中文")
            {

                LanguageHelper.SetLang("zh-Cn", this, typeof(FrmMain));


            }

            else if (cbxchose.SelectedItem.ToString() == "English")
            {
                LanguageHelper.SetLang("en-Us", this, typeof(FrmMain));
            }
            else
            {
            }
        }
    }

 

 

====》 DEMO下载《====

转载于:https://www.cnblogs.com/codefish/p/4778269.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WinForm中,combobox是一种常用的控件,用于显示和选择不同的选项。而在语言国际化的情景下,combobox也可以用来实现不同语言的选择和切换。 在WinForm实现combobox的语言国际化,可以通过以下步骤来完成: 1. 准备不同语言的资源文件:根据需要支持的语言,准备对应的资源文件,其中包含了不同语言的翻译文本。例如,可以准备一个英文资源文件和一个中文资源文件,分别包含英文和中文的翻译文本。 2. 设置combobox的数据源:将combobox的数据源设置为支持的语言列表。可以在代码中手动添加不同语言的选项,或者从资源文件中动态加载语言选项。 3. 根据用户选择的语言,动态加载对应的翻译文本:当用户选择了某个语言后,可以根据选择结果,动态加载对应语言的翻译文本,并更新combobox中各选项的显示文本。 4. 处理语言切换事件:当用户在combobox中选择了不同的语言后,需要相应地更新整个应用程序的界面文字。可以通过订阅combobox的SelectionChanged事件来实现,在事件处理程序中进行界面文本的更新操作。 总而言之,在combobox winform实现语言国际化的关键是准备不同语言的资源文件,并通过combobox选择不同语言后,动态加载对应的翻译文本并更新应用程序界面。这样,用户就可以在程序运行时选择不同的语言,从而实现语言国际化
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值