0
votes

I write an ASP .Net Core web application with data annotation validation. In attributes describing model properties I define ErrorMessageResourceType and ErrorMessageResourceName.

Resource files are placed in the different project outside main app solution. (using dotnet CLI with support of Visual Studio 2017 Community editor)

In my model I have:

[Required(AllowEmptyStrings = false, ErrorMessageResourceType = typeof(Resources.NewPetDataValidation),
                  ErrorMessageResourceName = "EmptyField")]
[StringLength(30, ErrorMessageResourceType = typeof(Resources.NewPetDataValidation),
                  ErrorMessageResourceName = "NameMaxLength")]
public string Name {get; set;}

In Resources project I created single .resx file per each language i.e. NewPetDataValidation.en.resx, NewPetDataValidation.de.resx.

Than in Visual studio Resource editor I added resource strings to each file with Resource names.

For English: Name: EmptyField Value: Empty field!

For German: Name: EmptyField Value: Leer Feld!

etc ...

In Razor view I have: @Html.ValidationMessageFor(Model => Model.Name, null, new { @style = "color:red"})

Everything works fine provided I don't access in the model more than one resource string in a certain .resx file. I'm positive that ErrorMessageResourceName matches certain resources Names.

On the other hand if I create seperated .resx files for every translated phrase application is working. However I don't think creating so many .resx files is a good idea.

Thanks in advance for your support.

1

1 Answers

1
votes

I figured out that my mistake was to rename the default resource created in Visual Studio.

Working steps:

  1. Create default resource MyResource.resx (syntax without language/culture suffix, it will generate the class file that could be referenced from another project)

  2. Add new MyResource.en.resx, MyResource.de.resx ... for all languages in question.