c# 多国语言转换问题
多语言转换时 主窗体的多语言转换无效。点击主窗体中的控件,弹出的窗体多语言转换有效。
当主窗体加入MainForm.ja_JP.rexc资源文件时,主窗体就显示日文,添加别国的资源文件,却只显示日语。
请问各位这个问题怎么解决。
[解决办法]
用过一段日语切换代码,楼主请参考:
要主窗口切换语言:Lan.SwitchLang(this);
public class Lan
{
private static List<CultureInfo> areas = new List<CultureInfo>();
private static int current = 0;
/// <summary>
/// 切换语言(默认和日语)
/// </summary>
/// <param name="form"></param>
public static void SwitchLang(Form form)
{
if (areas.Count == 0)
{
areas.Add(Thread.CurrentThread.CurrentCulture);
areas.Add(new CultureInfo("ja"));
}
current = (current + 1) % 2;
Thread.CurrentThread.CurrentUICulture = areas[current];
if (form != null)
{
SetLang(form);
}
}
/// <summary>
/// 设置当前线程的语言与主窗体相同
/// </summary>
public static void SetLang()
{
if (current < areas.Count)
{
Thread.CurrentThread.CurrentUICulture = areas[current];
}
}
public static void SetLang(Form form)
{
ComponentResourceManager resources = new ComponentResourceManager(form.GetType());
AppLang(form, resources);
FormWindowState fws = form.WindowState;
FormWindowState[] ss = new FormWindowState[]{
FormWindowState.Normal,
FormWindowState.Maximized
};
form.WindowState = ss[1 - Array.IndexOf(ss, fws)];
form.WindowState = fws;
}
private static void AppLang(Control control, ComponentResourceManager resources)
{
if (control != null)
{
resources.ApplyResources(control, control.Name);
Debug.WriteLine(string.Format("type={0}, name={1}, text={2}", control.GetType().Name, control.Name, control.Text));
if (control is Form)
{
Form form = control as Form;
resources.ApplyResources(form, "$this");
//foreach (Control c in form.Controls)
//{
// AppLang(c, resources);
//}
AppLang(form.MainMenuStrip, resources);
}
if (control is MenuStrip)
{
MenuStrip ms = control as MenuStrip;
foreach (ToolStripItem tsi in ms.Items)
{
AppLang(tsi, resources);
}
}
if (control.Controls != null)
{
foreach (Control ctl in control.Controls)
{
AppLang(ctl, resources);
}
}
if (control is ListView)
{
ListView lv = control as ListView;
foreach (ColumnHeader ch in lv.Columns)
{
Debug.WriteLine(string.Format("type={0}, name={1}, text={2}", ch.GetType().Name, ch.Name, ch.Text));
resources.ApplyResources(ch, ch.Tag.ToString());
}
}
}
}
private static void AppLang(ToolStripItem item, ComponentResourceManager resources)
{
if (item != null)
{
resources.ApplyResources(item, item.Name);
if (item is ToolStripMenuItem)
{
ToolStripMenuItem tsmi = (ToolStripMenuItem)item;
foreach (ToolStripItem c in tsmi.DropDownItems)
{
if (c is ToolStripSeparator)
{
AppLang(c, resources);
}
}
}
}
}
}