dotnet / dotnet/aspnetcore

Data annotations localization does not appear to work with positional records

Open
#39,551 3 comments 1 reaction 0 assignees View on GitHub
area-mvc feature-localization investigate
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

When using model binding and [data annotations localization](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-6.0#dataannotations-localization) with positional records, the error messages are not localized (whereas as they localised when exactly the same attributes are used with non-positional records).

For example, consider the two broadly equivalent record types that could be used as the model for a form:

```csharp
// version 1: Non-positional record
public record GreetingViewModel
{
[Required(ErrorMessage = "Error_NameRequired")]
public string? Name { get; init; }
}

// version 2: Positional record
public record GreetingViewModel([Required(ErrorMessage = "Error_NameRequired")] string? Name);
```

Together with the following resource file:

```xml


Please enter your name

```

When the form is submitted without specifying a name and we use the non-positional version of the model, the validation error message is correctly displayed as "Please enter your name". However when we use the positional version of the model the error message is incorrectly displayed as "Error_NameRequired".

### Expected Behavior

Error messages are localized regardless of whether the record is positional or not.

### Steps To Reproduce

1. Clone the minimalist repro project at https://github.com/smfeest/repro-positional-record-attribute-localization
2. Run the application and navigate to http://localhost:5251 or https://localhost:7045/
3. Submit the form without entering a name
4. The unlocalized validation error message ("Error_NameRequired") is unexpectedly displayed
5. Comment out the positional version of `GreetingViewModel` in Models/GreetingViewModel.cs and uncomment the non-positional version below it
6. Restart the application
7. Once gain, submit the form without entering a name
8. The localized validation error ("Please enter your name") is now correctly displayed

### Exceptions (if any)

_No response_

### .NET Version

6.0.101

### Anything else?

Output of `dotnet --info`

```
.NET SDK (reflecting any global.json):
Version: 6.0.101
Commit: ef49f6213a

Runtime Environment:
OS Name: ubuntu
OS Version: 20.04
OS Platform: Linux
RID: ubuntu.20.04-x64
Base Path: /usr/share/dotnet/sdk/6.0.101/

Host (useful for support):
Version: 6.0.1
Commit: 3a25a7f1cc

.NET SDKs installed:
5.0.404 [/usr/share/dotnet/sdk]
6.0.101 [/usr/share/dotnet/sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 5.0.13 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.1 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 5.0.13 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.1 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET runtimes or SDKs:
https://aka.ms/dotnet-download
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.