C#的Winform多语言实现(resx文件)

1. 简体中文

2. 繁体中文

3. 英文

下面子丰介绍一下实现的过程:

1. 为每个窗口创建相应语言的resx文件。子丰以英文为例,右键->添加->新建项->资源文件,文件名为窗口名.en-US,如上面的两个窗口,分别为LoginForm.en-US.resx和PasswordForm.en-US.resx。简体中文为LoginForm.zh-CN.resx和PasswordForm.zh-CN.resx,繁体中文为LoginForm.zh-CHT.resx和PasswordForm.zh-CHT.resx。下面给出LoginForm.en-US.resx文件的截图。

 

2. 在项目的Properties的Settings.settings中添加变量DefaultLanguage,用于保存当前设置的默认语言。当下次启动程序时,会读取该变量,从而将程序的语言设置为上次程序关闭时的语言。

 

3. 创建一个静态类(MultiLanguage.cs)用于编写与切换语言相关的变量和代码。

(1)变量DefaultLanguage,用于保存当前默认语言

 

[csharp] view plain copy
 
//当前默认语言  
public static string DefaultLanguage = "zh-CN";  

 

(2)函数SetDefaultLanguage修改当前默认语言

 

/// <summary>  
/// 修改默认语言  
/// </summary>  
/// <param name="lang">待设置默认语言</param>  
public static void SetDefaultLanguage(string lang)  
{  
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);  
    DefaultLanguage = lang;  
    Properties.Settings.Default.DefaultLanguage = lang;  
    Properties.Settings.Default.Save();  
}  

 

 

(3)函数LoadLanguage用于加载语言或切换语言

 

/// <summary>  
/// 加载语言  
/// </summary>  
/// <param name="form">加载语言的窗口</param>  
/// <param name="formType">窗口的类型</param>  
public static void LoadLanguage(Form form, Type formType)  
{  
    if (form != null)  
    {  
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(formType);  
        resources.ApplyResources(form, "$this");  
        Loading(form, resources);  
    }  
}  
  
/// <summary>  
/// 加载语言  
/// </summary>  
/// <param name="control">控件</param>  
/// <param name="resources">语言资源</param>  
private static void Loading(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)  
            {  
                //遍历菜单  
                Loading(c, resources);  
            }  
        }  
    }  
  
    foreach (Control c in control.Controls)  
    {  
        resources.ApplyResources(c, c.Name);  
        Loading(c, resources);  
    }  
}  
  
/// <summary>  
/// 遍历菜单  
/// </summary>  
/// <param name="item">菜单项</param>  
/// <param name="resources">语言资源</param>  
private static void Loading(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)  
            {  
                Loading(c, resources);  
            }  
        }  
    }  
}  

 

4. 在主窗口的Load事件中读取Properties.Settings.Default.DefaultLanguage,并将ComboBox赋值为当前默认语言,即简体中文、繁体中文或英文。

 

 

private void LoginForm_Load(object sender, EventArgs e)  
{  
    //设置combobox的值  
    string language = Properties.Settings.Default.DefaultLanguage;  
    if (language == "zh-CN")  
    {  
        languageTxt.Text = "简体中文(默认)";  
    }  
    else if (language == "zh-CHT")  
    {  
        languageTxt.Text = "繁體中文";  
    }  
    else if (language == "en-US")  
    {  
        languageTxt.Text = "English";  
    }  
}  

 

5. 在每个窗口的Load事件中调用函数MultiLanguage.LoadLanguage,使窗口在出现时即显示为当前默认语言。

private void PasswordForm_Load(object sender, EventArgs e)  
{  
    //加载语言  
    MultiLanguage.LoadLanguage(this, typeof(PasswordForm));  
}  

 

6. 编写用于切换语言的ComboBox的SelectedIndexChanged事件,使得当用户选择对应的语言时,程序会切换到该语言。

 

//切换语言  
private void languageTxt_SelectedIndexChanged(object sender, EventArgs e)  
{  
    languageTxt.Enabled = false;  
    if (languageTxt.Text == "简体中文(默认)")  
    {  
        //修改默认语言  
        MultiLanguage.SetDefaultLanguage("zh-CN");  
        //对所有打开的窗口重新加载语言  
        foreach (Form form in Application.OpenForms)  
        {  
            LoadAll(form);  
        }  
    }  
    else if (languageTxt.Text == "繁體中文")  
    {  
        //修改默认语言  
        MultiLanguage.SetDefaultLanguage("zh-CHT");  
        //对所有打开的窗口重新加载语言  
        foreach (Form form in Application.OpenForms)  
        {  
            LoadAll(form);  
        }  
    }  
    else if (languageTxt.Text == "English")  
    {  
        //修改默认语言  
        MultiLanguage.SetDefaultLanguage("en-US");  
        //对所有打开的窗口重新加载语言  
        foreach (Form form in Application.OpenForms)  
        {  
            LoadAll(form);  
        }  
    }  
    languageTxt.Enabled = true;  
}  
  
private void LoadAll(Form form)  
{  
    if (form.Name == "LoginForm")  
    {  
        MultiLanguage.LoadLanguage(form, typeof(LoginForm));  
    }  
    else if (form.Name == "PasswordForm")  
    {  
        MultiLanguage.LoadLanguage(form, typeof(PasswordForm));  
    }  
}  

 

转自:http://blog.csdn.net/softimite_zifeng/article/details/54177825

 
posted @ 2017-09-18 14:22  孤夏  阅读(3764)  评论(0编辑  收藏  举报