C# winform 多语言(json)方式实现

前后对比

shell 复制代码
使用nuget json工具包


1.总体思路

创建对应的json字典对照表

json 复制代码
{
"测试":"Test",
"语言":"Language",
"设置":"Set",
"中文(默认)":"Chinese (default)",
"英文":"English",
"标签测试":"Label Test",
"主界面-标题":"Main Title",
"组合框":"group box",
"选择框":"checkBox",
"单选按钮":"radioButton",
"列标题1":"ColumnHeader1",
"列标题2":"ColumnHeader2",
}

加载对应 json文件

csharp 复制代码
/// <summary>
/// 当前项目文件夹Debug\Language\参数文件夹
/// </summary>
/// <param name="language">配置文件所在文件夹名</param>
public static void LoadLanguage(TransType transType, string language = "")
{
    if (string.IsNullOrEmpty(language))
    {
        language = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
    }

    resources = new Dictionary<string, string>();

    string dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, 
                              string.Format("Language/{0}.json", language));
    if (File.Exists(dir))
    {
        LoadFile(transType, dir);
    }
}

/// <summary>
/// 配置文件加载
/// </summary>
/// <param name="path">配置文件绝对路径(包括文件本身)</param>
public static void LoadFile(TransType transType, string path)
{
    var content = File.ReadAllText(path, Encoding.UTF8);
    if (!string.IsNullOrEmpty(content))
    {
        var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(content);
        foreach (string key in dict.Keys)
        {
            if(transType == TransType.cn_en)
            {
                // 中文转英文
                if (!resources.ContainsKey(key))
                {
                    resources.Add(key, dict[key]);
                }
                else
                    resources[key] = dict[key];
            }
            else if(transType == TransType.en_cn)
            {
                // 英文转中文
                var value = dict[key];
                if (!resources.ContainsKey(value))
                {
                    resources.Add(value, key);
                }
                else
                    resources[value] = key;
            }
        }
    }
}

设置窗口语言

csharp 复制代码
/// <summary>
/// 遍历翻译 窗体或控件及其子控件
/// </summary>
/// <param name="control">需要翻译的控件或窗体</param>
public static void InitLanguage(Control control)
{
    SetControlLanguage(control);
    // 循环所有控件
    foreach (Control ctrl in control.Controls)
    {
        InitLanguage(ctrl);
    }

    //工具栏 或者菜单动态构建窗体或者控件的时候,重新对子控件进行处理
    control.ControlAdded += (sender, e) =>
    {
        InitLanguage(e.Control);
    };
}

/// <summary>
/// 控件及子控件翻译
/// </summary>
/// <param name="control">需要翻译的控件</param>
public static void SetControlLanguage(Control control)
{
    if (control is ComboBox)
    {
        // ComboBox 转换
        ComboBox combox = control as ComboBox;
        int curSelect = combox.SelectedIndex;
        string[] NewItems = new string[combox.Items.Count];
        for (int i = 0; i < combox.Items.Count; i++)
        {
            if (resources.ContainsKey(combox.Items[i].ToString()))
            {
                NewItems[i] = resources[combox.Items[i].ToString()];
            }
            else
                NewItems[i] = combox.Items[i].ToString();
        }

        combox.Text = (resources.ContainsKey(combox.Text)) ? resources[combox.Text] : combox.Text;

        combox.Items.Clear();
        combox.Items.AddRange(NewItems);

        combox.SelectedIndex = curSelect;
    }
    
    else if (control is TreeView)
    {
        //control is 其他控件或者特殊控件 如:TreeView
    }
    else if(control is ListView)
    {
        // ListView 标题修改
        var listView = control as ListView;
        for (int i = 0; i < listView.Columns.Count; i++)
        {
            string titleName = listView.Columns[i].Text.ToString();
            if (resources.ContainsKey(titleName))
            {
                listView.Columns[i].Text = resources[titleName];
            }
        }
        
    }
    else
    {
        control.Text = (resources.ContainsKey(control.Text)) ? resources[control.Text] : control.Text;
    }
}

完整代码包

链接:https://pan.baidu.com/s/1FXS5X4O5vBrKykgdSmAj3w

提取码:c7p0

相关推荐
微风中的麦穗5 小时前
【MATLAB】MATLAB R2025a 详细下载安装图文指南:下一代科学计算与工程仿真平台
开发语言·matlab·开发工具·工程仿真·matlab r2025a·matlab r2025·科学计算与工程仿真
2601_949146535 小时前
C语言语音通知API示例代码:基于标准C的语音接口开发与底层调用实践
c语言·开发语言
开源技术6 小时前
Python Pillow 优化,打开和保存速度最快提高14倍
开发语言·python·pillow
学嵌入式的小杨同学6 小时前
从零打造 Linux 终端 MP3 播放器!用 C 语言实现音乐自由
linux·c语言·开发语言·前端·vscode·ci/cd·vim
mftang7 小时前
Python 字符串拼接成字节详解
开发语言·python
jasligea8 小时前
构建个人智能助手
开发语言·python·自然语言处理
kokunka8 小时前
【源码+注释】纯C++小游戏开发之射击小球游戏
开发语言·c++·游戏
Traced back8 小时前
WinForms 线程安全三剑客详解
安全·c#·winform
喵叔哟8 小时前
05-LINQ查询语言入门
c#·solr·linq
云栖梦泽8 小时前
易语言开发从入门到精通:补充篇·网络编程进阶+实用爬虫开发·API集成·代理IP配置·异步请求·防封禁优化
开发语言