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
概要 完成一个程序,作用是统计一个文件夹下面所有文件的代码行数.输入是一个文件夹的绝对路径,输出是代码行数.所以此程序的新特点有两个: 统计某一文件夹下的所有文件: 可以任意指定本机硬盘上任何位置的某 ...
随机推荐
- 铁乐学python_day03-作业
1.有变量name = "aleX leNb" 完成如下操作: 移除name变量对应的值两边的空格,并输出处理结果 n1 = name.strip() print(n1) 结果:a ...
- ZT pthread_cleanup_push()/pthread_cleanup_pop()的详解
pthread_cleanup_push()/pthread_cleanup_pop()的详解 分类: Linux 2010-09-28 16:02 1271人阅读 评论(1) 收藏 举报 async ...
- 【模块化】 RequireJS入门教程总结与推荐
之所以学习RequireJS,肯定对 模块化有一定的理解.这里有几篇学习 RequireJS的文章,推荐给大家去学习. Javascript模块化编程(一):模块的写法 Javascript模块化编程 ...
- php添加mongo模块
可以从 http://pecl.php.net/package/mongo 下载目前的stable稳定版 我添加的是mongo-1.5.2.tgz # wget http://pecl.php.net ...
- BZOJ5418:[NOI2018]屠龙勇士(exCRT,exgcd,set)
Description Input Output Sample Input 23 33 5 74 6 107 3 91 9 10003 23 5 64 8 71 1 11 1 Sample Outpu ...
- 【openjudge】【递推】例3.6 过河卒(Noip2002)
[题目描述] 棋盘上A点有一个过河卒,需要走到目标B点.卒行走的规则:可以向下.或者向右.同时在棋盘上的某一点有一个对方的马(如C点),该马所在的点和所有跳跃一步可达的点称为对方马的控制点,如图3-1 ...
- 4521: [Cqoi2016]手机号码
4521: [Cqoi2016]手机号码 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 1030 Solved: 609 [Submit][Statu ...
- [修正] Firemonkey Windows & macOS 平台下 Edit & Memo 中文输入后会取消原选取文字的 BUG
问题:Firemonkey Windows & macOS 平台下 Edit & Memo 中文输入后会取消原选取文字的 BUG 适用版本:Delphi 10.1.2 & 10 ...
- 小米路由器设置端口转发远程登录WEB管理页及安装MT工具箱
1. 将小米路由器ROM升级到开发版 这一点是必须的,如果是稳定版是不行的 2. 获取高级管理权限 再次确认当前使用的是开发版ROM 到这个网址http://d.miwifi.com/rom/ssh ...
- Scala(二):元组、数组、映射
元组:Tuple,不同类型值的聚集.将固定数量的项目组合在一起,以便它们可以作为一个整体传递. 与数组或列表不同,元组可以容纳不同类型的对象,但它们也是不可变的.元祖的实际类型取决于它的分量的类型,比 ...