winform语言切换C#设计笔记(八)

一、修改当前区域性
string languageName=“zh-CN”;
Thread.CurrentThread.CurrentUICulture = new CultureInfo(languageName);
二、定义语言切换类Mullanguage或方法如下:
private static Dictionary ResManagerDic = new Dictionary();
1、在程序集中查找资源文件方法
private ResourceManagerGetResManager(Control ctr)
{
Assembly assembly = ctr.GetType().Assembly;
string assemblyName = assembly.GetName().Name;
if (!ResourceManagerDic.ContainsKey(assemblyName))
{
ResourceManager res = new ResourceManager(assemblyName + “.Properties.Resources”, assembly);
ResManagerDic .Add(assemblyName, res);
}
return ResManagerDic [assemblyName];
}

2、语言切换时调用相应方法修改label控件显示
2.1
public void ChangeLanguage(ResourceManager resources, string prefixText, Control control)
{
string text = GetResourcesText(resources, prefixText, control.Name);
if (!string.IsNullOrEmpty(text))
{
if (control is Label)
{
control.Text = text;
}
}
}
2.2
private void ChangeContainerLanguage(Control container)
{
ResourceManager resources = GetResourceManager(container);
string resText = resources.GetString(container.Name + FormNameResNameSuffix);
if (resText != null)
{
container.Text = resText;
}
ChangeLanguage(resources, container.Name, container);
}
三、中英俄文区域标识
1.中文:“zh-CN”;
2.英文:“en-US”
3.俄文:“ru-RU”
4.资源文件根据当前区域去选择
winform语言切换C#设计笔记(八)_第1张图片

你可能感兴趣的:(C#,c#,开发语言)