How to make Multilanguage supported Category and Description Attributes?

If you have written any .NET components you must be familiar with Category and Description attributes. We use Category attribute to identify the property’s category and Description attribute is for the short description of that property. But because of the nature of Attributes in .NET we cannot pass any parameter to these attributes for Multilanguage support.

[Category(Properties.Resources.MyCategory)]
[Description(Properties.Resources.MyDescription)]
public int MyProperty { … }

This kind of usage is not allowed for .NET, because we are only allowed to give constant values to the attributes constructor.  So how can we use these attributes as Multilanguage?

Solution is simple;
Deriving two new attributes from Category and Description attributes will handle our problem. Because PropertyGrid uses PropertyDescriptors which resolves properties description and category from these two attributes, so if we derived our new attributes from DescriptionAttribute and CategoryAttribute this will also continue to work for PropertyGrid and all kind of grids which uses PropertyDescriptors.

Our new attributes will be;

[AttributeUsage(AttributeTargets.All)]
internal class SRDescriptionAttribute :
DescriptionAttribute
{
    bool ps = false
;

    public SRDescriptionAttribute(string
description) : base(description)
    {    }

    public override string
Description
    {
       
get
       
{
            if
(!ps)
            {
                ps = true
;
                // Here we read the multilanguaged text from the resources
                // by using given Description text as Key
                base.DescriptionValue = Properties.Resources.ResourceManager.GetString(base
.Description);
            }
            return base
.Description;
        }
    }
}


[AttributeUsage(AttributeTargets.All)]
internal sealed class SRCategoryAttribute :
CategoryAttribute
{
    public SRCategoryAttribute(string
category) : base(category)
    {  }

    protected override string GetLocalizedString(string
value)
    {
        // Here we read the multilanguaged text from the resources
        // by using given Category text as Key
        return Properties.Resources
.ResourceManager.GetString(value);
    }
}



Now we can use these attributes. The only thing that we have to do is giving the resource names as the description and category to the attributes.

Usage:
[SRCategory(“Cat_MyCategory”)]
[SRDescription(“Des_MyProperty”)]
public int MyProperty { … }
* Cat_MyCategory and Des_MyProperty are the names of resources in the resource file.

Actually as you see, for the Category attribute GetLocalizedString method and for the Description attribute Description property were both was written as virtual, which means that derived classes can override them (They wish Smile so).


Related posts

Add comment


 

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

December 5. 2008 09:43 AM


Search

Calendar

<<  December 2008  >>
MonTueWedThuFriSatSun
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234
View posts in large calendar

Disclaimer

© 2007 - 2008
Ozcan DEGIRMENCI
All rights reserved. The content can be used elsewhere given that the source is properly acknowledged.