How to translate Identity Password validation messages

后端 未结 3 1914
抹茶落季
抹茶落季 2021-01-04 12:04

So far I have been able to translate everything in an ASP.Net Core 2.1 Web Application.

It has proven a little challenge, since the scaffolded Account Pages needed a

3条回答
  •  半阙折子戏
    2021-01-04 12:36

    These error messages are generated using IdentityErrorDescriber. Here's a sample of what the class itself looks like:

    public class IdentityErrorDescriber
    {
        ...
    
        public virtual IdentityError PasswordTooShort(int length)
        {
            return new IdentityError
            {
                Code = nameof(PasswordTooShort),
                Description = Resources.FormatPasswordTooShort(length)
            };
        }
    
    
        ...
    }
    

    To customise a specific message, create your own IdentityErrorDescriber implementation. Here's an example:

    public class MyIdentityErrorDescriber : IdentityErrorDescriber
    {    
        public override IdentityError PasswordTooShort(int length)
        {
            return new IdentityError
            {
                Code = nameof(PasswordTooShort),
                Description = "Your description goes here."
            };
        }
    }
    

    To use this new implementation, add it to the DI container in Startup.ConfigureServices:

    services.AddScoped();
    

提交回复
热议问题