localizating controls ?

  • Thread starter Thread starter Christian Schwarz
  • Start date Start date
C

Christian Schwarz

Hello,

I'm trying to find an easy way to localize control's Text properties.

For instance I could replace the following code:

void InitializeComponents()
{
....
this.m_Label1 = new Label();
this.m_Label1.Text = "Hallo Welt !"
....
}

with this code:

void InitializeComponents()
{
....
this.m_Label1 = new Label();
this.m_Label1.Text = Resource.GetString("Hallo Welt !")
....
}

In which Resource.GetString() is a static function searching the calling
assembly's resources for a translation of "Hallo Welt !" depending on
System.Globalization.CultureInfo.CurrentUICulture.

Unfortunately, editing InitializeComponents() would render the designer
useless. So that method is out of question. The next solution that comes to
mind is to set all translatable text again after InitializeComponents() has
finished. But this is cirumstantial, fault-prone and time-consuming (both,
run-time and design-time).

Another approach would be to extend all standard controls so that they call
Resource.GetString() automatically.

How would you or did you deal with this problem ?

Greets, Christian
 
Josh, thanks for your response.

If I got you right, you extended the standard controls like TextBox, Label,
Button and Form to load certain properties from data files (including
translated text). For my purposes, this solution seems a bit too
exaggerated. Also I'd like to use satellite assemblies to manage support of
different cultures.

But maybe I should follow your solution's basic thought to extend to
standard controls. So the derived controls (LocalizedTextBox,
LocalizedLabel, LocalizedButton, ...) could implement some additional
functionality to, for instance, translate the text assigned to the
Text-property ...

Greets, Christian
 
Well I'll be interested to see what others say and if anyone says I'm insane
but what I did was:
1. Subclassed every control into my own assembly/dll. Which allowed me to
modify the controls as I want, or add new custom controls.
2. I then added a property to each control called Defaults which exposed a
class say
MyDefaults
3. For the class MyDefaults I implemented:
a UITypeEditor and TypeConverter to allow VS.Net to generate code for the
class in InitialiseConponents. (that is not as difficult as one might
think.)
4. The UITypeEditor lets me change what my defaults are like I would change
any other property of a control in VS.
5. So with one click in the form editor visual studio automatically
generates my localisation stuff that looks like this:
this.m_myButton.Defaults = new MyDefaults(this.m_myButton,
"PinnacleButton.m_myButton", new System.Drawing.Size(240, 184), new
System.Drawing.Point(0, 88), "Ok!", false);

the second argument is used to lookup the text string, size and location
while the remaing arguments provide defaults should they not be found in the
lookup db.

My lookup db is implemented in files so that seperate laungage packs can be
distributed.

Although this was a lot of work I think it provided a good solution thats
really easy to use. My implementation has a few glitches but I'm 90% happy
with it, if I didnt have to implement an application as well I'm sure I'd
get it 100%! :-)

Not sure I've explained it well and no doubt someone will say there is an
easier way.

Regards

Josh
 
Click on the form that has the textbox. Go to its properties. Set
misc->localizable to true. All elements in the form should be automatically
localizable.

Thanks,
Sandy


This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top