Localizing attributes in properties

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve Teeples

    Localizing attributes in properties

    I have placed text within a resouce file.

    The class has a reference to a Resource Manager LocRM created in the
    constructor. I want to reference the text within the resource file when
    declaring a property. See below.

    [Description(thi s.LocRM.GetText ("strTestCatego ryID")),
    Category(TestCa seStrings.strLa belDescription) ,
    DefaultValueAtt ribute(TestingC ategories.Testi ngCategory.Misc ellaneous)]
    public virtual TestingCategori es.TestingCateg ory TestCategory
    {
    get { return category; }
    set { category = value; }
    }

    Obviously the compiler generates an error because it is expecting a constant
    string where I'd like the this.LocRM("... .").

    How do I reference text within a resource file and place it in an attribute
    above?

    --
    Steve
  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: Localizing attributes in properties

    Steve,
    I don't have a link to an example handy of overriding it, my understanding
    is you need to derive a new class from DescriptionAttr ibute & override the
    Description Property, saving its value in the DescriptionValu e property...



    Hope this helps
    Jay

    "Steve Teeples" <SteveTeeples@d iscussions.micr osoft.com> wrote in message
    news:AFC16648-0609-414C-B6DC-159C6AB721D3@mi crosoft.com...
    |I have placed text within a resouce file.
    |
    | The class has a reference to a Resource Manager LocRM created in the
    | constructor. I want to reference the text within the resource file when
    | declaring a property. See below.
    |
    | [Description(thi s.LocRM.GetText ("strTestCatego ryID")),
    | Category(TestCa seStrings.strLa belDescription) ,
    | DefaultValueAtt ribute(TestingC ategories.Testi ngCategory.Misc ellaneous)]
    | public virtual TestingCategori es.TestingCateg ory TestCategory
    | {
    | get { return category; }
    | set { category = value; }
    | }
    |
    | Obviously the compiler generates an error because it is expecting a
    constant
    | string where I'd like the this.LocRM("... .").
    |
    | How do I reference text within a resource file and place it in an
    attribute
    | above?
    |
    | --
    | Steve


    Comment

    • Steve Teeples

      #3
      Re: Localizing attributes in properties

      Jay,

      I derived the class. See below. By calling MyDesciption in the property
      it now correctly gets the string. Thanks so much.

      /// <summary>
      /// Overrides the DescriptionAttr ibute class in order to allow for
      localization.
      /// </summary>
      public class MyDescriptionAt tribute :
      System.Componen tModel.Descript ionAttribute
      {
      public MyDescriptionAt tribute(string attribute)
      {
      this.attribute = attribute;
      }
      #region Fields
      protected CheckItResource s LocRS = new CheckItResource s();
      protected string attribute;
      #endregion // Fields

      /// <summary>
      /// Returns the localized string for the attribute.
      /// </summary>
      public override string Description
      {
      get
      {
      return this.Descriptio nValue =
      LocRS.GetText(t his.attribute);
      }
      }
      }



      "Jay B. Harlow [MVP - Outlook]" wrote:
      [color=blue]
      > Steve,
      > I don't have a link to an example handy of overriding it, my understanding
      > is you need to derive a new class from DescriptionAttr ibute & override the
      > Description Property, saving its value in the DescriptionValu e property...
      >
      > http://msdn.microsoft.com/library/de...valuetopic.asp
      >
      > Hope this helps
      > Jay
      >
      > "Steve Teeples" <SteveTeeples@d iscussions.micr osoft.com> wrote in message
      > news:AFC16648-0609-414C-B6DC-159C6AB721D3@mi crosoft.com...
      > |I have placed text within a resouce file.
      > |
      > | The class has a reference to a Resource Manager LocRM created in the
      > | constructor. I want to reference the text within the resource file when
      > | declaring a property. See below.
      > |
      > | [Description(thi s.LocRM.GetText ("strTestCatego ryID")),
      > | Category(TestCa seStrings.strLa belDescription) ,
      > | DefaultValueAtt ribute(TestingC ategories.Testi ngCategory.Misc ellaneous)]
      > | public virtual TestingCategori es.TestingCateg ory TestCategory
      > | {
      > | get { return category; }
      > | set { category = value; }
      > | }
      > |
      > | Obviously the compiler generates an error because it is expecting a
      > constant
      > | string where I'd like the this.LocRM("... .").
      > |
      > | How do I reference text within a resource file and place it in an
      > attribute
      > | above?
      > |
      > | --
      > | Steve
      >
      >
      >[/color]

      Comment

      Working...