Tipp 0061 Mehrsprachige Anwendungen entwickeln
Autor/Einsender:
Datum:
  Michael Werner
11.11.2004
Entwicklungsumgebung:   VB.Net 2003
Framework:   1.1
Mit Hilfe der Klassen ResXResourceReader und ResXResourceWriter können sprach- und kulturabhängige Ressource-Dateien eine Mehrsprachigkeit im Programm gewährleisten, einschließlich der kulturspezifischen Formate, z.B. für Währung und Datum. Für den aktuellen Thread wird die ausgewählte Kultur eingestellt. Benötigt werden dafür die Namensräume System.Threading und System.Globalization.
Code in Form1
 
Imports System.Threading
Imports System.Globalization

Public Class Form1
  Inherits System.Windows.Forms.Form
Vom Windows Form Designer generierter Code
  Private Sub Read_Language()
    Dim res As New Michaels.StringRes
    Dim language As String
    If RadioButton1.Checked Then
      language = "de"
    ElseIf RadioButton2.Checked Then
      language = "gb"
    ElseIf RadioButton3.Checked Then
      language = "hu"
    Else
    End If

    Label1.Text = res.Read_ResX(language, "greeting")
    Label2.Text = res.Read_ResX(language, "question")
    Label3.Text = res.Read_ResX(language, "numbers")
    Label4.Text = res.Read_ResX(language, "apple")
    Label5.Text = res.Read_ResX(language, "costs")
    Label6.Text = res.Read_ResX(language, "end")
    Me.Text = res.Read_ResX(language, "me")
    RadioButton1.Text = res.Read_ResX(language, "rb1")
    RadioButton2.Text = res.Read_ResX(language, "rb2")
    RadioButton3.Text = res.Read_ResX(language, "rb3")
    Button1.Text = res.Read_ResX(language, "bt1")
    GroupBox1.Text = res.Read_ResX(language, "gp1")

    Const RES_Folder As String = "..\ressourcen"
    Dim picPath As String = System.IO.Path.Combine( _
                 RES_Folder, res.Read_ResX(language, "flag"))
    PictureBox1.Image = Image.FromFile(picPath)
  End Sub

  Private Sub Set_Culture()
    Dim cultName As String
    If RadioButton1.Checked Then
      cultName = "de-DE"
    ElseIf RadioButton2.Checked Then
      cultName = "en-GB"
    ElseIf RadioButton3.Checked Then
      cultName = "hu-HU"
    Else
    End If

    Dim currCult As CultureInfo = New CultureInfo(cultName)
    Thread.CurrentThread.CurrentCulture = currCult
    Thread.CurrentThread.CurrentUICulture = currCult

    Label7.Text = currCult.DisplayName
    Label8.Text = currCult.EnglishName
    Label9.Text = currCult.Name
    Label10.Text = currCult.LCID.ToString
    Label11.Text = FormatCurrency(123456.78)
    Label12.Text = Date.Now.ToLongDateString() & _
                 "   " & Date.Now.ToLongTimeString
  End Sub

  Private Sub RadioButton1_CheckedChanged(ByVal sender As _
          System.Object, ByVal e As System.EventArgs) Handles _
          RadioButton1.CheckedChanged
    Read_Language()
    Set_Culture()
  End Sub

  Private Sub RadioButton2_CheckedChanged(ByVal sender As _
          System.Object, ByVal e As System.EventArgs) Handles _
          RadioButton2.CheckedChanged
    Read_Language()
    Set_Culture()
  End Sub

  Private Sub RadioButton3_CheckedChanged(ByVal sender As _
          System.Object, ByVal e As System.EventArgs) Handles _
          RadioButton3.CheckedChanged
    Read_Language()
    Set_Culture()
  End Sub

  Private Sub Form1_Load(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles MyBase.Load
    Dim sr As New Michaels.StringRes
    sr.Make_ResX("de")
    sr.Make_ResX("gb")
    sr.Make_ResX("hu")

    Read_Language()
    Set_Culture()
  End Sub

  Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click
    Me.Close()
  End Sub
End Class
 
Code in StringRes
 
Imports System.Resources
Imports System.Globalization
Imports System.Threading

Namespace Michaels
  Public Class StringRes

    Const RES_Folder As String = "..\ressourcen"
    Const RESX_de As String = RES_Folder & "\language.de-DE.resx"
    Const RESX_gb As String = RES_Folder & "\language.en-GB.resx"
    Const RESX_hu As String = RES_Folder & "\language.hu-HU.resx"

    Function Read_ResX(ByVal language As String, ByVal key _
               As String) As String
      Dim reader As ResXResourceReader

      Select Case language
        Case "de"
          reader = New ResXResourceReader(RESX_de)
        Case "gb"
          reader = New ResXResourceReader(RESX_gb)
        Case "hu"
          reader = New ResXResourceReader(RESX_hu)
      End Select

      Dim id As IDictionaryEnumerator = reader.GetEnumerator()
      Dim d As DictionaryEntry
      Dim i As Int32

      For Each d In reader
        If d.Key.ToString = key Then
          Return d.Value.ToString()
        End If
      Next d

      reader.Close()
    End Function

    Sub Make_ResX(ByVal language As String)
      Select Case language
        Case "de"
          Dim w As New ResXResourceWriter(RESX_de)
          w.AddResource("greeting", _
                        "Guten Tag, Sie sprechen deutsch!")
          w.AddResource("question", "Möchten Sie")
          w.AddResource("numbers", "einen, zwei, drei oder vier")
          w.AddResource("apple", "Äpfel?")
          w.AddResource("costs", "1000 Stück kosten:")
          w.AddResource("end", "Danke und auf Wiedersehen!")
          w.AddResource("me", "Mehrsprachige Anwendung")
          w.AddResource("rb1", "Deutsch")
          w.AddResource("rb2", "Englisch")
          w.AddResource("rb3", "Ungarisch")
          w.AddResource("bt1", "&Beenden")
          w.AddResource("gp1", "Wählen Sie Ihre Sprache")
          w.AddResource("flag", "deutsch.gif")
          w.Close()

        Case "gb"
          Dim w As New ResXResourceWriter(RESX_gb)
          w.AddResource("greeting", _
                        "Hello, you are speaking English!")
          w.AddResource("question", "Do you want")
          w.AddResource("numbers", "one, two, three or four")
          w.AddResource("apple", "apples?")
          w.AddResource("costs", "1000 pieces costs:")
          w.AddResource("end", "Thank you and good-bye!")
          w.AddResource("me", "Multilingual Application")
          w.AddResource("rb1", "German")
          w.AddResource("rb2", "English")
          w.AddResource("rb3", "Hungary")
          w.AddResource("bt1", "&End")
          w.AddResource("gp1", "Choose your language")
          w.AddResource("flag", "britain.gif")
          w.Close()

        Case "hu"
          Dim w As New ResXResourceWriter(RESX_hu)
          w.AddResource("greeting", _
                        "Jo napot, maga beszèl magyarul!")
          w.AddResource("question", "Alkar maga")
          w.AddResource("numbers", "egy, kettö, hàrom vagy nègy")
          w.AddResource("apple", "almàt?")
          w.AddResource("costs", "Ezer darab kerül:")
          w.AddResource("end", "Köszönöm ès viszontlàtàsra!")
          w.AddResource("me", "Több nyelvi programm")
          w.AddResource("rb1", "Nèmet")
          w.AddResource("rb2", "Angol")
          w.AddResource("rb3", "Magyar")
          w.AddResource("bt1", "&Befejezni")
          w.AddResource("gp1", "Vàlasza a maga nyelvèt:")
          w.AddResource("flag", "ungar.gif")
          w.Close()
      End Select
      Exit Sub
    End Sub
  End Class
End Namespace
 
Weitere Links zum Thema
Programmeinstellungen in Ressource speichern
Cursor aus Ressource laden

Windows-Version
98/SE
ME
NT
2000
XP
Vista
Win 7


Download  (18 kB) Downloads bisher: [ 1038 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Tipps | Projekte | Tutorials | Bücherecke | VB-/VBA-Tipps | API-Referenz | Komponenten | VB.Net-Forum | VB/VBA-Forum | DirectX-Forum | Foren-Archiv | DirectX | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Montag, 23. Janur 2012