Search code examples
c#asp.net-corelocalizationattributesdata-annotations

Choose proper language for data annotations in ASP.NET


I'm trying to localize error messages for data annotations attributes. I created two resx-files:

DataAnnotationsLocalization.resx
> DataAnnotationsLocalization.Designer.cs
DataAnnotationsLocalization.uk.resx

I'm calling AddDataAnnotationsLocalization in Program.cs and now trying to use it in my page model:

[MinLength(10, ErrorMessage = "LocationAddressFormatError")]
public string Address { get; set; }

But as a result of this I see only my English resources shown while for other localized strings I see them according to the current culture (CultureInfo.CurrentUICulture).

I've put breakpoint into the Designer.cs but it never gets hit. Seems like the framework goes straight to DataAnnotationsLocalization.resx and doesn't consider other languages versions.

How can I show proper language version for the current culture?


Solution

  • Appeared that I had wrong Build Action for localized version of the resource file. After I'd changed it to Embedded resource everything started to work as expected.

    enter image description here