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

@@ -14,6 +14,7 @@
<PackageReference Include="Blazilla" Version="2.0.1" />
<PackageReference Include="Blazored.LocalStorage" Version="4.5.0" />
<PackageReference Include="Blazored.FluentValidation" Version="2.2.0" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="12.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="10.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="10.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="10.0.0" />

View File

@@ -113,7 +113,7 @@
{
try
{
jsResult = await JS.InvokeAsync<string>("CookieReader.Read", "atomx.culture");
jsResult = await JS.InvokeAsync<string>("cookies.Read", "atomx.culture");
}
catch (Exception ex)
{

View File

@@ -6,6 +6,9 @@ using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Localization;
using System.Net.Http;
using FluentValidation;
using System.Linq;
using System.Reflection;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
@@ -67,6 +70,8 @@ builder.Services.AddScoped<HttpService>(sp =>
return new HttpService(httpClient, httpContextAccessor);
});
builder.Services.AddValidatorsFromAssembly(typeof(Atomx.Admin.Client.Validators.LoginModelValidator).Assembly);
builder.Services.AddAntDesign();

View File

@@ -169,7 +169,7 @@ namespace Atomx.Admin.Client.Services
{
if (_jsRuntime != null && OperatingSystem.IsBrowser())
{
var cookieVal = await _jsRuntime.InvokeAsync<string>("CookieReader.Read", CookieName);
var cookieVal = await _jsRuntime.InvokeAsync<string>("cookies.Read", CookieName);
_logger?.LogDebug("Cookie '{CookieName}'='{CookieVal}'", CookieName, cookieVal);
if (!string.IsNullOrEmpty(cookieVal))
{

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
{
}
}

View File

@@ -1,4 +1,4 @@
window.CookieReader = {
window.cookies = {
Read: function (name) {
try {
const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
@@ -32,10 +32,3 @@ window.setHtmlLang = function (lang) {
if (document && document.documentElement) document.documentElement.lang = lang || '';
} catch (e) { }
};
// simple cookies wrapper used earlier as cookies.Write
window.cookies = {
Write: function (name, value, expiresIso) {
return window.CookieReader.Write(name, value, expiresIso);
}
};