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
概要 完成一个程序,作用是统计一个文件夹下面所有文件的代码行数.输入是一个文件夹的绝对路径,输出是代码行数.所以此程序的新特点有两个: 统计某一文件夹下的所有文件: 可以任意指定本机硬盘上任何位置的某 ...
随机推荐
- 解决由于显卡驱动BUG导致桌面右键卡顿的问题:bat文件源码
@ ECHO OFF%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe&q ...
- C++:sprintf()的用法(转)
转:http://blog.csdn.net/masikkk/article/details/5634886 更多:http://blog.csdn.net/zjuwispersure/article ...
- October 1st 2017 Week 40th Sunday
It's only after we've lost everything that we're free to do anything. 只有我们失去了所有之后我们才能随心而为. After los ...
- Hive学习之路 (十三)Hive分析窗口函数(一) SUM,AVG,MIN,MAX
数据准备 数据格式 cookie1,, cookie1,, cookie1,, cookie1,, cookie1,, cookie1,, cookie1,, 创建数据库及表 create datab ...
- virtualbox+vagrant学习-2(command cli)-23-vagrant version命令
Version 格式: vagrant version options只有 -h, --help 该命令将告诉你已安装的vagrant版本以及当前可用的vagrant最新版本. userdeMacBo ...
- head头的设计:rfcn light-head rfcn
faster缺点:1.不是全卷积,roi出来后是两个fc层,这样会丧失平移变性. 2.每个roi都要单独经过两个fc层,也就是分别进行分类和回归,耗时 也有种说法是roi-pooling后导致平移 ...
- week3编程作业: Logistic Regression中一些难点的解读
%% ============ Part : Compute Cost and Gradient ============ % In this part of the exercise, you wi ...
- POJ 2250 (LCS,经典输出LCS序列 dfs)
题目链接: http://poj.org/problem?id=2250 Compromise Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- Java开发工程师基础Math,Random,Scanner类的使用
Math类的使用(重点) (1)数学操作类:该类没有构造函数,方法均为静态的 (2)掌握内容 A:成员变量 **E:比任何其他值都更接近e(即自然对数的底数)的double值. **PI:比任何其他值 ...
- docker 简要学习
一.Docker的安装和启动 使用环境centos7 yum包更新到最新 sudo yum update 安装需要的软件包,yum-util提供yum-config-manager功能 sudo yu ...