using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
class LanguageHelp
{
private DataTable GetTxtInfo()
{
DbHelp dh = new DbHelp();
return dh.GetDataTableBySql("SELECT CN,EN,JA FROM dbo.Language");
}
public static string preLanguage = "";
public static string toLanguage = "";
public void GetLanguageText
{
int pre = 0;
int to = 0;
#region
if (preLanguage == toLanguage)
{
return;
}
switch (preLanguage)
{
case "中文":
pre = 0;
break;
case "英文":
pre = 1;
break;
case "日文":
pre = 2;
break;
default:
break;
}
switch (toLanguage)
{
case "中文":
to = 0;
preLanguage = "中文";
break;
case "英文":
to = 1;
preLanguage = "英文";
break;
case "日文":
to = 2;
preLanguage = "日文";
break;
default:
break;
}
#endregion
DataTable dt = GetTxtInfo();
if (dt==null)
{
return;
}
if (typeof(Form) == typeof(T).BaseType || typeof(Control) == typeof(T))
{
Control c = (Control)(object)obj;
if (c.Text != "")
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (c.Text == dt.Rows[i][pre].ToString())
{
c.Text = dt.Rows[i][to].ToString();
break;
}
}
}
foreach (Control c1 in c.Controls)
{
string b = c1.Name;
if (c1.Text != "")
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (c1.Text == dt.Rows[i][pre].ToString())
{
c1.Text = dt.Rows[i][to].ToString();
break;
}
}
}
ChangeLanguage_menue(c1, pre, to, dt);
ChangeLanguage_tools(c1, pre, to, dt);
ChangeLanguage_Conmenu(c1, pre, to, dt);
ChangeLanguage_DGV(c1, pre, to, dt);
ChangeLanguage_Panel(c1, pre, to, dt);
ChangeLanguage_groupbox(c1, pre, to, dt);
}
}
}
private static void ChangeLanguage_menue
{
if (typeof(MenuStrip) == obj.GetType().BaseType || typeof(MenuStrip) == obj.GetType())
{
MenuStrip c = (MenuStrip)(object)obj;
foreach (ToolStripMenuItem c1 in c.Items)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (c1.Text == dt.Rows[i][pre].ToString())
{
c1.Text = dt.Rows[i][to].ToString();
break;
}
}
foreach (ToolStripItem D1 in c1.DropDownItems)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (D1.Text == dt.Rows[i][pre].ToString())
{
D1.Text = dt.Rows[i][to].ToString();
break;
}
}
}
}
}
}
private static void ChangeLanguage_tools
{
if (typeof(ToolStrip) == obj.GetType().BaseType || typeof(ToolStrip) == obj.GetType())
{
ToolStrip c = (ToolStrip)(object)obj;
foreach (ToolStripItem c1 in c.Items)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (c1.Text == dt.Rows[i][pre].ToString())
{
c1.Text = dt.Rows[i][to].ToString();
break;
}
}
}
}
}
private static void ChangeLanguage_Conmenu
{
if (typeof(ContextMenuStrip) == obj.GetType().BaseType || typeof(ContextMenuStrip) == obj.GetType())
{
ContextMenuStrip c = (ContextMenuStrip)(object)obj;
foreach (ToolStripMenuItem c1 in c.Items)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (c1.Text == dt.Rows[i][pre].ToString())
{
c1.Text = dt.Rows[i][to].ToString();
break;
}
}
}
}
}
private static void ChangeLanguage_DGV
{
if (typeof(DataGridView) == obj.GetType().BaseType || typeof(DataGridView) == obj.GetType())
{
DataGridView c = (DataGridView)(object)obj;
foreach (DataGridViewColumn c1 in c.Columns)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (c1.HeaderText == dt.Rows[i][pre].ToString())
{
c1.HeaderText = dt.Rows[i][to].ToString();
break;
}
}
}
}
}
private static void ChangeLanguage_Panel
{
if (typeof(Panel) == obj.GetType().BaseType || typeof(Panel) == obj.GetType())
{
Panel c = (Panel)(object)obj;
foreach (Control c1 in c.Controls)
{
if (c1.Text != "")
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (c1.Text == dt.Rows[i][pre].ToString())
{
c1.Text = dt.Rows[i][to].ToString();
break;
}
}
}
}
}
}
private static void ChangeLanguage_groupbox
{
if (typeof(GroupBox) == obj.GetType().BaseType || typeof(GroupBox) == obj.GetType())
{
GroupBox c = (GroupBox)(object)obj;
foreach (Control c1 in c.Controls)
{
if (c1.Text != "")
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (c1.Text == dt.Rows[i][pre].ToString())
{
c1.Text = dt.Rows[i][to].ToString();
break;
}
}
}
}
}
}
}
DbHelp 类库参考博客DbHelp类 https://blog.csdn.net/fanwenhu/article/details/100030071
ic 参考ini读取博客 https://blog.csdn.net/fanwenhu/article/details/100030208
使用方法:
IniConfig ic;
LanguageHelp lh;
private void Form1_Load(object sender, EventArgs e)
{
string setlanguage = ic.IniReadValue("语言设置", "语言");
LanguageHelp.preLanguage = "中文";
LanguageHelp.toLanguage = setlanguage;
lh = new LanguageHelp();
lh.GetLanguageText(this);
switch (setlanguage)
{
case "中文":
CN_ToolStripMenuItem.Checked = true;
break;
case "英文":
EN_ToolStripMenuItem.Checked = true;
break;
case "日文":
JA_ToolStripMenuItem.Checked = true;
break;
default:
break;
}
}
切换界面文字
private void CN_ToolStripMenuItem_Click(object sender, EventArgs e)
{
ic.IniWriteValue("语言设置", "语言", "中文");
LanguageHelp lh = new LanguageHelp();
LanguageHelp.toLanguage = "中文";
lh.GetLanguageText(this);
JA_ToolStripMenuItem.Checked = false;
CN_ToolStripMenuItem.Checked = true;
EN_ToolStripMenuItem.Checked = false;
}
//备注 三种文字要维护到数据库中