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

核心逻辑:

预览效果演示:

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

一,添加LanguageHelper类

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8.  
  9. namespace WFInfor
  10. {
  11. public class LanguageHelper
  12. {
  13.  
  14. #region SetAllLang
  15. /// <summary>
  16. /// Set language
  17. /// </summary>
  18. /// <param name="lang">language:zh-CN, en-US</param>
  19. private static void SetAllLang(string lang)
  20. {
  21. System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
  22. Form frm = null;
  23.  
  24. string name = "MainForm";
  25.  
  26. frm = (Form)Assembly.Load("CameraTest").CreateInstance(name);
  27.  
  28. if (frm != null)
  29. {
  30. System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager();
  31. resources.ApplyResources(frm, "$this");
  32. AppLang(frm, resources);
  33. }
  34. }
  35. #endregion
  36.  
  37. #region SetLang
  38. /// <summary>
  39. ///
  40. /// </summary>
  41. /// <param name="lang">language:zh-CN, en-US</param>
  42. /// <param name="form">the form you need to set</param>
  43. /// <param name="formType">the type of the form </param>
  44. public static void SetLang(string lang, Form form, Type formType)
  45. {
  46. System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
  47. if (form != null)
  48. {
  49. System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(formType);
  50. resources.ApplyResources(form, "$this");
  51. AppLang(form, resources);
  52. }
  53. }
  54. #endregion
  55.  
  56. #region AppLang for control
  57. /// <summary>
  58. /// loop set the propery of the control
  59. /// </summary>
  60. /// <param name="control"></param>
  61. /// <param name="resources"></param>
  62. private static void AppLang(Control control, System.ComponentModel.ComponentResourceManager resources)
  63. {
  64. if (control is MenuStrip)
  65. {
  66. resources.ApplyResources(control, control.Name);
  67. MenuStrip ms = (MenuStrip)control;
  68. if (ms.Items.Count > 0)
  69. {
  70. foreach (ToolStripMenuItem c in ms.Items)
  71. {
  72. AppLang(c, resources);
  73. }
  74. }
  75. }
  76.  
  77. foreach (Control c in control.Controls)
  78. {
  79. resources.ApplyResources(c, c.Name);
  80. AppLang(c, resources);
  81. }
  82. }
  83. #endregion
  84.  
  85. #region AppLang for menuitem
  86. /// <summary>
  87. /// set the toolscript
  88. /// </summary>
  89. /// <param name="item"></param>
  90. /// <param name="resources"></param>
  91. private static void AppLang(ToolStripMenuItem item, System.ComponentModel.ComponentResourceManager resources)
  92. {
  93. if (item is ToolStripMenuItem)
  94. {
  95. resources.ApplyResources(item, item.Name);
  96. ToolStripMenuItem tsmi = (ToolStripMenuItem)item;
  97. if (tsmi.DropDownItems.Count > 0)
  98. {
  99. foreach (ToolStripMenuItem c in tsmi.DropDownItems)
  100. {
  101. AppLang(c, resources);
  102. }
  103. }
  104. }
  105. }
  106. #endregion
  107. }
  108. }

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

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

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

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

中文资源文件修改:

英文资源文件修改:

