Giter Site home page Giter Site logo

jsakamoto / toolbelt.systemresourcemanager Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 0.0 61 KB

Inject your custom resource manager to your apps as the System Resource Manager to localize DataAnnotations validation error messages.

License: Mozilla Public License 2.0

C# 43.43% HTML 56.57%
blazor blazor-server blazor-webassembly data-annotation data-annotations dataannotations localization

toolbelt.systemresourcemanager's Introduction

Toolbelt.SystemResourceManager NuGet Package

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.

Usage

  1. Install this library as a NuGet package.
dotnet add package Toolbelt.SystemResourceManager
  1. 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.

Example

  1. Create a new Blazor application project.

  2. Create a model class on your Blazor app project like below.

using System.ComponentModel.DataAnnotations;
public class ValidationTestModel
{
    [Required]
    public string RequiredField { get; set; } = "";
}
  1. 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();
}
  1. Prepare Resource1.resx and Resource1.ja.resx resource files on your Blazor project. Make the Resource1.ja.resx to be below.
Name Value
RequiredAttribute_ValidationError フィールド {0} は必須です。
  1. Inject the Resource1 resource as a system resource.
// Program.cs
...
using SampleApp;
...
builder.Services.AddSystemResourceManager<Resource1>();
...
  1. Finally, you will see localized validation error messages on your Blazor app, like this.

Release Note

Release notes

License

Mozilla Public License Version 2.0

toolbelt.systemresourcemanager's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.