Alex Rivera | Logout

how to localize a property description in c#?

Asked 2011-09-13T08:07:23.677
23

I'm working on a class that is going to be used by some people from another countries. I have to localize every message, warning e.c. so that they can understand what we mean. In many cases i achieved my goal. But these property attributes like descriptions are such a pain in the ass.

Here`s what I have right now:

[Category("Editable Values"), Description("Sets the minimum select...")]
    public Ampere Simin
    {
        get
        {...}
        set
        {...}
    }

and

[Category("Editable Values"), Description(Localisation.Simin)] // "Localisation" here is the internal resource file that i wrote for messages, warnings, exceptions and -unfortunately- descriptions
        public Ampere Simin
        {
            get
            {...}
            set
            {...}
        }

That's what I'm trying to do. But it's not possible to use Localisations this way. Any Suggestions about something that I can use instead of it?

Edit
Report

1 Answer

3

Let's take this sample class:

// ------------------------------------------------------------------------
public class Customer
{
    // ----------------------------------------------------------------------
    [Category( "Editable Values" ), LocDescription( "FirstName", "Sets the first name..." )]
    public string FirstName { get; set; }

    // ----------------------------------------------------------------------
    [Category( "Editable Values" ), LocDescription(  Key = "LastName", DefaultDescription = "Sets the last name..." )]
    public string LastName { get; set; }
} // class Customer

Now you can implement a custom attribute class:

// ------------------------------------------------------------------------
public class LocDescriptionAttribute : DescriptionAttribute
{
    // ----------------------------------------------------------------------
    public LocDescriptionAttribute()
    {
    } // LocDescriptionAttribute

    // ----------------------------------------------------------------------
    public LocDescriptionAttribute( string key, string defaultDescription ) :
        base( defaultDescription )
    {
        Key = key;
        DefaultDescription = defaultDescription;
    } // LocDescriptionAttribute

    // ----------------------------------------------------------------------
    public string Key { get; set; }

    // ----------------------------------------------------------------------
    public string DefaultDescription { get; set; }

    // ----------------------------------------------------------------------
    public override string Description
    {
        get
        {
            // load from resx
            string description = Strings.GetString( Key );
            if ( string.IsNullOrEmpty( description ) )
            {
                description = DefaultDescription;
            }
            return description;
        }
    } // Description
} // class LocDescriptionAttri
answered 2011-09-13T09:32:48.467

Your Answer