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下载《====
Winform下的语言国际化,几行代码轻松实现的更多相关文章
- 仅需几行代码 轻松实现ETH代币空投
仅需几行代码 轻松实现ETH代币空投 批量发送以太坊,部署下面的合约,然后往下面的合约打币,就可以分发 ragma solidity ^0.4.21; contract batchTransfer { ...
- Python黑科技:6行代码轻松搭建FTP服务器
Python 黑科技 六行代码轻松搭建个人FTP服务器 什么是FTP服务器? FTP (File Transfer Protocol) 是一个用于客户端与服务器之间文件的协议.利用FTP我们就能做到在 ...
- winform实现QQ聊天气泡200行代码
c# winform实现QQ聊天气泡界面,原理非常简单,通过webKitBrowser(第三方浏览器控件,因为自带的兼容性差)加载html代码实现,聊天界面是一个纯HTML的代码,与QQ的聊天界面可以 ...
- Linux环境下C语言线程创建---简单代码
在Linux环境下用C语言编写线程创建. //file name: pthreadtext.c #include <stdio.h> #include <pthread.h> ...
- 几行代码轻松搞定python的sqlite3的存取
很简单: 存数据: 1.加载sqlite3驱动(只需一行代码) 2.用驱动执行查询语句(只需一行代码) 取数据: 1.加载sqlite3驱动(只需一行代码) 2.用驱动执行查询语句(只需一行代码) 乍 ...
- 几行代码轻松实现PHP文件打包下载zip
<?php //获取文件列表 function list_dir($dir){ $result = array(); if (is_dir($dir)){ $file_dir = scandir ...
- Spring Boot + Freemarker多语言国际化的实现
最近在写一些Web的东西,技术上采用了Spring Boot + Bootstrap + jQuery + Freemarker.过程中查了大量的资料,也感受到了前端技术的分裂,每种东西都有N种实现, ...
- i18next-页面层语言国际化js框架介绍
因为工作需要,最近研究了下网站语言国际化的问题,根据当前项目架构,寻求一种较好的解决方案.首先总结下项目中语言切换实现方式大概有以下几种: 1,一种语言一套页面,如:index_CN.html,ind ...
- 写一个程序,统计自己C语言共写了多少行代码。ver2.00
概要 完成一个程序,作用是统计一个文件夹下面所有文件的代码行数.输入是一个文件夹的绝对路径,输出是代码行数.所以此程序的新特点有两个: 统计某一文件夹下的所有文件: 可以任意指定本机硬盘上任何位置的某 ...
随机推荐
- 分析 org.hibernate.HibernateException: No Session found for current thread
/** * * org.hibernate.HibernateException: No Session found for current thread * 分析:ge ...
- SQL Sever——妙用种子列
/****** Script for SelectTopNRows command from SSMS ******/ SELECT TOP 1000 [OFFRCD_STATUS_ID] ,[OFF ...
- windows 2012R2 上必须要用sharepoint 2013 sp1.
已经确认. 虽然有人讲以下powershell可以帮助安装sharepoint 2013. 不过不是每次都可以的 Import-Module ServerManager Add-WindowsFeat ...
- 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 ...
- APP的案例分析
很多同学有误解,软件项目管理是否就是理论课?或者是几个牛人拼命写代码,其他人打酱油的课?要不然就是学习一个程序语言,搞一个职业培训的课?都不对,软件项目管理有理论,有实践,更重要的是分析,思辨,总结. ...
- [李居丽][다이아몬드][Diamond]
歌词来源:http://music.163.com/#/song?id=484056974 作曲 : Damon Sharpe/JOHN HO/JEFF SHUM [作曲 : Damon Sharpe ...
- Javascript能做什么 不能做什么。
JavaScript可以做什么?用JavaScript可以做很多事情,使网页更具交互性,给站点的用户提供更好,更令人兴奋的体验. JavaScript使你可以创建活跃的用户界面,当用户在页面间导航时向 ...
- Uva1395 POJ3522 Slim Span (最小生成树)
Description Given an undirected weighted graph G, you should find one of spanning trees specified as ...
- mysql 数据库数据迁移 The user specified as a definer ('root'@'%') does not exist 解决方法
从一个数据库数据迁移到本地localhost 程序在调用到数据库的视图时报错,直接在数据库中打开视图时也报错,类似: mysql 1449 : The user specified as a defi ...
- snip
首先明确物体太小太大都不好检测(都从roi的角度来分析): 1.小物体: a.本身像素点少,如果从anchor的点在gt像素内来说,能提取出来的正样本少 b.小物体会出现iou过低.具体来说 ...