views:

799

answers:

3

Hi,

I want to use localization feature for Validation messages, for eg-

    [Required(ErrorMessageResourceName = "RequiredField", ErrorMessageResourceType = typeof( ))]
     public string someText
     { get... set...}

I'm using MVVM pattern so this property is in my model(its a differnt project inside same solution of silverlight) and all my localization resources are in the App.current.Resources. How can I set the ErrorMessageResourceType to my App resources?

Please suggest.

Thanks in advance
Sai

A: 

Well apparently Localization of error messages isnt as straightforward. You are supposed to add a resource file to the MyApp.Web project, that is the asp.net site that hosts your silverlight app, then add that resource to the silverlight app, then you will be able todo the code you stated in your question after some tweaks, follow the instructions below

This section explores how error messages can be localized by storing them in resource files and sharing them across tiers.

The example uses .NET RIA Services walkthrough project as the base project and builds on top of it.

Let's say we want to add a validation error as a resource for LoginID field.

  • Create a new ‘Resources' folder in the HRApp.Web project (server project)

  • Add a new resource file to this folder and name it ValidationErrorResources.resx

  • Double click on the .RESX file to bring up resource designer page

  • Add a new string resource with Name= LoginIDValidationError and Value= "LoginID field is required"

  • Change the access modifier to ‘Public' by clicking on the ‘Access Modifier' drop down UI and selecting ‘Public' and save the project. This generates a ValidationErrorResources class in the HRApp.Web.Resources namespace.

  • Open ‘OrganizationService.metadata.cs' file and add the following ‘Required' field validation to LoginID member. Specify the error message resource name and resource type values by setting the corresponding attribute members as shown below.

[Required(ErrorMessageResourceName = "LoginIDValidationError", ErrorMessageResourceType = typeof(ValidationErrorResources))]

public string LoginID;

Now we want to share this resource file in the Silverlight project (client project). To do this,

  • Create a folder Web\Resources in the HRApp project (folder structure must match the resource file namespace on the server side)

  • Select Resources folder and bring up Add Existing file dialog, browse to the server side resource file folder location

  • Select ValidationErrorResources.resx and ValidationErrorResources.designer.cs files, and add them as link files to the Silverlight project. Save the project file

  • Open HRApp.csproj file in notepad , locate the section where .designer.cs file is included and add the highlighted 3 lines to this section

   <Compile
 Include="..\HRApp.Web\Resources\ValidationErrorResources.Designer.cs">

     <AutoGen>True</AutoGen>

     <DesignTime>True</DesignTime>

     <DependentUpon>ValidationErrorResources.resx</DependentUpon>

     <Link>Web\Resources\ValidationErrorResources.Designer.cs</Link>
 </Compile>
  • Save the project file and reload the project in Visual Studio

  • Build the solution and run

Now whenever the validation fails for the LoginID field the error message from the resource file is shown to the user. The resource file can now be customized to store locale specific error messages.

Neil
A: 

When I did this recently this thred helped alot: http://forums.asp.net/t/1433699.aspx

In particular "...the resource file must be converter to a class before being able to reference it in the typeof of the ErrorMessageResourceType in the data annotation..."

Also there are a few other useful hits from the main search engines: http://www.liquidjelly.co.uk/supersearch/?q=silverlight%20dataannotations%20localization&amp;lang=en-GB

HTH,
Mark

Mark Cooper
A: 

This solution almost worked for me. I had to made some arrangements to work with a data model (edmx) located in one project, DataDomainService (Ria) in other and the Silverlight access layer in other project. When i compile the HRApp equivalent in my situation, the metadata containing the validation info for some property is not generated. It says that the client has no access to the ValidationErrorResources type. But after following all the instructions mentioned above plus some others to get a correct resource namespace, the client CAN access ValidationErrorResources. It works if i write it myself to the generated Silverlight class. So seems like this kind of project separation is not quite supported by the class generator... But thanks anyway, this post was quite helpful and maybe i'll make it all work in a couple of days. :D

André Matos