C#WinForm实现多语言切换的示例

  using System;

  using System.Collections.Generic;

  using System.Linq;

  using System.Text;

  using System.Threading.Tasks;

  using System.Windows.Forms;

  namespace CodePrint

  {

  //用于编写与切换语言相关的变量和代码

  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(Logins 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)

  {

  //将资源与控件对应

  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);

  }

  }

  ///

  /// 遍历菜单

  ///

  /// 菜单项

  /// 语言资源

  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);

  }

  }

  }

  }

  }

  }