Built-in validation error messages for validation attributes such as [Required]
, [StringLength]
, etc., on your ASP.NET Core and Blazor apps will be shown localized after installing this NuGet package.
Warning
⚠️ This library touches undocumented areas and private implementations of the .NET runtime, using the "Reflection" technology. So please remember that it might not be working on future .NET versions.
- Install the NuGet package for the language you want to localize. The following command example shows the case for localizing standard validation error messages to Japanese.
dotnet add package Toolbelt.ComponentModel.Annotations.Resources.ja
- Call the
AddSystemComponentModelAnnotationsLocalization()
extension method for a service collection at the startup of your apps.
// Program.cs
...
using Toolbelt.Extensions.DependencyInjection;
...
builder.Services.AddSystemComponentModelAnnotationsLocalization();
...
After doing the above steps, you will see localized validation error messages on your ASP.NET Core and Blazor apps.
Please remember to set the current thread culture on your apps the way you want. On a Blazor server app case, for example, you may have to implement your startup code like this:
// Program.cs
...
using Toolbelt.Extensions.DependencyInjection;
...
var builder = WebApplication.CreateBuilder(args);
...
// 👇 Register services to the DI container involved with the localization feature.
builder.Services.AddLocalization();
...
builder.Services.AddSystemComponentModelAnnotationsLocalization();
...
var app = builder.Build();
...
// 👇 Configure the Request Localization middleware.
app.UseRequestLocalization(options =>
{
var supportedCultures = new[] { "en", "ja" };
options.AddSupportedCultures(supportedCultures);
options.AddSupportedUICultures(supportedCultures);
});
...
app.Run();
See also: 🔗 "ASP.NET Core Blazor globalization and localization" | Microsoft Docs