Winform下的语言国际化,几行代码轻松实现
最近做了一些关于winform的项目,需要用到winform的语言国际化,在初使化的时候用起来非常方便。可以参考一下:
核心逻辑:
预览效果演示:
OK,以下是核心代码和操作流程
一,添加LanguageHelper类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | 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
我添加了两个,一个中文,一个英文
三,修改资源文件,添加需要切换语言的控件
中文资源文件修改:
英文资源文件修改:
四,调用代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | 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下载《====
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?