views:

879

answers:

2

Overview

In another question, I asked about deploying localizations for some runtime compiled UserControl's. However, before I can get to deploying the localizations, I need a way of localizing the controls.

Background

The controls are created by our own WinForms-style designer (using .NET's support for design surfaces, etc.) and saved as a binary format that combines the CodeCompileUnit, resource resx, and user source into one file. These files are then compiled into an assembly as appropriate at runtime by another tool.

In order to localize these, we need to tell the designer and serialization that localizable property values are to be stored in the resources. The VisualStudio WinForms designer does this using an extension property called Localizable and an associated property for specifying the default culture. We need this property in our custom designer, if possible.

Constraints

We need our standalone designer tool that is easy to use for non-developer types as well as restricting certain actions so using a free edition of Visual Studio (i.e. C# Express) is not going to work (I've already pitched it and failed); therefore, any solution to how we localize these UserControl's needs to compensate for this.

Question

Can we get the Localizable support into our custom WinForms designer?

  • If yes, how?
  • If no, what alternatives are there to localizing our UserControl's? e.g. post-processing somehow, different file format, etc.
A: 

I'm not sure if I understood your question correctly.

Just check for the System.ComponentModel.LocalizableAttribute on all properties to (de-)serialize if your control is Localizable.

// Gets the attributes for the property.
AttributeCollection attributes = 
TypeDescriptor.GetProperties(this)["MyProperty"].Attributes;

// Checks to see if the property needs to be localized.
LocalizableAttribute myAttribute = 
(LocalizableAttribute)attributes[typeof(LocalizableAttribute)];
if(myAttribute.IsLocalizable) {
// Insert code for handling resource files here.
}

Since you decided to write your own designer you have to do this yourself.

Stefan Haubold
Thanks, I appreciate the time you took to answer. Unfortunately, this doesn't answer the question. I know how to specify that something is localizable and how to localize it, what I don't know is how to make the designer provide the property that governs localization, as seen in the winforms designer.
Jeff Yates
You have to localize each user control individually when designing it in the designer. Then you can set the property Localizable on the user control to true and choose your language. The surrounding Form however does not escalate these two properties to its user controls when the Form is designed. And that is your problem. Workaround: expose all things to localize as localizable (see attribute above) public properties on your user controls and localize them in the form. That duplicates a lot of work and on some level defeats the purpose of user controls, but I don't know any other way.
Stefan Haubold
When you create your own designer, the Localizable property is not available to set. It is this functionality we want to include. I think you misunderstand the issue. It is not with localizing a user control, it is with providing localization property support within a custom forms designer and design surface.
Jeff Yates
+1  A: 

You need to add a System.ComponentModel.Design.LocalizationExtenderProvider to your design surface.

Alex Lyman
Ah, excellent. I presume there is more work than this to actually provide the support behind the scenes, but this will be a great start. Thanks!
Jeff Yates
FYI - LocalizationExtenderProvider is obsolete
Mark