如何?多語言

來源:互聯網
上載者:User

之前做過幾個的多語言程式,包括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 方法來擷取某個控制項的名字

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.