Hello world  
They don't know that we know they know we know!
在项目的开发过程中,我们遇到了这种情况,由于客户的工作需要,要求我们的项目能够支持至少两种语言,在这里给出我们在实现该功能的最初的一种方式。
我们将不同版本的语言信息写入到不同的xml配置文件中,格式如下:
<?xml version="1.0" encoding="utf-16" ?>
<mainfile name="Chinese" description="This is Chinese language pack." author="Benny" team="Saturnus Team" version="1.0" copyright="Copyright © 2007 Saturnus Team.All rights reserved.">
  
<window id="FormXMLTest" title="中文版测试窗口">
    
<component name="btn01">
      
<text>鸣人</text>
    
</component>

    
<component name="btn02">
      
<text>卡卡西</text>
    
</component>

    
<component name="label2">
      
<text>我爱罗</text>
    
</component>

    
<component name="linkLabel1">
      
<text>佐助</text>
    
</component>
  
</window>

  
<window id="FrmTest" title="另一个测试窗口">
  
</window>

  
<window id="FrmTest2" title="还是一个测试窗口">
    
<component name="radioButton1">
      
<text>哈哈</text>
    
</component>

    
<component name="radioButton4">
      
<text>嘻嘻</text>
    
</component>
  
</window>
</mainfile>
Type text here
一个window标签代表一个界面,一个component标签代表界面里的一个控件。
我们接下来需要做的就是便利整个窗口的控件,并根据对应的名字进行更新。首先我们需要读出对应窗口的配置信息,并作为Dictionary类返回信息:
public Dictionary<string,string> getWindowsLanguageText(string formName)
        {
            Dictionary
<string ,string> result = new Dictionary<string,string>();    //返回结果
            readXML();  //打开xml文件
            XmlNodeList windowNodeList = xmlDoc.SelectSingleNode("mainfile").ChildNodes;    //获取window子节点
            
//遍历所以子节点,并找到formName指定的节点
            foreach (XmlNode windowNode in windowNodeList)
            { 
                XmlElement windowElement 
= (XmlElement)windowNode;
                if (windowElement.GetAttribute("id"== formName)
                { 
                    
//找到指定窗口的语言配置
                    
//先将该窗口的信息纪录下来
                    
//获取所有该窗体节点下面的子节点(控件节点)
                    
//这些控件节点就包含控件的显示文本
                    
//获取他们
                    result.Add(formName, windowElement.GetAttribute("title"));   //加载窗口文本
                    XmlNodeList componentNodeList = windowNode.ChildNodes;
                    
foreach (XmlNode componentNode in componentNodeList)
                    {
                        XmlElement componentElement 
= (XmlElement)componentNode;
                        
//遍历控件节点的子节点,获取文本信息
                        XmlNodeList textNodeList = componentNode.ChildNodes;
                        
foreach (XmlNode textNode in textNodeList)
                        {
                            
//添加text
                            XmlElement textElement = (XmlElement)textNode;
                            
if (textElement.Name == "text")
                                result.Add(componentElement.GetAttribute(
"name"), textElement.InnerText);
                        }
                    }
                    
break;  //完成一个界面的结果即可
                }
            }
            
return result;
        }

public void readXML()
        {
            try
            {xmlDoc.Load(xmlName);}//打开xml文件,xmlName包含该xml
            catch(Exception ex)
            {throw ex;}
        }


获取了界面的配置信息,就需要对相应的界面进行配置,首先需要获取界面下的相关控件:
public Control.ControlCollection[] GetControls(Control control)
        {

            Control.ControlCollection[] returnResult;   
//保存返回值
            Control.ControlCollection conList = control.Controls;
            Control.ControlCollection[] childConList 
= null;    //保存容器性质子控件所含有的控件
            for (int i = 0; i < conList.Count; i++)
            {
                
//遍历子控件
                if ((conList[i] is GroupBox) || (conList[i] is Panel))
                {
                    childConList 
= GetControls(conList[i]);
                }
            }

            if (childConList != null)
            {
                returnResult 
= new Control.ControlCollection[childConList.Length + 1];
                returnResult[
0= conList;
                
for (int i = 0; i < childConList.Length; i++)
                {
                    returnResult[i 
+ 1= childConList[i];
                }
                
return returnResult;
            }
            
else
            {
                returnResult 
= new Control.ControlCollection[1];
                returnResult[
0= conList;
                
return returnResult;
            }
        }

有了这些控件,就可以对控件进行配置:
public void SetFormLanguageHigh(Form winForm)
        {

            Dictionary
<stringstring> languageInfo;
            
//由窗口获取文本信息
            try
            {
                languageInfo 
= getWindowsLanguageText(winForm.Name);
            }
            
catch
            {

                System.Console.WriteLine("File error.");
                
return;
            }
            
//先设置该窗体文本
            if (languageInfo.ContainsKey(winForm.Name))
            {
                winForm.Text 
= languageInfo[winForm.Name];
            }

            
//遍历该窗口所有控件,并设置其文本
            Control.ControlCollection[] conList = GetControls(winForm);
            
for (int i = 0; i < conList.Length; i++)
            {
                
//如果返回的字典信息中含有当前控件的文本则更新它,否则不更新
                for (int m = 0; m < conList[i].Count; m++)
                {
                    
if (languageInfo.ContainsKey(conList[i][m].Name))
                    {
                        conList[i][m].Text 
= languageInfo[conList[i][m].Name];
                    }
                }
            }
        }

这样,只需修改xml的配置信息,即可改变界面的语言性质版本,当然这只是一个最初的方案,我们最终的实现或许不是这样.这里仅供参考,如果大家有新的实现方法,请赐教.
posted on 2007-08-02 10:09  土星的冰人  阅读(413)  评论(3)    收藏  举报