Click here to Skip to main content
15,885,216 members
Articles / Mobile Apps
Article

Localizable DataSet

Rate me:
Please Sign up or sign in to vote.
2.25/5 (3 votes)
23 Nov 20042 min read 49.1K   469   18   4
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:

VB
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


Written By
Software Developer
Netherlands Netherlands
My first encounters with computers where Sinclair, TI99-4A, C64, Apple II. Have programmed ever since. Worked for a few logistic companies.
I have been a VB.NET programmer for quite some years. I work with a small firm, that specializes in supply chain planning for bulk products. We have our own application, Delivery+, that helps vehicle planners with their daily job.
I am married and I have two children. I am building an eco-friendly house and try to live green.

Comments and Discussions

 
Generalhelp Pin
sreejith ss nair18-Sep-05 21:16
sreejith ss nair18-Sep-05 21:16 
AnswerRe: help Pin
pinx20-Sep-05 22:03
pinx20-Sep-05 22:03 
QuestionDataGridView i GridView(DevXpress) Pin
winx23-Aug-07 20:28
winx23-Aug-07 20:28 
GeneralRe: help Pin
Stonkie15-Jul-08 11:44
Stonkie15-Jul-08 11:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.