四,调用代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace WFInfor
  13. {
  14. public partial class FrmMain : Form
  15. {
  16. public FrmMain()
  17. {
  18. InitializeComponent();
  19. }
  20.  
  21. private void cbxchose_SelectedIndexChanged(object sender, EventArgs e)
  22. {
  23. if (cbxchose.SelectedItem.ToString() == "中文")
  24. {
  25.  
  26. LanguageHelper.SetLang("zh-Cn", this, typeof(FrmMain));
  27.  
  28. }
  29.  
  30. else if (cbxchose.SelectedItem.ToString() == "English")
  31. {
  32. LanguageHelper.SetLang("en-Us", this, typeof(FrmMain));
  33. }
  34. else
  35. {
  36. }
  37. }
  38. }

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

Winform下的语言国际化,几行代码轻松实现的更多相关文章

  1. 仅需几行代码 轻松实现ETH代币空投

    仅需几行代码 轻松实现ETH代币空投 批量发送以太坊,部署下面的合约,然后往下面的合约打币,就可以分发 ragma solidity ^0.4.21; contract batchTransfer { ...

  2. Python黑科技:6行代码轻松搭建FTP服务器

    Python 黑科技 六行代码轻松搭建个人FTP服务器 什么是FTP服务器? FTP (File Transfer Protocol) 是一个用于客户端与服务器之间文件的协议.利用FTP我们就能做到在 ...

  3. winform实现QQ聊天气泡200行代码

    c# winform实现QQ聊天气泡界面,原理非常简单,通过webKitBrowser(第三方浏览器控件,因为自带的兼容性差)加载html代码实现,聊天界面是一个纯HTML的代码,与QQ的聊天界面可以 ...

  4. Linux环境下C语言线程创建---简单代码

    在Linux环境下用C语言编写线程创建. //file name: pthreadtext.c #include <stdio.h> #include <pthread.h> ...

  5. 几行代码轻松搞定python的sqlite3的存取

    很简单: 存数据: 1.加载sqlite3驱动(只需一行代码) 2.用驱动执行查询语句(只需一行代码) 取数据: 1.加载sqlite3驱动(只需一行代码) 2.用驱动执行查询语句(只需一行代码) 乍 ...

  6. 几行代码轻松实现PHP文件打包下载zip

    <?php //获取文件列表 function list_dir($dir){ $result = array(); if (is_dir($dir)){ $file_dir = scandir ...

  7. Spring Boot + Freemarker多语言国际化的实现

    最近在写一些Web的东西,技术上采用了Spring Boot + Bootstrap + jQuery + Freemarker.过程中查了大量的资料,也感受到了前端技术的分裂,每种东西都有N种实现, ...

  8. i18next-页面层语言国际化js框架介绍

    因为工作需要,最近研究了下网站语言国际化的问题,根据当前项目架构,寻求一种较好的解决方案.首先总结下项目中语言切换实现方式大概有以下几种: 1,一种语言一套页面,如:index_CN.html,ind ...

  9. 写一个程序,统计自己C语言共写了多少行代码。ver2.00

    概要 完成一个程序,作用是统计一个文件夹下面所有文件的代码行数.输入是一个文件夹的绝对路径,输出是代码行数.所以此程序的新特点有两个: 统计某一文件夹下的所有文件: 可以任意指定本机硬盘上任何位置的某 ...

随机推荐

  1. 分析 org.hibernate.HibernateException: No Session found for current thread

    /**      *      * org.hibernate.HibernateException: No Session found for current thread      * 分析:ge ...

  2. SQL Sever——妙用种子列

    /****** Script for SelectTopNRows command from SSMS ******/ SELECT TOP 1000 [OFFRCD_STATUS_ID] ,[OFF ...

  3. windows 2012R2 上必须要用sharepoint 2013 sp1.

    已经确认. 虽然有人讲以下powershell可以帮助安装sharepoint 2013. 不过不是每次都可以的 Import-Module ServerManager Add-WindowsFeat ...

  4. November 26th 2016 Week 48th Saturday

    All growth is a leap in the dark. 所有的成长都是黑暗中的一跃. But it is a dark and long night, I can't see any st ...

  5. APP的案例分析

    很多同学有误解,软件项目管理是否就是理论课?或者是几个牛人拼命写代码,其他人打酱油的课?要不然就是学习一个程序语言,搞一个职业培训的课?都不对,软件项目管理有理论,有实践,更重要的是分析,思辨,总结. ...

  6. [李居丽][다이아몬드][Diamond]

    歌词来源:http://music.163.com/#/song?id=484056974 作曲 : Damon Sharpe/JOHN HO/JEFF SHUM [作曲 : Damon Sharpe ...

  7. Javascript能做什么 不能做什么。

    JavaScript可以做什么?用JavaScript可以做很多事情,使网页更具交互性,给站点的用户提供更好,更令人兴奋的体验. JavaScript使你可以创建活跃的用户界面,当用户在页面间导航时向 ...

  8. Uva1395 POJ3522 Slim Span (最小生成树)

    Description Given an undirected weighted graph G, you should find one of spanning trees specified as ...

  9. mysql 数据库数据迁移 The user specified as a definer ('root'@'%') does not exist 解决方法

    从一个数据库数据迁移到本地localhost 程序在调用到数据库的视图时报错,直接在数据库中打开视图时也报错,类似: mysql 1449 : The user specified as a defi ...

  10. snip

    首先明确物体太小太大都不好检测(都从roi的角度来分析):   1.小物体: a.本身像素点少,如果从anchor的点在gt像素内来说,能提取出来的正样本少    b.小物体会出现iou过低.具体来说 ...