Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleLoad internationalization strings from a database
Keywordsdatabase, internationalization, language
CategoriesDatabase, Software Engineering
 
This program's database contains records that give a control name, a value, and a language. For example, the following records define the values for the program's Yes and No buttons in English, French, and German.

    cmdYes    Yes    English
    cmdYes    Oui    French
    cmdYes    Ja     German
    cmdNo     No     English
    cmdNo     Non    French
    cmdNo     Nein   German

To load a language, the program uses a SQL SELECT statement to find the records for the language. It loops through them and uses the control name field as an index into the form's Controls collection. Depending on the control's type (TextBox, Label, Frame, etc.), the program uses the record's value to set the appropriate control property (Text, Caption, and so forth).

 
' Load the control strings for this language.
Private Sub LoadStrings(ByVal language As String)
Dim db_name As String
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim query As String
Dim ctl As Control

    ' Open the database.
    db_name = App.Path
    If Right$(db_name, 1) <> "\" Then db_name = db_name & _
        "\"
    db_name = db_name & "intl.mdb"
    Set conn = New ADODB.Connection
    conn.ConnectionString = _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & db_name & ";" & _
        "Mode=Read;" & _
        "Persist Security Info=False"
    conn.Open

    ' Compose the query.
    query = "SELECT * FROM Strings " & _
        "WHERE Lang='" & language & "'"

    ' Execute the query.
    Set rs = conn.Execute(query, , adCmdText)

    ' Process the records.
    Do While Not rs.EOF
        ' Look for the Form's name.
        If rs!ControlName = Name Then
            ' This is the form. Set the caption.
            Caption = rs!Value
        Else
            ' Get the control with this name. Note that
            ' this doesn't work with control arrays.
            Set ctl = Controls(rs!ControlName)

            ' Set the control's string.
            If (TypeOf ctl Is Label) Or _
               (TypeOf ctl Is Frame) Or _
               (TypeOf ctl Is OptionButton) Or _
               (TypeOf ctl Is CommandButton) _
            Then
                ctl.Caption = rs!Value
            ElseIf (TypeOf ctl Is TextBox) Then
                ctl.Text = rs!Value
            Else
                MsgBox "Error: Control " & ctl.Name & _
                    " has unknown type " & TypeName(ctl)
            End If
        End If

        ' Get the next record.
        rs.MoveNext
    Loop

    rs.Close
    conn.Close
End Sub
 
This technique has the advantages that it is simple, you can edit the strings using an database editor, and it allows you to load a new language while the program is running. Some internationalization methods load the language based on the computer's settings when the program starts and you cannot change the language after the program is running. This makes it hard to test different languages or switch them if you must.

If your program is more complicated, you might want to add form name to the database table so you can load a particular form's data.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated