You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
3.1 KiB
C#
98 lines
3.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Rs.MotionPlat.Commom
|
|
{
|
|
public class ControlLanguageHelper
|
|
{
|
|
public static string DefaultLanguage = "zh-CN";
|
|
/// <summary>
|
|
/// 修改默认语言
|
|
/// </summary>
|
|
/// <param name="lang">待设置默认语言</param>
|
|
public static void SetDefaultLanguage(string lang)
|
|
{
|
|
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
|
|
DefaultLanguage = lang;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 加载语言
|
|
/// </summary>
|
|
/// <param name="form">加载语言的窗口</param>
|
|
/// <param name="formType">窗口的类型</param>
|
|
public static void LoadLanguage(Control 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)
|
|
{
|
|
try
|
|
{
|
|
resources.ApplyResources(c, c.Name);
|
|
Loading(c, resources);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/// <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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|