如何实现多语言

之前做过几个的多语言程序,包括C/S结构和B/S结构的。总体来说都是差不多,都是将语言信息保存在数据库中,或者保存在XML中.在Form load的时候加载控件的信息.其实感觉都差不多,以下以B/S为例,简单介绍一下保存在XML方法

1:对于每种语言的所有元素全部写在一个XML文件里面。如:English的话,那么所有包含English的语言信息全部写在一个XML里面,并且将每种语言的文件全部放在一个目录下面,这样比较好统一处理。比如说统一找目录等

2:创建一个类,该类的主要负责读取某种语言的xml文件,并且将数据保存在这个类里面,并且根据传入的key来读取信息.
类成员主要包含以下信息:
private Hashtable m_arrInterfaceString; //用来保存语言信息(key,VALUE)。

private const string XmlKey_ConstVariable = "//Const_Variable"; //主要是指名xml文件下语言信息保存在哪个Nodes下
private const string XmlKey_Key = "Key";//语言元素的key
private const string XmlKey_Value = "Value"; //语言元素的key
以下是保存在XML中的例子
    <Const_Variable>
      <Key>VBT_UserLogin_lbl_password</Key>
      <Name>UnKnow</Name>
      <Value>密码</Value>
      <Describe>UnKnow</Describe>
    </Const_Variable>

以下的方法是读取xml

  public bool LoadXMLDoc(string i_strXMLDocPath)
  {
   bool bResult = false;

   if(m_arrInterfaceString == null)
   {
    m_arrInterfaceString = new Hashtable();
   }

   XmlDocument xdoc = new XmlDocument();
      try{
   xdoc.Load(i_strXMLDocPath);
   }
   catch(Exception ex)
   {
    string a = ex.Message;
   }

   //解吸
   XmlNodeList nodes = xdoc.SelectNodes(XmlKey_ConstVariable);
   
   XmlNode KeyNode   = null;
   XmlNode ValueNode = null;
   string strKey = null;
   string strValue = null;

   foreach(XmlNode node in nodes)
   {
       KeyNode = node.SelectSingleNode(XmlKey_Key);
        ValueNode = node.SelectSingleNode(XmlKey_Value);
    
    if((KeyNode != null) && (ValueNode != null))
    {
     strKey = KeyNode.InnerText;
     strValue = ValueNode.InnerText;

     try
     {
      m_arrInterfaceString.Add(strKey,strValue);
     }
     catch
     {
      //int i = 0;

     }
    }
    else
    {
     bResult = false;
    }
   }
   
   return bResult;
  }


以下方法是读取值
public string GetString(string i_strKey)
  {
   string strResult = "";

   if(i_strKey != null && m_arrInterfaceString[i_strKey] != null)
   {
    strResult = (string)m_arrInterfaceString[i_strKey];
   }
   
   return strResult;
  }

以上的方法是创建某种语言的一个类,如果是包含多种语言的话,需要再创建一个Hashtable,其中key是语言的ID,上面创建的类。
对于在某个页面的page_load里面,根据语言类型到Hashtable 里面获取该类的实例,然后通过该实例的GetString 方法来获取某个控件的名字




 

posted @ 2008-01-08 10:56  Farrell  阅读(488)  评论(0编辑  收藏  举报
友情连接