一、VB.NET 多语言版本的开发之类文件

ReadAppConfig.cs
using  System;
using  System.Collections;
using  System.Xml;
using  System.IO;
using  System.Windows.Forms;
using  System.Text;

namespace MuchLanguage
{
    
/// <summary>
    
/// ReadAppResource 的摘要说明。
    
/// </summary>
    
public class ReadAppResource
    {
        
public ReadAppResource()
        {
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }
        
public  Hashtable ReadResource(string frmName,string lang)
        {
            Hashtable result
=new Hashtable();
            XmlReader reader
=null;
            FileInfo fi
=new FileInfo("AppResource_"+lang+".xml");
            
if(!fi.Exists)
            {
//                reader=new XmlTextReader(System.Environment.CurrentDirectory.ToString()+"AppResource.xml");
                reader
=new XmlTextReader( Application.ExecutablePath +"AppResource.xml");
            }
            
else
            {
                reader
=new XmlTextReader("AppResource_"+lang+".xml");
            }
            XmlDocument doc
=new XmlDocument();
            doc.Load(reader);
            XmlNode root
=doc.DocumentElement;
            XmlNodeList nodelist
=root.SelectNodes("Form[Name='" + frmName + "']/Controls/Control");
            foreach(XmlNode node 
in nodelist)
            {
                
try
                {
                    XmlNode node1
=node.SelectSingleNode("@name");
                    XmlNode node2
=node.SelectSingleNode("@text");
                    
if(node1!=null)
                    {result.Add(node1.InnerText.ToLower(),node2.InnerText);}            
                }
                
catch(FormatException fe)
                {Console.WriteLine(fe.ToString());}
            }
            reader.Close();
            
return result;
        }

        
//在NET中处理XML数据是很方便的,但是对于初学者来说,可能有时候还是有些困难,
        
//以下代码是将表转换成XmlDocument,然后在读取出来的示例,很简单,可以自己在进行扩充. 

        
private XmlDocument CreateXmlDocument()
        {
            
            XmlDocument xmldoc 
= new XmlDocument();
            XmlNode xnod;
            XmlElement xenl;

            
//描述信息
            xnod 
= xmldoc.CreateNode(XmlNodeType.XmlDeclaration, """");
            xnod.InnerText 
+= "encoding='UTF-8'";
            xmldoc.AppendChild(xnod);

            
//根元素
            xenl 
= xmldoc.CreateElement("""body""");
            xmldoc.AppendChild(xenl);

            XmlNode nod 
= xmldoc.SelectSingleNode("body");

            
//设置元素的值
            XmlElement element;
            element 
= xmldoc.CreateElement("item");                    
            element.SetAttribute(
"Name","Manqing Yu");                                      
            element.SetAttribute(
"Age","24");                                     
            element.SetAttribute(
"Sex""");            
            nod.AppendChild(element);                
    
            xmldoc.Save(
"c:XmlTest.xml");
            
return xmldoc;
        }
    }
}
 ReadAppResource.cs
using  System;
using  System.Collections;
using  System.Xml;
using  System.Text;

