From 1dc2dcd4558a532d9f2cc19ff2c2cb1a54a7a1dc Mon Sep 17 00:00:00 2001 From: biss Date: Fri, 13 Nov 2026 19:59:22 +0800 Subject: [PATCH] Localize base data validation errors --- .../Controllers/BaseDataController.cs | 41 ++++++++++++++++--- .../Localization/SharedResource.en-US.resx | 20 +++++++++ .../Localization/SharedResource.resx | 20 +++++++++ 3 files changed, 76 insertions(+), 5 deletions(-) diff --git a/src/Jiaowu.Api/Controllers/BaseDataController.cs b/src/Jiaowu.Api/Controllers/BaseDataController.cs index 9c9c998..a68ebe1 100644 --- a/src/Jiaowu.Api/Controllers/BaseDataController.cs +++ b/src/Jiaowu.Api/Controllers/BaseDataController.cs @@ -7,9 +7,11 @@ using Jiaowu.Api.Domain.Common; using Jiaowu.Api.Domain.Identity; using Jiaowu.Api.Infrastructure.Caching; using Jiaowu.Api.Infrastructure.Persistence; +using Jiaowu.Api.Infrastructure.Localization; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Localization; namespace Jiaowu.Api.Controllers; @@ -17,8 +19,31 @@ namespace Jiaowu.Api.Controllers; [Authorize] [Route("api/base-data")] public sealed class BaseDataController(AppDbContext db, IAppCache cache, - ICurrentUserDataScope? currentUserDataScope = null) : ControllerBase + ICurrentUserDataScope? currentUserDataScope = null, + IStringLocalizer? localizer = null) : ControllerBase { + private static readonly IReadOnlyDictionary ErrorKeys = new Dictionary + { + ["所选校区不存在。"] = "BaseData.Campus.NotFound", + ["所选学院不存在。"] = "BaseData.College.NotFound", + ["页码必须大于 0,且每页条数应在 1 至 100 之间。"] = "BaseData.Pagination.Invalid", + ["所选专业不存在。"] = "BaseData.Major.NotFound", + ["所选辅导员账号不存在、已停用或没有辅导员角色。"] = "BaseData.Counselor.Unavailable", + ["辅导员所属学院与行政班所属学院不一致。"] = "BaseData.Counselor.CollegeMismatch", + ["学期结束日期必须晚于开始日期。"] = "BaseData.Term.DateRangeInvalid", + ["当前学期必须保持启用。"] = "BaseData.Term.CurrentMustBeEnabled", + ["不能直接取消当前学期,请将另一个学期设为当前。"] = "BaseData.Term.CurrentCannotDisable", + ["已归档学期请先恢复后再编辑。"] = "BaseData.Term.ArchivedCannotEdit", + ["已归档学期不能直接设为当前,请先撤销归档。"] = "BaseData.Term.ArchivedCannotBeCurrent", + ["停用学期不能设为当前,请先启用。"] = "BaseData.Term.DisabledCannotBeCurrent", + ["当前学期不能归档,请先切换到新的当前学期。"] = "BaseData.Term.CurrentCannotArchive", + ["学期数据已变化,请刷新后重试。"] = "BaseData.Term.Changed", + ["所选教学楼不存在。"] = "BaseData.Building.NotFound", + ["当前学期不能删除,请先切换到其他学期。"] = "BaseData.Term.CurrentCannotDelete", + ["已归档学期不能删除,请先恢复。"] = "BaseData.Term.ArchivedCannotDelete", + ["该数据已被其他业务引用,请先停用,或移除关联数据后再删除。"] = "BaseData.Delete.HasReferences", + }; + private const string Administrators = $"{SystemRoles.SuperAdmin},{SystemRoles.AcademicAdmin}"; @@ -728,8 +753,8 @@ public sealed class BaseDataController(AppDbContext db, IAppCache cache, { return Conflict(new ProblemDetails { - Title = "无法删除", - Detail = "该数据已被其他业务引用,请先停用,或移除关联数据后再删除。", + Title = SharedResourceText.Get(localizer, "BaseData.Delete.Title"), + Detail = LocalizeMessage("该数据已被其他业务引用,请先停用,或移除关联数据后再删除。"), Status = StatusCodes.Status409Conflict }); } @@ -802,11 +827,17 @@ public sealed class BaseDataController(AppDbContext db, IAppCache cache, entity.IsEnabled = request.IsEnabled; } + private ActionResult ValidationProblem(string detail) => + base.ValidationProblem(detail: LocalizeMessage(detail)); + + private string LocalizeMessage(string detail) => + ErrorKeys.TryGetValue(detail, out var key) ? SharedResourceText.Get(localizer, key) : detail; + private ObjectResult ConflictProblem(string detail) => Conflict(new ProblemDetails { - Title = "当前操作无法完成", - Detail = detail, + Title = SharedResourceText.Get(localizer, "BaseData.Errors.Title"), + Detail = LocalizeMessage(detail), Status = StatusCodes.Status409Conflict }); } diff --git a/src/Jiaowu.Api/Resources/Infrastructure/Localization/SharedResource.en-US.resx b/src/Jiaowu.Api/Resources/Infrastructure/Localization/SharedResource.en-US.resx index d25121b..596319f 100644 --- a/src/Jiaowu.Api/Resources/Infrastructure/Localization/SharedResource.en-US.resx +++ b/src/Jiaowu.Api/Resources/Infrastructure/Localization/SharedResource.en-US.resx @@ -648,4 +648,24 @@ The audience contains an invalid role. Holiday data is invalid or unavailable for this year. Holiday data contains an invalid date. + Unable to complete the current operation + Unable to delete + The selected campus does not exist. + The selected college does not exist. + Page must be greater than 0 and page size must be between 1 and 100. + The selected major does not exist. + The selected counselor account does not exist, is disabled, or lacks the counselor role. + The counselor’s college does not match the administrative class. + The term end date must be later than its start date. + The current term must remain enabled. + You cannot disable the current term directly. Set another term as current first. + Restore the archived term before editing it. + Restore the archived term before setting it as current. + Enable the term before setting it as current. + Switch to a new current term before archiving this term. + Term data has changed. Refresh and try again. + The selected building does not exist. + Switch to another term before deleting the current term. + Restore the archived term before deleting it. + This data is referenced by other business records. Disable it or remove related records before deleting it. diff --git a/src/Jiaowu.Api/Resources/Infrastructure/Localization/SharedResource.resx b/src/Jiaowu.Api/Resources/Infrastructure/Localization/SharedResource.resx index 6bd11ba..8c6d9c5 100644 --- a/src/Jiaowu.Api/Resources/Infrastructure/Localization/SharedResource.resx +++ b/src/Jiaowu.Api/Resources/Infrastructure/Localization/SharedResource.resx @@ -648,4 +648,24 @@ 包含无效的目标角色。 节假日数据格式无效或该年份暂无数据。 节假日数据包含无效日期。 + 当前操作无法完成 + 无法删除 + 所选校区不存在。 + 所选学院不存在。 + 页码必须大于 0,且每页条数应在 1 至 100 之间。 + 所选专业不存在。 + 所选辅导员账号不存在、已停用或没有辅导员角色。 + 辅导员所属学院与行政班所属学院不一致。 + 学期结束日期必须晚于开始日期。 + 当前学期必须保持启用。 + 不能直接取消当前学期,请将另一个学期设为当前。 + 已归档学期请先恢复后再编辑。 + 已归档学期不能直接设为当前,请先撤销归档。 + 停用学期不能设为当前,请先启用。 + 当前学期不能归档,请先切换到新的当前学期。 + 学期数据已变化,请刷新后重试。 + 所选教学楼不存在。 + 当前学期不能删除,请先切换到其他学期。 + 已归档学期不能删除,请先恢复。 + 该数据已被其他业务引用,请先停用,或移除关联数据后再删除。