ValidationLocalizationServiceCollectionExtensions.AddValidationLocalization Method

Definition

Overloads

Name Description
AddValidationLocalization(IServiceCollection, Action<ValidationLocalizationOptions>)

Adds the default IStringLocalizer-based validation localizer to the service collection and wires it up by setting Localizer.

AddValidationLocalization<TResource>(IServiceCollection, Action<ValidationLocalizationOptions>)

Adds the default IStringLocalizer-based validation localizer configured to resolve localized strings against the resource type TResource for all types being validated.

AddValidationLocalization(IServiceCollection, Action<ValidationLocalizationOptions>)

Source:
ValidationLocalizationServiceCollectionExtensions.cs

Adds the default IStringLocalizer-based validation localizer to the service collection and wires it up by setting Localizer.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddValidationLocalization(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, Action<Microsoft.Extensions.Validation.Localization.ValidationLocalizationOptions>? configureOptions = default);
static member AddValidationLocalization : Microsoft.Extensions.DependencyInjection.IServiceCollection * Action<Microsoft.Extensions.Validation.Localization.ValidationLocalizationOptions> -> Microsoft.Extensions.DependencyInjection.IServiceCollection
<Extension()>
Public Function AddValidationLocalization (services As IServiceCollection, Optional configureOptions As Action(Of ValidationLocalizationOptions) = Nothing) As IServiceCollection

Parameters

services
IServiceCollection

The IServiceCollection to add services to.

configureOptions
Action<ValidationLocalizationOptions>

An optional callback to configure ValidationLocalizationOptions.

Returns

The IServiceCollection for chaining.

Remarks

Internally calls AddLocalization(IServiceCollection) to ensure an IStringLocalizerFactory is registered, and registers an IConfigureOptions<TOptions> bridge that sets Localizer to a Microsoft.Extensions.Validation.Localization.DefaultValidationLocalizer instance (only when Localizer has not already been set).

Call AddValidation() separately to register the validation pipeline itself. Call order does not matter.

Minimal API parameter validation: top-level Minimal API parameters do not have a declaring type. For applications that validate Minimal API parameters, prefer the AddValidationLocalization<TResource>(IServiceCollection, Action<ValidationLocalizationOptions>) overload (shared-resource pattern), or set LocalizerProvider explicitly to a delegate that does not depend on the declaring type (which is passed as null in the parameter case).

The default IStringLocalizerFactory registered by AddLocalization() reads strings from .resx resource files. To localize against other sources (databases, JSON files, in-memory dictionaries, third-party translation services), register your own IStringLocalizerFactory implementation either before or after AddValidationLocalization(IServiceCollection, Action<ValidationLocalizationOptions>). The validation localizer resolves the factory at validation time, so registration order does not matter:

builder.Services.AddValidation();
builder.Services.AddValidationLocalization();
builder.Services.AddSingleton<IStringLocalizerFactory, MyDatabaseLocalizerFactory>();

Applies to

AddValidationLocalization<TResource>(IServiceCollection, Action<ValidationLocalizationOptions>)

Source:
ValidationLocalizationServiceCollectionExtensions.cs

Adds the default IStringLocalizer-based validation localizer configured to resolve localized strings against the resource type TResource for all types being validated.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddValidationLocalization<TResource>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, Action<Microsoft.Extensions.Validation.Localization.ValidationLocalizationOptions>? configureOptions = default);
static member AddValidationLocalization : Microsoft.Extensions.DependencyInjection.IServiceCollection * Action<Microsoft.Extensions.Validation.Localization.ValidationLocalizationOptions> -> Microsoft.Extensions.DependencyInjection.IServiceCollection
<Extension()>
Public Function AddValidationLocalization(Of TResource) (services As IServiceCollection, Optional configureOptions As Action(Of ValidationLocalizationOptions) = Nothing) As IServiceCollection

Type Parameters

TResource

The type that identifies the shared resource source.

Parameters

services
IServiceCollection

The IServiceCollection to add services to.

configureOptions
Action<ValidationLocalizationOptions>

An optional callback to further configure ValidationLocalizationOptions.

Returns

The IServiceCollection for chaining.

Remarks

Use this when validation messages live in a single shared resource file rather than per-type resource files.

Equivalent to setting LocalizerProvider to (_, factory) => factory.Create(typeof(TResource)), but the configured provider resolves the IStringLocalizer once and reuses the same instance for every declaring type, avoiding repeated factory lookups.

Applies to