Localization in System.Data.ComponentModel.DataAnnotations

Last post 09-15-2009 4:33 PM by geomar. 11 replies.

Sort Posts:

  • Localization in System.Data.ComponentModel.DataAnnotations

    05-25-2009, 5:53 AM

    I am having some issues with localization in System.Data.ComponentModel.DataAnnotations

     If I set the error message as string it works.

     

    1    [Required(ErrorMessage = "Name is required.")]
    2 public string FullName{ get; set; }
     

    but if i set error message as resource it doesnot work.

     

    1    [Required(ErrorMessageResourceName= "FullNameRequired", ErrorMessageResourceType = typeof(MyResource)]
    2 public string FullName{ get; set; }
     

     

    any suggestions???

  • Re: Localization in System.Data.ComponentModel.DataAnnotations

    05-26-2009, 10:58 PM
    Answer

    Hi,

    From your description, it seems that you get the error when you use ErrorMessageResourceName attribute, right?

    As you didn't provide the error details, I just list some general error causes related to that attribute for you to refer.

    This property specifies the error message resource that is associated with a validation control. You can use this property to specify a resource file that contains localizable error messages.

    Note the exception type:

    InvalidOperationException : If the ValidationAttribute is already in explicit mode or if you set the property multiple times.
     
    ArgumentException : If the value of error message is nullNothingnullptra null reference (Nothing in Visual Basic).

    Thanks.
     

    Michael Jin.
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Localization in System.Data.ComponentModel.DataAnnotations

    05-27-2009, 1:00 AM

     Thanks Nai-Dong Jin for your reply.

     I am not getting any error or expection. The error message is not displayed, if I specipfy the name and type of the resource file. But if I use the ErrorMessage attribute it works.

  • Re: Localization in System.Data.ComponentModel.DataAnnotations

    06-10-2009, 10:10 AM
    • Member
      22 point Member
    • bmoyno
    • Member since 02-28-2006, 12:44 PM
    • Posts 45

    Hi anonymouswrites,

    Could you pass on a bit of advice about using ErrorMessageResourceName and ErrorMessageResourceType with resource files. I am using an MVC app and we are setting the data annotations in the Data Model. Should the resource file (in a App_LocalResources folder) be at the Data Model or where the View is contained?

    Also, could you give a concrete example of 

    [Required(ErrorMessageResourceName= "FullNameRequired", ErrorMessageResourceType = typeof(MyResource)]
     
    For example, what do "FullNameRequired" and "MyResource" stand for ... just a little confusing looking in.
  • Re: Localization in System.Data.ComponentModel.DataAnnotations

    06-11-2009, 8:30 AM

     I am having my resource files in the place where I have my model.

     

    [Required(ErrorMessageResourceName= "FullNameRequired", ErrorMessageResourceType = typeof(MyResource)]
    As per the example the FullNameRequired is the key for the  Resource that has error message for validation. and MyResource is the name of my resource file "MyResource.resx".  
  • Re: Localization in System.Data.ComponentModel.DataAnnotations

    06-11-2009, 9:03 AM
    • Member
      22 point Member
    • bmoyno
    • Member since 02-28-2006, 12:44 PM
    • Posts 45

    Thanks anonymouswrites, I get the "FullNameRequired" bit and understand the "MyResource" bit also, but I'm getting a "'MyResource' could not be found" compile error.

    The MyResource.resx file is contained within the 'App_LocalResources' folder now under the Model folder where my Data Models are.

    I'm struggling to understand how you can reference the resource in a typeof(). Sorry about this as I'm sure it's very straighforward.

  • Re: Localization in System.Data.ComponentModel.DataAnnotations

    06-13-2009, 2:08 AM

    I am having my MyResource.resx directly under the model folder but not in side the App_LocalResources. If you want to use your resource values in view then you need to put your Resources in App_LocalRescorces.

    hope this helps

  • Re: Localization in System.Data.ComponentModel.DataAnnotations

    06-24-2009, 7:09 AM
    • Member
      22 point Member
    • bmoyno
    • Member since 02-28-2006, 12:44 PM
    • Posts 45

    This only worked for me when I converted the .resx into a .cs file. This seems a very awkward solution so I might stick with the web forms approach to localisation and resource files.

    Am I missing something with the resource files and having to convert them to .cs files?

  • Re: Localization in System.Data.ComponentModel.DataAnnotations

    06-26-2009, 7:07 AM
    • Member
      22 point Member
    • bmoyno
    • Member since 02-28-2006, 12:44 PM
    • Posts 45

    I looked into this some more and it turns out the in the designer of the resources file one must set the 'Access Modifier' to 'Public' to create the .designer.cs file.

    Also in the properties of the .resx file, set the 'Build Action' to 'Embedded Resource'.

    You should now be able to use the 'typeof()' method against your resource type.

  • Re: Localization in System.Data.ComponentModel.DataAnnotations

    06-29-2009, 4:28 AM
    • Member
      22 point Member
    • bmoyno
    • Member since 02-28-2006, 12:44 PM
    • Posts 45

    How do I get localised (localized) content to appear. If I want french, german, spanish etc to be displayed instead of the english version?

    Any ideas?

  • Re: Localization in System.Data.ComponentModel.DataAnnotations

    07-09-2009, 4:51 AM
    • Member
      22 point Member
    • bmoyno
    • Member since 02-28-2006, 12:44 PM
    • Posts 45

    I needed to add the following entry under <server.web> in the web.config of my mvc app ...


    <globalization enableClientBasedCulture="true" culture="auto" uiCulture="auto" />


    Now the data annotations are finally appearing localised Smile

  • Re: Localization in System.Data.ComponentModel.DataAnnotations

    09-15-2009, 4:33 PM
    • Member
      144 point Member
    • geomar
    • Member since 10-10-2006, 3:23 PM
    • Posts 58

     Check out Pro ASP.NET MVC Framework By APress.

    1) Create App_GlobalResources folder at the root of your application

    2) Place resx file(s) in the App_GlobalResources folder.

    3) Set BuildAction for each .resx file to EmbeddedResource

    4) The .resx file generates a .cs file.  Open the .cs file.  You will see that each of the error messages is represented as a property within the .cs file.  Change the scope of each property from internal to public. 

    (If you place the .resx file(s) into a separate class library project, the UI for .resx will provide a dropdown list to set the properties to public. In this case, you will not have to open the .cs generated files, neither will you need the App_GlobalResources folder. This dropdown list is disabled when the .resx file resides in the web application.)

    5) In web.config under <system.web> enter the following:

        <globalization enableClientBasedCulture="true" culture="auto" uiCulture="auto" />

     

Page 1 of 1 (12 items)