This is the library for .NET that allows you to inject your custom resource manager into your .NET apps, such as Blazor, as the System Resource Manager.
That means you can localize DataAnnotations validation error messages provided by the .NET runtime with this library.
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 this library as a NuGet package.
dotnet add package Toolbelt.SystemResourceManager
- Call the
AddSystemResourceManager()
extension method for a service collection at the startup of your apps with specifying the resource name you want to inject and its assembly.
// Program.cs
...
builder.Services.AddSystemResourceManager("SampleApp.Resource1", typeof(SampleApp.Resource1).Assembly);
...
You can also use the AddSystemResourceManager<TResource>()
overload version instead.
// Program.cs
...
using SampleApp;
...
builder.Services.AddSystemResourceManager<Resource1>();
...
After doing that, the resource strings for the DataAnnotations validation error messages will be retrieved from the resource you specified at first. If the resource string with the specified key doesn't exist in the resource you specified, it will be retrieved from the resource that is before of you injected it.
You can call the AddSystemsResourceManager()
extension method multiple with each different resource to inject it. The last injected resource is the most high-priority resource for retrieving resource strings.
-
Create a new Blazor application project.
-
Create a model class on your Blazor app project like below.
using System.ComponentModel.DataAnnotations;
public class ValidationTestModel
{
[Required]
public string RequiredField { get; set; } = "";
}
- Implement a form on a Razor component on your Blazor app project.
<EditForm Model="_Model">
<DataAnnotationsValidator/>
<InputText @bind-Value="_Model.RequiredField" />
<ValidationSummary />
<button type="submit">Submit</button>
</EditForm>
@code {
private ValidationTestModel _Model = new();
}
- Prepare
Resource1.resx
andResource1.ja.resx
resource files on your Blazor project. Make theResource1.ja.resx
to be below.
Name | Value |
---|---|
RequiredAttribute_ValidationError | フィールド {0} は必須です。 |
- Inject the
Resource1
resource as a system resource.
// Program.cs
...
using SampleApp;
...
builder.Services.AddSystemResourceManager<Resource1>();
...
- Finally, you will see localized validation error messages on your Blazor app, like this.