1

I'm trying to extend the DisplayAttrubte Logic from Mvc. The bad thing is since DisplayAttribute is a sealed class I'm not able to inherit. Therefore I inherit from DisplayNameAttribute.

public class TranslatedDisplayAttribute : DisplayNameAttribute
{
    public override string DisplayName
    {
        get
        {
            var myVariableName = ???;
            return TranslationService.String( myVariableName  );
        }
    }
}

And I want to use this Attribute just like the DisplayAttribute.

public class MyDto {

    [TranslatedDisplay]
    public string myVariable { get; set; }
}

Is it possible to get the variables name, where the Attribute is assigned so that in the TranslatedDisplayAttribute implementaion the variable myVariableName is automatically set to the variables name (myVariable)? I dont'want to do something like this.

public class MyDto {

    [TranslatedDisplay( Name = "myVariable" )]
    public string myVariable { get; set; }
}

Edit

And I dont want to use the translation approach with Resx files. But @Romias thank you for your answer.

[Display(Name = "XXX_YourPropertyItem", ResourceType = typeof(YourResourceFile))]
public string YourProperty { get; set; }
Arndt Bieberstein
  • 1,128
  • 1
  • 14
  • 28

1 Answers1

1

May be you are aware of this, and you want to do the translation in another way, but the built-in method is using another overload of the Display attribute, using Resource files.

[Display(Name = "XXX_YourPropertyItem", ResourceType = typeof(YourResourceFile))]
public string YourProperty { get; set; }

So, "XXX_YourPropertyItem" is the item in the resource file, and the "YourResourceFile" is the name of the ResourceFile class.

Romias
  • 13,783
  • 7
  • 56
  • 85
  • I don't use Resx files that is why I don't want to use this approach. My translated resources are stored in a SQL database. – Arndt Bieberstein Feb 14 '15 at 09:19
  • May be your approach could be extend the Resource provider... to get the translation from database. Check this: http://stackoverflow.com/a/2466530/7720 – Romias Feb 14 '15 at 21:39