4

not sure why, but the data annotation in MVC3 insist on having constant values, which i just can't understand for things like error messages and display names. I love these annotations, they are so easy to use and so powerful, but what if you need to support multiple languages?

Imagine i have the following model:

public class Person
{
    public string First_Name { get; set; }
}

If i dont change anything and use the CRUD views that MVC3 will build for me I get a label next to the text fields that says "First_Name", so i add the data annotation to change the display name like so:

public class Person
{
    [Display(Name="First name")]
    public string First_Name { get; set; }
}

Which works just fine. But i want to supply a different display name depending on the users language, using a function i made previously GetDisplayName(string ToGet, string Language) which just returns the string i am interested in, but if i change my data annotation to this:

public class Person
{
    [Display(Name=GetDisplayName("First_Name", "English"))]
    public string First_Name { get; set; }
}

Then i get a compiler error telling me that the annotation requires a constant values, WHY????

Does anyone know a way to accomplish what i am trying to do? Thanks

UPDATE

Ok, it appears that the best way to do this is with .resx resource files as per several answers below and those in other posts. which works great for the most part.

Does anyone know how i can request a resource with a variable name? not in the context of data attributes this time, but just in controllers and views.

Basically at the moment i am getting at the resources with @Resources.SomeKey but i would like to be able to use that within a function in a @Resources["SomeOtherKey"] where SomeOtherKey is a dynamically generated string.

5 Answers 5

7

If you have your translated property names in Resource Files, that is supported out of the box... no need to extend the framework.

Just use:

[Display(Name = "Resource_Key", ResourceType = typeof(DataFieldLabels))]
public string myProperty { get; set; }

Where the "Resource_Key" is the key in your resource files... and the "DataFieldLabels" is the name of your base resource file.

EDIT:

In order to access resource files dynamically you can do:

ResourceSet resset = ResourceManager.GetResourceSet(culture, true, false);
var translated = resset.GetString("myToken")
6
  • 1
    Oh right, I have been using my database to store all the localised Text, i should imagine you are all going to tell me that's the wrong thing to do now..... ;-)
    – Ben
    Jul 25, 2012 at 19:06
  • 1
    no but you'll beed a db resource provider: weblogs.asp.net/thangchung/archive/2010/06/25/… Jul 25, 2012 at 19:53
  • I have spent the day switch over to use the .resx XML files for storing my text, because follow your (and others) answer i have decided that is the way to go. Romias or @AdamTuliper , do you know how i can request a resource with a variable name? not in the context of data attributes this time, but just in controllers and views. Basically at the moment i am getting at the resources with @Resources.SomeKey but i would like to be able to use that within a function in a @Resources["SomeOtherKey"] where SomeOtherKey is a dynamically generated string. will update question too
    – Ben
    Jul 26, 2012 at 15:21
  • 1
    ResourceSet resset = ResourceManager.GetResourceSet(culture, true, false); resset.GetString("myToken")
    – Romias
    Jul 26, 2012 at 15:29
  • my ResourceManager doesnt seem to have a GetResourceSet() method :-(
    – Ben
    Jul 27, 2012 at 9:17
1

DataAnnotations param values require constants, i.e. actual strings.

2
  • Actually, this is true about all Attributes, not just DataAnnotations, but you've got the right idea.
    – Jim Counts
    Jul 25, 2012 at 17:08
  • That's what I figured, but the question was why and is there anyway around it.
    – Ben
    Jul 25, 2012 at 19:01
1

You can only assign constants.

You can do an extension and assign keyValues there and then the extension fetches from somewhere else, like a database or a resource file or a webservice or whatever.

0

You may want to check Hanselman's post regarding internationalization. Maybe it could help

-1
[Display(Name = "strResourceString", ResourceType = typeof(Resourcefile.strResourceString))]
1
  • the question was asking if variables can be used; not resources. Sep 21, 2012 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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