最近做了一些关于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下的语言国际化,几行代码轻松实现的更多相关文章

  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. Automapper实现自动映射

    出于安全考虑,在后台与前台进行数据传输时,往往不会直接传输实体模型,而是使用Dto(Data transfer object 数据传输对象),这样在后台往前台传递数据时可以省略不必要的信息,只保留必要 ...

  2. if 里面嵌套一个if&else (我自己又细分了别的条件,加了elif)

    场景: 一个陌生人敲门..... gender = input("你是男的是女的?") if gender == "女": print("请进&quo ...

  3. 第二次作业--APP案例分析

    网易云音乐APP分析 第一部分 调研, 评测 1.APP打开界面简洁,一进入APP便能看到APP推荐的歌单,再使用的时候可以更多的了解新的歌曲 2.APP顶部分为三个板块为音乐管理.音乐推荐(音乐推荐 ...

  4. bin/hdfs dfs命令

    appendToFile Usage: hdfs dfs -appendToFile <localsrc> ... <dst> 追加一个或者多个文件到hdfs制定文件中.也可以 ...

  5. Spring 源代码阅读之声明式事务

    事务控制流程 例如对如下代码进行事务控制 class service1{ method1(){ method2(); } } class service2{ method2(); } 原理:建立一个m ...

  6. ubuntu常见问题排查

    1. ubuntu recovery mode read-only 获取写的权限 1.1. 选择fsck check all file systems 进去直接选择YES就可以了 1.2. 选择roo ...

  7. Java & Groovy & Scala & Kotlin - 20.Switch 与模式匹配

    Overview 本章主要介绍高级条件语句中的 switch 语句以及其增强版的模式匹配. Java 篇 Switch 特点 Java 中 switch 语句功能类似 if,但是 switch 主要用 ...

  8. Odoo图片如何显示

    转载请注明原文地址:https://www.cnblogs.com/cnodoo/p/9281423.html odoo没有专门的图片标签,但是可以通过widget来控制field标签来显示图片内容. ...

  9. Js apply()使用详解

    Js apply方法详解 我在一开始看到javascript的函数apply和call时,非常的模糊,看也看不懂,最近在网上看到一些文章对apply方法和call的一些示例,总算是看的有点眉目了,在这 ...

  10. 很清晰的解读i2c协议

    很清晰的解读i2c协议 转载:http://dpinglee.blog.163.com/blog/static/14409775320112239374615/ 1.I2C协议 2条双向串行线,一条数 ...