meta info

Localizing Model Binding Errors

By Ziya Mollamahmut

All model binding errors are localized by XLocalizer with the default setup, so no additional steps required to localize data annotations errors.

When the source translation culture is "en", no additional setup required for localizing model binding error messages, it will be localized automatically by XLocalizer. But if the source translation culture is different than "en", then we have to provide the default model binding errors in the relevant default culture, so XLocalizer can use them as source for translation.

Customize model binding errors

Model binding errors can be customized by providing a new object of type XLocalizer.ErrorMessages.ModelBindingErrors into XLocalizerOptions in startup file.

services.AddRazorPages()
        .AddXLocalizer<...>(ops =>
        {
            // ...
            ops.ModelBindingErrors = new ModelBindingErrors 
            {
                AttemptedValueIsInvalidAccessor = "The value '{0}' is not valid for {1}.",
                MissingBindRequiredValueAccessor = "A value for the '{0}' parameter or property was not provided.",
                MissingKeyOrValueAccessor = "A value is required.",
                // ...
            };
        });

Same way we can provide the default model binding errors in a different culture other than "en".

Customize via json

See Setup XLocalizer via JSON Settings.