using System; using System.Windows.Forms; namespace GrasscutterTools { internal static class MultiLanguage { public static string DefaultLanguage = "zh-CN"; 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(); } /// /// 加载语言 /// /// 加载语言的窗口 /// 窗口的类型 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); } } /// /// 加载语言 /// /// 控件 /// 语言资源 private static void Loading(Control control, System.ComponentModel.ComponentResourceManager resources) { if (control is MenuStrip strip) { //将资源与控件对应 resources.ApplyResources(control, control.Name); if (strip.Items.Count > 0) { foreach (ToolStripMenuItem c in strip.Items) { //遍历菜单 Loading(c, resources); } } } foreach (Control c in control.Controls) { resources.ApplyResources(c, c.Name); Loading(c, resources); } } /// /// 遍历菜单 /// /// 菜单项 /// 语言资源 private static void Loading(ToolStripMenuItem item, System.ComponentModel.ComponentResourceManager resources) { if (item is ToolStripMenuItem tsmi) { resources.ApplyResources(item, item.Name); if (tsmi.DropDownItems.Count > 0) { foreach (ToolStripMenuItem c in tsmi.DropDownItems) { Loading(c, resources); } } } } } }