namespace MuchLanguage
{
    
/// <summary>
    
/// ReadppConfig 的摘要说明。
    
/// </summary>
    
public class ReadAppConfig
    {
        
public ReadAppConfig()
        {
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }
        
public IList GetLanguageList(string lang)
        {
            IList result 
=new ArrayList ();
            XmlReader reader
=new XmlTextReader("AppConfig.xml");
            XmlDocument doc
=new XmlDocument();
            doc.Load(reader);
            XmlNode root
=doc.DocumentElement;
            XmlNodeList nodelist
=root.SelectNodes("Area[Language='"+lang +"']/List/Item");

            foreach(XmlNode node 
in nodelist)
            {
                
try
                {                    
                    
if(node!=null)
                    {result.Add(node.InnerText);}        
                    
                }
                
catch(FormatException fe)
                {Console.WriteLine(fe.ToString());}
            }

            reader.Close();
            
return result;
        }
        
public string GetChildLanguage(string Setlang,string lang)
        {
            IList result 
=new ArrayList ();
            XmlReader reader
=new XmlTextReader("AppConfig.xml");
            XmlDocument doc
=new XmlDocument();
            doc.Load(reader);
            XmlNode root
=doc.DocumentElement;
            XmlNodeList nodelist
=root.SelectNodes("Area[Language='"+lang +"']/List/Item");           
            foreach(XmlNode node 
in nodelist)
            {
                
try
                {                    
                    
if(node!=null)
                    {
                        
if (Setlang==node.InnerText)
                        {
                            
int len=Setlang.Length;
                            Setlang
=Setlang.Substring(len-4);
                            Setlang
=Setlang.Substring(1,2);
                        }
                    }
                    
                }
                
catch(FormatException fe)
                {Console.WriteLine(fe.ToString());}
            }

            reader.Close();
            
return Setlang;
        }
        
public string Getfullnamelanguage(string lang)
        {
            
string Getlanguage=null;
            IList result 
=new ArrayList ();
            XmlReader reader
=new XmlTextReader("AppConfig.xml");
            XmlDocument doc
=new XmlDocument();
            doc.Load(reader);
            XmlNode root
=doc.DocumentElement;
            XmlNodeList nodelist
=root.SelectNodes("Area[Language='"+lang +"']/List/Item");           
            foreach(XmlNode node 
in nodelist)
            {
                
try
                {                    
                    
if(node!=null)
                    {                       
                        
string Setlang=node.InnerText;
                        
int len=Setlang.Length;
                        Setlang
=Setlang.Substring(len-4);
                        Setlang
=Setlang.Substring(1,2);     
                        
if (Setlang==lang)
                        {
                            Getlanguage
=node.InnerText;
                        }
                    }
                    
                }
                
catch(FormatException fe)
                {Console.WriteLine(fe.ToString());}
            }

            reader.Close();
            
return Getlanguage;
        }
    }
}

ReadDefaultLanguage.cs
using  System;
using  System.Xml;
using  System.Data;
using  System.Collections;
using  System.Windows.Forms;


namespace MuchLanguage
{
    
/// <summary>
    
/// ReadDefaultLanguage 的摘要说明。
    
/// </summary>
    
public class ReadDefaultLanguage
    {
        
public ReadDefaultLanguage()
        {
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }    
        
public string ReadDefaultLanguages()
        {
            XmlReader reader
=new XmlTextReader("LanguageDefine.xml");
            XmlDocument doc 
=new XmlDocument();
            doc.Load(reader);
            XmlNode root
=doc.DocumentElement;
            XmlNode node
=root.SelectSingleNode("DefaultLanguage");

            
string result="EN";
            
if (node!=null)
                result
=node.InnerText;
            reader.Close();
            
return result;            
        }

        
public void  WriteDefaultLanguage(string lang)
        {
            DataSet ds
=new DataSet();
            ds.ReadXml(
"LanguageDefine.xml");
            DataTable dt
=ds.Tables["Language"];
            dt.Rows[
0]["DefaultLanguage"]=lang;
            ds.AcceptChanges();
            ds.WriteXml(
"LanguageDefine.xml");
        }
        
public void getNames(Form form,string lang)
        {
            ReadAppResource AppConfig
=new ReadAppResource();
            Hashtable table
=AppConfig.ReadResource(form.Name,lang);
            Control.ControlCollection controlNames
=form.Controls;
            
try
            {
                foreach(Control control 
in controlNames)
                {
                    
if(control.GetType()==typeof(System.Windows.Forms.Panel))
                        GetSubControls(control.Controls,table);
                    
if(control.GetType()==typeof(System.Windows.Forms.GroupBox))
                        GetSubControls(control.Controls,table);
                    
if(table.Contains(control.Name.ToLower()))
                        control.Text
=(string)table[control.Name.ToLower()];
                }    
                
if(table.Contains(form.Name.ToLower()))
                    form.Text
=(string)table[form.Name.ToLower()];                           
            }
            
catch(Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        
public void GetSubControls(Control.ControlCollection controls,Hashtable table)
        {
            foreach(Control control 
in controls)
            {
                
if(control.GetType()==typeof(System.Windows.Forms.Panel))
                    GetSubControls(control.Controls,table);
                
if(control.GetType()==typeof(System.Windows.Forms.GroupBox))
                    GetSubControls(control.Controls,table);
                
if(table.Contains(control.Name.ToLower()))
                    control.Text
=(string)table[control.Name.ToLower()];
            }
        }
    }
}

你可能感兴趣的:(一、VB.NET 多语言版本的开发之类文件)