Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Localizable DataSet

0.00/5 (No votes)
23 Nov 2004 1  
Put language-dependent column captions in the DataSet itself and use it in your code wherever you need it!

Introduction

The .NET Framework has great support for multi-language applications. You can put all your translated form text in resource files. What happens when you bind a DataSet to a grid? In our case, we use Component One's TrueDBGrid. If you make a form localizable, all the formatting of the grid becomes language-dependent. It would be great to make only the columns' captions language-dependent. Then, the next problem arises: you have to maintain all the forms. It would make more sense to put the translations of the data fields' captions in the DataSet definition itself. And that's what I have made, using an adapted version of Shawn Wildermuth's DataSet Generator.

Usage

First, read my previous article on Inherited DataSets. The version of the DataSet Generator with this article is basically a further developed version.

The magic is done with this piece of generated code:

Public Overridable Property Localizable() As Boolean
  Get
    Return Me._Localizable
  End Get
  Set(ByVal Value As Boolean)
    _Localizable = value
    If value Then
      Dim resourceManager As System.Resources.ResourceManager = New 
        System.Resources.ResourceManager(GetType(TestDataSet1))
      Dim tableIndex As Integer
      Dim columnIndex As Integer
      Dim caption As String
      tableIndex = 0
      Do While (tableIndex < Me.Tables.Count)
        columnIndex = 0
        Do While (columnIndex < Me.Tables(tableIndex).Columns.Count)
          Try
            caption = resourceManager.GetString( _
              String.Join(".", New String()_
              {Me.Tables(tableIndex).TableName, _
               Me.Tables(tableIndex).Columns(columnIndex).ColumnName, "Caption"}))
          Catch ex As System.Exception
          End Try
          If (Not (caption) Is Nothing) Then
            Me.Tables(tableIndex).Columns(columnIndex).Caption = caption
          End If
          columnIndex = (columnIndex + 1)
        Loop
      tableIndex = (tableIndex + 1)
      Loop
    End If
  End Set
End Property

All you have to do to make the Caption property language dependent, is put the translations in .resx files with the same name as the DataSet's generated code file. If your DataSet's XSD is called DataSet1.xsd, the generated code file will be called DataSet1.vb (if you're working on a VB.NET project). The files you have to add to your project must be named DataSet1.vb.resx, DataSet1.vb.nl.resx, etc. To give you a good start, I put a little routine in the test project that creates these files for you and fills it with all the columns' captions. Just press the button on the correct form.

The choice is yours!

Of course, you don't want to translate all captions all the time. Accessing resource files may create unnecessary overhead. First, if you leave out Caption translations from the resource files, no translation work will be done. Second, as long as you don't set the Localizable property to True (e.g. from the Form Designer in Visual Studio), no translation work is done at all.

Other options

Wout de Zeeuw proposed translating object properties so they would display correctly in a PropertyGrid. I spent some time trying to port this Attribute centered approach to the DataSet, but it gave me too many headaches. If you want to use the .NET PropertyGrid, my solution may not be the best one.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here