Arndt Bieberstein
Arndt Bieberstein

Reputation: 1128

.NET Mvc Localization

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; }

Upvotes: 1

Views: 59

Answers (1)

Romias
Romias

Reputation: 14133

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.

Upvotes: 1

Related Questions