As most of my followers know I am now working on MVC Enterprise project and when the time come to start refactoring my team code I found that, they didn't use DsipalyName attribute to read from the resource file (No hard coded string to support multilingual) and when I search why they doing this I found that there is no one, so I decide to create an extension method that support this feature, of curse I search on the internet and I found a solution included in a new feature in MVC (MVC Futures release.
but it needs some configuration and I don't want any new modification to my project until I completely convert it to MVC 3.0 RTM soon. so here my custom method to create a localized version of DsipalyName attribute. So first I will create a class that override DisplayName
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
using System.ComponentModel;
using System.Reflection;
using System.Resources;
namespace Radwan
{
public class DisplayNameLocalizedAttribute : DisplayNameAttribute
{
private readonly string resourceFileName;
private readonly string resourceKey;
private readonly Type type;
public DisplayNameLocalizedAttribute(Type type, string resourceKey)
{
this.type = type;
this.resourceKey = resourceKey;
}
public override string DisplayName
{
get
{
return new ResourceManager(this.type.FullName, Assembly.GetExecutingAssembly()).GetString(this.resourceKey);
}
}
}
}
so now I can call the new version like the following:
1
2
3
4
5
6
7
8
public class Employee : IEmployee
{
public int Id { get; set; }
[DisplayNameLocalized(typeof(ResourceFileName), "Name")]
public string Name { get; set; }
}
That's it.