Localized Resources in Smartsite WinForm Applications

Release 1.0 - ...

For a localized Windows Form, the form designer generates code in the InitializeComponent method that creates an instance of the system ComponentResourceManager and uses it to assign localizable property values from the associated form resource file for the current UI culture.

As a result of using a standard system resource manager, when a localized form is loaded, all resources are retrieved from the embedded assembly resource and any existing Smartsite custom resource files are ignored.

In order to apply custom resources, the form should create and use a custom SmartsiteComponentResourceManager. Since the Visual Studio Forms Designer knows nothing about our custom class, this code needs to be added manually, e.g. in the form constructor after the call to the InitializeComponent method.

The helper class ChangeFormCulture exposes several static methods that facilitate changing the culture of a form (or all forms) at runtime. The methods will recursively enumerate all controls on the form and re-assign the values of localized control properties from the resource that matches a given culture.

The downside of this approach is that the form will fetch and apply resource values twice; first the form constructor will call the InitializeComponent method and assign resources using the system ResourceManager. Then, after the function returns, the form creates a SmartsiteComponentResourceManager instance and re-initializes all localizable (control) properties.

As of now, there is no specific solution for this problem – except hacking the form-designer generated code by hand and changing the InitializeComponent method to create a SmartsiteComponentResourceManager instead of a system ComponentResourceManager. However, each time that the form is edited in the form designer, it regenerates the initialization code and any custom changes are lost.

Another approach is to author a custom form designer serializer that ouputs custom code to the InitializeComponent method. The Smartsite.Tools library currently exposes such a component called SmartsiteResourceManagerSetter, displayed in the ToolBox window. To use the component with a form, open the form in the form designer and drag an instance from the toolbox onto the form. This serializer component will append a SmartsiteComponentResourceManager declaration to the InitializeComponent method. However, the original ComponentResourceManager declaration cannot be removed, and the repeated assignment apparently confuses the form designer in such a way that it can’t display the form anymore. Therefore, this serializer component is currently not usable in practice.