diff --git a/src/Jiaowu.Api/Controllers/CourseSelectionsController.cs b/src/Jiaowu.Api/Controllers/CourseSelectionsController.cs index a288861..f723acc 100644 --- a/src/Jiaowu.Api/Controllers/CourseSelectionsController.cs +++ b/src/Jiaowu.Api/Controllers/CourseSelectionsController.cs @@ -1,5 +1,6 @@ using System.ComponentModel.DataAnnotations; using System.Data; +using System.Globalization; using Jiaowu.Api.Contracts; using Jiaowu.Api.Domain.Academic; using Jiaowu.Api.Domain.Identity; @@ -826,7 +827,7 @@ public sealed class CourseSelectionsController( return ValidationProblem("存在无效的学生档案。"); var inactive = students.FirstOrDefault(x => x.Status != StudentStatus.Active); if (inactive is not null) - return ConflictProblem($"学生 {inactive.StudentNumber} {inactive.Name} 当前不是在籍状态。"); + return ConflictProblem(SelectionMessage($"学生 {inactive.StudentNumber} {inactive.Name} 当前不是在籍状态。", $"Student {inactive.StudentNumber} {inactive.Name} is not currently active.")); var eligibleGrades = round.EligibleGrades .Select(x => x.Grade) .ToArray(); @@ -837,7 +838,7 @@ public sealed class CourseSelectionsController( if (wrongGrade is not null) { return ConflictProblem( - $"学生 {wrongGrade.StudentNumber} {wrongGrade.Name} 所在年级不属于本轮选课对象。"); + SelectionMessage($"学生 {wrongGrade.StudentNumber} {wrongGrade.Name} 所在年级不属于本轮选课对象。", $"Student {wrongGrade.StudentNumber} {wrongGrade.Name} is not in an eligible year for this round.")); } var outOfScope = students.FirstOrDefault(student => !offering.IsOpenToAll && @@ -846,7 +847,7 @@ public sealed class CourseSelectionsController( if (outOfScope is not null) { return ConflictProblem( - $"学生 {outOfScope.StudentNumber} {outOfScope.Name} 不属于该教学班的选课对象。"); + SelectionMessage($"学生 {outOfScope.StudentNumber} {outOfScope.Name} 不属于该教学班的选课对象。", $"Student {outOfScope.StudentNumber} {outOfScope.Name} is not eligible for this class.")); } var existingEnrollments = await db.CourseEnrollments @@ -859,7 +860,7 @@ public sealed class CourseSelectionsController( { var student = students.First(x => x.Id == alreadyEnrolled.StudentId); return ConflictProblem( - $"学生 {student.StudentNumber} {student.Name} 已在该教学班名单中。"); + SelectionMessage($"学生 {student.StudentNumber} {student.Name} 已在该教学班名单中。", $"Student {student.StudentNumber} {student.Name} is already on this class roster.")); } var enrolledCount = await db.CourseEnrollments.CountAsync( @@ -870,7 +871,7 @@ public sealed class CourseSelectionsController( if (enrolledCount + students.Count > offering.Capacity) { return ConflictProblem( - $"教学班仅剩 {Math.Max(0, offering.Capacity - enrolledCount)} 个名额,无法完成本次代选。"); + SelectionMessage($"教学班仅剩 {Math.Max(0, offering.Capacity - enrolledCount)} 个名额,无法完成本次代选。", $"Only {Math.Max(0, offering.Capacity - enrolledCount)} places remain; this enrollment cannot be completed.")); } var duplicateStudentIds = await db.CourseEnrollments.AsNoTracking() @@ -887,7 +888,7 @@ public sealed class CourseSelectionsController( { var student = students.First(x => duplicateStudentIds.Contains(x.Id)); return ConflictProblem( - $"学生 {student.StudentNumber} {student.Name} 本学期已选择相同课程。"); + SelectionMessage($"学生 {student.StudentNumber} {student.Name} 本学期已选择相同课程。", $"Student {student.StudentNumber} {student.Name} has already selected this course in this term.")); } var candidateEntries = await PublishedScheduleEntries( @@ -911,7 +912,7 @@ public sealed class CourseSelectionsController( if (selectedCredits + task.Course.Credits > round.MaxCredits) { return ConflictProblem( - $"学生 {student.StudentNumber} {student.Name} 代选后将超过本轮 {round.MaxCredits:0.#} 学分上限。"); + SelectionMessage($"学生 {student.StudentNumber} {student.Name} 代选后将超过本轮 {round.MaxCredits:0.#} 学分上限。", $"Enrolling student {student.StudentNumber} {student.Name} would exceed the round limit of {round.MaxCredits:0.#} credits.")); } var selectedCourseCount = await db.CourseEnrollments.CountAsync( x => @@ -924,8 +925,7 @@ public sealed class CourseSelectionsController( selectedCourseCount)) { return ConflictProblem( - $"学生 {student.StudentNumber} {student.Name} 已达到本轮最多 " + - $"{round.MaxCourseCount} 门课程限制。"); + SelectionMessage($"学生 {student.StudentNumber} {student.Name} 已达到本轮最多 {round.MaxCourseCount} 门课程限制。", $"Student {student.StudentNumber} {student.Name} has reached the round limit of {round.MaxCourseCount} courses.")); } var selectedTaskIds = await db.CourseEnrollments.AsNoTracking() @@ -944,7 +944,7 @@ public sealed class CourseSelectionsController( if (CourseSelectionRules.HasScheduleConflict(candidateEntries, selectedEntries)) { return ConflictProblem( - $"学生 {student.StudentNumber} {student.Name} 的已选课程与该教学班时间冲突。"); + SelectionMessage($"学生 {student.StudentNumber} {student.Name} 的已选课程与该教学班时间冲突。", $"Student {student.StudentNumber} {student.Name} has a scheduling conflict with this class.")); } } @@ -1038,7 +1038,7 @@ public sealed class CourseSelectionsController( { var dup = students.First(x => x.Id == alreadyEnrolled.StudentId); return ConflictProblem( - $"学生 {dup.StudentNumber} {dup.Name} 已在该教学班名单中。"); + SelectionMessage($"学生 {dup.StudentNumber} {dup.Name} 已在该教学班名单中。", $"Student {dup.StudentNumber} {dup.Name} is already on this class roster.")); } var now = DateTime.UtcNow; @@ -1486,8 +1486,7 @@ public sealed class CourseSelectionsController( if (selectedCredits + task.Course!.Credits > round.MaxCredits) { return ConflictProblem( - $"选课后将达到 {selectedCredits + task.Course.Credits:0.#} 学分," + - $"超过本轮 {round.MaxCredits:0.#} 学分上限。"); + SelectionMessage($"选课后将达到 {selectedCredits + task.Course.Credits:0.#} 学分,超过本轮 {round.MaxCredits:0.#} 学分上限。", $"Enrollment would total {selectedCredits + task.Course.Credits:0.#} credits, exceeding the round limit of {round.MaxCredits:0.#}.")); } var selectedCourseCount = await db.CourseEnrollments.CountAsync( x => @@ -1500,7 +1499,7 @@ public sealed class CourseSelectionsController( selectedCourseCount)) { return ConflictProblem( - $"你已达到本轮最多 {round.MaxCourseCount} 门课程限制。"); + SelectionMessage($"你已达到本轮最多 {round.MaxCourseCount} 门课程限制。", $"You have reached the limit of {round.MaxCourseCount} courses for this round.")); } // Schedule conflict check @@ -1532,7 +1531,7 @@ public sealed class CourseSelectionsController( candidateEntries, selectedEntries); if (overlap > 50) return ConflictProblem( - $"重修课程时间冲突 {overlap:F0}%,超过 50% 上限,无法选课。"); + SelectionMessage($"重修课程时间冲突 {overlap:F0}%,超过 50% 上限,无法选课。", $"The retake overlaps by {overlap:F0}%, exceeding the 50% limit.")); } if (existing is null) @@ -2024,7 +2023,7 @@ public sealed class CourseSelectionsController( if (task.Status != TeachingTaskStatus.Published) return ValidationProblem("只有已发布的教学班可以进入选课。"); if (request.Capacity > task.Capacity) - return ValidationProblem($"选课容量不能超过教学班容量 {task.Capacity}。"); + return ValidationProblem(SelectionMessage($"选课容量不能超过教学班容量 {task.Capacity}。", $"Enrollment capacity cannot exceed the class capacity of {task.Capacity}.")); return null; } @@ -2511,11 +2510,73 @@ public sealed class CourseSelectionsController( private ActionResult ProfileNotFound() => ConflictProblem("当前账号未关联有效学生档案,请联系教务管理员。"); + private static readonly IReadOnlyDictionary EnglishErrors = new Dictionary + { + ["只有草稿选课批次可以修改。"] = "Only draft enrollment rounds can be edited.", + ["只有草稿选课批次可以删除。"] = "Only draft enrollment rounds can be deleted.", + ["只有草稿选课批次可以开放。"] = "Only draft enrollment rounds can be opened.", + ["至少配置一个可选教学班后才能开放。"] = "Add at least one teaching class before opening enrollment.", + ["只有开放中的选课批次可以关闭。"] = "Only open enrollment rounds can be closed.", + ["选课批次开放后不能调整教学班。"] = "Teaching classes cannot be changed after the enrollment round opens.", + ["管理员代选仅适用于公共必修课。"] = "Administrative enrollment is only available for required general courses.", + ["选课批次开放后才能办理。"] = "This action is only available after enrollment opens.", + ["请至少选择一名学生。"] = "Select at least one student.", + ["单次最多可为 100 名学生代选。"] = "At most 100 students can be enrolled at once.", + ["选课批次开放后才能办理管理员代选。"] = "Administrative enrollment is only available after the round opens.", + ["该教学班当前不可办理代选。"] = "Administrative enrollment is unavailable for this class.", + ["存在无效的学生档案。"] = "The selection contains invalid student records.", + ["该教学班尚未发布课表,暂时不能办理代选。"] = "The class timetable is not published; administrative enrollment is unavailable.", + ["单次最多可为 100 名学生强制选课。"] = "At most 100 students can be force-enrolled at once.", + ["选课批次开放后才能办理强制选课。"] = "Force enrollment is only available after the round opens.", + ["该教学班当前不可选。"] = "This teaching class is currently unavailable for enrollment.", + ["管理员名单调整仅适用于公共必修课。"] = "Administrative roster changes are only available for required general courses.", + ["该教学班当前不可调整名单。"] = "The roster for this class cannot currently be changed.", + ["该学生已不在教学班名单中。"] = "The student is no longer on this class roster.", + ["只有在籍学生可以选课。"] = "Only active students can enroll in courses.", + ["只有在籍学生可以选课或候补。"] = "Only active students can enroll or join a waitlist.", + ["你不属于该教学班的选课对象。"] = "You are not eligible to enroll in this class.", + ["该批次仅限有未通过历史成绩的学生办理重修选课或候补。"] = "Only students retaking a previously failed course can enroll or join a waitlist in this round.", + ["该教学班尚未发布课表,暂时不能选课或候补。"] = "The class timetable is not published; enrollment and waitlisting are unavailable.", + ["当前不在该选课批次的开放时间内。"] = "The enrollment round is not currently open.", + ["你所在的年级不属于本轮选课对象。"] = "Your year is not eligible for this enrollment round.", + ["该批次仅限有未通过历史成绩的学生办理重修选课。"] = "This round is limited to students retaking a previously failed course.", + ["你已经选择了该教学班。"] = "You are already enrolled in this class.", + ["该教学班名额已满。"] = "This teaching class is full.", + ["同一学期不能重复选择相同课程。"] = "You cannot select the same course twice in one term.", + ["该教学班尚未发布课表,暂时不能选课。"] = "The class timetable is not published; enrollment is unavailable.", + ["该教学班与已选课程的上课时间冲突。"] = "This class conflicts with a course you have already selected.", + ["你已经在该教学班的候补队列中。"] = "You are already on this class waitlist.", + ["你已在本学期同一课程的其他教学班候补。"] = "You are already waitlisted for another class of this course in this term.", + ["该教学班当前仍有名额,请直接选择课程。"] = "This class still has places; enroll directly.", + ["该课程已经退选或候补已经结束。"] = "The course has already been dropped or the waitlist has ended.", + ["当前批次已停止退课和候补调整。"] = "This round no longer permits withdrawals or waitlist changes.", + ["所选教学班不存在或超出数据范围。"] = "The selected teaching class does not exist or is outside your data scope.", + ["教学班与选课批次必须属于同一学期。"] = "The teaching class and enrollment round must belong to the same term.", + ["只有已发布的教学班可以进入选课。"] = "Only published teaching classes can be offered for enrollment.", + ["选课开始时间必须早于结束时间。"] = "Enrollment must start before it ends.", + ["退课截止时间不能早于选课结束时间。"] = "The withdrawal deadline cannot be earlier than the enrollment end time.", + ["选课批次类型无效。"] = "The enrollment round type is invalid.", + ["单个选课批次最多配置 20 个适用年级。"] = "An enrollment round can specify at most 20 eligible years.", + ["适用年级必须在 2000—2200 之间。"] = "Eligible years must be between 2000 and 2200.", + ["所选学期不存在或已停用。"] = "The selected term does not exist or is disabled.", + ["记录重复、容量已变化,或关联数据已失效。"] = "The record is duplicated, capacity has changed, or related data is no longer valid.", + ["当前账号未关联有效学生档案,请联系教务管理员。"] = "The current account is not linked to an active student record. Contact academic administration.", + }; + + private static string SelectionMessage(string chinese, string english) => + CultureInfo.CurrentUICulture.TwoLetterISOLanguageName == "en" ? english : chinese; + + private static string LocalizeSelectionError(string detail) => + CultureInfo.CurrentUICulture.TwoLetterISOLanguageName == "en" && EnglishErrors.TryGetValue(detail, out var translated) + ? translated : detail; + + private ActionResult ValidationProblem(string detail) => base.ValidationProblem(detail: LocalizeSelectionError(detail)); + private ActionResult ConflictProblem(string detail) => Conflict(new ProblemDetails { - Title = "无法完成选课操作", - Detail = detail, + Title = SelectionMessage("无法完成选课操作", "Course enrollment could not be completed"), + Detail = LocalizeSelectionError(detail), Status = StatusCodes.Status409Conflict }); diff --git a/tests/Jiaowu.Api.Tests/CourseSelectionErrorLocalizationTests.cs b/tests/Jiaowu.Api.Tests/CourseSelectionErrorLocalizationTests.cs new file mode 100644 index 0000000..64dcdb2 --- /dev/null +++ b/tests/Jiaowu.Api.Tests/CourseSelectionErrorLocalizationTests.cs @@ -0,0 +1,41 @@ +using System.Globalization; +using System.Reflection; +using Jiaowu.Api.Controllers; +using Microsoft.AspNetCore.Mvc; + +namespace Jiaowu.Api.Tests; + +public sealed class CourseSelectionErrorLocalizationTests +{ + [Fact] + public void ConflictResponseUsesRequestCultureAndPreservesStatus() + { + var controller = new CourseSelectionsController(null!, null!); + var method = typeof(CourseSelectionsController).GetMethod("ConflictProblem", BindingFlags.NonPublic | BindingFlags.Instance)!; + var original = CultureInfo.CurrentUICulture; + try + { + CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("en-US"); + var english = Assert.IsType(method.Invoke(controller, ["该教学班名额已满。"])); + Assert.Equal(409, english.StatusCode); + Assert.Equal("This teaching class is full.", Assert.IsType(english.Value).Detail); + CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("zh-CN"); + var chinese = Assert.IsType(method.Invoke(controller, ["该教学班名额已满。"])); + Assert.Equal("该教学班名额已满。", Assert.IsType(chinese.Value).Detail); + } + finally { CultureInfo.CurrentUICulture = original; } + } + + [Fact] + public void UnknownDetailsArePreserved() + { + var method = typeof(CourseSelectionsController).GetMethod("LocalizeSelectionError", BindingFlags.NonPublic | BindingFlags.Static)!; + var original = CultureInfo.CurrentUICulture; + try + { + CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("en-US"); + Assert.Equal("custom detail", method.Invoke(null, ["custom detail"])); + } + finally { CultureInfo.CurrentUICulture = original; } + } +}