fix localization

This commit is contained in:
yxw
2025-12-09 17:39:21 +08:00
parent 7334a9576f
commit 2318dff192
11 changed files with 95 additions and 43 deletions

View File

@@ -1,25 +1,36 @@
using Atomx.Admin.Client.Models;
using FluentValidation;
using Microsoft.Extensions.Localization;
namespace Atomx.Admin.Client.Validators
{
public class LoginModelValidator : AbstractValidator<LoginModel>
{
public LoginModelValidator()
public LoginModelValidator(IStringLocalizer<LoginModelValidator> localizer)
{
RuleFor(p => p.Account).NotEmpty().WithMessage("登录账号不能为空");
RuleFor(p => p.Account).Length(2, 100).When(p => !string.IsNullOrEmpty(p.Account)).WithMessage("用户名长度必须再2-100个字符之间");
//RuleFor(p => p.Account).EmailAddress().When(p => !p.Account.Contains("@") && !string.IsNullOrEmpty(p.Account)).WithMessage("电子邮件地址不正确");
// helper funcs to get localized text or fallback
string AccountEmpty() => localizer?["Login.Account.Empty"].Value ?? "登录账号不能为空";
string AccountLength() => localizer?["Login.Account.Length"].Value ?? "用户名长度必须再2-100个字符之间";
string PasswordEmpty() => localizer?["Login.Password.Empty"].Value ?? "请输入登录密码";
string PasswordLength() => localizer?["Login.Password.Length"].Value ?? "登录密码必须在6-32位长度之间";
//RuleFor(p => p.Username).NotEmpty().WithMessage("用户名不能为空");
//RuleFor(p => p.Username).Length(2, 50).When(p => !string.IsNullOrEmpty(p.Username)).WithMessage("用户名长度必须再2-50个字符之间");
//RuleFor(p => p.Account).NotEmpty().WithMessage("电子邮件地址不能为空");
//RuleFor(p => p.Email).EmailAddress().When(p => !string.IsNullOrEmpty(p.Email)).WithMessage(p => localizer["Form.Email.Invalid"]);
//RuleFor(p => p.Email).MaximumLength(128).When(p => !string.IsNullOrEmpty(p.Email)).WithMessage(p => localizer["Form.Email.LengthInvalid"]);
RuleFor(p => p.Password).NotEmpty().WithMessage("请输入登录密码");
RuleFor(p => p.Password).Length(6, 32).When(p => !string.IsNullOrEmpty(p.Password)).WithMessage("登录密码必须在6-32位长度之间");
//RuleFor(p => p.ConfirmPassword).NotEmpty().WithMessage(p => localizer["Form.ConfirmPassword.Empty"]);
//RuleFor(p => p.ConfirmPassword).Equal(p => p.Password).When(p => !string.IsNullOrEmpty(p.Password) && !string.IsNullOrEmpty(p.ConfirmPassword)).WithMessage(p => localizer["Form.ConfirmPassword.Different"]);
RuleFor(p => p.Account)
.NotEmpty()
.WithMessage(_ => AccountEmpty());
RuleFor(p => p.Account)
.Length(2, 100)
.When(p => !string.IsNullOrEmpty(p.Account))
.WithMessage(_ => AccountLength());
RuleFor(p => p.Password)
.NotEmpty()
.WithMessage(_ => PasswordEmpty());
RuleFor(p => p.Password)
.Length(6, 32)
.When(p => !string.IsNullOrEmpty(p.Password))
.WithMessage(_ => PasswordLength());
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>

View File

@@ -0,0 +1,6 @@
namespace Atomx.Admin.Client.Validators
{
public sealed class ValidatorsMarker
{
}
}