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