diff --git a/src/Jiaowu.Api/Controllers/CourseSelectionsController.cs b/src/Jiaowu.Api/Controllers/CourseSelectionsController.cs index f723acc..48b4b97 100644 --- a/src/Jiaowu.Api/Controllers/CourseSelectionsController.cs +++ b/src/Jiaowu.Api/Controllers/CourseSelectionsController.cs @@ -1609,7 +1609,7 @@ public sealed class CourseSelectionsController( offering, cancellationToken); if (eligibility.Error is not null) - return ConflictProblem(eligibility.Error); + return ConflictProblem(SelectionMessage(eligibility.Error, eligibility.EnglishError ?? LocalizeSelectionError(eligibility.Error))); var sameCourseWaitlistExists = await db.CourseEnrollments.AnyAsync( x => @@ -2190,7 +2190,8 @@ public sealed class CourseSelectionsController( return new( isRetake, $"获得名额后将达到 {selectedCredits + task.Course.Credits:0.#} 学分," + - $"超过本轮 {round.MaxCredits:0.#} 学分上限。"); + $"超过本轮 {round.MaxCredits:0.#} 学分上限。", + $"Receiving a place would total {selectedCredits + task.Course.Credits:0.#} credits, exceeding the round limit of {round.MaxCredits:0.#}."); } var selectedCourseCount = await db.CourseEnrollments.CountAsync( x => @@ -2204,7 +2205,8 @@ public sealed class CourseSelectionsController( { return new( isRetake, - $"你已达到本轮最多 {round.MaxCourseCount} 门课程限制。"); + $"你已达到本轮最多 {round.MaxCourseCount} 门课程限制。", + $"You have reached the limit of {round.MaxCourseCount} courses for this round."); } var candidateEntries = await PublishedScheduleEntries( @@ -2239,7 +2241,8 @@ public sealed class CourseSelectionsController( { return new( true, - $"重修课程时间冲突 {overlap:F0}%,超过 50% 上限。"); + $"重修课程时间冲突 {overlap:F0}%,超过 50% 上限。", + $"The retake overlaps by {overlap:F0}%, exceeding the 50% limit."); } } @@ -2586,7 +2589,7 @@ public sealed class CourseSelectionsController( private static int[] NormalizeGrades(IEnumerable? values) => values?.Distinct().OrderBy(x => x).ToArray() ?? []; - private sealed record EnrollmentEligibility(bool IsRetake, string? Error); + private sealed record EnrollmentEligibility(bool IsRetake, string? Error, string? EnglishError = null); private sealed record RetakeGradeAttempt( Guid CourseId, diff --git a/tests/Jiaowu.Api.Tests/CourseSelectionErrorLocalizationTests.cs b/tests/Jiaowu.Api.Tests/CourseSelectionErrorLocalizationTests.cs index 64dcdb2..c505ce6 100644 --- a/tests/Jiaowu.Api.Tests/CourseSelectionErrorLocalizationTests.cs +++ b/tests/Jiaowu.Api.Tests/CourseSelectionErrorLocalizationTests.cs @@ -38,4 +38,19 @@ public sealed class CourseSelectionErrorLocalizationTests } finally { CultureInfo.CurrentUICulture = original; } } + + [Fact] + public void DynamicEligibilityMessageUsesTheRequestLanguage() + { + var method = typeof(CourseSelectionsController).GetMethod("SelectionMessage", BindingFlags.NonPublic | BindingFlags.Static)!; + var original = CultureInfo.CurrentUICulture; + try + { + CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("en-US"); + Assert.Equal("You have reached the limit of 3 courses.", method.Invoke(null, ["你已达到最多 3 门课程限制。", "You have reached the limit of 3 courses."])); + CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("zh-CN"); + Assert.Equal("你已达到最多 3 门课程限制。", method.Invoke(null, ["你已达到最多 3 门课程限制。", "You have reached the limit of 3 courses."])); + } + finally { CultureInfo.CurrentUICulture = original; } + } }