Skip to content

Localization

Vsevolod Pilipenko edited this page Jul 17, 2023 · 4 revisions

ReactiveValidation supports localization of messages using ReactiveValidation.LanguageManager.

Default supported languages

Now there are four supported languages for default validators:

  • English
  • German
  • Russian
  • Czech

If you don't find your language here, you can specify default messages for them using IStringProvider (described below). Also, it would be really wonderful if you create Pull Request with your language: sample with English language.

Set culture

LanguageManager uses CultureInfo.CurrentUICulture as a default culture. But you can also changed it:

ValidationOptions.LanguageManager.Culture = culture;

Runtime localization

If at your application it's possible to change culture runtime, you need to enable this settings:

ValidationOptions
    .Setup()
    .TrackCultureChanged();

But when you will change culture, don't forget to change CultureInfo.CurrentUICulture. Also you need special error validation templates. Read more for WPF or Avalonia.

Localization messages from .resx files

If you use .resx files to store localization strings, you can provide it for ReactiveValidation messages. You can setup it like this:

ValidationOptions
    .Setup()
    .UseStringProvider(new ResourceStringProvider(
        Samples.Resources.Default.ResourceManager,
        new Dictionary<string, ResourceManager>
        {
            { nameof(Samples.Resources.Additional), Samples.Resources.Additional.ResourceManager },
        }));

Here you can see, that Samples.Resources.Default.ResourceManager is using as a default provider. But also specified additional resource manager with resourceKey = Additional. How you can use additional resource described below.

Localization messages from other sources

If you store localization strings in another places (for example in file), you also can provide it. For this create your own realization of ReactiveValidation.Resources.StringProviders.IStringProvider and setup it:

ValidationOptions
    .Setup()
    .UseStringProvider(new YourStringProvider);

Override default validator's messages

The library contains default messages for validators, but you can override them using IStringProvider. LanguageManager calls IStringProvider.GetString(string key, CultureInfo culture) to get message for validator. List of keys you can find here. So if you want to override message (or add messages for another culture) just make sure that you return message for this key.

Localized message for property validator

If you need to use localization message for one property you can do it like that:

builder.RuleFor(vm => vm.Password)
    .NotEmpty()
    .MinLength(8, ValidationMessageType.Warning)
        .WithLocalizedMessage("key");
// OR
builder.RuleFor(vm => vm.Password)
    .NotEmpty()
    .MinLength(8, ValidationMessageType.Warning)
        .WithLocalizedMessage("resource", "key");

In this case will be used default source, and at the second - source with specified name.

Localize property name

Some validators uses string.Format and paste a name of property in a message. For example: {PropertyName} should not be empty. You can also specify localization value for them with ReactiveValidation.Attributes.DisplayNameAttribute. You can do it like that:

[DisplayName(DisplayNameKey = "key")]
public string? PhoneNumber { get; set; }
// OR
[DisplayName(DisplayNameResource = "resource", DisplayNameKey = "key")]
public string? PhoneNumber { get; set; }

In this case will be used default source, and at the second - source with specified name.

Your also can override how to get display names. It described here.