[VB.NET].NET多語系程式(三)
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }]]>Imports System.GlobalizationImports System.ThreadingImports System.Xml.Serialization Public Class MultiLanguage Implements IXmlSerializable #Region “Var” Private _pool As New Hashtable#End Region #Region “Public Shared Method” Public Shared Function GetFromXml(ByVal file As String) As MultiLanguage Dim m As MultiLanguage Dim x As New Xml.Serialization.XmlSerializer(GetType(MultiLanguage)) Dim fs As New IO.FileStream(file, IO.FileMode.Open) m = CType(x.Deserialize(fs), MultiLanguage) fs.Dispose() Return m End Function#End Region #Region “Public Method” Public Sub AddString(ByVal key As String, ByVal msg As String, ByVal culture As String) If Not _pool.ContainsKey(key) Then _pool.Add(key, New Hashtable) End If CType(_pool(key), Hashtable).Add(culture, msg) End Sub Public Sub AddString(ByVal key As String, ByVal msg As String) AddString(key, msg, Thread.CurrentThread.CurrentCulture.Name) End Sub Public Function GetString(ByVal key As String, ByVal culture As String) As String If Not _pool.ContainsKey(key) Then Return String.Empty End If Dim msgPool As Hashtable = CType(_pool(key), Hashtable) If Not msgPool.ContainsKey(culture) Then Return String.Empty End If Return msgPool(culture).ToString End Function Public Function GetString(ByVal key As String) As String Return GetString(key, Thread.CurrentThread.CurrentCulture.Name) End Function Public Sub SaveXml(ByVal file As String) Dim x As New Xml.Serialization.XmlSerializer(GetType(MultiLanguage)) Dim fs As New IO.FileStream(file, IO.FileMode.Create) x.Serialize(fs, Me) fs.Dispose() End Sub#End Region #Region “Implements IXmlSerializable” Public Function GetSchema() As System.Xml.Schema.XmlSchema Implements System.Xml.Serialization.IXmlSerializable.GetSchema Return Nothing End Function Public Sub ReadXml(ByVal reader As System.Xml.XmlReader) Implements System.Xml.Serialization.IXmlSerializable.ReadXml Dim startElementName As String = reader.Name Dim currentElementName As String Do currentElementName = reader.Name If currentElementName = startElementName AndAlso (reader.MoveToContent = Xml.XmlNodeType.EndElement OrElse reader.IsEmptyElement) Then reader.Read() Exit Do End If Select Case currentElementName Case “Item” Dim key As String = reader.GetAttribute(“Key”) Dim cultureKey As String = reader.GetAttribute(“Culture”) Dim value As String = reader.ReadString() reader.ReadEndElement() AddString(key, value, cultureKey) Case Else reader.Read() End Select Loop End Sub Public Sub WriteXml(ByVal writer As System.Xml.XmlWriter) Implements System.Xml.Serialization.IXmlSerializable.WriteXml Dim msgPool As Hashtable For Each key As String In _pool.Keys msgPool = CType(_pool(key), Hashtable) For Each cultureKey As String In msgPool.Keys writer.WriteStartElement(“Item”) writer.WriteAttributeString(“Key”, key) writer.WriteAttributeString(“Culture”, cultureKey) writer.WriteString(msgPool(cultureKey).ToString) writer.WriteEndElement() Next Next End Sub#End Region End Class<![CDATA[
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }]]> 建立各語系字串 m.AddString(“title”, “Title”, “en”) m.AddString(“title”, “標題”, “zh-CHT”) m.AddString(“title”, “标题”, “zh-CHS”)<![CDATA[
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }]]> 切換語系 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.Text = m.GetString(“title”, “en”) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.Text = m.GetString(“title”, “zh-CHT”) End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click Me.Text = m.GetString(“title”, “zh-CHS”) End Sub<![CDATA[
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }]]> 儲存XML m.SaveXml(“c: est.xml”)<![CDATA[
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }]]> 讀取XMLDim m As MultiLanguage = MultiLanguage.GetFromXml(“c: est.xml”)