ASP.NET MVC localization DisplayNameAttribute alternatives: a good way

Technorati Tags:,,

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…

This article is part of the GWB Archives. Original Author: Brian Schroer

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.