167

I have a localized application, and I am wondering if it is possible to have the DisplayName for a certain model property set from a Resource.

I'd like to do something like this:

public class MyModel {
  [Required]
  [DisplayName(Resources.Resources.labelForName)]
  public string name{ get; set; }
}

But I can't to it, as the compiler says: "An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type" :(

Are there any workarounds? I am outputting labels manually, but I need these for the validator output!

6 Answers 6

388

If you use MVC 3 and .NET 4, you can use the new Display attribute in the System.ComponentModel.DataAnnotations namespace. This attribute replaces the DisplayName attribute and provides much more functionality, including localization support.

In your case, you would use it like this:

public class MyModel
{
    [Required]
    [Display(Name = "labelForName", ResourceType = typeof(Resources.Resources))]
    public string name{ get; set; }
}

As a side note, this attribute will not work with resources inside App_GlobalResources or App_LocalResources. This has to do with the custom tool (GlobalResourceProxyGenerator) these resources use. Instead make sure your resource file is set to 'Embedded resource' and use the 'ResXFileCodeGenerator' custom tool.

(As a further side note, you shouldn't be using App_GlobalResources or App_LocalResources with MVC. You can read more about why this is the case here)

9
  • This is good for the specific example used here but it won't work for the majority of dynamic property setters that want strings.
    – kingdango
    Dec 6, 2011 at 15:28
  • 1
    This is good when we have all our locale with us before deploying to production. But if I want to call db for db strings then this kind of approach is not proper. Also the disadvantage of resource file, is that it requires compilation again to effect the changes, that means you need to release another version to client. I am not saying this as a bad approach, please dont feel like that
    – kbvishnu
    Oct 15, 2012 at 11:28
  • Just a problem : we have to know resource type in the model. I have a model DLL and a website in two distinct projects. I'd like to be able to set display names in model and set resource type in website...
    – Kek
    Oct 23, 2012 at 19:29
  • 1
    Get Resharper, and create the resource file in your project, and it will automatically remind and then help you to move the Name into the resource file.
    – anIBMer
    May 22, 2013 at 2:14
  • 17
    With C# 6, instead of Name = "labelForName" you can also use Name = nameof(Resources.Resources.labelForName).
    – Uwe Keim
    May 31, 2016 at 5:18
115

How about writing a custom attribute:

public class LocalizedDisplayNameAttribute: DisplayNameAttribute
{
    public LocalizedDisplayNameAttribute(string resourceId) 
        : base(GetMessageFromResource(resourceId))
    { }

    private static string GetMessageFromResource(string resourceId)
    {
        // TODO: Return the string from the resource file
    }
}

which could be used like this:

public class MyModel 
{
    [Required]
    [LocalizedDisplayName("labelForName")]
    public string Name { get; set; }
}
11
  • Yes, I worked on a similar solution this morning and it it feasible. Then I found this post which has the same approach: adamyan.blogspot.com/2010/02/…
    – Palantir
    Mar 12, 2010 at 12:40
  • 26
    @Gunder his post, below this one (with the most votes), is a much nicer solution. Just for people who only read the Accepted posts
    – 321X
    May 2, 2012 at 21:18
  • 1
    This actually does NOT work for accessing different translations since it will return the same value for all users no mater what. Store resourceid in a local variable and override DisplayName instead
    – Fischer
    Dec 4, 2012 at 11:24
  • 5
    Suggestion for TODO: return Resources.Language.ResourceManager.GetString(resourceId); Mar 25, 2013 at 4:22
  • 4
    You should use: [Display(Name = "labelForName", ResourceType = typeof(Resources.Resources))] as describe below... Oct 17, 2013 at 9:47
39

If you open your resource file and change the access modifier to public or internal it will generate a class from your resource file which allows you to create strongly typed resource references.

Option for resource file code generation

Which means you can do something like this instead (using C# 6.0). Then you dont have to remember if firstname was lowercased or camelcased. And you can see if other properties use the same resource value with a find all references.

[Display(Name = nameof(PropertyNames.FirstName), ResourceType = typeof(PropertyNames))]
public string FirstName { get; set; }
3
  • will this work with Winforms? I have a class which i added Display annotation from resources and i used GetAttributeFrom method from link to get the name of the property but it doesn't show the localized one! Jul 24, 2016 at 14:54
  • 3
    Are you using attribute.Name or attribute.GetName() to get the localized text? Documentation for .Name says "Do not use this property to get the value of the Name property. Use the GetName method instead." msdn.microsoft.com/en-us/library/…
    – Tikall
    Jul 27, 2016 at 10:30
  • Yes, i figured it out that i had to use GetName(). Thanks Aug 14, 2016 at 18:43
20

Update:

I know it's too late but I'd like to add this update:

I'm using the Conventional Model Metadata Provider which presented by Phil Haacked it's more powerful and easy to apply take look at it : ConventionalModelMetadataProvider


Old Answer

Here if you wanna support many types of resources:

public class LocalizedDisplayNameAttribute : DisplayNameAttribute
{
    private readonly PropertyInfo nameProperty;

    public LocalizedDisplayNameAttribute(string displayNameKey, Type resourceType = null)
        : base(displayNameKey)
    {
        if (resourceType != null)
        {
            nameProperty = resourceType.GetProperty(base.DisplayName,
                                           BindingFlags.Static | BindingFlags.Public);
        }
    }

    public override string DisplayName
    {
        get
        {
            if (nameProperty == null)
            {
                return base.DisplayName;
            }
            return (string)nameProperty.GetValue(nameProperty.DeclaringType, null);
        }
    }
}

Then use it like this:

    [LocalizedDisplayName("Password", typeof(Res.Model.Shared.ModelProperties))]
    public string Password { get; set; }

For the full localization tutorial see this page.

1
  • 1
    +1. Haack's solution is definitely the most elegant one compared to the others here. It fits very well into the convention-based style of programming in ASP.NET MVC and is easily implemented through a single nuget-command and a single line of code in Global.asax.cs.
    – Nilzor
    Feb 14, 2014 at 13:08
11

I got Gunders answer working with my App_GlobalResources by choosing the resources properties and switch "Custom Tool" to "PublicResXFileCodeGenerator" and build action to "Embedded Resource". Please observe Gunders comment below.

enter image description here

Works like a charm :)

1
  • This results in a resource file that will be compiled just as if you had added a resource file outside of App_GlobalResources. So your resource file will no longer behave as a "App_GlobalResources" resource file, which is absolutely fine. But you should just be aware of it. So you no longer have the "benefits" of putting the resource file in the App_GlobalResources. You could just as well have put it somewhere else.
    – René
    May 22, 2013 at 8:07
6
public class Person
{
    // Before C# 6.0
    [Display(Name = "Age", ResourceType = typeof(Testi18n.Resource))]
    public string Age { get; set; }

    // After C# 6.0
    // [Display(Name = nameof(Resource.Age), ResourceType = typeof(Resource))]
}
  1. Define ResourceType of the attribute so it looks for a resource
  2. Define Name of the attribute which is used for the key of resource, after C# 6.0, you can use nameof for strong typed support instead of hard coding the key.

  3. Set the culture of current thread in the controller.

Resource.Culture = CultureInfo.GetCultureInfo("zh-CN");

  1. Set the accessibility of the resource to public

  2. Display the label in cshtml like this

@Html.DisplayNameFor(model => model.Age)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.