views:

672

answers:

2

I'm trying to localise a WinForms app for multiple languages. I'm trying to find a way to set my form labels/buttons text properties to read from the resources file in the designer (rather than having to maintain a chunk of code that sets them programatically).

I've found I can set form.Localizable=true, but then the resources are read from a file alongside the form, but many of mine are shared across multiple forms.

Is there any way to set a label's text in the designer, to a value stored in a project-level resx file?

A: 

The only way I can think of would be to create a custom control that would add a property for the resource name. When the property is set, grab the value from the project resource file and set the text property with it. You will want to make sure that Text doesn't get serialized or it might overwrite the value set by ResourceName.

public class ResourceLabel
    : Label
{
    private string mResourceName;
    public string ResourceName
    {
        get { return mResourceName; }
        set
        {
            mResourceName = value;
            if (!string.IsNullOrEmpty(mResourceName))
                base.Text = Properties.Resources.ResourceManager.GetString(mResourceName);
        }
    }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override string Text
    {
        get { return base.Text; }
        set 
        { 
            // Set is done by resource name.
        }
    }
}
Brian
I thought about something similar, but this seems more nasty than having one resx per file or setting them in code.I'm disappointed that there appears no built-in way to do this, it seems only a minor tweak from what exists with the Localizable property, only that it needs a Resx file property too! :(
Danny Tuppeny
A: 

To answer the question, no.

But IMO, this should not be done anyways if the text will be static.

Have a read at my answers on localization and resources:
Resource string location
Globalize an existing Windows Forms application
Using .resx files for global application messages

Jon Seigel