ASP.NET MVC localization DisplayNameAttribute alternatives: a good way
- by Brian Schroer
The ASP.NET MVC HTML helper methods like .LabelFor and .EditorFor use model metadata to autogenerate labels for model properties. By default it uses the property name for the label text, but if that’s not appropriate, you can use a DisplayName attribute to specify the desired label text: [DisplayName("Remember me?")]
public bool RememberMe { get; set; }
I’m working on a multi-language web site, so the labels need to be localized. I tried pointing the DisplayName attribute to a resource string:
[DisplayName(MyResource.RememberMe)]
public bool RememberMe { get; set; }
…but that results in the compiler error "An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type”.
I got around this by creating a custom LocalizedDisplayNameAttribute class that inherits from DisplayNameAttribute:
1: public class LocalizedDisplayNameAttribute : DisplayNameAttribute
2: {
3: public LocalizedDisplayNameAttribute(string resourceKey)
4: {
5: ResourceKey = resourceKey;
6: }
7:
8: public override string DisplayName
9: {
10: get
11: {
12: string displayName = MyResource.ResourceManager.GetString(ResourceKey);
13:
14: return string.IsNullOrEmpty(displayName)
15: ? string.Format("[[{0}]]", ResourceKey)
16: : displayName;
17: }
18: }
19:
20: private string ResourceKey { get; set; }
21: }
Instead of a display string, it takes a constructor argument of a resource key. The DisplayName method is overridden to get the display string from the resource file (line 12).
If the key is not found, I return a formatted string containing the key (e.g. “[[RememberMe]]”) so I can tell by looking at my web pages which resource keys I haven’t defined yet (line 15).
The usage of my custom attribute in the model looks like this:
[LocalizedDisplayName("RememberMe")]
public bool RememberMe { get; set; }
That was my first attempt at localized display names, and it’s a technique that I still use in some cases, but in my next post I’ll talk about the method that I now prefer, a custom DataAnnotationsModelMetadataProvider class…