• 【原创】:WinForm使用XML动态加载多语言


     

     

    View Code
    <?xml version="1.0" encoding="utf-8" ?>
    <Language>
    <!--报告录入 -->
    <ReportInputFrm value="Report Input">
    <grpBasic>Basic Info</grpBasic>
    <lblSampleDate>Date</lblSampleDate>
    <lblSampleID>ID</lblSampleID>
    <lblPatientType>PType</lblPatientType>
    <lblCHNO>CHNo</lblCHNO>
    <lblName>Name</lblName>
    <lblSex>Sex</lblSex>
    <lblAge>Age</lblAge>
    <lblChargetype>Type</lblChargetype>
    <lblSection>Section</lblSection>
    <lblBedNo>BedNo</lblBedNo>
    <lblSampletype>SType</lblSampletype>
    <lblSendDoctor>Send Dr</lblSendDoctor>
    <lblRequestNo>REQ No</lblRequestNo>
    <lblSendTime>SendTime</lblSendTime>
    <lblReportDate>RPT Date</lblReportDate>
    <lblTestMedic>Test Dr</lblTestMedic>
    <lblCheckMedic>CHK Dr</lblCheckMedic>
    <lblClincDiagnose>CLI DIAG</lblClincDiagnose>
    <lblRemark>Remark</lblRemark>
    <lblState>State</lblState>
    <lblOthers>Others</lblOthers>

    <btnAdd>Add</btnAdd>
    <btnDel>Del</btnDel>
    <btnSave>Save</btnSave>
    <btnAudit>Audit</btnAudit>
    <btnConfig>Options</btnConfig>
    <btnLast>Last↑</btnLast>
    <btnNext>Next↓</btnNext>
    <btnPreView>PreView</btnPreView>
    <btnPrint>Print</btnPrint>

    <grpPatientCheckItem>Patient Check Item</grpPatientCheckItem>
    <index>Id</index>
    <ckItem>CHK Item</ckItem>
    <ckResult>CHK Result</ckResult>
    <Range>Range</Range>

    <!--dgvCheckItem-->
    <AssayAb>AssayAb</AssayAb>
    <newFemaleRages>Range</newFemaleRages>


    <!--tabWindows-->
    <tpList>List</tpList>
    <tplistRefrsh>Refresh</tplistRefrsh>
    <tblistDel>Delete</tblistDel>
    <!--dgvPatientList-->
    <SampleID>SampleId</SampleID>
    <PatientName>Name</PatientName>
    <CHNO>CHONO</CHNO>
    <SampleType></SampleType>
    <BedNO>BedNO</BedNO>
    <SectionClass></SectionClass>
    <Sex>Sex</Sex>
    <Age>Age</Age>
    <AgeType>AgeType</AgeType>
    <Sample></Sample>
    <TestMedic>Test Dr</TestMedic>
    <PrintSign>PrintSign</PrintSign>
    <CheckSign></CheckSign>

    <tabCompare>Compare</tabCompare>

    <tabBatchInput>Batch Input</tabBatchInput>
    <inputModels>Input</inputModels>
    <autoInput>Auto</autoInput>

    <tabCommuncation>Communcation</tabCommuncation>

    <!--dgvPatientList-->

    <grpCheckItem>Check Item Fro Add</grpCheckItem>
    </ReportInputFrm>

    <!-- 检验结果批次调整 -->
    <BatchAdjust value="Batch Resize Results">
    <lblSampleDate>SampleDate</lblSampleDate>
    <lblSampleId>Sample No</lblSampleId>
    <lblCheckItem>Check Items</lblCheckItem>
    <lblExpress>Express</lblExpress>
    <kbtnAdjust>Resize</kbtnAdjust>
    <kbtnSave>Save</kbtnSave>
    <kbtnRead>Read</kbtnRead>
    <kbntDel>Del</kbntDel>
    <kbtnDelAll>Del All</kbtnDelAll>
    <kbtnClose>Close</kbtnClose>
    <lblWaring>Attention:Resize Express should contains Result As R,for exapmle(result*2):R*2</lblWaring>
    <SampleId>Sample Id</SampleId>
    <Conc>Result</Conc>
    </BatchAdjust>

    <!--批次审核 -->
    <BatchAuditFrm value="Batch Audit">
    <grpSearch>Search</grpSearch>
    <lblSampleDate>Date</lblSampleDate>
    <lblSampleId>Id</lblSampleId>
    <lblTestDr>Test Dr</lblTestDr>
    <lblAudit>Audit</lblAudit>
    <btnxQuery>Search</btnxQuery>
    <btnxAudit>Audit</btnxAudit>
    <btnxNotPass>Free Audit</btnxNotPass>
    <btnxClose>Close</btnxClose>
    <CheckSign>CheckSign</CheckSign>
    <SampleDate>Test Date</SampleDate>
    <SampleID>Sample Id</SampleID>
    <CHNO>CHNO</CHNO>
    <PatientName>Name</PatientName>
    <Sex>Sex</Sex>
    <Age>Age</Age>
    <TestMedic>Test Dr</TestMedic>
    <CheckMedic>Check Dr</CheckMedic>
    <SectionClass></SectionClass>
    <BedNO>Bed No</BedNO>
    <SendDoctor>Send Dr</SendDoctor>
    <SampleType>P Type</SampleType>
    <ClinicDiagnose>Diagnose</ClinicDiagnose>

    <Assay>Assay</Assay>
    <AssayName>Name</AssayName>
    <Conc>Result</Conc>
    <newFemaleRages>Rages</newFemaleRages>
    <Units>Unit</Units>

    </BatchAuditFrm>

    <!-- 批次输入 -->
    <BatchInputFrm value="Batch Input">
    <lblDate>Date</lblDate>
    <lblSampleId>Id</lblSampleId>
    <grpbmaintenance>Template Maintenance</grpbmaintenance>
    <lblInputTemplate>Input Template</lblInputTemplate>
    <btnSaveModel>Save</btnSaveModel>
    <btnSaveWithValue>Save With Val</btnSaveWithValue>
    <btnDelModel>Delete</btnDelModel>
    <kbtnAdd>Add</kbtnAdd>
    <btnxSave>Save</btnxSave>
    <btnxDel>Delete</btnxDel>
    <btnxClose>Cloase</btnxClose>

    <groupBox3>Add Items</groupBox3>
    <AssayAb>AssayAb</AssayAb>
    <newFemaleRages>Rages</newFemaleRages>
    <RangeMin>Range Min</RangeMin>
    <RangeMax>Range Max</RangeMax>
    <AssayChineseName>CHName</AssayChineseName>

    <!--dgvCkItem-->
    <ckItem>Item</ckItem>
    <Conc>Result</Conc>

    <lblRemark>
    Attention:
    标本号可以输入如:1,2,5,5-8等形式。(注:标点符号必须为英文字符)
    </lblRemark>
    </BatchInputFrm>
    </Language>

      

     xml格式类似上面编写,每个窗体为一个子节点。

    然后编写一个公共类,用于根据窗体名称读取控件并显示相应文字

    View Code
    publicclass LanguageSeting
    {
    publicstring chLanguage ="Chinese";
    publicstring strLanguage = ConfigurationManager.AppSettings["Language"];
    string languagePath = ConfigurationManager.AppSettings["English"];
    XmlDocument doc
    =new XmlDocument();

    public LanguageSeting()
    {
    doc.Load(languagePath);
    }

    ///<summary>
    /// 获取窗体控件对应的text
    ///</summary>
    ///<param name="frmName"></param>
    ///<param name="ctlName"></param>
    ///<returns></returns>
    publicstring GetFrmCtlLanguage(string frmName, string ctlName)
    {
    XmlNode rootNode
    = doc.SelectSingleNode("Language");
    XmlNode frmNode
    = rootNode.SelectSingleNode(frmName);
    foreach (XmlNode ctlNode in frmNode.ChildNodes)
    {
    if (ctlNode.Name.Equals(ctlName))
    return ctlNode.InnerText;
    }
    return"";
    }

    ///<summary>
    /// 获取窗体的text
    ///</summary>
    ///<param name="frmName"></param>
    ///<returns></returns>
    publicstring GetFrmLanguage(string frmName)
    {
    XmlNode rootNode
    = doc.SelectSingleNode("Language");
    XmlNode frmNode
    = rootNode.SelectSingleNode(frmName);
    return frmNode.Attributes[0].Value;
    }

    ///<summary>
    /// 设置DataGridview的文字显示
    ///</summary>
    ///<param name="ctlGrid"></param>
    ///<param name="frmName"></param>
    publicvoid SetDataGridView(Control ctlGrid, string frmName)
    {
    string tempStr ="";
    foreach (DataGridViewColumn col in (ctlGrid as DataGridView).Columns)
    {
    tempStr
    =this.GetFrmCtlLanguage(frmName, col.Name);
    if (tempStr.Length >0)
    col.HeaderText
    = tempStr;
    }
    }

    ///<summary>
    /// 加载语言设置
    ///</summary>
    publicvoid LoadLanuageSettings(Form frm)
    {
    string tempStr ="";
    //如果当前语言不是中文
    if (!this.strLanguage.Equals(this.chLanguage))
    {
    frm.Text
    =this.GetFrmLanguage(frm.Name);
    foreach (Control ctl in frm.Controls)
    {
    tempStr
    =this.GetFrmCtlLanguage(frm.Name, ctl.Name);
    if (tempStr.Length >0)
    ctl.Text
    = tempStr;
    switch (ctl.GetType().Name)
    {
    case"GroupBox":
    {
    // ctl.Font = new Font("Arial", 10);
    foreach (Control childCtl in ctl.Controls)
    {

    if (childCtl is DataGridView)
    {
    this.SetDataGridView(childCtl, frm.Name);
    }
    else
    {
    tempStr
    =this.GetFrmCtlLanguage(frm.Name, childCtl.Name);
    if (tempStr.Length >0)
    {
    childCtl.Text
    = tempStr;
    }
    }

    }
    }
    break;
    case"TabControl":
    {
    foreach (Control tabCtl in (ctl as TabControl).TabPages)
    {

    tempStr
    =this.GetFrmCtlLanguage(frm.Name, tabCtl.Name);
    tabCtl.Text
    = tempStr;
    foreach (Control childCtl in (tabCtl as TabPage).Controls)
    {
    if (childCtl is DataGridView || childCtl is ComponentFactory.Krypton.Toolkit.KryptonDataGridView)
    {
    this.SetDataGridView(childCtl, frm.Name);
    }
    elseif (childCtl is GroupBox)
    {
    foreach (Control grpCtl in childCtl.Controls)
    {
    grpCtl.Text
    =this.GetFrmCtlLanguage(frm.Name, grpCtl.Name);
    }
    }
    else
    childCtl.Text
    =this.GetFrmCtlLanguage(frm.Name, childCtl.Name);
    }

    }
    }
    break;
    case"Panel":
    {
    foreach (Control pnlCtl in (ctl as Panel).Controls)
    {
    tempStr
    =this.GetFrmCtlLanguage(frm.Name, pnlCtl.Name);
    pnlCtl.Text
    = tempStr;
    }
    }
    break;
    case"KryptonPanel":
    gotocase"Panel";
    case"DataGridView":
    this.SetDataGridView(ctl, frm.Name);
    break;
    case"KryptonDataGridView":
    gotocase"DataGridView";
    }


    }
    }
    }

    ///<summary>
    /// 导出xml节点
    ///</summary>
    ///<param name="frm"></param>
    publicvoid WriteControlToXml(Form frm)
    {
    StringBuilder sb
    =new StringBuilder("");
    sb.Append(
    "\r\n<"+ frm.Name +" value=\""+ frm.Name +"\">\r\n");
    foreach (Control ctl in frm.Controls)
    {
    if (ctl.Text.Length >0)
    {
    sb.Append(
    "<"+ ctl.Name +">"+ ctl.Text +"</"+ ctl.Name +">");
    sb.Append(
    "\r\n");
    }
    switch (ctl.GetType().Name)
    {
    case"GroupBox":
    {
    foreach (Control childCtl in ctl.Controls)
    {

    if (childCtl is DataGridView)
    {
    foreach (DataGridViewColumn col in (childCtl as DataGridView).Columns)
    {
    sb.Append(
    "<"+ col.Name +">"+ col.HeaderText +"</"+ col.Name +">"); sb.Append("\r\n");
    }
    }
    elseif (childCtl.Text.Length >0&&!childCtl.GetType().Name.Equals("TextBox") &&!childCtl.GetType().Name.Equals("DateTimePicker"))
    {
    sb.Append(
    "<"+ childCtl.Name +">"+ childCtl.Text +"</"+ childCtl.Name +">"); sb.Append("\r\n");
    }

    }
    }
    break;
    case"TabControl":
    {
    foreach (Control tabCtl in (ctl as TabControl).TabPages)
    {
    sb.Append(
    "<"+ tabCtl.Name +">"+ tabCtl.Text +"</"+ tabCtl.Name +">"); sb.Append("\r\n");
    foreach (Control childCtl in (tabCtl as TabPage).Controls)
    {
    if (childCtl is DataGridView || childCtl is ComponentFactory.Krypton.Toolkit.KryptonDataGridView)
    {
    foreach (DataGridViewColumn col in (childCtl as DataGridView).Columns)
    {
    sb.Append(
    "<"+ col.Name +">"+ col.HeaderText +"</"+ col.Name +">"); sb.Append("\r\n");
    }
    }
    elseif (childCtl is GroupBox)
    {
    foreach (Control grpCtl in childCtl.Controls)
    {
    sb.Append(
    "<"+ grpCtl.Name +">"+ grpCtl.Text +"</"+ grpCtl.Name +">"); sb.Append("\r\n");
    }
    }
    elseif (childCtl.Text.Length >0&&!childCtl.GetType().Name.Equals("TextBox") &&!childCtl.GetType().Name.Equals("DateTimePicker"))
    {

    sb.Append(
    "<"+ childCtl.Name +">"+ childCtl.Text +"</"+ childCtl.Name +">"); sb.Append("\r\n");
    }
    }

    }
    }
    break;
    case"Panel":
    {
    foreach (Control pnlCtl in (ctl as Panel).Controls)
    {
    if (pnlCtl.Text.Length >0&&!pnlCtl.GetType().Name.Equals("TextBox") &&!pnlCtl.GetType().Name.Equals("DateTimePicker"))
    {
    sb.Append(
    "<"+ pnlCtl.Name +">"+ pnlCtl.Text +"</"+ pnlCtl.Name +">"); sb.Append("\r\n");
    }
    }
    }
    break;
    case"KryptonPanel":
    gotocase"Panel";
    case"DataGridView":
    foreach (DataGridViewColumn col in (ctl as DataGridView).Columns)
    {
    sb.Append(
    "<"+ col.Name +">"+ col.HeaderText +"</"+ col.Name +">"); sb.Append("\r\n");
    }
    break;
    case"KryptonDataGridView":
    gotocase"DataGridView";
    }
    }
    sb.Append(
    "</"+ frm.Name +">");
    BaseTools.AddLog(sb.ToString());
    }
    }

      

      WriteControlToXml

    方法用于自动生成xml语言配置文件的节点,省去手动一个个的复制的麻烦.

    LoadLanuageSettings

    用于遍历窗体控件并赋值到text属性

    至于主窗体的menustrip,因为属于特例,没有封装

    方法如下:

    View Code
    ///<summary>
    /// 加载语言文件
    ///</summary>
    privatevoid LoadLanguage()
    {
    LanguageSeting langset
    =new LanguageSeting();
    string strLanguage = ConfigurationManager.AppSettings["Language"];
    if (!strLanguage.Equals("Chinese"))
    {
    langset.GetFrmLanguage(
    this.Name);
    string temp ="";
    //加载菜单
    foreach (ToolStripMenuItem ctl inthis.menuStrip1.Items)
    {
    temp
    = langset.GetFrmCtlLanguage(this.Name, ctl.Name);
    if (temp.Length >0)
    ctl.Text
    = temp;
    for (int i =0; i < ctl.DropDownItems.Count; i++)
    {
    if (ctl.GetType() ==typeof(ToolStripMenuItem))
    {
    temp
    = langset.GetFrmCtlLanguage(this.Name, ctl.DropDownItems[i].Name);
    if (temp.Length >0)
    ctl.DropDownItems[i].Text
    = temp;
    }
    }
    }

    foreach (ToolStripItem ctl inthis.toolStrip1.Items)
    {
    temp
    = langset.GetFrmCtlLanguage(this.Name, ctl.Name);
    if (temp.Length >0&& ctl.GetType()==typeof(ToolStripButton))
    ctl.Text
    = temp;
    }
    }
    }

    每个窗体加载的时候

    调用

    LoadLanuageSettings(this);方法即可

    效率还行,40多个控件的页面加载文字并显示的速度在12毫秒左右,可以忽略不计.

    如果需要动态切换,再次调用即可.

    作为一个初学者,可能代码中还有不恰当的地方,还请各位多多指出.

    运行效果图:

    该方法优点:

    1.语言文件配置文件可以完全自动生成,只要稍加修改即可.

    2.可以动态加载语言配置文件.

    3.配置文件修改方便.

  • 相关阅读:
    C语言学生管理系统(原版本)(自编)
    【JavaScript】解构赋值
    【JavaScript】箭头函数
    【JavaScript】typeof 和 instanceof
    【JavaScript】Proxy 实例方法(二)
    【JavaScript】Proxy 实例方法(一)
    【JavaScript】Reflect 静态方法(二)
    【JavaScript】Reflect 静态方法(一)
    【JavaScript】async function
    开发系统关键字(例子)
  • 原文地址:https://www.cnblogs.com/MarsPanda/p/2140622.html
Copyright © 2020-2023  润新知