0

I'm using CommunityToolkit.Mvvm.

My class is defined in the following way:

public partial class User : ObservableValidator
{
    ...

    [ObservableProperty]
    [NotifyDataErrorInfo]
    [Required(ErrorMessageResourceName = "RequiredAttribute_ValidationError", ErrorMessageResourceType = typeof(DataAnnotations))]
    [Display(Name = "Name_Field", ResourceType = typeof(Fields))]
    private string name = "";

    ...
}

Where typeof(DataAnnotations)and typeof(Field)are the types of the resources (resx) where I keep the translations.

I have a TextBox bound to the Name, so that if I type something into it and then deleted, it shows the error: The 'Name' field is required.

Now I added a button that once pressed changed CurrentCulture and CurrentUICulture between pt-PT and en-US and that changes all the text in the app between those two languages... Except for Display Name.

If culture is set to pt-PT before any validation and a validation occurs, the error reads: O campo 'Nome' é obrigatório, but if I then change the culture to en-US, the message now reads: The 'Nome' field is required.

Converselly, if culture is set to en-US before any validation and a validation occurs, the error reads: The 'Name' field is required, but if I then change the culture to pt-PT, the message now reads: O campo 'Name' é obrigatório.

So apparently the culture used to translate Display Name is set the first time the resource is used and is kept until application restart, while the translation of Required (and similar annotations like Phone, Email, etc) change when I change the UI culture.

Q1: Why is the translation of Display Name work differently?

Q2: How can I get the translation of Display Name to adapt to UI Culture changes, like other annotations do?

Thanks in advance

537mfb
  • 1,374
  • 1
  • 16
  • 32

0 Answers0